diff --git a/Contains.go b/Contains.go index cc66a96..ee839af 100644 --- a/Contains.go +++ b/Contains.go @@ -12,11 +12,7 @@ func Contains(t testing.TB, a any, b any) { return } - t.Errorf(` -file: %s -assert: Contains -container:%v -element: %v`, file(t), a, b) + t.Errorf(formatTwoParameters, file(), "Contains", a, b) t.FailNow() } @@ -26,11 +22,7 @@ func NotContains(t testing.TB, a any, b any) { return } - t.Errorf(` -file: %s -assert: NotContains -container:%v -element: %v`, file(t), a, b) + t.Errorf(formatTwoParameters, file(), "NotContains", a, b) t.FailNow() } diff --git a/Contains_test.go b/Contains_test.go index c801692..36f9992 100644 --- a/Contains_test.go +++ b/Contains_test.go @@ -19,11 +19,12 @@ func TestContains(t *testing.T) { } func TestNotContains(t *testing.T) { - assert.NotContains(t, "Hello", "404") - assert.NotContains(t, []string{"Hello", "World"}, "404") + assert.NotContains(t, "Hello", "h") + assert.NotContains(t, "Hello", "hello") + 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, []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}, "404") + assert.NotContains(t, map[string]int{"Hello": 1, "World": 2}, "hello") } diff --git a/Equal.go b/Equal.go index 80bcea0..9bc4776 100644 --- a/Equal.go +++ b/Equal.go @@ -11,11 +11,7 @@ func Equal[T comparable](t testing.TB, a T, b T) { return } - t.Errorf(` -file: %s -assert: Equal -value: %v -expected: %v`, file(t), a, b) + t.Errorf(formatTwoParameters, file(), "Equal", a, b) t.FailNow() } @@ -25,10 +21,7 @@ func NotEqual[T comparable](t testing.TB, a T, b T) { return } - t.Errorf(` -file: %s -assert: NotEqual -value: %v`, file(t), a) + t.Errorf(formatTwoParameters, file(), "NotEqual", a, b) t.FailNow() } @@ -38,10 +31,6 @@ func DeepEqual[T comparable](t testing.TB, a T, b T) { return } - t.Errorf(` -file: %s -assert: DeepEqual -value: %v -expected: %v`, file(t), a, b) + t.Errorf(formatTwoParameters, file(), "DeepEqual", a, b) t.FailNow() } diff --git a/Nil.go b/Nil.go index cc2aa9f..aa51423 100644 --- a/Nil.go +++ b/Nil.go @@ -11,11 +11,7 @@ func Nil(t testing.TB, a any) { return } - t.Errorf(` -file: %s -assert: Nil -value: %v -expected: nil`, file(t), a) + t.Errorf(formatOneParameter, file(), "Nil", a) t.FailNow() } @@ -25,11 +21,7 @@ func NotNil(t testing.TB, a any) { return } - t.Errorf(` -file: %s -assert: NotNil -value: %v -expected: not nil`, file(t), a) + t.Errorf(formatOneParameter, file(), "NotNil", a) t.FailNow() } diff --git a/Nil_test.go b/Nil_test.go index 83ee265..e0e9f1f 100644 --- a/Nil_test.go +++ b/Nil_test.go @@ -1,4 +1,3 @@ - package assert_test import ( @@ -8,18 +7,31 @@ import ( ) func TestNil(t *testing.T) { - var nilPointer *T - var nilSlice []byte + var ( + nilPointer *T + nilInterface any + nilSlice []byte + nilMap map[byte]byte + nilChannel chan byte + nilFunction func() + ) assert.Nil(t, nil) assert.Nil(t, nilPointer) + assert.Nil(t, nilInterface) assert.Nil(t, nilSlice) + assert.Nil(t, nilMap) + assert.Nil(t, nilChannel) + assert.Nil(t, nilFunction) } func TestNotNil(t *testing.T) { assert.NotNil(t, 0) assert.NotNil(t, "Hello") - assert.NotNil(t, []byte{}) assert.NotNil(t, T{}) assert.NotNil(t, &T{}) + assert.NotNil(t, make([]byte, 0)) + assert.NotNil(t, make(map[byte]byte)) + assert.NotNil(t, make(chan byte)) + assert.NotNil(t, TestNotNil) } diff --git a/file.go b/file.go index aeba87c..4872594 100644 --- a/file.go +++ b/file.go @@ -3,16 +3,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 { +func file() string { stack := string(debug.Stack()) lines := strings.Split(stack, "\n") for _, line := range lines { if strings.Contains(line, "_test.go") { + space := strings.LastIndex(line, " ") + + if space != -1 { + line = line[:space] + } + return strings.TrimSpace(line) } } diff --git a/format.go b/format.go new file mode 100644 index 0000000..c668f2e --- /dev/null +++ b/format.go @@ -0,0 +1,12 @@ +package assert + +const formatOneParameter = ` + %s +󰅙 assert.%s + 󰯬 %v` + +const formatTwoParameters = ` + %s +󰅙 assert.%s + 󰯬 %v + 󰯯 %v`