-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathwhat_test.go
143 lines (122 loc) · 3.86 KB
/
what_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
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
//go:build what || whatpackage || whatis || whatfunc || whathappens || whatif
package what
// IMPORTANT: to run these tests, use build tag "what":
//
// go test -tags what
import (
"bytes"
"io"
"log"
"os"
"regexp"
"strings"
"testing"
)
func TestOutput(t *testing.T) {
got := &bytes.Buffer{}
log.SetOutput(NewTeeWriter(got, os.Stderr)) // write all log output into "got" for later matching
log.SetFlags(0) // no extra decorations
n := 23
// no package name set - all packages are enabled
enabled = map[string]bool{}
Happens("what.Happens")
verify(t, got, `appliedgo\.net/what\.TestOutput: what\.Happens`)
got.Reset()
// Structured output should get colorized:
Happens("WARN", "Something is fishy", "code", 23, "err", "Quartotube exceeded max flotic burst")
verify(t, got, strings.Replace(dim+`\d\d\d\d-\d\d-\d\d \d\d:\d\d:\d\d`+reset+` `+yellow+`WARN`+reset+" "+blue+`appliedgo.net/what.TestOutput`+reset+` `+white+underscore+`Something is fishy`+reset+" "+cyan+`code`+reset+`=`+magenta+`23`+reset+` `+cyan+`err`+reset+`=`+red+bold+`Quartotube exceeded max flotic burst`+reset+" ", `[`, `\[`, -1))
got.Reset()
If(true, "If true")
verify(t, got, `appliedgo\.net/what\.TestOutput: If true`)
got.Reset()
If(false, "If false")
verify(t, got, ``)
got.Reset()
Is(n)
verify(t, got, `\(int\) 23`)
got.Reset()
Func()
verify(t, got, `Func appliedgo\.net/what\.TestOutput in line \d+ of file .*/what_test\.go`)
got.Reset()
Package()
verify(t, got, `Package appliedgo\.net/what`)
}
func verify(t *testing.T, gotbuf *bytes.Buffer, want string) {
wantRE := regexp.MustCompile("^" + want + "$")
got := gotbuf.String()[:max(0, gotbuf.Len()-1)] // trim the trailing \n
if !wantRE.MatchString(got) {
t.Errorf("Got: `%s` Want: `%s`", got, want)
}
}
type testStruct struct {
name string
}
// Implement DebugStringer for testStruct
func (ts testStruct) DebugString() string {
return "DEBUG:" + ts.name
}
// TestDebugStringer tests the support for DebugString()
func TestDebugStringer(t *testing.T) {
got := &bytes.Buffer{}
log.SetOutput(got)
log.SetFlags(0)
test := testStruct{name: "Test"}
Is(test)
want := "DEBUG:Test"
if got.String() != want {
t.Errorf("Got: `%s` Want: `%s`", got.String(), want)
}
}
func TestEnabling(t *testing.T) {
got := &bytes.Buffer{}
log.SetOutput(got) // write all log output into "got" for later matching
log.SetFlags(0) // no extra decorations
// no package name set - all packages are enabled
enabled = map[string]bool{}
Happens("what.Happens - all packages enabled")
enabled = map[string]bool{
"what": true,
}
Happens("what.Happens - package 'what' enabled")
enabled = map[string]bool{
"appliedgo.net/what": true,
}
Happens("what.Happens - package 'appliedgo.net/what' enabled")
enabled = map[string]bool{
"someotherpackage": true,
}
Happens("what.Happens - package 'what' NOT enabled") // this should not print
wantRE := regexp.MustCompile(`appliedgo.net/what\.TestEnabling: what\.Happens - all packages enabled
appliedgo\.net/what\.TestEnabling: what.Happens - package 'what' enabled
appliedgo\.net/what\.TestEnabling: what.Happens - package 'appliedgo\.net/what' enabled
`)
// "got" contains all log output from the above calls
if !wantRE.Match(got.Bytes()) {
t.Errorf("Got: %s\n\nWant: %s", got, wantRE)
}
}
// for pre-Go 1.21 clients
func max(a, b int) int {
if a > b {
return a
}
return b
}
type TeeWriter struct {
writers []io.Writer
}
// NewTeeWriter creates a new TeeWriter with the given io.Writers.
func NewTeeWriter(writers ...io.Writer) *TeeWriter {
return &TeeWriter{writers: writers}
}
// Write writes bytes to each of the writers in TeeWriter, returning the number
// of bytes written and an error, if any occurs.
func (t *TeeWriter) Write(p []byte) (n int, err error) {
for _, w := range t.writers {
n, err = w.Write(p)
if err != nil {
return n, err
}
}
return len(p), nil
}