Skip to content

Commit

Permalink
some tests for the testhelper
Browse files Browse the repository at this point in the history
  • Loading branch information
linkdata committed Oct 20, 2023
1 parent f972741 commit ee80642
Show file tree
Hide file tree
Showing 2 changed files with 78 additions and 42 deletions.
42 changes: 0 additions & 42 deletions helpers_test.go

This file was deleted.

78 changes: 78 additions & 0 deletions testhelper_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
package jaws

import (
"reflect"
"testing"
)

type testHelper struct{ *testing.T }

func (th testHelper) equal(a, b any) bool {
if reflect.DeepEqual(a, b) {
return true
}
aIsNil, aType := testNil(a)
bIsNil, bType := testNil(b)
if !(aIsNil && bIsNil) {
return false
}
return aType == nil || bType == nil || (aType == bType)
}

func (th testHelper) Equal(a, b any) {
th.Helper()
if !th.equal(a, b) {
th.Errorf("%#v != %#v", a, b)
}
}

func (th testHelper) True(a bool) {
th.Helper()
if !a {
th.Error("not true")
}
}

func (th testHelper) NoErr(err error) {
th.Helper()
if err != nil {
th.Error(err)
}
}

func testNil(object any) (bool, reflect.Type) {
if object == nil {
return true, nil
}
value := reflect.ValueOf(object)
kind := value.Kind()
return kind >= reflect.Chan && kind <= reflect.Slice && value.IsNil(), value.Type()
}

func Test_testHelper(t *testing.T) {
is := testHelper{t}

mustEqual := func(a, b any) {
t.Helper()
if !is.equal(a, b) {
t.Errorf("%#v != %#v", a, b)
}
}

mustNotEqual := func(a, b any) {
t.Helper()
if is.equal(a, b) {
t.Errorf("%#v == %#v", a, b)
}
}

mustEqual(1, 1)
mustEqual(nil, nil)
mustEqual(nil, (*testHelper)(nil))
mustNotEqual(1, nil)
mustNotEqual(nil, 1)
mustNotEqual((*testing.T)(nil), 1)
mustNotEqual(1, 2)
mustNotEqual((*testing.T)(nil), (*testHelper)(nil))
mustNotEqual(int(1), int32(1))
}

0 comments on commit ee80642

Please sign in to comment.