-
Notifications
You must be signed in to change notification settings - Fork 0
/
example_test.go
104 lines (80 loc) · 2.49 KB
/
example_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
package testr_test
import (
"encoding/json"
"fmt"
"github.com/minizilla/testr"
)
func ExampleAssertion_Equal() {
assert := testr.New(t) // using *testing.T
assert.Equal(nil, nil) // PASS
assert.Equal(false, true) // FAIL
assert.Equal("nil", nil) // FAIL
assert.Equal([]string{"hello\nworld"}, 0) // FAIL
// Output:
// false != expected:true
// "nil" != expected:<nil>
// []string{"hello\nworld"} != expected:0
}
func ExampleAssertion_ErrorIs() {
assert := testr.New(t) // using *testing.T
assert.ErrorIs(nil, nil) // PASS
assert.ErrorIs(errFoo, errFoo) // PASS
assert.ErrorIs(errWrapFoo, errFoo) // PASS
assert.ErrorIs(errFoo, nil) // FAIL
assert.ErrorIs(errFoo, errBar) // FAIL
assert.ErrorIs(errWrapFoo, errBar) // FAIL
// Output:
// error(foo) != expected:<nil>
// error(foo) != expected:error(bar)
// error(wrap foo) != expected:error(bar)
}
func ExampleAssertion_ErrorAs() {
assert := testr.New(t) // using *testing.T
var e customError
assert.ErrorAs(customError("err"), &e) // PASS
assert.ErrorAs(errFoo, &e) // FAIL
// Output:
// error(foo) != expected:as(*testr_test.customError)
}
func ExampleAssertion_Panic() {
assert := testr.New(t) // using *testing.T
assert.Panic(func() { panic("panic") }) // PASS
assert.Panic(func() {}) // FAIL
// Output:
// func() != expected:panic()
}
func ExampleWithMessage() {
assert := testr.New(t) // using *testing.T
assert.Equal(false, true, testr.WithMessage("assert equality"))
assert.ErrorIs(errFoo, nil, testr.WithMessage("assert err is nil"))
var e customError
assert.ErrorAs(errFoo, &e, testr.WithMessage("assert err as customError"))
assert.Panic(func() {}, testr.WithMessage("assert function is panic"))
// Output:
// false != expected:true // assert equality
// error(foo) != expected:<nil> // assert err is nil
// error(foo) != expected:as(*testr_test.customError) // assert err as customError
// func() != expected:panic() // assert function is panic
}
func ExampleWithFailNow() {
assert := testr.New(t) // using *testing.T
assert.ErrorIs(errFoo, nil,
testr.WithFailNow(),
testr.WithMessage("using t.FailNow"),
)
// Output:
// error(foo) != expected:<nil> // using t.FailNow
}
func ExampleMust() {
var v string
testr.Must(json.Unmarshal([]byte(`"testr"`), &v))
fmt.Println(v)
// Output:
// testr
}
func ExampleMustV() {
b := testr.MustV(json.Marshal("testr"))
fmt.Println(string(b))
// Output:
// "testr"
}