From 95976e39bf4e6692265b37d534431fa72583c5c3 Mon Sep 17 00:00:00 2001 From: Eduard Urbach Date: Thu, 31 Aug 2023 17:03:52 +0200 Subject: [PATCH] Improved code quality --- .editorconfig | 9 --------- .gitignore | 19 +++++-------------- Contains.go | 9 ++++----- Contains_test.go | 9 +++++++++ Equal.go | 13 ++++++------- Equal_test.go | 12 ++++++++++++ LICENSE | 9 --------- Nil.go | 9 ++++----- Nil_test.go | 8 ++++++++ README.md | 37 ++++++++++++++++++++++++++++++++++++- True.go | 6 ++---- file.go => errors.go | 17 +++++++++++++++-- errors_test.go | 19 +++++++++++++++++++ format.go | 12 ------------ go.mod | 2 +- test.go | 7 +++++++ 16 files changed, 128 insertions(+), 69 deletions(-) delete mode 100644 .editorconfig delete mode 100644 LICENSE rename file.go => errors.go (65%) create mode 100644 errors_test.go delete mode 100644 format.go create mode 100644 test.go diff --git a/.editorconfig b/.editorconfig deleted file mode 100644 index 3c94c19..0000000 --- a/.editorconfig +++ /dev/null @@ -1,9 +0,0 @@ -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 342a59c..7dbfd92 100644 --- a/.gitignore +++ b/.gitignore @@ -1,16 +1,7 @@ -# Ignore everything * - -# But not these files... -!/.gitignore -!/.editorconfig - -!*.go -!go.sum -!go.mod - -!README.md -!LICENSE - -# ...even if they are in subdirectories !*/ +!.gitignore +!*.go +!*.md +!*.mod +!*.sum diff --git a/Contains.go b/Contains.go index ee839af..11621af 100644 --- a/Contains.go +++ b/Contains.go @@ -3,26 +3,25 @@ package assert import ( "reflect" "strings" - "testing" ) // Contains asserts that a contains b. -func Contains(t testing.TB, a any, b any) { +func Contains(t test, a any, b any) { if contains(a, b) { return } - t.Errorf(formatTwoParameters, file(), "Contains", a, b) + t.Errorf(twoParameters, file(), "Contains", a, b) t.FailNow() } // NotContains asserts that a doesn't contain b. -func NotContains(t testing.TB, a any, b any) { +func NotContains(t test, a any, b any) { if !contains(a, b) { return } - t.Errorf(formatTwoParameters, file(), "NotContains", a, b) + t.Errorf(twoParameters, file(), "NotContains", a, b) t.FailNow() } diff --git a/Contains_test.go b/Contains_test.go index 36f9992..a514b09 100644 --- a/Contains_test.go +++ b/Contains_test.go @@ -24,7 +24,16 @@ func TestNotContains(t *testing.T) { assert.NotContains(t, []string{"Hello", "World"}, "hello") assert.NotContains(t, []int{1, 2, 3}, 4) assert.NotContains(t, []int{1, 2, 3}, []int{2, 1}) + assert.NotContains(t, []int{1, 2, 3}, []int{1, 2, 3, 4}) assert.NotContains(t, []byte{'H', 'e', 'l', 'l', 'o'}, byte('a')) assert.NotContains(t, []byte{'H', 'e', 'l', 'l', 'o'}, []byte{'l', 'e'}) assert.NotContains(t, map[string]int{"Hello": 1, "World": 2}, "hello") } + +func TestFailContains(t *testing.T) { + assert.Contains(fail(t), "Hello", "h") +} + +func TestFailNotContains(t *testing.T) { + assert.NotContains(fail(t), "Hello", "H") +} diff --git a/Equal.go b/Equal.go index 7ea45f6..75ccfde 100644 --- a/Equal.go +++ b/Equal.go @@ -2,35 +2,34 @@ package assert import ( "reflect" - "testing" ) // Equal asserts that the two parameters are equal. -func Equal[T comparable](t testing.TB, a T, b T) { +func Equal[T comparable](t test, a T, b T) { if a == b { return } - t.Errorf(formatTwoParameters, file(), "Equal", a, b) + t.Errorf(twoParameters, file(), "Equal", a, b) t.FailNow() } // NotEqual asserts that the two parameters are not equal. -func NotEqual[T comparable](t testing.TB, a T, b T) { +func NotEqual[T comparable](t test, a T, b T) { if a != b { return } - t.Errorf(formatTwoParameters, file(), "NotEqual", a, b) + t.Errorf(twoParameters, file(), "NotEqual", a, b) t.FailNow() } // DeepEqual asserts that the two parameters are deeply equal. -func DeepEqual[T any](t testing.TB, a T, b T) { +func DeepEqual[T any](t test, a T, b T) { if reflect.DeepEqual(a, b) { return } - t.Errorf(formatTwoParameters, file(), "DeepEqual", a, b) + t.Errorf(twoParameters, file(), "DeepEqual", a, b) t.FailNow() } diff --git a/Equal_test.go b/Equal_test.go index 64fe1ea..6a0e345 100644 --- a/Equal_test.go +++ b/Equal_test.go @@ -28,3 +28,15 @@ func TestDeepEqual(t *testing.T) { assert.DeepEqual(t, T{A: 10}, T{A: 10}) assert.DeepEqual(t, &T{A: 10}, &T{A: 10}) } + +func TestFailEqual(t *testing.T) { + assert.Equal(fail(t), 0, 1) +} + +func TestFailNotEqual(t *testing.T) { + assert.NotEqual(fail(t), 0, 0) +} + +func TestFailDeepEqual(t *testing.T) { + assert.DeepEqual(fail(t), "Hello", "World") +} diff --git a/LICENSE b/LICENSE deleted file mode 100644 index 40d5bcd..0000000 --- a/LICENSE +++ /dev/null @@ -1,9 +0,0 @@ -MIT License - -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: - -The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. - -THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. diff --git a/Nil.go b/Nil.go index aa51423..c7d7cc1 100644 --- a/Nil.go +++ b/Nil.go @@ -2,26 +2,25 @@ package assert import ( "reflect" - "testing" ) // Nil asserts that the given parameter equals nil. -func Nil(t testing.TB, a any) { +func Nil(t test, a any) { if isNil(a) { return } - t.Errorf(formatOneParameter, file(), "Nil", a) + t.Errorf(oneParameter, file(), "Nil", a) t.FailNow() } // NotNil asserts that the given parameter does not equal nil. -func NotNil(t testing.TB, a any) { +func NotNil(t test, a any) { if !isNil(a) { return } - t.Errorf(formatOneParameter, file(), "NotNil", a) + t.Errorf(oneParameter, file(), "NotNil", a) t.FailNow() } diff --git a/Nil_test.go b/Nil_test.go index e0e9f1f..39f5c3c 100644 --- a/Nil_test.go +++ b/Nil_test.go @@ -35,3 +35,11 @@ func TestNotNil(t *testing.T) { assert.NotNil(t, make(chan byte)) assert.NotNil(t, TestNotNil) } + +func TestFailNil(t *testing.T) { + assert.Nil(fail(t), 0) +} + +func TestFailNotNil(t *testing.T) { + assert.NotNil(fail(t), nil) +} diff --git a/README.md b/README.md index f7bea4c..7387945 100644 --- a/README.md +++ b/README.md @@ -2,13 +2,19 @@ A minimal & stateless assert package for writing tests. +## Features + +- Simple interface +- Tiny codebase +- Zero dependencies + ## Installation ```shell go get git.akyoto.dev/go/assert ``` -## API +## Usage ```go assert.Nil(t, nil) @@ -17,3 +23,32 @@ assert.Equal(t, "Hello", "Hello") assert.DeepEqual(t, "Hello", "Hello") assert.Contains(t, "Hello", "ello") ``` + +## Tests + +``` +PASS: TestContains +PASS: TestNotContains +PASS: TestFailContains +PASS: TestFailNotContains +PASS: TestEqual +PASS: TestNotEqual +PASS: TestDeepEqual +PASS: TestFailEqual +PASS: TestFailNotEqual +PASS: TestFailDeepEqual +PASS: TestNil +PASS: TestNotNil +PASS: TestFailNil +PASS: TestFailNotNil +PASS: TestTrue +coverage: 100.0% of statements +``` + +## License + +Please see the [license documentation](https://akyoto.dev/license). + +## Copyright + +© 2019 Eduard Urbach diff --git a/True.go b/True.go index 2e4b00b..fbcb053 100644 --- a/True.go +++ b/True.go @@ -1,13 +1,11 @@ package assert -import "testing" - // True asserts that the given parameter is true. -func True(t testing.TB, a bool) { +func True(t test, a bool) { Equal(t, a, true) } // False asserts that the given parameter is false. -func False(t testing.TB, a bool) { +func False(t test, a bool) { Equal(t, a, false) } diff --git a/file.go b/errors.go similarity index 65% rename from file.go rename to errors.go index 4872594..3e83117 100644 --- a/file.go +++ b/errors.go @@ -5,10 +5,22 @@ import ( "strings" ) +const oneParameter = ` + %s +󰅙 assert.%s + 󰯬 %v` + +const twoParameters = ` + %s +󰅙 assert.%s + 󰯬 %v + 󰯯 %v` + // file returns the first line containing "_test.go" in the debug stack. func file() string { stack := string(debug.Stack()) lines := strings.Split(stack, "\n") + name := "" for _, line := range lines { if strings.Contains(line, "_test.go") { @@ -18,9 +30,10 @@ func file() string { line = line[:space] } - return strings.TrimSpace(line) + name = strings.TrimSpace(line) + break } } - return "" + return name } diff --git a/errors_test.go b/errors_test.go new file mode 100644 index 0000000..313eef8 --- /dev/null +++ b/errors_test.go @@ -0,0 +1,19 @@ +package assert_test + +import ( + "testing" +) + +// noop implements the Test interface with noop functions so you can test the failure cases. +type noop struct{} + +// Errorf does nothing. +func (t *noop) Errorf(format string, args ...any) {} + +// FailNow does nothing. +func (t *noop) FailNow() {} + +// fail creates a new test that is expected to fail. +func fail(t *testing.T) *noop { + return &noop{} +} diff --git a/format.go b/format.go deleted file mode 100644 index c668f2e..0000000 --- a/format.go +++ /dev/null @@ -1,12 +0,0 @@ -package assert - -const formatOneParameter = ` - %s -󰅙 assert.%s - 󰯬 %v` - -const formatTwoParameters = ` - %s -󰅙 assert.%s - 󰯬 %v - 󰯯 %v` diff --git a/go.mod b/go.mod index 8ac2d6e..b98499d 100644 --- a/go.mod +++ b/go.mod @@ -1,3 +1,3 @@ module git.akyoto.dev/go/assert -go 1.20 +go 1.21 diff --git a/test.go b/test.go new file mode 100644 index 0000000..f61394d --- /dev/null +++ b/test.go @@ -0,0 +1,7 @@ +package assert + +// test is the interface used for tests. +type test interface { + Errorf(format string, args ...any) + FailNow() +}