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 145 146 147 148
|
// Licensed under the MIT license, see LICENSE file for details.
package qtsuite_test
import (
"bytes"
"fmt"
"testing"
qt "github.com/frankban/quicktest"
"github.com/frankban/quicktest/qtsuite"
)
func TestRunSuite(t *testing.T) {
c := qt.New(t)
var calls []call
tt := &testingT{}
qtsuite.Run(qt.New(tt), testSuite{calls: &calls})
c.Assert(calls, qt.DeepEquals, []call{
{"Test1", 0},
{"Test4", 0},
})
}
func TestRunSuiteEmbedded(t *testing.T) {
c := qt.New(t)
var calls []call
tt := &testingT{}
suite := struct {
testSuite
}{testSuite: testSuite{calls: &calls}}
qtsuite.Run(qt.New(tt), suite)
c.Assert(calls, qt.DeepEquals, []call{
{"Test1", 0},
{"Test4", 0},
})
}
func TestRunSuitePtr(t *testing.T) {
c := qt.New(t)
var calls []call
tt := &testingT{}
qtsuite.Run(qt.New(tt), &testSuite{calls: &calls})
c.Assert(calls, qt.DeepEquals, []call{
{"Init", 0},
{"Test1", 1},
{"Init", 0},
{"Test4", 1},
})
}
type testSuite struct {
init int
calls *[]call
}
func (s testSuite) addCall(name string) {
*s.calls = append(*s.calls, call{Name: name, Init: s.init})
}
func (s *testSuite) Init(*qt.C) {
s.addCall("Init")
s.init++
}
func (s testSuite) Test1(*qt.C) {
s.addCall("Test1")
}
func (s testSuite) Test2() {
s.addCall("Test2")
}
func (s testSuite) Test3(*testing.T) {
s.addCall("Test3")
}
func (s testSuite) Test4(*qt.C) {
s.addCall("Test4")
}
func (s testSuite) Test5(*qt.C) bool {
s.addCall("Test5")
return false
}
func (s testSuite) Testa(*qt.C) {
s.addCall("Testa")
}
type call struct {
Name string
Init int
}
func TestInvalidInit(t *testing.T) {
c := qt.New(t)
tt := &testingT{}
tc := qt.New(tt)
qtsuite.Run(tc, invalidTestSuite{})
c.Assert(tt.fatalString(), qt.Equals, "wrong signature for Init, must be Init(*quicktest.C)")
}
type invalidTestSuite struct{}
func (invalidTestSuite) Init() {}
// testingT can be passed to qt.New for testing purposes.
type testingT struct {
testing.TB
errorBuf bytes.Buffer
fatalBuf bytes.Buffer
subTestResult bool
subTestName string
subTestT *testing.T
}
// Error overrides *testing.T.Error so that messages are collected.
func (t *testingT) Error(a ...interface{}) {
fmt.Fprint(&t.errorBuf, a...)
}
// Fatal overrides *testing.T.Fatal so that messages are collected and the
// goroutine is not killed.
func (t *testingT) Fatal(a ...interface{}) {
fmt.Fprint(&t.fatalBuf, a...)
}
// Run overrides *testing.T.Run.
func (t *testingT) Run(name string, f func(t *testing.T)) bool {
t.subTestName, t.subTestT = name, &testing.T{}
ch := make(chan struct{})
// Run the subtest in its own goroutine so that if it calls runtime.GoExit,
// we can still return appropriately.
go func() {
defer close(ch)
f(t.subTestT)
}()
<-ch
return t.subTestResult
}
// fatalString returns the fatal error message.
func (t *testingT) fatalString() string {
return t.fatalBuf.String()
}
|