Improved error messages

This commit is contained in:
Eduard Urbach 2023-07-10 11:36:17 +02:00
parent 6b735732b5
commit c4ee12d433
Signed by: akyoto
GPG Key ID: C874F672B1AF20C0
7 changed files with 46 additions and 43 deletions

View File

@ -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()
}

View File

@ -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")
}

View File

@ -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()
}

12
Nil.go
View File

@ -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()
}

View File

@ -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)
}

View File

@ -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)
}
}

12
format.go Normal file
View File

@ -0,0 +1,12 @@
package assert
const formatOneParameter = `
%s
󰅙 assert.%s
󰯬 %v`
const formatTwoParameters = `
%s
󰅙 assert.%s
󰯬 %v
󰯯 %v`