Skip to content

Commit 15725ab

Browse files
docs: add more examples (#127)
1 parent f9e3762 commit 15725ab

2 files changed

Lines changed: 122 additions & 0 deletions

File tree

examples_test.go

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
package test
44

55
import (
6+
"errors"
67
"fmt"
78
"os"
89
"strings"
@@ -54,6 +55,10 @@ func (s score) Less(other score) bool {
5455
return s < other
5556
}
5657

58+
func (s score) Equal(other score) bool {
59+
return s == other
60+
}
61+
5762
func ExampleAscending() {
5863
nums := []int{1, 3, 4, 4, 9}
5964
Ascending(t, nums)
@@ -187,22 +192,78 @@ func ExampleEq() {
187192
}
188193

189194
// EqError
195+
func ExampleEqError() {
196+
err := errors.New("undefined error")
197+
EqError(t, err, "undefined error")
198+
// Output:
199+
}
190200

191201
// 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+
}
192217

193218
// 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+
}
194225

195226
// EqOp
227+
func ExampleEqOp() {
228+
EqOp(t, 123, 123)
229+
// Output:
230+
}
196231

197232
// Equal
233+
func ExampleEqual() {
234+
// score implements .Equal method
235+
Equal(t, score(1000), score(1000))
236+
// Output:
237+
}
198238

199239
// Error
240+
func ExampleError() {
241+
Error(t, errors.New("error"))
242+
// Output:
243+
}
200244

201245
// ErrorContains
246+
func ExampleErrorContains() {
247+
err := errors.New("error beer not found")
248+
ErrorContains(t, err, "beer")
249+
// Output:
250+
}
202251

203252
// 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+
}
204261

205262
// False
263+
func ExampleFalse() {
264+
False(t, 1 == int('a'))
265+
// Output:
266+
}
206267

207268
// FileContains
208269

must/examples_test.go

Lines changed: 61 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)