File: example_test.go

package info (click to toggle)
golang-github-go-quicktest-qt 1.101.0-2
  • links: PTS, VCS
  • area: main
  • in suites: sid, trixie
  • size: 236 kB
  • sloc: makefile: 2
file content (331 lines) | stat: -rw-r--r-- 6,952 bytes parent folder | download
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
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
package qt_test

import (
	"errors"
	"fmt"
	"io"
	"math"
	"net"
	"os"
	"testing"

	"github.com/go-quicktest/qt"
	"github.com/google/go-cmp/cmp"
	"github.com/google/go-cmp/cmp/cmpopts"
)

func ExampleComment() {
	runExampleTest(func(t testing.TB) {
		a := 42
		qt.Assert(t, qt.Equals(a, 42), qt.Commentf("no answer to life, the universe, and everything"))
	})
	// Output: PASS
}

func ExampleEquals() {
	runExampleTest(func(t testing.TB) {
		answer := int64(42)
		qt.Assert(t, qt.Equals(answer, 42))
	})
	// Output: PASS
}

func ExampleDeepEquals() {
	runExampleTest(func(t testing.TB) {
		list := []int{42, 47}
		qt.Assert(t, qt.DeepEquals(list, []int{42, 47}))
	})
	// Output: PASS
}

func ExampleCmpEquals() {
	runExampleTest(func(t testing.TB) {
		list := []int{42, 47}
		qt.Assert(t, qt.CmpEquals(list, []int{47, 42}, cmpopts.SortSlices(func(i, j int) bool {
			return i < j
		})))
	})
	// Output: PASS
}

type myStruct struct {
	a int
}

func customDeepEquals[T any](got, want T) qt.Checker {
	return qt.CmpEquals(got, want, cmp.AllowUnexported(myStruct{}))
}

func ExampleCmpEquals_customfunc() {
	runExampleTest(func(t testing.TB) {
		got := &myStruct{
			a: 1234,
		}
		qt.Assert(t, customDeepEquals(got, &myStruct{
			a: 1234,
		}))
	})
	// Output: PASS
}

func ExampleContentEquals() {
	runExampleTest(func(t testing.TB) {
		got := []int{1, 23, 4, 5}
		qt.Assert(t, qt.ContentEquals(got, []int{1, 4, 5, 23}))
	})
	// Output: PASS
}

func ExampleMatches() {
	runExampleTest(func(t testing.TB) {
		qt.Assert(t, qt.Matches("these are the voyages", "these are .*"))
		qt.Assert(t, qt.Matches(net.ParseIP("1.2.3.4").String(), "1.*"))
	})
	// Output: PASS
}

func ExampleErrorMatches() {
	runExampleTest(func(t testing.TB) {
		err := errors.New("bad wolf at the door")
		qt.Assert(t, qt.ErrorMatches(err, "bad wolf .*"))
	})
	// Output: PASS
}

func ExamplePanicMatches() {
	runExampleTest(func(t testing.TB) {
		divide := func(a, b int) int {
			return a / b
		}
		qt.Assert(t, qt.PanicMatches(func() {
			divide(5, 0)
		}, "runtime error: .*"))
	})
	// Output: PASS
}

func ExampleIsNil() {
	runExampleTest(func(t testing.TB) {
		got := (*int)(nil)
		qt.Assert(t, qt.IsNil(got))
	})
	// Output: PASS
}

func ExampleIsNotNil() {
	runExampleTest(func(t testing.TB) {
		got := new(int)

		qt.Assert(t, qt.IsNotNil(got))

		// Note that unlike reflection-based APIs, a nil
		// value inside an interface still counts as non-nil,
		// just as if we were comparing the actual interface
		// value against nil.
		nilValueInInterface := any((*int)(nil))
		qt.Assert(t, qt.IsNotNil(nilValueInInterface))
	})
	// Output: PASS
}

func ExampleHasLen() {
	runExampleTest(func(t testing.TB) {
		qt.Assert(t, qt.HasLen([]int{42, 47}, 2))

		myMap := map[string]int{
			"a": 13,
			"b": 4,
			"c": 10,
		}
		qt.Assert(t, qt.HasLen(myMap, 3))
	})
	// Output: PASS
}

func ExampleImplements() {
	runExampleTest(func(t testing.TB) {
		var myReader struct {
			io.ReadCloser
		}
		qt.Assert(t, qt.Implements[io.ReadCloser](myReader))
	})
	// Output: PASS
}

func ExampleSatisfies() {
	runExampleTest(func(t testing.TB) {
		// Check that an error from os.Open satisfies os.IsNotExist.
		_, err := os.Open("/non-existent-file")
		qt.Assert(t, qt.Satisfies(err, os.IsNotExist))

		// Check that a floating point number is a not-a-number.
		f := math.NaN()
		qt.Assert(t, qt.Satisfies(f, math.IsNaN))

	})
	// Output: PASS
}

func ExampleIsTrue() {
	runExampleTest(func(t testing.TB) {
		isValid := func() bool {
			return true
		}
		qt.Assert(t, qt.IsTrue(1 == 1))
		qt.Assert(t, qt.IsTrue(isValid()))
	})
	// Output: PASS

}

func ExampleIsFalse() {
	runExampleTest(func(t testing.TB) {
		isValid := func() bool {
			return false
		}
		qt.Assert(t, qt.IsFalse(1 == 0))
		qt.Assert(t, qt.IsFalse(isValid()))
	})
	// Output: PASS

}

func ExampleNot() {
	runExampleTest(func(t testing.TB) {

		got := []int{1, 2}
		qt.Assert(t, qt.Not(qt.IsNil(got)))

		answer := 13
		qt.Assert(t, qt.Not(qt.Equals(answer, 42)))
	})
	// Output: PASS
}

func ExampleStringContains() {
	runExampleTest(func(t testing.TB) {
		qt.Assert(t, qt.StringContains("hello world", "hello"))
	})
	// Output: PASS
}

func ExampleSliceContains() {
	runExampleTest(func(t testing.TB) {
		qt.Assert(t, qt.SliceContains([]int{3, 5, 7, 99}, 99))
		qt.Assert(t, qt.SliceContains([]string{"a", "cd", "e"}, "cd"))
	})
	// Output: PASS
}

func ExampleMapContains() {
	runExampleTest(func(t testing.TB) {
		qt.Assert(t, qt.MapContains(map[string]int{
			"hello": 1234,
		}, 1234))
	})
	// Output: PASS
}

func ExampleSliceAny() {
	runExampleTest(func(t testing.TB) {
		qt.Assert(t, qt.SliceAny([]int{3, 5, 7, 99}, qt.F2(qt.Equals[int], 7)))
		qt.Assert(t, qt.SliceAny([][]string{{"a", "b"}, {"c", "d"}}, qt.F2(qt.DeepEquals[[]string], []string{"c", "d"})))
	})
	// Output: PASS
}

func ExampleMapAny() {
	runExampleTest(func(t testing.TB) {
		qt.Assert(t, qt.MapAny(map[string]int{"x": 2, "y": 3}, qt.F2(qt.Equals[int], 3)))
	})
	// Output: PASS
}

func ExampleSliceAll() {
	runExampleTest(func(t testing.TB) {
		qt.Assert(t, qt.SliceAll([]int{3, 5, 8}, func(e int) qt.Checker {
			return qt.Not(qt.Equals(e, 0))
		}))
		qt.Assert(t, qt.SliceAll([][]string{{"a", "b"}, {"a", "b"}}, qt.F2(qt.DeepEquals[[]string], []string{"a", "b"})))
	})
	// Output: PASS
}

func ExampleMapAll() {
	runExampleTest(func(t testing.TB) {
		qt.Assert(t, qt.MapAll(map[string]int{
			"x": 2,
			"y": 2,
		}, qt.F2(qt.Equals[int], 2)))
	})
	// Output: PASS
}

func ExampleJSONEquals() {
	runExampleTest(func(t testing.TB) {
		data := `[1, 2, 3]`
		qt.Assert(t, qt.JSONEquals(data, []uint{1, 2, 3}))
	})
	// Output: PASS
}

func ExampleErrorAs() {
	runExampleTest(func(t testing.TB) {
		_, err := os.Open("/non-existent-file")

		// Checking for a specific error type.
		qt.Assert(t, qt.ErrorAs(err, new(*os.PathError)))
		qt.Assert(t, qt.ErrorAs[*os.PathError](err, nil))

		// Checking fields on a specific error type.
		var pathError *os.PathError
		if qt.Check(t, qt.ErrorAs(err, &pathError)) {
			qt.Assert(t, qt.Equals(pathError.Path, "/non-existent-file"))
		}
	})
	// Output: PASS
}

func ExampleErrorIs() {
	runExampleTest(func(t testing.TB) {
		_, err := os.Open("/non-existent-file")

		qt.Assert(t, qt.ErrorIs(err, os.ErrNotExist))
	})
	// Output: PASS
}

func runExampleTest(f func(t testing.TB)) {
	defer func() {
		if err := recover(); err != nil && err != exampleTestFatal {
			panic(err)
		}
	}()
	var t exampleTestingT
	f(&t)
	if t.failed {
		fmt.Println("FAIL")
	} else {
		fmt.Println("PASS")
	}
}

type exampleTestingT struct {
	testing.TB
	failed bool
}

var exampleTestFatal = errors.New("example test fatal error")

func (t *exampleTestingT) Helper() {}

func (t *exampleTestingT) Error(args ...any) {
	fmt.Printf("ERROR: %s\n", fmt.Sprint(args...))
	t.failed = true
}

func (t *exampleTestingT) Fatal(args ...any) {
	fmt.Printf("FATAL: %s\n", fmt.Sprint(args...))
	t.failed = true
	panic(exampleTestFatal)
}