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