-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
be_test.go
65 lines (59 loc) · 1.57 KB
/
be_test.go
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
package be_test
import (
"errors"
"fmt"
"io"
"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) {
beOkay := func(callback func(tb testing.TB)) {
t.Helper()
var buf strings.Builder
tb := &testingTB{w: &buf}
callback(tb)
if tb.failed {
t.Fatal("failed too soon")
}
if buf.String() != "" {
t.Fatal("wrote too much")
}
}
beOkay(func(tb testing.TB) { be.Zero(tb, time.Time{}.Local()) })
beOkay(func(tb testing.TB) { be.Zero(tb, []string(nil)) })
beOkay(func(tb testing.TB) { be.Nonzero(tb, []string{""}) })
beOkay(func(tb testing.TB) { be.NilErr(tb, nil) })
beOkay(func(tb testing.TB) { be.True(tb, true) })
beOkay(func(tb testing.TB) { be.False(tb, false) })
beBad := func(callback func(tb testing.TB)) {
t.Helper()
var buf strings.Builder
tb := &testingTB{w: &buf}
callback(tb)
if !tb.failed {
t.Fatal("did not fail")
}
if buf.String() == "" {
t.Fatal("wrote too little")
}
}
beBad(func(tb testing.TB) { be.AllEqual(tb, []string{}, []string{""}) })
beBad(func(tb testing.TB) { be.Nonzero(tb, time.Time{}.Local()) })
beBad(func(tb testing.TB) { be.Zero(tb, []string{""}) })
beBad(func(tb testing.TB) { be.Nonzero(tb, []string(nil)) })
beBad(func(tb testing.TB) { be.NilErr(tb, errors.New("")) })
beBad(func(tb testing.TB) { be.True(tb, false) })
beBad(func(tb testing.TB) { be.False(tb, true) })
}