From 13850a3acd336da85cd4d77b160f2c4cc2924647 Mon Sep 17 00:00:00 2001 From: Eduard Urbach Date: Wed, 5 Jul 2023 13:14:37 +0200 Subject: [PATCH] Added equality checks --- .editorconfig | 9 +++++++++ .gitignore | 11 +---------- Equal.go | 47 +++++++++++++++++++++++++++++++++++++++++++++++ Equal_test.go | 29 +++++++++++++++++++++++++++++ LICENSE | 2 +- README.md | 12 +++++++++++- file.go | 21 +++++++++++++++++++++ go.mod | 3 +++ 8 files changed, 122 insertions(+), 12 deletions(-) create mode 100644 .editorconfig create mode 100644 Equal.go create mode 100644 Equal_test.go create mode 100644 file.go create mode 100644 go.mod diff --git a/.editorconfig b/.editorconfig new file mode 100644 index 0000000..3c94c19 --- /dev/null +++ b/.editorconfig @@ -0,0 +1,9 @@ +root = true + +[*] +indent_style = tab +indent_size = 4 +end_of_line = lf +charset = utf-8 +trim_trailing_whitespace = true +insert_final_newline = false diff --git a/.gitignore b/.gitignore index 5cbdfa9..342a59c 100644 --- a/.gitignore +++ b/.gitignore @@ -1,15 +1,9 @@ -# ---> Go.AllowList -# Allowlisting gitignore template for GO projects prevents us -# from adding various unwanted local files, such as generated -# files, developer configurations or IDE-specific files etc. -# -# Recommended: Go.AllowList.gitignore - # Ignore everything * # But not these files... !/.gitignore +!/.editorconfig !*.go !go.sum @@ -18,8 +12,5 @@ !README.md !LICENSE -# !Makefile - # ...even if they are in subdirectories !*/ - diff --git a/Equal.go b/Equal.go new file mode 100644 index 0000000..e5816e9 --- /dev/null +++ b/Equal.go @@ -0,0 +1,47 @@ +package assert + +import ( + "reflect" + "testing" +) + +// Equal asserts that the two parameters are equal. +func Equal[T comparable](t testing.TB, a T, b T) { + if a == b { + return + } + + t.Errorf(`Equal: +file: %s +assert: Equal +value: %v +expected: %v`, file(t), a, b) + t.FailNow() +} + +// NotEqual asserts that the two parameters are not equal. +func NotEqual[T comparable](t testing.TB, a T, b T) { + if a != b { + return + } + + t.Errorf(`NotEqual: +file: %s +assert: NotEqual +value: %v`, file(t), a) + t.FailNow() +} + +// DeepEqual asserts that the two parameters are deeply equal. +func DeepEqual[T comparable](t testing.TB, a T, b T) { + if reflect.DeepEqual(a, b) { + return + } + + t.Errorf(` +file: %s +assert: DeepEqual +value: %v +expected: %v`, file(t), a, b) + t.FailNow() +} diff --git a/Equal_test.go b/Equal_test.go new file mode 100644 index 0000000..5edbedc --- /dev/null +++ b/Equal_test.go @@ -0,0 +1,29 @@ +package assert_test + +import ( + "testing" + + "git.akyoto.dev/go/assert" +) + +type T struct{ A int } + +func TestEqual(t *testing.T) { + assert.Equal(t, 0, 0) + assert.Equal(t, "Hello", "Hello") + assert.Equal(t, T{A: 10}, T{A: 10}) +} + +func TestNotEqual(t *testing.T) { + assert.NotEqual(t, 0, 1) + assert.NotEqual(t, "Hello", "World") + assert.NotEqual(t, &T{A: 10}, &T{A: 10}) + assert.NotEqual(t, T{A: 10}, T{A: 20}) +} + +func TestDeepEqual(t *testing.T) { + assert.DeepEqual(t, 0, 0) + assert.DeepEqual(t, "Hello", "Hello") + assert.DeepEqual(t, T{A: 10}, T{A: 10}) + assert.DeepEqual(t, &T{A: 10}, &T{A: 10}) +} diff --git a/LICENSE b/LICENSE index 2071b23..40d5bcd 100644 --- a/LICENSE +++ b/LICENSE @@ -1,6 +1,6 @@ MIT License -Copyright (c) +Copyright (c) 2023 Eduard Urbach Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: diff --git a/README.md b/README.md index 4dcfb30..0dfc476 100644 --- a/README.md +++ b/README.md @@ -1,3 +1,13 @@ # assert -A minimal & stateless assert package for writing tests. \ No newline at end of file +A minimal & stateless assert package for writing tests. + +## API + +```go +assert.Nil(t, nil) +assert.True(t, true) +assert.Equal(t, "Hello", "Hello") +assert.DeepEqual(t, "Hello", "Hello") +assert.Contains(t, "Hello", "ello") +``` diff --git a/file.go b/file.go new file mode 100644 index 0000000..aeba87c --- /dev/null +++ b/file.go @@ -0,0 +1,21 @@ +package assert + +import ( + "runtime/debug" + "strings" + "testing" +) + +// file returns the first line containing "_test.go" in the debug stack. +func file(t testing.TB) string { + stack := string(debug.Stack()) + lines := strings.Split(stack, "\n") + + for _, line := range lines { + if strings.Contains(line, "_test.go") { + return strings.TrimSpace(line) + } + } + + return "" +} diff --git a/go.mod b/go.mod new file mode 100644 index 0000000..8ac2d6e --- /dev/null +++ b/go.mod @@ -0,0 +1,3 @@ +module git.akyoto.dev/go/assert + +go 1.20