-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstubreporter.go
144 lines (127 loc) · 3.59 KB
/
stubreporter.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
144
// Copyright 2023 Patrick Smith
// Use of this source code is subject to the MIT-style license in the LICENSE file.
package gotest
import (
"fmt"
"strings"
)
// Type *StubReporter is a simple implementation of the Reporter interface.
//
// It is intended to assist in testing test helper functions.
// The methods save the results of calls to be queried later,
// but do not do anything else.
type StubReporter struct {
log strings.Builder
failed, killed bool
}
// Helper marks a function as a helper function.
//
// The StubReporter version of Helper does nothing.
func (sr *StubReporter) Helper() {}
// Fail marks a test as failed.
func (sr *StubReporter) Fail() {
sr.failed = true
}
// Failed returns whether the test was marked failed.
func (sr *StubReporter) Failed() bool {
return sr.failed
}
// FailNow marks a test as failed.
//
// The versions of FailNow in testing also terminate the test case, so they do not return.
// However, the FailNow, Fatal, and Fatalf methods of StubReporter do return.
// Careful attention should be paid to this point when using StubReporter
// to write test cases for helper functions.
func (sr *StubReporter) FailNow() {
sr.Fail()
sr.killed = true
}
// Killed returns whether FailNow was called.
func (sr *StubReporter) Killed() bool {
return sr.killed
}
// Log formats its arguments as if by fmt.Println and records the resulting text.
//
// If the last argument is a string ending with a newline, Log still prints an
// additional newline, creating a blank line in the output. This seems undesirable,
// but it is what the testing package does, so we do the same.
func (sr *StubReporter) Log(args ...any) {
_, e := fmt.Fprintln(&sr.log, args...)
if e != nil {
// Should be impossible
panic(e)
}
}
// Logf formats its arguments as if by fmt.Printf and records the resulting text.
func (sr *StubReporter) Logf(format string, args ...any) {
oldLen := sr.log.Len()
_, e := fmt.Fprintf(&sr.log, format, args...)
if e != nil {
// Should be impossible
panic(e)
}
if sr.log.Len() == oldLen || !strings.HasSuffix(sr.log.String(), "\n") {
sr.log.WriteByte('\n')
}
}
// Logged returns the text recorded by Log and Logf.
func (sr *StubReporter) Logged() string {
return sr.log.String()
}
// Error calls Fail and Log.
func (sr *StubReporter) Error(args ...any) {
sr.Fail()
sr.Log(args...)
}
// Errorf calls Fail and Logf.
func (sr *StubReporter) Errorf(format string, args ...any) {
sr.Fail()
sr.Logf(format, args...)
}
// Fatal calls FailNow and Log.
func (sr *StubReporter) Fatal(args ...any) {
sr.FailNow()
sr.Log(args...)
}
// Fatalf calls FailNow and Logf.
func (sr *StubReporter) Fatalf(format string, args ...any) {
sr.FailNow()
sr.Logf(format, args...)
}
// Expect verifies the status of the StubReporter.
//
// The failed, killed, and log parameters are compared to the StubReporter status.
// If they do not match, an error is reported to t.
func (sr *StubReporter) Expect(t Reporter, failed, killed bool, log string) {
t.Helper()
ok := true
if sr.Failed() != failed {
ok = false
if sr.Failed() {
t.Error("StubReporter marked failed")
} else {
t.Error("StubReporter marked not failed")
}
}
if sr.Killed() != killed {
ok = false
if sr.Killed() {
t.Error("StubReporter marked killed")
} else {
t.Error("StubReporter marked not killed")
}
}
if actual := sr.Logged(); actual != log {
ok = false
t.Errorf("StubReporter log is '%s'; expected '%s'", actual, log)
}
if !ok {
t.FailNow()
}
}
// Reset returns a StubReporter to the initial state.
func (sr *StubReporter) Reset() {
sr.log.Reset()
sr.failed = false
sr.killed = false
}