Include JSONEquals and CodecEquals in docs
Francesco Banconi
4 years ago
37 | 37 | } |
38 | 38 | |
39 | 39 | // Equals is a Checker checking equality of two comparable values. |
40 | // | |
40 | 41 | // For instance: |
41 | 42 | // |
42 | 43 | // c.Assert(answer, qt.Equals, 42) |
74 | 75 | // CmpEquals returns a Checker checking equality of two arbitrary values |
75 | 76 | // according to the provided compare options. See DeepEquals as an example of |
76 | 77 | // such a checker, commonly used when no compare options are required. |
78 | // | |
77 | 79 | // Example calls: |
78 | 80 | // |
79 | 81 | // c.Assert(list, qt.CmpEquals(cmpopts.SortSlices), []int{42, 47}) |
126 | 128 | // The comparison is done using the github.com/google/go-cmp/cmp package. |
127 | 129 | // When comparing structs, by default no exported fields are allowed. CmpEquals |
128 | 130 | // can be used when more customized compare options are required. |
131 | // | |
129 | 132 | // Example call: |
130 | 133 | // |
131 | 134 | // c.Assert(got, qt.DeepEquals, []int{42, 47}) |
141 | 144 | |
142 | 145 | // Matches is a Checker checking that the provided string or fmt.Stringer |
143 | 146 | // matches the provided regular expression pattern. |
147 | // | |
144 | 148 | // For instance: |
145 | 149 | // |
146 | 150 | // c.Assert("these are the voyages", qt.Matches, "these are .*") |
170 | 174 | |
171 | 175 | // ErrorMatches is a Checker checking that the provided value is an error whose |
172 | 176 | // message matches the provided regular expression pattern. |
177 | // | |
173 | 178 | // For instance: |
174 | 179 | // |
175 | 180 | // c.Assert(err, qt.ErrorMatches, "bad wolf .*") |
198 | 203 | |
199 | 204 | // PanicMatches is a Checker checking that the provided function panics with a |
200 | 205 | // message matching the provided regular expression pattern. |
206 | // | |
201 | 207 | // For instance: |
202 | 208 | // |
203 | 209 | // c.Assert(func() {panic("bad wolf ...")}, qt.PanicMatches, "bad wolf .*") |
240 | 246 | } |
241 | 247 | |
242 | 248 | // IsNil is a Checker checking that the provided value is nil. |
249 | // | |
243 | 250 | // For instance: |
244 | 251 | // |
245 | 252 | // c.Assert(got, qt.IsNil) |
265 | 272 | } |
266 | 273 | |
267 | 274 | // HasLen is a Checker checking that the provided value has the given length. |
275 | // | |
268 | 276 | // For instance: |
269 | 277 | // |
270 | 278 | // c.Assert([]int{42, 47}, qt.HasLen, 2) |
303 | 311 | // Satisfies is a Checker checking that the provided value, when used as |
304 | 312 | // argument of the provided predicate function, causes the function to return |
305 | 313 | // true. The function must be of type func(T) bool, having got assignable to T. |
314 | // | |
306 | 315 | // For instance: |
307 | 316 | // |
308 | 317 | // // Check that an error from os.Open satisfies os.IsNotExist. |
351 | 360 | } |
352 | 361 | |
353 | 362 | // Not returns a Checker negating the given Checker. |
363 | // | |
354 | 364 | // For instance: |
355 | 365 | // |
356 | 366 | // c.Assert(got, qt.Not(qt.IsNil)) |
390 | 400 | // |
391 | 401 | // For example: |
392 | 402 | // |
393 | // c.Assert("hello world", qt.Contains, "world") | |
394 | // c.Assert([]int{3,5,7,99}, qt.Contains, 7) | |
403 | // c.Assert("hello world", qt.Contains, "world") | |
404 | // c.Assert([]int{3,5,7,99}, qt.Contains, 7) | |
405 | // | |
395 | 406 | var Contains Checker = &containsChecker{ |
396 | 407 | argNames: []string{"got", "want"}, |
397 | 408 | } |
421 | 432 | // |
422 | 433 | // For example: |
423 | 434 | // |
424 | // c.Assert([]int{3,5,7,99}, qt.Any(qt.Equals), 7) | |
425 | // c.Assert([][]string{{"a", "b"}, {"c", "d"}}, qt.Any(qt.DeepEquals), []string{"c", "d"}) | |
435 | // c.Assert([]int{3,5,7,99}, qt.Any(qt.Equals), 7) | |
436 | // c.Assert([][]string{{"a", "b"}, {"c", "d"}}, qt.Any(qt.DeepEquals), []string{"c", "d"}) | |
426 | 437 | // |
427 | 438 | // See also All and Contains. |
428 | 439 | func Any(c Checker) Checker { |
472 | 483 | // |
473 | 484 | // For example: |
474 | 485 | // |
475 | // c.Assert([]int{3, 5, 8}, qt.All(qt.Not(qt.Equals)), 0) | |
476 | // c.Assert([][]string{{"a", "b"}, {"a", "b"}}, qt.All(qt.DeepEquals), []string{"c", "d"}) | |
486 | // c.Assert([]int{3, 5, 8}, qt.All(qt.Not(qt.Equals)), 0) | |
487 | // c.Assert([][]string{{"a", "b"}, {"a", "b"}}, qt.All(qt.DeepEquals), []string{"c", "d"}) | |
477 | 488 | // |
478 | 489 | // See also Any and Contains. |
479 | 490 | func All(c Checker) Checker { |
536 | 547 | // |
537 | 548 | // It uses DeepEquals to do the comparison. If a more sophisticated |
538 | 549 | // comparison is required, use CodecEquals directly. |
550 | // | |
551 | // For instance: | |
552 | // | |
553 | // c.Assert(`{"First": 47.11}`, qt.JSONEquals, &MyStruct{First: 47.11}) | |
554 | // | |
539 | 555 | var JSONEquals = CodecEquals(json.Marshal, json.Unmarshal) |
540 | 556 | |
541 | 557 | type codecEqualChecker struct { |
547 | 563 | |
548 | 564 | // CodecEquals returns a checker that checks for codec value equivalence. |
549 | 565 | // |
550 | // It expects two arguments: a byte slice or a string containing some codec-marshaled | |
551 | // data, and a Go value. | |
566 | // It expects two arguments: a byte slice or a string containing some | |
567 | // codec-marshaled data, and a Go value. | |
552 | 568 | // |
553 | 569 | // It uses unmarshal to unmarshal the data into an interface{} value. |
554 | 570 | // It marshals the Go value using marshal, then unmarshals the result into |
555 | 571 | // an interface{} value. |
556 | 572 | // |
557 | // It then checks that the two interface{} values are deep-equal to one another, | |
558 | // using CmpEquals(opts) to perform the check. | |
573 | // It then checks that the two interface{} values are deep-equal to one | |
574 | // another, using CmpEquals(opts) to perform the check. | |
559 | 575 | // |
560 | 576 | // See JSONEquals for an example of this in use. |
561 | 577 | func CodecEquals( |
72 | 72 | Equals |
73 | 73 | |
74 | 74 | Equals checks that two values are equal, as compared with Go's == operator. |
75 | ||
75 | 76 | For instance: |
76 | 77 | |
77 | 78 | c.Assert(answer, qt.Equals, 42) |
86 | 87 | |
87 | 88 | DeepEquals checks that two arbitrary values are deeply equal. |
88 | 89 | The comparison is done using the github.com/google/go-cmp/cmp package. |
89 | When comparing structs, by default no exported fields are allowed. CmpEquals | |
90 | (see below) can be used when more customized compare options are required. | |
90 | When comparing structs, by default no exported fields are allowed. | |
91 | If a more sophisticated comparison is required, use CmpEquals (see below). | |
92 | ||
91 | 93 | Example call: |
92 | 94 | |
93 | 95 | c.Assert(got, qt.DeepEquals, []int{42, 47}) |
97 | 99 | CmpEquals checks equality of two arbitrary values according to the provided |
98 | 100 | compare options. DeepEquals is more commonly used when no compare options are |
99 | 101 | required. |
102 | ||
100 | 103 | Example calls: |
101 | 104 | |
102 | 105 | c.Assert(list, qt.CmpEquals(cmpopts.SortSlices), []int{42, 47}) |
106 | 109 | |
107 | 110 | Matches checks that a string or result of calling the String method |
108 | 111 | (if the value implements fmt.Stringer) matches the provided regular expression. |
112 | ||
109 | 113 | For instance: |
110 | 114 | |
111 | 115 | c.Assert("these are the voyages", qt.Matches, `these are .*`) |
115 | 119 | |
116 | 120 | ErrorMatches checks that the provided value is an error whose message matches |
117 | 121 | the provided regular expression. |
122 | ||
118 | 123 | For instance: |
119 | 124 | |
120 | 125 | c.Assert(err, qt.ErrorMatches, `bad wolf .*`) |
123 | 128 | |
124 | 129 | PanicMatches checks that the provided function panics with a message matching |
125 | 130 | the provided regular expression. |
131 | ||
126 | 132 | For instance: |
127 | 133 | |
128 | 134 | c.Assert(func() {panic("bad wolf ...")}, qt.PanicMatches, `bad wolf .*`) |
130 | 136 | IsNil |
131 | 137 | |
132 | 138 | IsNil checks that the provided value is nil. |
139 | ||
133 | 140 | For instance: |
134 | 141 | |
135 | 142 | c.Assert(got, qt.IsNil) |
137 | 144 | HasLen |
138 | 145 | |
139 | 146 | HasLen checks that the provided value has the given length. |
147 | ||
140 | 148 | For instance: |
141 | 149 | |
142 | 150 | c.Assert([]int{42, 47}, qt.HasLen, 2) |
147 | 155 | Satisfies checks that the provided value, when used as argument of the provided |
148 | 156 | predicate function, causes the function to return true. The function must be of |
149 | 157 | type func(T) bool, having got assignable to T. |
158 | ||
150 | 159 | For instance: |
151 | 160 | |
152 | 161 | // Check that an error from os.Open satisfies os.IsNotExist. |
158 | 167 | Not |
159 | 168 | |
160 | 169 | Not returns a Checker negating the given Checker. |
170 | ||
161 | 171 | For instance: |
162 | 172 | |
163 | 173 | c.Assert(got, qt.Not(qt.IsNil)) |
169 | 179 | same as using Any(Equals), except that it has a special case for strings - if |
170 | 180 | the first argument is a string, the second argument must also be a string and |
171 | 181 | strings.Contains will be used. |
182 | ||
172 | 183 | For example: |
173 | 184 | |
174 | 185 | c.Assert("hello world", qt.Contains, "world") |
178 | 189 | |
179 | 190 | Any returns a Checker that uses the given checker to check elements of a slice |
180 | 191 | or array or the values from a map. It succeeds if any element passes the check. |
192 | ||
181 | 193 | For example: |
182 | 194 | |
183 | 195 | c.Assert([]int{3,5,7,99}, qt.Any(qt.Equals), 7) |
190 | 202 | All returns a Checker that uses the given checker to check elements of slice or |
191 | 203 | array or the values of a map. It succeeds if all elements pass the check. |
192 | 204 | On failure it prints the error from the first index that failed. |
205 | ||
193 | 206 | For example: |
194 | 207 | |
195 | 208 | c.Assert([]int{3, 5, 8}, qt.All(qt.Not(qt.Equals)), 0) |
196 | 209 | c.Assert([][]string{{"a", "b"}, {"a", "b"}}, qt.All(qt.DeepEquals), []string{"c", "d"}) |
197 | 210 | |
198 | 211 | See also Any and Contains. |
212 | ||
213 | JSONEquals | |
214 | ||
215 | JSONEquals checks whether a byte slice or string is JSON-equivalent to a Go | |
216 | value. See CodecEquals for more information. | |
217 | ||
218 | It uses DeepEquals to do the comparison. If a more sophisticated comparison is | |
219 | required, use CodecEquals directly. | |
220 | ||
221 | For instance: | |
222 | ||
223 | c.Assert(`{"First": 47.11}`, qt.JSONEquals, &MyStruct{First: 47.11}) | |
224 | ||
225 | CodecEquals | |
226 | ||
227 | CodecEquals returns a checker that checks for codec value equivalence. | |
228 | ||
229 | func CodecEquals( | |
230 | marshal func(interface{}) ([]byte, error), | |
231 | unmarshal func([]byte, interface{}) error, | |
232 | opts ...cmp.Option, | |
233 | ) Checker | |
234 | ||
235 | It expects two arguments: a byte slice or a string containing some | |
236 | codec-marshaled data, and a Go value. | |
237 | ||
238 | It uses unmarshal to unmarshal the data into an interface{} value. | |
239 | It marshals the Go value using marshal, then unmarshals the result into | |
240 | an interface{} value. | |
241 | ||
242 | It then checks that the two interface{} values are deep-equal to one another, | |
243 | using CmpEquals(opts) to perform the check. | |
244 | ||
245 | See JSONEquals for an example of this in use. | |
199 | 246 | */ |
200 | 247 | package quicktest |