-
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathbe_test.go
More file actions
114 lines (105 loc) · 2.56 KB
/
be_test.go
File metadata and controls
114 lines (105 loc) · 2.56 KB
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
package be_test
import (
"errors"
"fmt"
"io"
"maps"
"strings"
"testing"
"time"
"github.com/carlmjohnson/be"
)
type testingTB struct {
testing.TB
failed bool
w io.Writer
}
func (t *testingTB) Helper() {}
func (t *testingTB) Fatalf(format string, args ...any) {
t.failed = true
fmt.Fprintf(t.w, format, args...)
}
func Test(t *testing.T) {
okayTests := []func(tb testing.TB){
func(tb testing.TB) { be.Zero(tb, time.Time{}.Local()) },
func(tb testing.TB) { be.Zero(tb, []string(nil)) },
func(tb testing.TB) { be.Nonzero(tb, []string{""}) },
func(tb testing.TB) { be.NilErr(tb, nil) },
func(tb testing.TB) { be.True(tb, true) },
func(tb testing.TB) { be.False(tb, false) },
func(tb testing.TB) { be.EqualLength(tb, 0, map[int]int{}) },
func(tb testing.TB) { be.EqualLength(tb, 1, map[int]int{1: 1}) },
func(tb testing.TB) {
ch := make(chan int, 1)
be.EqualLength(tb, 0, ch)
},
func(tb testing.TB) {
ch := make(chan int, 1)
ch <- 1
be.EqualLength(tb, 1, ch)
},
func(tb testing.TB) {
seq2 := maps.All(map[int]int{1: 1})
be.EqualLength(tb, 1, seq2)
},
func(tb testing.TB) {
be.In(tb, "world", "Hello, world!")
be.NotIn(tb, "\x01", []byte("\a\b\x00\r\t"))
},
}
for _, test := range okayTests {
var buf strings.Builder
tb := &testingTB{w: &buf}
test(tb)
if tb.failed {
t.Fatal("failed too soon")
}
if buf.String() != "" {
t.Fatal("wrote too much")
}
}
badTests := []func(tb testing.TB){
func(tb testing.TB) { be.AllEqual(tb, []string{}, []string{""}) },
func(tb testing.TB) { be.Nonzero(tb, time.Time{}.Local()) },
func(tb testing.TB) { be.Zero(tb, []string{""}) },
func(tb testing.TB) { be.Nonzero(tb, []string(nil)) },
func(tb testing.TB) { be.NilErr(tb, errors.New("")) },
func(tb testing.TB) { be.True(tb, false) },
func(tb testing.TB) { be.False(tb, true) },
func(tb testing.TB) {
seq2 := maps.All(map[int]int{1: 1})
be.EqualLength(tb, 0, seq2)
},
func(tb testing.TB) {
ch := make(chan int, 1)
be.EqualLength(tb, 1, ch)
},
func(tb testing.TB) {
ch := make(chan int, 1)
ch <- 1
be.EqualLength(tb, 0, ch)
},
func(tb testing.TB) {
ch := make(chan int, 1)
close(ch)
be.EqualLength(tb, 1, ch)
},
func(tb testing.TB) {
be.In(tb, "World", "Hello, world!")
},
func(tb testing.TB) {
be.NotIn(tb, "\x00", []byte("\a\b\x00\r\t"))
},
}
for _, test := range badTests {
var buf strings.Builder
tb := &testingTB{w: &buf}
test(tb)
if !tb.failed {
t.Fatal("did not fail")
}
if buf.String() == "" {
t.Fatal("wrote too little")
}
}
}