11package assert
22
33import (
4- "reflect "
4+ "github.com/gravitton/assert/internal "
55)
66
77// Testing is an interface wrapper around *testing.T
88type Testing interface {
9- Errorf (format string , args ... any )
10- }
11-
12- // helper is an interface wrapper around *testing.T
13- type helper interface {
149 Helper ()
10+ Errorf (format string , args ... any )
1511}
1612
1713// True asserts that the specified value is true.
1814//
1915// assert.True(t, condition)
20- func True (t Testing , actual bool ) bool {
21- if h , ok := t .(helper ); ok {
22- h .Helper ()
23- }
16+ func True (t Testing , condition bool ) bool {
17+ t .Helper ()
2418
25- if ! actual {
19+ if ! internal . Assert ( condition ) {
2620 return Fail (t , "Should be true" )
2721 }
2822
@@ -32,12 +26,10 @@ func True(t Testing, actual bool) bool {
3226// False asserts that the specified value is false.
3327//
3428// assert.False(t, condition)
35- func False (t Testing , actual bool ) bool {
36- if h , ok := t .(helper ); ok {
37- h .Helper ()
38- }
29+ func False (t Testing , condition bool ) bool {
30+ t .Helper ()
3931
40- if actual {
32+ if internal . Assert ( condition ) {
4133 return Fail (t , "Should be false" )
4234 }
4335
@@ -51,12 +43,10 @@ func False(t Testing, actual bool) bool {
5143// Pointer variable equality is determined based on the equality of the
5244// referenced values (as opposed to the memory addresses).
5345func Equal (t Testing , actual , expected any ) bool {
54- if h , ok := t .(helper ); ok {
55- h .Helper ()
56- }
46+ t .Helper ()
5747
58- if ! equal (actual , expected ) {
59- return Failf (t , "Should be equal: \n actual : %#v\n expected : %#v" , actual , expected )
48+ if ! internal . Equal (actual , expected ) {
49+ return Failf (t , "Should be equal:\n object : %#v\n element : %#v" , actual , expected )
6050 }
6151
6252 return true
@@ -69,38 +59,26 @@ func Equal(t Testing, actual, expected any) bool {
6959// Pointer variable equality is determined based on the equality of the
7060// referenced values (as opposed to the memory addresses).
7161func NotEqual (t Testing , actual , expected any ) bool {
72- if h , ok := t .(helper ); ok {
73- h .Helper ()
74- }
62+ t .Helper ()
7563
76- if equal (actual , expected ) {
77- return Failf (t , "Should not be equal: %#v" , actual )
64+ if internal . Equal (actual , expected ) {
65+ return Failf (t , "Should not be equal\n object : %#v" , actual )
7866 }
7967
8068 return true
8169}
8270
83- func equal (actual , expected any ) bool {
84- if actual == nil || expected == nil {
85- return actual == expected
86- }
87-
88- return reflect .DeepEqual (actual , expected )
89- }
90-
9171// Same asserts that two pointers reference the same object.
9272//
9373// assert.Same(t, actual, expected)
9474//
9575// Both arguments must be pointer variables. Pointer variable sameness is
9676// determined based on the equality of both type and value.
9777func Same (t Testing , actual , expected any ) bool {
98- if h , ok := t .(helper ); ok {
99- h .Helper ()
100- }
78+ t .Helper ()
10179
102- if ! same (expected , actual ) {
103- return Failf (t , "Should be same: \n actual : %[1]p %#[1]v\n expected : %[2]p %#[2]v" , actual , expected )
80+ if ! internal . Same (expected , actual ) {
81+ return Failf (t , "Should be same\n object : %[1]p %#[1]v\n element : %[2]p %#[2]v" , actual , expected )
10482 }
10583
10684 return true
@@ -113,37 +91,92 @@ func Same(t Testing, actual, expected any) bool {
11391// Both arguments must be pointer variables. Pointer variable sameness is
11492// determined based on the equality of both type and value.
11593func NotSame (t Testing , actual , expected any ) bool {
116- if h , ok := t .(helper ); ok {
117- h .Helper ()
94+ t .Helper ()
95+
96+ if internal .Same (expected , actual ) {
97+ return Failf (t , "Should not be same\n object: %[1]p %#[1]v" , actual )
98+ }
99+
100+ return true
101+ }
102+
103+ func Length (t Testing , object any , expected int ) bool {
104+ t .Helper ()
105+
106+ if actual := internal .Length (object ); actual != expected {
107+ return Failf (t , "Should have element length\n object: %#v\n object: %d\n element: %d" , object , actual , expected )
108+ }
109+
110+ return true
111+ }
112+
113+ func Contains (t Testing , object , element any ) bool {
114+ t .Helper ()
115+
116+ if found , ok := internal .Contains (object , element ); ! ok {
117+ return Failf (t , "Should be iterable\n object: %#v" , object )
118+ } else if ! found {
119+ return Failf (t , "Should contain element\n object: %#v\n element: %#v" , object , element )
118120 }
119121
120- if same (expected , actual ) {
121- return Failf (t , "Should not be same: %[1]p %#[1]v" , actual )
122+ return true
123+ }
124+
125+ func NotContains (t Testing , object any , element any ) bool {
126+ t .Helper ()
127+
128+ if found , ok := internal .Contains (object , element ); ! ok {
129+ return Failf (t , "Should be iterable\n object: %#v" , object )
130+ } else if found {
131+ return Failf (t , "Should not contain element\n object: %#v\n element: %#v" , object , element )
122132 }
123133
124134 return true
125135}
126136
127- func same (actual , expected any ) bool {
128- actualPtr , expectedPtr := reflect .ValueOf (actual ), reflect .ValueOf (expected )
129- if actualPtr .Kind () != reflect .Ptr || expectedPtr .Kind () != reflect .Ptr {
130- return false
137+ func Error (t Testing , err error ) bool {
138+ t .Helper ()
139+
140+ if ! internal .Error (err ) {
141+ return Failf (t , "Should be error" )
131142 }
132143
133- actualType , expectedType := reflect .TypeOf (actual ), reflect .TypeOf (expected )
134- if actualType != expectedType {
135- return false
144+ return true
145+ }
146+
147+ func NoError (t Testing , err error ) bool {
148+ t .Helper ()
149+
150+ if internal .Error (err ) {
151+ return Failf (t , "Should not be error\n error: %#v" , err )
136152 }
137153
138- // compare pointer addresses
139- return actual == expected
154+ return true
155+ }
156+
157+ func ErrorIs (t Testing , err error , target error ) bool {
158+ t .Helper ()
159+
160+ if ! internal .ErrorIs (err , target ) {
161+ return Failf (t , "Should be same error\n error: %#v\n target: %#v" , err , target )
162+ }
163+
164+ return true
165+ }
166+
167+ func NotErrorIs (t Testing , err error , target error ) bool {
168+ t .Helper ()
169+
170+ if internal .ErrorIs (err , target ) {
171+ return Failf (t , "Should not be same error\n error: %#v" , err , target )
172+ }
173+
174+ return true
140175}
141176
142177// Fail reports a failure through
143178func Fail (t Testing , message string ) bool {
144- if h , ok := t .(helper ); ok {
145- h .Helper ()
146- }
179+ t .Helper ()
147180
148181 t .Errorf (message )
149182
@@ -152,9 +185,7 @@ func Fail(t Testing, message string) bool {
152185
153186// Failf reports a failure through
154187func Failf (t Testing , format string , args ... any ) bool {
155- if h , ok := t .(helper ); ok {
156- h .Helper ()
157- }
188+ t .Helper ()
158189
159190 t .Errorf (format , args ... )
160191
0 commit comments