-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpackage_test.go
95 lines (75 loc) · 1.71 KB
/
package_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
package expect_test
import (
"testing"
"github.com/halimath/expect"
"github.com/halimath/expect/is"
)
func Test_Something(t *testing.T) {
slice := make([]string, 5)
var err error
expect.That(t,
is.NoError(err),
is.SliceOfLen(slice, 5),
)
}
func Test_Slices(t *testing.T) {
slice := []string{"a", "b", "c"}
expect.That(t,
is.SliceOfLen(slice, 3),
is.SliceContaining(slice, "b", "a"),
is.SliceContainingInOrder(slice, "a", "b"),
)
}
func TestEqual_string(t *testing.T) {
got := "something"
expect.That(t, is.EqualTo(got, "something"))
}
func TestDeepEqual_strings(t *testing.T) {
got := []string{"foo", "bar"}
expect.That(t,
is.DeepEqualTo(got, []string{"foo", "bar"}),
)
}
func TestLen_string(t *testing.T) {
got := "hello"
expect.That(t, is.StringOfLen(got, 5))
}
func TestMap(t *testing.T) {
got := map[string]int{
"foo": 1,
"bar": 2,
}
expect.That(t,
is.MapContaining(got, "foo", 1),
is.MapOfLen(got, 2),
)
}
func TestSlice(t *testing.T) {
got := []int{1, 2, 3}
expect.That(t,
is.SliceOfLen(got, 3),
is.SliceContainingInOrder(got, 1, 3),
)
}
// The following type definitions can also be used from the golang.org/x/constraints module. We use these
// "copies" here to avoid a build-time dependency just for this demonstrations case.
type Signed interface {
~int | ~int8 | ~int16 | ~int32 | ~int64
}
type Unsigned interface {
~uint | ~uint8 | ~uint16 | ~uint32 | ~uint64 | ~uintptr
}
type Integer interface {
Signed | Unsigned
}
func IsEven[T Integer](got T) expect.Expectation {
return expect.ExpectFunc(func(t expect.TB) {
if got%2 != 0 {
t.Errorf("expected <%v> to be even", got)
}
})
}
func TestCustomMatcher(t *testing.T) {
var i int = 22
expect.That(t, IsEven(i))
}