| 1 |
// Licensed under the MIT license, see LICENSE file for details. |
| 2 |
|
| 3 |
package qtsuite_test |
| 4 |
|
| 5 |
import ( |
| 6 |
"bytes" |
| 7 |
"fmt" |
| 8 |
"testing" |
| 9 |
|
| 10 |
qt "github.com/frankban/quicktest" |
| 11 |
"github.com/frankban/quicktest/qtsuite" |
| 12 |
) |
| 13 |
|
| 14 |
func TestRunSuite(t *testing.T) { |
| 15 |
c := qt.New(t) |
| 16 |
var calls []call |
| 17 |
tt := &testingT{} |
| 18 |
qtsuite.Run(qt.New(tt), testSuite{calls: &calls}) |
| 19 |
c.Assert(calls, qt.DeepEquals, []call{ |
| 20 |
{"Test1", 0}, |
| 21 |
{"Test4", 0}, |
| 22 |
}) |
| 23 |
} |
| 24 |
|
| 25 |
func TestRunSuiteEmbedded(t *testing.T) { |
| 26 |
c := qt.New(t) |
| 27 |
var calls []call |
| 28 |
tt := &testingT{} |
| 29 |
suite := struct { |
| 30 |
testSuite |
| 31 |
}{testSuite: testSuite{calls: &calls}} |
| 32 |
qtsuite.Run(qt.New(tt), suite) |
| 33 |
c.Assert(calls, qt.DeepEquals, []call{ |
| 34 |
{"Test1", 0}, |
| 35 |
{"Test4", 0}, |
| 36 |
}) |
| 37 |
} |
| 38 |
|
| 39 |
func TestRunSuitePtr(t *testing.T) { |
| 40 |
c := qt.New(t) |
| 41 |
var calls []call |
| 42 |
tt := &testingT{} |
| 43 |
qtsuite.Run(qt.New(tt), &testSuite{calls: &calls}) |
| 44 |
c.Assert(calls, qt.DeepEquals, []call{ |
| 45 |
{"Init", 0}, |
| 46 |
{"Test1", 1}, |
| 47 |
{"Init", 0}, |
| 48 |
{"Test4", 1}, |
| 49 |
}) |
| 50 |
} |
| 51 |
|
| 52 |
type testSuite struct { |
| 53 |
init int |
| 54 |
calls *[]call |
| 55 |
} |
| 56 |
|
| 57 |
func (s testSuite) addCall(name string) { |
| 58 |
*s.calls = append(*s.calls, call{Name: name, Init: s.init}) |
| 59 |
} |
| 60 |
|
| 61 |
func (s *testSuite) Init(*qt.C) { |
| 62 |
s.addCall("Init") |
| 63 |
s.init++ |
| 64 |
} |
| 65 |
|
| 66 |
func (s testSuite) Test1(*qt.C) { |
| 67 |
s.addCall("Test1") |
| 68 |
} |
| 69 |
|
| 70 |
func (s testSuite) Test2() { |
| 71 |
s.addCall("Test2") |
| 72 |
} |
| 73 |
|
| 74 |
func (s testSuite) Test3(*testing.T) { |
| 75 |
s.addCall("Test3") |
| 76 |
} |
| 77 |
|
| 78 |
func (s testSuite) Test4(*qt.C) { |
| 79 |
s.addCall("Test4") |
| 80 |
} |
| 81 |
|
| 82 |
func (s testSuite) Test5(*qt.C) bool { |
| 83 |
s.addCall("Test5") |
| 84 |
return false |
| 85 |
} |
| 86 |
|
| 87 |
func (s testSuite) Testa(*qt.C) { |
| 88 |
s.addCall("Testa") |
| 89 |
} |
| 90 |
|
| 91 |
type call struct { |
| 92 |
Name string |
| 93 |
Init int |
| 94 |
} |
| 95 |
|
| 96 |
func TestInvalidInit(t *testing.T) { |
| 97 |
c := qt.New(t) |
| 98 |
tt := &testingT{} |
| 99 |
tc := qt.New(tt) |
| 100 |
qtsuite.Run(tc, invalidTestSuite{}) |
| 101 |
c.Assert(tt.fatalString(), qt.Equals, "wrong signature for Init, must be Init(*quicktest.C)") |
| 102 |
} |
| 103 |
|
| 104 |
type invalidTestSuite struct{} |
| 105 |
|
| 106 |
func (invalidTestSuite) Init() {} |
| 107 |
|
| 108 |
// testingT can be passed to qt.New for testing purposes. |
| 109 |
type testingT struct { |
| 110 |
testing.TB |
| 111 |
|
| 112 |
errorBuf bytes.Buffer |
| 113 |
fatalBuf bytes.Buffer |
| 114 |
|
| 115 |
subTestResult bool |
| 116 |
subTestName string |
| 117 |
subTestT *testing.T |
| 118 |
} |
| 119 |
|
| 120 |
// Error overrides *testing.T.Error so that messages are collected. |
| 121 |
func (t *testingT) Error(a ...interface{}) { |
| 122 |
fmt.Fprint(&t.errorBuf, a...) |
| 123 |
} |
| 124 |
|
| 125 |
// Fatal overrides *testing.T.Fatal so that messages are collected and the |
| 126 |
// goroutine is not killed. |
| 127 |
func (t *testingT) Fatal(a ...interface{}) { |
| 128 |
fmt.Fprint(&t.fatalBuf, a...) |
| 129 |
} |
| 130 |
|
| 131 |
// Run overrides *testing.T.Run. |
| 132 |
func (t *testingT) Run(name string, f func(t *testing.T)) bool { |
| 133 |
t.subTestName, t.subTestT = name, &testing.T{} |
| 134 |
ch := make(chan struct{}) |
| 135 |
// Run the subtest in its own goroutine so that if it calls runtime.GoExit, |
| 136 |
// we can still return appropriately. |
| 137 |
go func() { |
| 138 |
defer close(ch) |
| 139 |
f(t.subTestT) |
| 140 |
}() |
| 141 |
<-ch |
| 142 |
return t.subTestResult |
| 143 |
} |
| 144 |
|
| 145 |
// fatalString returns the fatal error message. |
| 146 |
func (t *testingT) fatalString() string { |
| 147 |
return t.fatalBuf.String() |
| 148 |
} |