1 package foo
2
3 import (
4 "fmt"
5 "testing"
6
7 "github.com/go-check/check"
8 "gotest.tools/v3/assert"
9 "gotest.tools/v3/assert/cmp"
10 )
11
12 type mystruct struct {
13 a int
14 expected int
15 }
16
17 func TestFirstThing(t *testing.T) {
18 rt := assert.TestingT(t)
19 assert.Check(t, cmp.Equal("foo", "bar"))
20 assert.Check(t, cmp.Equal(1, 2))
21 assert.Check(t, false)
22 assert.Check(t, !true)
23 assert.NilError(rt, nil)
24
25 assert.Check(t, cmp.DeepEqual(map[string]bool{"a": true}, nil))
26 assert.Check(t, cmp.DeepEqual([]int{1}, nil))
27 assert.Equal(rt, "a", "B")
28 }
29
30 func TestSecondThing(t *testing.T) {
31 var foo mystruct
32 assert.DeepEqual(t, foo, mystruct{})
33
34 assert.DeepEqual(t, mystruct{}, mystruct{})
35
36 assert.Check(t, nil, "foo %d", 3)
37 assert.NilError(t, nil, "foo %d", 3)
38
39 assert.Check(t, cmp.ErrorContains(fmt.Errorf("foo"), ""))
40
41 assert.Assert(t, 77 != 0)
42 }
43
44 func TestOthers(t *testing.T) {
45 assert.Check(t, cmp.Contains([]string{}, "foo"))
46 assert.Assert(t, cmp.Len([]int{}, 3))
47 assert.Check(t, cmp.Panics(func() { panic("foo") }))
48 assert.Error(t, fmt.Errorf("bad days"), "good days")
49 assert.Check(t, nil != nil)
50
51 t.Error("why")
52 t.Fatal("why not")
53 assert.Assert(t, len([]bool{}) != 0)
54
55
56 assert.NotContains(t, []bool{}, true)
57 }
58
59 func TestAssertNew(t *testing.T) {
60
61 assert.Check(t, cmp.Equal("a", "b"))
62 }
63
64 type unit struct {
65 c *testing.T
66 }
67
68 func thing(t *testing.T) unit {
69 return unit{c: t}
70 }
71
72 func TestStoredTestingT(t *testing.T) {
73 u := thing(t)
74 assert.Check(u.c, cmp.Equal("A", "b"))
75
76 u = unit{c: t}
77 assert.Check(u.c, cmp.Equal("A", "b"))
78 }
79
80 func TestNotNamedT(c *testing.T) {
81 assert.Check(c, cmp.Equal("A", "b"))
82 }
83
84 func TestEqualsWithComplexTypes(t *testing.T) {
85 expected := []int{1, 2, 3}
86 assert.Check(t, cmp.DeepEqual(expected, nil))
87
88 expectedM := map[int]bool{}
89 assert.Check(t, cmp.DeepEqual(expectedM, nil))
90
91 expectedI := 123
92 assert.Check(t, cmp.Equal(expectedI, 0))
93
94 assert.Check(t, cmp.Equal(doInt(), 3))
95
96 }
97
98 func doInt() int {
99 return 1
100 }
101
102 func TestEqualWithPrimitiveTypes(t *testing.T) {
103 s := "foo"
104 ptrString := &s
105 assert.Check(t, cmp.Equal(*ptrString, "foo"))
106
107 assert.Check(t, cmp.Equal(doInt(), doInt()))
108
109 x := doInt()
110 y := doInt()
111 assert.Check(t, cmp.Equal(x, y))
112
113 tc := mystruct{a: 3, expected: 5}
114 assert.Check(t, cmp.Equal(tc.a, tc.expected))
115 }
116
117 func TestTableTest(t *testing.T) {
118 var testcases = []struct {
119 opts []string
120 actual string
121 expected string
122 expectedOpts []string
123 }{
124 {
125 opts: []string{"a", "b"},
126 actual: "foo",
127 expected: "else",
128 },
129 }
130
131 for _, testcase := range testcases {
132 assert.Check(t, cmp.Equal(testcase.actual, testcase.expected))
133 assert.Check(t, cmp.DeepEqual(testcase.opts, testcase.expectedOpts))
134 }
135 }
136
137 func TestWithChecker(c *check.C) {
138 var err error
139 assert.Check(c, err)
140 }
141
142 func HelperWithAssertTestingT(t assert.TestingT) {
143 var err error
144 assert.Check(t, err, "with assert.TestingT")
145 }
146
147 func BenchmarkSomething(b *testing.B) {
148 var err error
149 assert.Check(b, err)
150 }
151
View as plain text