File: expect_test.go

package info (click to toggle)
golang-github-karlseguin-expect 1.0.1+git20160716.12.5c2eadb-1
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 148 kB
  • ctags: 204
  • sloc: makefile: 6
file content (242 lines) | stat: -rw-r--r-- 6,089 bytes parent folder | download | duplicates (2)
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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
package expect

import (
	"fmt"
	"testing"
)

var originalErrorf = Errorf
var beforeEachCalled = 0

type ExpectTests struct{}

func init() {
	BeforeEach(func() {
		beforeEachCalled++
	})
}

func Test_Expect(t *testing.T) {
	Expectify(new(ExpectTests), t)
}

func (_ ExpectTests) Equal_Success() {
	Expect("abc").To.Equal("abc")
	Expect(true).To.Equal(true)
	Expect(false).To.Equal(false)
	Expect([]int{1, 2, 3}).To.Equal([]int{1, 2, 3})
	Expect([]bool{true, false, false}).To.Equal([]bool{true, false, false})
	Expect(map[string]int{"a": 1, "b": 2}).To.Equal(map[string]int{"a": 1, "b": 2})
}

func (_ ExpectTests) Equal_Failures() {
	failing(`expected "abc" to be equal to "123"`, 1, func() {
		Expect("abc").To.Equal("123")
	})

	failing("expected true to be equal to false", 1, func() {
		Expect(true).To.Equal(false)
	})

	failing("expected [1 2] to be equal to [1]", 1, func() {
		Expect([]int{1, 2}).To.Equal([]int{1})
	})

	// hard to test, the print out is indeterministic, maps aren't sorted
	// failing("expected map[a:1 b:2] to be equal to map[b:1 a:2]", 1, func(){
	// 	Expect(map[string]int{"a":1,"b":2}).Equal.To(map[string]int{"b":1,"a":2})
	// })
}

func (_ ExpectTests) NotEqual_Success() {
	Expect("abc").Not.To.Equal("123")
}

func (_ ExpectTests) NoEqual_Failures() {
	failing(`expected "123" not to equal "123"`, 1, func() {
		Expect("123").Not.To.Equal("123")
	})

	failing("expected true not to equal true", 1, func() {
		Expect(true).Not.To.Equal(true)
	})

	failing("expected [1 2] not to equal [1 2]", 1, func() {
		Expect([]int{1, 2}).Not.To.Equal([]int{1, 2})
	})
}

func (_ ExpectTests) Contain_Success() {
	Expect("abc").To.Contain("bc")
	Expect("abc").Not.To.Contain("99")

	Expect([]int{1, 3, 4}).To.Contain(4)
	Expect([]int{2, 3, 4}).Not.To.Contain(5)

	Expect(map[string]int{"a": 3}).To.Contain("a")
	Expect(map[string]int{"a": 3}).Not.To.Contain("b")

	Expect([]byte{1, 2, 3}).To.Contain(byte(1))
	Expect([]byte{1, 2, 3}).Not.To.Contain(byte(5))
	Expect([]byte{1, 2, 3}).Not.To.Contain([]byte{2, 3, 4})
}

func (_ ExpectTests) Contain_Failures() {
	failing("abc does not contain zz", 1, func() {
		Expect("abc").To.Contain("zz")
	})
	failing("aazzbb contains zz", 1, func() {
		Expect("aazzbb").Not.To.Contain("zz")
	})

	failing("[1 2] does not contain 3", 1, func() {
		Expect([]int{1, 2}).To.Contain(3)
	})
	failing("[1 2] contains 2", 1, func() {
		Expect([]int{1, 2}).Not.To.Contain(2)
	})

	failing("map[a:3] does not contain b", 1, func() {
		Expect(map[string]int{"a": 3}).To.Contain("b")
	})
	failing("map[a:3] contains a", 1, func() {
		Expect(map[string]int{"a": 3}).Not.To.Contain("a")
	})

	failing("[1 2 3] does not contain [3 4]", 1, func() {
		Expect([]byte{1, 2, 3}).To.Contain([]byte{3, 4})
	})
	failing("[1 2 3] contains [2 3]", 1, func() {
		Expect([]byte{1, 2, 3}).Not.To.Contain([]byte{2, 3})
	})
}

func (_ ExpectTests) ExpectMulipleValues_Success() {
	Expect(1, true, "over 9000").To.Equal(1, true, "over 9000")
}

func (_ ExpectTests) ExpectMulipleValues_Failure() {
	failing("mismatch number of values and expectations 3 != 2", 1, func() {
		Expect(1, true, "over 9000").To.Equal(1, true)
	})
	failing("mismatch number of values and expectations 2 != 3", 1, func() {
		Expect(1, true).To.Equal(1, true, "a")
	})
	failing("expected 1 to be equal to 2", 1, func() {
		Expect(1, "a").To.Equal(2, "a")
	})
	failing(`expected "a" to be equal to "b"`, 1, func() {
		Expect(1, "a").To.Equal(1, "b")
	})
}

func (_ ExpectTests) ExpectNil_Success() {
	var n *string
	Expect(nil).To.Equal(nil)
	Expect(n).To.Equal(nil)
	Expect(n).To.Equal(n)
	Expect(nil).Not.To.Equal(32)
	Expect(32).Not.To.Equal(n)
}

func (_ ExpectTests) ExpectNil_Failure() {
	var n *string
	failing("expected <nil> to be equal to 44", 2, func() {
		Expect(nil).To.Equal(44)
		Expect(n).To.Equal(44)
	})
	failing("expected <nil> not to equal <nil>", 3, func() {
		Expect(nil).Not.To.Equal(nil)
		Expect(nil).Not.To.Equal(n)
		Expect(n).Not.To.Equal(n)
	})
}

func (_ ExpectTests) Fail() {
	failing("the failure reason 123", 1, func() {
		Fail("the failure reason %d", 123)
	})
}

func (_ ExpectTests) Skip_After() {
	Fail("failed")
	Skip("the skip reason %d", 9001)
}

func (_ ExpectTests) Skip_Before() {
	Skip("the skip reason")
	Fail("failed")
}

func (_ ExpectTests) Skip_NoPanic() {
	Skip("the skip reason")
	panic("should be swallowed")
}

func (_ ExpectTests) AltenrativeSyntax() {
	Expect(true).ToEqual(true)
	Expect(123).Not.ToEqual(11)
	Expect("it's over", 9000).ToEqual("it's over", 9000)
	Expect("it's not over", 90000).Not.ToEqual("it's over", 9000)

	Expect(94).GreaterThan(93)
	Expect(94).Not.GreaterThan(94)
	Expect(94).Not.GreaterThan(95)
	Expect(94).GreaterOrEqualTo(93)
	Expect(94).GreaterOrEqualTo(94)
	Expect(94).Not.GreaterOrEqualTo(95)

	Expect(12).LessThan(13)
	Expect(12).Not.LessThan(12)
	Expect(12).Not.LessThan(-1)
	Expect(12).LessOrEqualTo(12)
	Expect(12).LessOrEqualTo(12)
	Expect(12).Not.LessOrEqualTo(1)
}

func (_ ExpectTests) Json() {
	Expect(`{"about":true}`).To.Equal(JSON(`{
		"about": true
	}`))
}

func (_ ExpectTests) GlobalBeforEEach() {
	Expect(beforeEachCalled).Greater.Than(0)
}

func (_ ExpectTests) EqlForInts() {
	Expect(uint16(1)).To.Eql(1)
	Expect(uint32(2)).To.Eql(2)
	Expect(uint64(3)).To.Eql(3)
	Expect(int16(4)).To.Eql(4)
	Expect(int32(5)).To.Eql(5)
	Expect(int64(6)).To.Eql(6)
	Expect(byte(7)).To.Eql(7)
}

func (_ ExpectTests) EqlForStringAndBytes() {
	Expect([]byte{65, 66}).To.Eql("AB")
	Expect("CD").To.Eql([]byte{67, 68})
}

func failing(expected string, count int, f func()) {
	actuals := make([]string, 0, 5)
	Errorf = func(format string, args ...interface{}) {
		actuals = append(actuals, fmt.Sprintf(format, args...))
	}
	f()
	Errorf = originalErrorf
	if len(actuals) != count {
		Errorf("expected %d failures got %d", count, len(actuals))
		return
	}
	for i := 0; i < count; i++ {
		if actuals[i] != expected {
			if count == 1 {
				Errorf("expected failure '%s' got '%s'", expected, actuals[i])
			} else {
				Errorf("expected failure '%s' got '%s' index %d", expected, actuals[i], i+1)
			}
		}
	}
}