Improved code quality

This commit is contained in:
Eduard Urbach 2023-08-31 17:03:52 +02:00
parent a7e65ca27f
commit 95976e39bf
Signed by: akyoto
GPG Key ID: C874F672B1AF20C0
16 changed files with 128 additions and 69 deletions

View File

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

19
.gitignore vendored
View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

9
Nil.go
View File

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

View File

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

View File

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

View File

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

View File

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

19
errors_test.go Normal file
View File

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

View File

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

2
go.mod
View File

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

7
test.go Normal file
View File

@ -0,0 +1,7 @@
package assert
// test is the interface used for tests.
type test interface {
Errorf(format string, args ...any)
FailNow()
}