Skip to content

Commit

Permalink
migrated most methods to use github.com/atomicgo/assert
Browse files Browse the repository at this point in the history
  • Loading branch information
MarvinJWendt committed Nov 22, 2022
1 parent 304bcb0 commit 093d539
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 98 deletions.
53 changes: 23 additions & 30 deletions assert.go
@@ -1,6 +1,7 @@
package testza

import (
"atomicgo.dev/assert"
"errors"
"fmt"
"os"
Expand Down Expand Up @@ -68,7 +69,7 @@ func AssertKindOf(t testRunner, expectedKind reflect.Kind, object any, msg ...an
test.Helper()
}

if !internal.IsKind(expectedKind, object) {
if !assert.Kind(object, expectedKind) {
internal.Fail(t,
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()),
internal.NewObjectsExpectedActual(expectedKind, object),
Expand All @@ -93,7 +94,7 @@ func AssertNotKindOf(t testRunner, kind reflect.Kind, object any, msg ...any) {
test.Helper()
}

if internal.IsKind(kind, object) {
if assert.Kind(object, kind) {
internal.Fail(t,
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()),
internal.Objects{
Expand All @@ -117,7 +118,7 @@ func AssertNotKindOf(t testRunner, kind reflect.Kind, object any, msg ...any) {
// testza.AssertNumeric(t, 1.23)
// testza.AssertNumeric(t, uint(123))
func AssertNumeric(t testRunner, object any, msg ...any) {
if !internal.IsNumber(object) {
if !assert.Number(object) {
internal.Fail(t, "An object that !!should be a number!! is not of a numeric type.", internal.NewObjectsSingleUnknown(object), msg...)
}
}
Expand All @@ -133,7 +134,7 @@ func AssertNumeric(t testRunner, object any, msg ...any) {
// testza.AssertNotNumeric(t, true)
// testza.AssertNotNumeric(t, "123")
func AssertNotNumeric(t testRunner, object any, msg ...any) {
if internal.IsNumber(object) {
if assert.Number(object) {
internal.Fail(t, "An object that !!should not be a number!! is of a numeric type.", internal.NewObjectsSingleUnknown(object), msg...)
}
}
Expand All @@ -152,7 +153,7 @@ func AssertZero(t testRunner, value any, msg ...any) {
test.Helper()
}

if !internal.IsZero(value) {
if !assert.Zero(value) {
internal.Fail(t, "An object that !!should have its zero value!!, does not have its zero value.", internal.NewObjectsSingleUnknown(value), msg...)
}
}
Expand All @@ -171,7 +172,7 @@ func AssertNotZero(t testRunner, value any, msg ...any) {
test.Helper()
}

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

if !internal.IsEqual(expected, actual) {
if !assert.Equal(expected, actual) {
internal.Fail(t, "Two objects that !!should be equal!!, are not equal.", internal.NewObjectsExpectedActualWithDiff(expected, actual), msg...)
}
}
Expand All @@ -207,7 +208,7 @@ func AssertNotEqual(t testRunner, expected any, actual any, msg ...any) {
test.Helper()
}

if internal.IsEqual(expected, actual) {
if assert.Equal(expected, actual) {
objects := internal.Objects{
{
Name: "Both Objects",
Expand Down Expand Up @@ -341,7 +342,7 @@ func AssertImplements(t testRunner, interfaceObject, object any, msg ...any) {
test.Helper()
}

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

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

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

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

defer func() {
if r := recover(); r == nil {
internal.Fail(t, "A function that !!should panic!! did not panic.", internal.Objects{}, msg...)
}
}()

f()
if !assert.Panic(f) {
internal.Fail(t, "A function that !!should panic!! did not panic.", internal.Objects{}, msg...)
}
}

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

defer func() {
if r := recover(); r != nil {
internal.Fail(t, "A function that !!should not panic!! did panic.", internal.Objects{}, msg...)
}
}()

f()
if assert.Panic(f) {
internal.Fail(t, "A function that !!should not panic!! did panic.", internal.Objects{}, msg...)
}
}

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

if !internal.IsNil(object) {
if !assert.Nil(object) {
internal.Fail(t, "An object that !!should be nil!! is not nil.", internal.NewObjectsExpectedActual(nil, object), msg...)
}
}
Expand All @@ -485,7 +478,7 @@ func AssertNotNil(t testRunner, object any, msg ...any) {
test.Helper()
}

if internal.IsNil(object) {
if assert.Nil(object) {
internal.Fail(t, "An object that !!should not be nil!! is nil.", internal.NewObjectsSingleUnknown(object), msg...)
}
}
Expand Down Expand Up @@ -1067,7 +1060,7 @@ func AssertUnique[elementType comparable](t testRunner, list []elementType, msg
test.Helper()
}

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

if len(FuzzUtilDistinctSet(list)) == len(list) {
if assert.Unique(list) {
internal.Fail(t, "The list !!is unique!!, but should not.", internal.NewObjectsSingleNamed("List", list), msg...)
}
}
Expand Down
1 change: 1 addition & 0 deletions go.mod
Expand Up @@ -10,6 +10,7 @@ require (
)

require (
atomicgo.dev/assert v0.0.2 // indirect
atomicgo.dev/cursor v0.1.1 // indirect
atomicgo.dev/keyboard v0.2.8 // indirect
github.com/containerd/console v1.0.3 // indirect
Expand Down
2 changes: 2 additions & 0 deletions go.sum
@@ -1,3 +1,5 @@
atomicgo.dev/assert v0.0.2 h1:FiKeMiZSgRrZsPo9qn/7vmr7mCsh5SZyXY4YGYiYwrg=
atomicgo.dev/assert v0.0.2/go.mod h1:ut4NcI3QDdJtlmAxQULOmA13Gz6e2DWbSAS8RUOmNYQ=
atomicgo.dev/cursor v0.1.1 h1:0t9sxQomCTRh5ug+hAMCs59x/UmC9QL6Ci5uosINKD4=
atomicgo.dev/cursor v0.1.1/go.mod h1:Lr4ZJB3U7DfPPOkbH7/6TOtJ4vFGHlgj1nc+n900IpU=
atomicgo.dev/keyboard v0.2.8 h1:Di09BitwZgdTV1hPyX/b9Cqxi8HVuJQwWivnZUEqlj4=
Expand Down
71 changes: 3 additions & 68 deletions internal/assertion_helper.go
@@ -1,6 +1,7 @@
package internal

import (
"atomicgo.dev/assert"
"bytes"
"errors"
"fmt"
Expand All @@ -12,53 +13,6 @@ import (
"time"
)

// IsKind returns if an object is kind of a specific kind.
func IsKind(expectedKind reflect.Kind, value any) bool {
return reflect.TypeOf(value).Kind() == expectedKind
}

// IsNil checks if an object is nil.
func IsNil(object any) bool {
if object == nil {
return true
}

switch reflect.ValueOf(object).Kind() {
case reflect.Chan, reflect.Func, reflect.Interface, reflect.Map, reflect.Ptr, reflect.Slice:
return reflect.ValueOf(object).IsNil()
}

return false
}

// IsNumber checks if the value is of a numeric kind.
func IsNumber(value any) bool {
numberKinds := []reflect.Kind{
reflect.Int,
reflect.Int8,
reflect.Int16,
reflect.Int32,
reflect.Int64,
reflect.Float32,
reflect.Float64,
reflect.Uint,
reflect.Uint8,
reflect.Uint16,
reflect.Uint32,
reflect.Uint64,
reflect.Complex64,
reflect.Complex128,
}

for _, k := range numberKinds {
if IsKind(k, value) {
return true
}
}

return false
}

// CompletesIn returns if a function completes in a specific time.
func CompletesIn(duration time.Duration, f func()) bool {
done := make(chan bool)
Expand All @@ -75,11 +29,6 @@ func CompletesIn(duration time.Duration, f func()) bool {
}
}

// IsZero checks if a value is the zero value of its type.
func IsZero(value any) bool {
return value == nil || reflect.DeepEqual(value, reflect.Zero(reflect.TypeOf(value)).Interface())
}

// IsEqual checks if two objects are equal.
func IsEqual(expected any, actual any) bool {
if expected == nil || actual == nil {
Expand Down Expand Up @@ -121,20 +70,6 @@ func HasEqualValues(expected any, actual any) bool {
return false
}

// DoesImplement checks if an objects implements an interface.
func DoesImplement(interfaceObject, object any) bool {
interfaceType := reflect.TypeOf(interfaceObject).Elem()

if object == nil {
return false
}
if !reflect.TypeOf(object).Implements(interfaceType) {
return false
}

return true
}

// DoesContain checks that ab objects contains an element.
func DoesContain(object, element any) bool {
objectValue := reflect.ValueOf(object)
Expand Down Expand Up @@ -285,7 +220,7 @@ func IsList(list any) bool {
}

func HasSameElements(expected any, actual any) bool {
if IsNil(expected) || IsNil(actual) {
if assert.Nil(expected) || assert.Nil(actual) {
return expected == actual
}

Expand Down Expand Up @@ -339,7 +274,7 @@ func IsSubset(t testRunner, list any, subset any) bool {
test.Helper()
}

if IsNil(subset) {
if assert.Nil(subset) {
return true
}

Expand Down

0 comments on commit 093d539

Please sign in to comment.