Added equality checks

This commit is contained in:
Eduard Urbach 2023-07-05 13:14:37 +02:00
parent e6865a34ff
commit 13850a3acd
Signed by: akyoto
GPG Key ID: C874F672B1AF20C0
8 changed files with 122 additions and 12 deletions

9
.editorconfig Normal file
View File

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

11
.gitignore vendored
View File

@ -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
!*/

47
Equal.go Normal file
View File

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

29
Equal_test.go Normal file
View File

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

View File

@ -1,6 +1,6 @@
MIT License
Copyright (c) <year> <copyright holders>
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:

View File

@ -1,3 +1,13 @@
# assert
A minimal & stateless assert package for writing tests.
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")
```

21
file.go Normal file
View File

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

3
go.mod Normal file
View File

@ -0,0 +1,3 @@
module git.akyoto.dev/go/assert
go 1.20