-
Notifications
You must be signed in to change notification settings - Fork 22
/
reporter_test.go
105 lines (79 loc) · 2.45 KB
/
reporter_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
package approvals
import (
"os"
"testing"
"github.com/approvals/go-approval-tests/reporters"
"github.com/approvals/go-approval-tests/utils"
)
// TestFailable is a fake replacing testing.T
// It implements the parts of the testing.T interface approvals uses,
// ie the approvaltests.Failable interface
type TestFailable struct {
name string
}
func (s *TestFailable) Fail() {}
func (s *TestFailable) Name() string {
return s.name
}
func (s *TestFailable) Fatalf(format string, args ...interface{}) {}
func (s *TestFailable) Fatal(args ...interface{}) {}
func (s *TestFailable) Log(args ...interface{}) {}
func (s *TestFailable) Logf(format string, args ...interface{}) {}
func (s *TestFailable) Helper() {}
func NewTestFailable() *TestFailable {
return &TestFailable{
name: "TestFailable",
}
}
func NewTestFailableWithName(name string) *TestFailable {
return &TestFailable{
name: name,
}
}
type testReporter struct {
called bool
succeeded bool
}
func newTestReporter(succeeded bool) *testReporter {
return &testReporter{
called: false,
succeeded: succeeded,
}
}
func (s *testReporter) Report(approved, received string) bool {
s.called = true
os.Remove(received)
return s.succeeded
}
func TestUseReporter(t *testing.T) {
front := UseFrontLoadedReporter(newTestReporter(false))
defer front.Close()
old := getReporter()
a := newTestReporter(true)
r := UseReporter(reporters.Reporter(a))
f := &TestFailable{}
VerifyString(f, "foo")
utils.AssertEqual(t, true, a.called, "a.called")
r.Close()
current := getReporter()
oldT, _ := old.(*reporters.FirstWorkingReporter)
currentT, _ := current.(*reporters.FirstWorkingReporter)
utils.AssertEqual(t, oldT.Reporters[1], currentT.Reporters[1], "reporters[1]")
}
func TestFrontLoadedReporter(t *testing.T) {
old := getReporter()
front := newTestReporter(false)
next := newTestReporter(true)
frontCloser := UseFrontLoadedReporter(reporters.Reporter(front))
nextCloser := UseReporter(reporters.Reporter(next))
defer nextCloser.Close()
f := &TestFailable{}
VerifyString(f, "foo")
utils.AssertEqual(t, true, front.called, "front.called")
utils.AssertEqual(t, true, next.called, "next.called")
frontCloser.Close()
current := getReporter()
oldT, _ := old.(*reporters.FirstWorkingReporter)
currentT, _ := current.(*reporters.FirstWorkingReporter)
utils.AssertEqual(t, oldT.Reporters[0], currentT.Reporters[0], "reporters[0]")
}