44package assertions
55
66import (
7- "errors"
87 "fmt"
98 "iter"
109 "slices"
1110 "testing"
12- "time"
1311)
1412
1513const shortpkg = "assertions"
1614
1715func TestEqualUnexportedImplementationDetails (t * testing.T ) {
1816 t .Parallel ()
1917
20- t .Run ("samePointers" , testSamePointers ())
2118 t .Run ("formatUnequalValue" , testFormatUnequalValues ())
22- t .Run ("isEmpty" , testIsEmpty ())
2319 t .Run ("validateEqualArgs" , testValidateEqualArgs ())
2420}
2521
26- func testSamePointers () func (* testing.T ) {
27- return func (t * testing.T ) {
28- t .Parallel ()
29-
30- for tt := range equalSamePointersCases () {
31- t .Run (tt .name , func (t * testing.T ) {
32- t .Parallel ()
33-
34- same , ok := samePointers (tt .args .first , tt .args .second )
35- tt .same (t , same )
36- tt .ok (t , ok )
37- })
38- }
39- }
40- }
41-
4222func testFormatUnequalValues () func (* testing.T ) {
4323 return func (t * testing.T ) {
4424 t .Parallel ()
@@ -55,131 +35,6 @@ func testFormatUnequalValues() func(*testing.T) {
5535 }
5636}
5737
58- func testIsEmpty () func (* testing.T ) {
59- return func (t * testing.T ) {
60- t .Parallel ()
61-
62- chWithValue := make (chan struct {}, 1 )
63- chWithValue <- struct {}{}
64-
65- True (t , isEmpty ("" ))
66- True (t , isEmpty (nil ))
67- True (t , isEmpty (error (nil )))
68- True (t , isEmpty ((* int )(nil )))
69- True (t , isEmpty ((* string )(nil )))
70- True (t , isEmpty (new (string )))
71- True (t , isEmpty ([]string {}))
72- True (t , isEmpty ([]string (nil )))
73- True (t , isEmpty ([]byte (nil )))
74- True (t , isEmpty ([]byte {}))
75- True (t , isEmpty ([]byte ("" )))
76- True (t , isEmpty ([]bool (nil )))
77- True (t , isEmpty ([]bool {}))
78- True (t , isEmpty ([]any (nil )))
79- True (t , isEmpty ([]any {}))
80- True (t , isEmpty (struct {}{}))
81- True (t , isEmpty (& struct {}{}))
82- True (t , isEmpty (struct { A int }{A : 0 }))
83- True (t , isEmpty (struct { a int }{a : 0 }))
84- True (t , isEmpty (struct {
85- a int
86- B int
87- }{a : 0 , B : 0 }))
88- True (t , isEmpty (0 ))
89- True (t , isEmpty (int (0 )))
90- True (t , isEmpty (int8 (0 )))
91- True (t , isEmpty (int16 (0 )))
92- True (t , isEmpty (uint16 (0 )))
93- True (t , isEmpty (int32 (0 )))
94- True (t , isEmpty (uint32 (0 )))
95- True (t , isEmpty (int64 (0 )))
96- True (t , isEmpty (uint64 (0 )))
97- True (t , isEmpty ('\u0000' )) // rune => int32
98- True (t , isEmpty (float32 (0 )))
99- True (t , isEmpty (float64 (0 )))
100- True (t , isEmpty (0i )) // complex
101- True (t , isEmpty (0.0i )) // complex
102- True (t , isEmpty (false ))
103- True (t , isEmpty (new (bool )))
104- True (t , isEmpty (map [string ]string {}))
105- True (t , isEmpty (map [string ]string (nil )))
106- True (t , isEmpty (new (time.Time )))
107- True (t , isEmpty (time.Time {}))
108- True (t , isEmpty (make (chan struct {})))
109- True (t , isEmpty (chan struct {}(nil )))
110- True (t , isEmpty (chan <- struct {}(nil )))
111- True (t , isEmpty (make (chan struct {})))
112- True (t , isEmpty (make (chan <- struct {})))
113- True (t , isEmpty (make (chan struct {}, 1 )))
114- True (t , isEmpty (make (chan <- struct {}, 1 )))
115- True (t , isEmpty ([1 ]int {0 }))
116- True (t , isEmpty ([2 ]int {0 , 0 }))
117- True (t , isEmpty ([8 ]int {}))
118- True (t , isEmpty ([... ]int {7 : 0 }))
119- True (t , isEmpty ([... ]bool {false , false }))
120- True (t , isEmpty (errors .New ("" ))) // BEWARE
121- True (t , isEmpty ([]error {}))
122- True (t , isEmpty ([]error (nil )))
123- True (t , isEmpty (& [1 ]int {0 }))
124- True (t , isEmpty (& [2 ]int {0 , 0 }))
125- False (t , isEmpty ("something" ))
126- False (t , isEmpty (errors .New ("something" )))
127- False (t , isEmpty ([]string {"something" }))
128- False (t , isEmpty (1 ))
129- False (t , isEmpty (int (1 )))
130- False (t , isEmpty (uint (1 )))
131- False (t , isEmpty (byte (1 )))
132- False (t , isEmpty (int8 (1 )))
133- False (t , isEmpty (uint8 (1 )))
134- False (t , isEmpty (int16 (1 )))
135- False (t , isEmpty (uint16 (1 )))
136- False (t , isEmpty (int32 (1 )))
137- False (t , isEmpty (uint32 (1 )))
138- False (t , isEmpty (int64 (1 )))
139- False (t , isEmpty (uint64 (1 )))
140- False (t , isEmpty ('A' )) // rune => int32
141- False (t , isEmpty (true ))
142- False (t , isEmpty (1.0 ))
143- False (t , isEmpty (1i )) // complex
144- False (t , isEmpty ([]byte {0 })) // elements values are ignored for slices
145- False (t , isEmpty ([]byte {0 , 0 })) // elements values are ignored for slices
146- False (t , isEmpty ([]string {"" })) // elements values are ignored for slices
147- False (t , isEmpty ([]string {"a" })) // elements values are ignored for slices
148- False (t , isEmpty ([]bool {false })) // elements values are ignored for slices
149- False (t , isEmpty ([]bool {true })) // elements values are ignored for slices
150- False (t , isEmpty ([]error {errors .New ("xxx" )}))
151- False (t , isEmpty ([]error {nil })) // BEWARE
152- False (t , isEmpty ([]error {errors .New ("" )})) // BEWARE
153- False (t , isEmpty (map [string ]string {"Hello" : "World" }))
154- False (t , isEmpty (map [string ]string {"" : "" }))
155- False (t , isEmpty (map [string ]string {"foo" : "" }))
156- False (t , isEmpty (map [string ]string {"" : "foo" }))
157- False (t , isEmpty (chWithValue ))
158- False (t , isEmpty ([1 ]bool {true }))
159- False (t , isEmpty ([2 ]bool {false , true }))
160- False (t , isEmpty ([... ]bool {10 : true }))
161- False (t , isEmpty ([]int {0 }))
162- False (t , isEmpty ([]int {42 }))
163- False (t , isEmpty ([1 ]int {42 }))
164- False (t , isEmpty ([2 ]int {0 , 42 }))
165- False (t , isEmpty (& [1 ]int {42 }))
166- False (t , isEmpty (& [2 ]int {0 , 42 }))
167- False (t , isEmpty ([1 ]* int {new (int )})) // array elements must be the zero value, not any Empty value
168- False (t , isEmpty (struct { A int }{A : 42 }))
169- False (t , isEmpty (struct { a int }{a : 42 }))
170- False (t , isEmpty (struct { a * int }{a : new (int )})) // fields must be the zero value, not any Empty value
171- False (t , isEmpty (struct { a []int }{a : []int {}})) // fields must be the zero value, not any Empty value
172- False (t , isEmpty (struct {
173- a int
174- B int
175- }{a : 0 , B : 42 }))
176- False (t , isEmpty (struct {
177- a int
178- B int
179- }{a : 42 , B : 0 }))
180- }
181- }
182-
18338func testValidateEqualArgs () func (* testing.T ) {
18439 return func (t * testing.T ) {
18540 t .Parallel ()
@@ -230,63 +85,3 @@ func formatUnequalCases() iter.Seq[formatUnequalCase] {
23085 {uint64 (123 ), uint64 (124 ), `123` , `124` , "uint64 should print clean" },
23186 })
23287}
233-
234- type samePointersCase struct {
235- name string
236- args args
237- same BoolAssertionFunc
238- ok BoolAssertionFunc
239- }
240-
241- type args struct {
242- first any
243- second any
244- }
245-
246- func equalSamePointersCases () iter.Seq [samePointersCase ] {
247- p := ptr (2 )
248- return slices .Values ([]samePointersCase {
249- {
250- name : "1 != 2" ,
251- args : args {first : 1 , second : 2 },
252- same : False ,
253- ok : False ,
254- },
255- {
256- name : "1 != 1 (not same ptr)" ,
257- args : args {first : 1 , second : 1 },
258- same : False ,
259- ok : False ,
260- },
261- {
262- name : "ptr(1) == ptr(1)" ,
263- args : args {first : p , second : p },
264- same : True ,
265- ok : True ,
266- },
267- {
268- name : "int(1) != float32(1)" ,
269- args : args {first : int (1 ), second : float32 (1 )},
270- same : False ,
271- ok : False ,
272- },
273- {
274- name : "array != slice" ,
275- args : args {first : [2 ]int {1 , 2 }, second : []int {1 , 2 }},
276- same : False ,
277- ok : False ,
278- },
279- {
280- name : "non-pointer vs pointer (1 != ptr(2))" ,
281- args : args {first : 1 , second : p },
282- same : False ,
283- ok : False ,
284- },
285- {
286- name : "pointer vs non-pointer (ptr(2) != 1)" ,
287- args : args {first : p , second : 1 },
288- same : False ,
289- ok : False ,
290- },
291- })
292- }
0 commit comments