Skip to content

Commit 348b256

Browse files
authored
Merge pull request #192 from MarvinJWendt/191-switch-to-atomicgoassert
2 parents 304bcb0 + e60da62 commit 348b256

File tree

5 files changed

+30
-99
lines changed

5 files changed

+30
-99
lines changed

README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
</a>
1717

1818
<a href="https://codecov.io/gh/MarvinJWendt/testza">
19-
<!-- unittestcount:start --><img src="https://img.shields.io/badge/Unit_Tests-3007-magenta?style=flat-square" alt="Unit test count"><!-- unittestcount:end -->
19+
<!-- unittestcount:start --><img src="https://img.shields.io/badge/Unit_Tests-3009-magenta?style=flat-square" alt="Unit test count"><!-- unittestcount:end -->
2020
</a>
2121

2222
<a href="https://pkg.go.dev/github.com/MarvinJWendt/testza" target="_blank">

assert.go

+23-30
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package testza
22

33
import (
4+
"atomicgo.dev/assert"
45
"errors"
56
"fmt"
67
"os"
@@ -68,7 +69,7 @@ func AssertKindOf(t testRunner, expectedKind reflect.Kind, object any, msg ...an
6869
test.Helper()
6970
}
7071

71-
if !internal.IsKind(expectedKind, object) {
72+
if !assert.Kind(object, expectedKind) {
7273
internal.Fail(t,
7374
fmt.Sprintf("A value that !!should be a type of kind %s!! is a type of kind %s.", expectedKind.String(), reflect.TypeOf(object).Kind().String()),
7475
internal.NewObjectsExpectedActual(expectedKind, object),
@@ -93,7 +94,7 @@ func AssertNotKindOf(t testRunner, kind reflect.Kind, object any, msg ...any) {
9394
test.Helper()
9495
}
9596

96-
if internal.IsKind(kind, object) {
97+
if assert.Kind(object, kind) {
9798
internal.Fail(t,
9899
fmt.Sprintf("A value that !!should not be a type of kind %s!! is a type of kind %s.", kind.String(), reflect.TypeOf(object).Kind().String()),
99100
internal.Objects{
@@ -117,7 +118,7 @@ func AssertNotKindOf(t testRunner, kind reflect.Kind, object any, msg ...any) {
117118
// testza.AssertNumeric(t, 1.23)
118119
// testza.AssertNumeric(t, uint(123))
119120
func AssertNumeric(t testRunner, object any, msg ...any) {
120-
if !internal.IsNumber(object) {
121+
if !assert.Number(object) {
121122
internal.Fail(t, "An object that !!should be a number!! is not of a numeric type.", internal.NewObjectsSingleUnknown(object), msg...)
122123
}
123124
}
@@ -133,7 +134,7 @@ func AssertNumeric(t testRunner, object any, msg ...any) {
133134
// testza.AssertNotNumeric(t, true)
134135
// testza.AssertNotNumeric(t, "123")
135136
func AssertNotNumeric(t testRunner, object any, msg ...any) {
136-
if internal.IsNumber(object) {
137+
if assert.Number(object) {
137138
internal.Fail(t, "An object that !!should not be a number!! is of a numeric type.", internal.NewObjectsSingleUnknown(object), msg...)
138139
}
139140
}
@@ -152,7 +153,7 @@ func AssertZero(t testRunner, value any, msg ...any) {
152153
test.Helper()
153154
}
154155

155-
if !internal.IsZero(value) {
156+
if !assert.Zero(value) {
156157
internal.Fail(t, "An object that !!should have its zero value!!, does not have its zero value.", internal.NewObjectsSingleUnknown(value), msg...)
157158
}
158159
}
@@ -171,7 +172,7 @@ func AssertNotZero(t testRunner, value any, msg ...any) {
171172
test.Helper()
172173
}
173174

174-
if internal.IsZero(value) {
175+
if assert.Zero(value) {
175176
internal.Fail(t, "An object that !!should not have its zero value!!, does have its zero value.", internal.NewObjectsSingleUnknown(value), msg...)
176177
}
177178
}
@@ -189,7 +190,7 @@ func AssertEqual(t testRunner, expected any, actual any, msg ...any) {
189190
test.Helper()
190191
}
191192

192-
if !internal.IsEqual(expected, actual) {
193+
if !assert.Equal(expected, actual) {
193194
internal.Fail(t, "Two objects that !!should be equal!!, are not equal.", internal.NewObjectsExpectedActualWithDiff(expected, actual), msg...)
194195
}
195196
}
@@ -207,7 +208,7 @@ func AssertNotEqual(t testRunner, expected any, actual any, msg ...any) {
207208
test.Helper()
208209
}
209210

210-
if internal.IsEqual(expected, actual) {
211+
if assert.Equal(expected, actual) {
211212
objects := internal.Objects{
212213
{
213214
Name: "Both Objects",
@@ -341,7 +342,7 @@ func AssertImplements(t testRunner, interfaceObject, object any, msg ...any) {
341342
test.Helper()
342343
}
343344

344-
if !internal.DoesImplement(interfaceObject, object) {
345+
if !assert.Implements(object, interfaceObject) {
345346
internal.Fail(t, fmt.Sprintf("An object that !!should implement %s!! does not implement it.", reflect.TypeOf(interfaceObject).String()), internal.Objects{}, msg...)
346347
}
347348
}
@@ -359,7 +360,7 @@ func AssertNotImplements(t testRunner, interfaceObject, object any, msg ...any)
359360
test.Helper()
360361
}
361362

362-
if internal.DoesImplement(interfaceObject, object) {
363+
if assert.Implements(object, interfaceObject) {
363364
internal.Fail(t, fmt.Sprintf("An object that !!should not implement %s!! does implement it.", reflect.TypeOf(interfaceObject).String()), internal.Objects{}, msg...)
364365
}
365366
}
@@ -378,7 +379,7 @@ func AssertContains(t testRunner, object, element any, msg ...any) {
378379
test.Helper()
379380
}
380381

381-
if !internal.DoesContain(object, element) {
382+
if !assert.Contains(object, element) {
382383
internal.Fail(t, "An object !!does not contain!! the object it should contain.", internal.Objects{
383384
internal.NewObjectsSingleNamed("Missing Object", element)[0],
384385
internal.NewObjectsSingleNamed("Full Object", object)[0],
@@ -399,7 +400,7 @@ func AssertNotContains(t testRunner, object, element any, msg ...any) {
399400
test.Helper()
400401
}
401402

402-
if internal.DoesContain(object, element) {
403+
if assert.Contains(object, element) {
403404
internal.Fail(t, "An object !!does contain!! an object it should not contain.", internal.Objects{
404405
internal.NewObjectsSingleUnknown(object)[0],
405406
internal.NewObjectsSingleNamed("Element that should not be in the object", element)[0],
@@ -422,13 +423,9 @@ func AssertPanics(t testRunner, f func(), msg ...any) {
422423
test.Helper()
423424
}
424425

425-
defer func() {
426-
if r := recover(); r == nil {
427-
internal.Fail(t, "A function that !!should panic!! did not panic.", internal.Objects{}, msg...)
428-
}
429-
}()
430-
431-
f()
426+
if !assert.Panic(f) {
427+
internal.Fail(t, "A function that !!should panic!! did not panic.", internal.Objects{}, msg...)
428+
}
432429
}
433430

434431
// AssertNotPanics asserts that a function does not panic.
@@ -445,13 +442,9 @@ func AssertNotPanics(t testRunner, f func(), msg ...any) {
445442
test.Helper()
446443
}
447444

448-
defer func() {
449-
if r := recover(); r != nil {
450-
internal.Fail(t, "A function that !!should not panic!! did panic.", internal.Objects{}, msg...)
451-
}
452-
}()
453-
454-
f()
445+
if assert.Panic(f) {
446+
internal.Fail(t, "A function that !!should not panic!! did panic.", internal.Objects{}, msg...)
447+
}
455448
}
456449

457450
// AssertNil asserts that an object is nil.
@@ -466,7 +459,7 @@ func AssertNil(t testRunner, object any, msg ...any) {
466459
test.Helper()
467460
}
468461

469-
if !internal.IsNil(object) {
462+
if !assert.Nil(object) {
470463
internal.Fail(t, "An object that !!should be nil!! is not nil.", internal.NewObjectsExpectedActual(nil, object), msg...)
471464
}
472465
}
@@ -485,7 +478,7 @@ func AssertNotNil(t testRunner, object any, msg ...any) {
485478
test.Helper()
486479
}
487480

488-
if internal.IsNil(object) {
481+
if assert.Nil(object) {
489482
internal.Fail(t, "An object that !!should not be nil!! is nil.", internal.NewObjectsSingleUnknown(object), msg...)
490483
}
491484
}
@@ -1067,7 +1060,7 @@ func AssertUnique[elementType comparable](t testRunner, list []elementType, msg
10671060
test.Helper()
10681061
}
10691062

1070-
if len(FuzzUtilDistinctSet(list)) != len(list) {
1063+
if !assert.Unique(list) {
10711064
internal.Fail(t, "The list is !!not unique!!.", internal.NewObjectsSingleNamed("List", list), msg...)
10721065
}
10731066
}
@@ -1084,7 +1077,7 @@ func AssertNotUnique[elementType comparable](t testRunner, list []elementType, m
10841077
test.Helper()
10851078
}
10861079

1087-
if len(FuzzUtilDistinctSet(list)) == len(list) {
1080+
if assert.Unique(list) {
10881081
internal.Fail(t, "The list !!is unique!!, but should not.", internal.NewObjectsSingleNamed("List", list), msg...)
10891082
}
10901083
}

go.mod

+1
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ module github.com/MarvinJWendt/testza
33
go 1.18
44

55
require (
6+
atomicgo.dev/assert v0.0.2
67
github.com/davecgh/go-spew v1.1.1
78
github.com/klauspost/cpuid/v2 v2.2.0
89
github.com/pterm/pterm v0.12.49

go.sum

+2
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
atomicgo.dev/assert v0.0.2 h1:FiKeMiZSgRrZsPo9qn/7vmr7mCsh5SZyXY4YGYiYwrg=
2+
atomicgo.dev/assert v0.0.2/go.mod h1:ut4NcI3QDdJtlmAxQULOmA13Gz6e2DWbSAS8RUOmNYQ=
13
atomicgo.dev/cursor v0.1.1 h1:0t9sxQomCTRh5ug+hAMCs59x/UmC9QL6Ci5uosINKD4=
24
atomicgo.dev/cursor v0.1.1/go.mod h1:Lr4ZJB3U7DfPPOkbH7/6TOtJ4vFGHlgj1nc+n900IpU=
35
atomicgo.dev/keyboard v0.2.8 h1:Di09BitwZgdTV1hPyX/b9Cqxi8HVuJQwWivnZUEqlj4=

internal/assertion_helper.go

+3-68
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package internal
22

33
import (
4+
"atomicgo.dev/assert"
45
"bytes"
56
"errors"
67
"fmt"
@@ -12,53 +13,6 @@ import (
1213
"time"
1314
)
1415

15-
// IsKind returns if an object is kind of a specific kind.
16-
func IsKind(expectedKind reflect.Kind, value any) bool {
17-
return reflect.TypeOf(value).Kind() == expectedKind
18-
}
19-
20-
// IsNil checks if an object is nil.
21-
func IsNil(object any) bool {
22-
if object == nil {
23-
return true
24-
}
25-
26-
switch reflect.ValueOf(object).Kind() {
27-
case reflect.Chan, reflect.Func, reflect.Interface, reflect.Map, reflect.Ptr, reflect.Slice:
28-
return reflect.ValueOf(object).IsNil()
29-
}
30-
31-
return false
32-
}
33-
34-
// IsNumber checks if the value is of a numeric kind.
35-
func IsNumber(value any) bool {
36-
numberKinds := []reflect.Kind{
37-
reflect.Int,
38-
reflect.Int8,
39-
reflect.Int16,
40-
reflect.Int32,
41-
reflect.Int64,
42-
reflect.Float32,
43-
reflect.Float64,
44-
reflect.Uint,
45-
reflect.Uint8,
46-
reflect.Uint16,
47-
reflect.Uint32,
48-
reflect.Uint64,
49-
reflect.Complex64,
50-
reflect.Complex128,
51-
}
52-
53-
for _, k := range numberKinds {
54-
if IsKind(k, value) {
55-
return true
56-
}
57-
}
58-
59-
return false
60-
}
61-
6216
// CompletesIn returns if a function completes in a specific time.
6317
func CompletesIn(duration time.Duration, f func()) bool {
6418
done := make(chan bool)
@@ -75,11 +29,6 @@ func CompletesIn(duration time.Duration, f func()) bool {
7529
}
7630
}
7731

78-
// IsZero checks if a value is the zero value of its type.
79-
func IsZero(value any) bool {
80-
return value == nil || reflect.DeepEqual(value, reflect.Zero(reflect.TypeOf(value)).Interface())
81-
}
82-
8332
// IsEqual checks if two objects are equal.
8433
func IsEqual(expected any, actual any) bool {
8534
if expected == nil || actual == nil {
@@ -121,20 +70,6 @@ func HasEqualValues(expected any, actual any) bool {
12170
return false
12271
}
12372

124-
// DoesImplement checks if an objects implements an interface.
125-
func DoesImplement(interfaceObject, object any) bool {
126-
interfaceType := reflect.TypeOf(interfaceObject).Elem()
127-
128-
if object == nil {
129-
return false
130-
}
131-
if !reflect.TypeOf(object).Implements(interfaceType) {
132-
return false
133-
}
134-
135-
return true
136-
}
137-
13873
// DoesContain checks that ab objects contains an element.
13974
func DoesContain(object, element any) bool {
14075
objectValue := reflect.ValueOf(object)
@@ -285,7 +220,7 @@ func IsList(list any) bool {
285220
}
286221

287222
func HasSameElements(expected any, actual any) bool {
288-
if IsNil(expected) || IsNil(actual) {
223+
if assert.Nil(expected) || assert.Nil(actual) {
289224
return expected == actual
290225
}
291226

@@ -339,7 +274,7 @@ func IsSubset(t testRunner, list any, subset any) bool {
339274
test.Helper()
340275
}
341276

342-
if IsNil(subset) {
277+
if assert.Nil(subset) {
343278
return true
344279
}
345280

0 commit comments

Comments
 (0)