Skip to content

Commit 470bf71

Browse files
committed
Add Cleanup and Only
1 parent 68cdeae commit 470bf71

File tree

1 file changed

+47
-19
lines changed

1 file changed

+47
-19
lines changed

neko.go

Lines changed: 47 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,11 @@ type test struct {
1818
type Organizer struct {
1919
t *testing.T
2020

21-
mocks []*mock.Mock
22-
setup []func()
23-
tests []test
21+
only *test
22+
mocks []*mock.Mock
23+
setup []func()
24+
cleanup []func()
25+
tests []test
2426
}
2527

2628
// Create a new Organizer against testing's T interface
@@ -38,11 +40,20 @@ func (o *Organizer) Setup(f func()) {
3840
o.setup = append(o.setup, f)
3941
}
4042

43+
// Add some work to be done after each test
44+
func (o *Organizer) Cleanup(f func()) {
45+
o.cleanup = append(o.cleanup, f)
46+
}
47+
4148
// Add a test.
4249
func (o *Organizer) It(name string, f func()) {
4350
o.tests = append(o.tests, test{name, f})
4451
}
4552

53+
func (o *Organizer) Only(name string, f func()) {
54+
o.only = &test{name, f}
55+
}
56+
4657
// Useful by allowing the developer to simply add 'N' before
4758
// It to disable a block.
4859
func (o *Organizer) NIt(name string, f func()) {
@@ -51,28 +62,45 @@ func (o *Organizer) NIt(name string, f func()) {
5162

5263
// Coordinate running the tests with the setups and mocks
5364
func (o *Organizer) Run() {
65+
if o.only != nil {
66+
o.runTest(o.only)
67+
return
68+
}
69+
5470
for _, test := range o.tests {
55-
if test.Func == nil {
56-
o.t.Logf("==== DISABLED: %s ====", test.Name)
57-
continue
58-
}
71+
o.runTest(&test)
72+
}
73+
}
5974

60-
o.t.Logf("==== %s ====", test.Name)
75+
func (o *Organizer) runTest(test *test) {
76+
if test.Func == nil {
77+
o.t.Logf("==== DISABLED: %s ====", test.Name)
78+
return
79+
}
80+
81+
o.t.Logf("==== %s ====", test.Name)
82+
83+
for _, mock := range o.mocks {
84+
mock.ExpectedCalls = nil
85+
mock.Calls = nil
86+
}
6187

62-
for _, mock := range o.mocks {
63-
mock.ExpectedCalls = nil
64-
mock.Calls = nil
65-
}
88+
for _, setup := range o.setup {
89+
setup()
90+
}
91+
92+
defer o.runCleanup()
6693

67-
for _, setup := range o.setup {
68-
setup()
69-
}
94+
test.Func()
7095

71-
test.Func()
96+
for _, mock := range o.mocks {
97+
mock.AssertExpectations(o.t)
98+
}
99+
}
72100

73-
for _, mock := range o.mocks {
74-
mock.AssertExpectations(o.t)
75-
}
101+
func (o *Organizer) runCleanup() {
102+
for _, cleanup := range o.cleanup {
103+
cleanup()
76104
}
77105
}
78106

0 commit comments

Comments
 (0)