|
3 | 3 | package test |
4 | 4 |
|
5 | 5 | import ( |
| 6 | + "errors" |
6 | 7 | "fmt" |
7 | 8 | "os" |
8 | 9 | "strings" |
@@ -54,6 +55,10 @@ func (s score) Less(other score) bool { |
54 | 55 | return s < other |
55 | 56 | } |
56 | 57 |
|
| 58 | +func (s score) Equal(other score) bool { |
| 59 | + return s == other |
| 60 | +} |
| 61 | + |
57 | 62 | func ExampleAscending() { |
58 | 63 | nums := []int{1, 3, 4, 4, 9} |
59 | 64 | Ascending(t, nums) |
@@ -187,22 +192,78 @@ func ExampleEq() { |
187 | 192 | } |
188 | 193 |
|
189 | 194 | // EqError |
| 195 | +func ExampleEqError() { |
| 196 | + err := errors.New("undefined error") |
| 197 | + EqError(t, err, "undefined error") |
| 198 | + // Output: |
| 199 | +} |
190 | 200 |
|
191 | 201 | // EqFunc |
| 202 | +func ExampleEqFunc() { |
| 203 | + EqFunc(t, "abcd", "dcba", func(a, b string) bool { |
| 204 | + if len(a) != len(b) { |
| 205 | + return false |
| 206 | + } |
| 207 | + l := len(a) |
| 208 | + for i := 0; i < l; i++ { |
| 209 | + if a[i] != b[l-1-i] { |
| 210 | + return false |
| 211 | + } |
| 212 | + } |
| 213 | + return true |
| 214 | + }) |
| 215 | + // Output: |
| 216 | +} |
192 | 217 |
|
193 | 218 | // EqJSON |
| 219 | +func ExampleEqJSON() { |
| 220 | + a := `{"foo":"bar","numbers":[1,2,3]}` |
| 221 | + b := `{"numbers":[1,2,3],"foo":"bar"}` |
| 222 | + EqJSON(t, a, b) |
| 223 | + // Output: |
| 224 | +} |
194 | 225 |
|
195 | 226 | // EqOp |
| 227 | +func ExampleEqOp() { |
| 228 | + EqOp(t, 123, 123) |
| 229 | + // Output: |
| 230 | +} |
196 | 231 |
|
197 | 232 | // Equal |
| 233 | +func ExampleEqual() { |
| 234 | + // score implements .Equal method |
| 235 | + Equal(t, score(1000), score(1000)) |
| 236 | + // Output: |
| 237 | +} |
198 | 238 |
|
199 | 239 | // Error |
| 240 | +func ExampleError() { |
| 241 | + Error(t, errors.New("error")) |
| 242 | + // Output: |
| 243 | +} |
200 | 244 |
|
201 | 245 | // ErrorContains |
| 246 | +func ExampleErrorContains() { |
| 247 | + err := errors.New("error beer not found") |
| 248 | + ErrorContains(t, err, "beer") |
| 249 | + // Output: |
| 250 | +} |
202 | 251 |
|
203 | 252 | // ErrorIs |
| 253 | +func ExampleErrorIs() { |
| 254 | + e1 := errors.New("e1") |
| 255 | + e2 := errors.New("e2") |
| 256 | + e3 := errors.New("e3") |
| 257 | + errorChain := errors.Join(e1, e2, e3) |
| 258 | + ErrorIs(t, errorChain, e2) |
| 259 | + // Output: |
| 260 | +} |
204 | 261 |
|
205 | 262 | // False |
| 263 | +func ExampleFalse() { |
| 264 | + False(t, 1 == int('a')) |
| 265 | + // Output: |
| 266 | +} |
206 | 267 |
|
207 | 268 | // FileContains |
208 | 269 |
|
|
0 commit comments