23
23
var suitesRun int = 0
25
25
func Test(t *testing.T) {
27
if suitesRun != suitesRunExpected &&
28
flag.Lookup("f").Value.String() == "" {
29
critical(fmt.Sprintf("Expected %d suites to run rather than %d",
30
suitesRunExpected, suitesRun))
27
if suitesRun != suitesRunExpected &&
28
flag.Lookup("f").Value.String() == "" {
29
critical(fmt.Sprintf("Expected %d suites to run rather than %d",
30
suitesRunExpected, suitesRun))
38
38
// Break down badly. This is used in test cases which can't yet assume
39
39
// that the fundamental bits are working.
40
40
func critical(error string) {
41
fmt.Fprintln(os.Stderr, "CRITICAL: "+error)
41
fmt.Fprintln(os.Stderr, "CRITICAL: "+error)
46
46
// Return the file line where it's called.
47
47
func getMyLine() int {
48
if _, _, line, ok := runtime.Caller(1); ok {
48
if _, _, line, ok := runtime.Caller(1); ok {
58
58
// Type implementing the io.Writer interface for analyzing output.
59
59
type String struct {
63
63
// The only function required by the io.Writer interface. Will append
64
64
// written data to the String.value string.
65
65
func (s *String) Write(p []byte) (n int, err os.Error) {
70
70
// Trivial wrapper to test errors happening on a different file
71
71
// than the test itself.
72
72
func checkEqualWrapper(c *gocheck.C, obtained, expected interface{}) (result bool, line int) {
73
return c.Check(obtained, gocheck.Equals, expected), getMyLine()
73
return c.Check(obtained, gocheck.Equals, expected), getMyLine()
78
78
// Helper suite for testing basic fail behavior.
80
80
type FailHelper struct {
84
84
func (s *FailHelper) TestLogAndFail(c *gocheck.C) {
85
s.testLine = getMyLine() - 1
86
c.Log("Expected failure!")
85
s.testLine = getMyLine() - 1
86
c.Log("Expected failure!")
102
102
// Helper suite for testing ordering and behavior of fixture.
104
104
type FixtureHelper struct {
112
112
func (s *FixtureHelper) trace(name string, c *gocheck.C) {
116
if name == s.panicOn {
119
if s.skip && s.skipOnN == n {
120
c.Skip("skipOnN == n")
116
if name == s.panicOn {
119
if s.skip && s.skipOnN == n {
120
c.Skip("skipOnN == n")
124
124
func (s *FixtureHelper) SetUpSuite(c *gocheck.C) {
125
s.trace("SetUpSuite", c)
125
s.trace("SetUpSuite", c)
128
128
func (s *FixtureHelper) TearDownSuite(c *gocheck.C) {
129
s.trace("TearDownSuite", c)
129
s.trace("TearDownSuite", c)
132
132
func (s *FixtureHelper) SetUpTest(c *gocheck.C) {
133
s.trace("SetUpTest", c)
133
s.trace("SetUpTest", c)
136
136
func (s *FixtureHelper) TearDownTest(c *gocheck.C) {
137
s.trace("TearDownTest", c)
137
s.trace("TearDownTest", c)
140
140
func (s *FixtureHelper) Test1(c *gocheck.C) {
144
144
func (s *FixtureHelper) Test2(c *gocheck.C) {
152
152
// be used to test this one function.
154
154
type expectedState struct {
161
161
// Verify the state of the test. Note that since this also verifies if
162
162
// the test is supposed to be in a failed state, no other checks should
163
163
// be done in addition to what is being tested.
164
164
func checkState(c *gocheck.C, result interface{}, expected *expectedState) {
167
log := c.GetTestLog()
168
matched, matchError := regexp.MatchString("^"+expected.log+"$", log)
169
if matchError != nil {
170
c.Errorf("Error in matching expression used in testing %s",
173
c.Errorf("%s logged %#v which doesn't match %#v",
174
expected.name, log, expected.log)
176
if result != expected.result {
177
c.Errorf("%s returned %#v rather than %#v",
178
expected.name, result, expected.result)
180
if failed != expected.failed {
182
c.Errorf("%s has failed when it shouldn't", expected.name)
184
c.Errorf("%s has not failed when it should", expected.name)
167
log := c.GetTestLog()
168
matched, matchError := regexp.MatchString("^"+expected.log+"$", log)
169
if matchError != nil {
170
c.Errorf("Error in matching expression used in testing %s",
173
c.Errorf("%s logged %#v which doesn't match %#v",
174
expected.name, log, expected.log)
176
if result != expected.result {
177
c.Errorf("%s returned %#v rather than %#v",
178
expected.name, result, expected.result)
180
if failed != expected.failed {
182
c.Errorf("%s has failed when it shouldn't", expected.name)
184
c.Errorf("%s has not failed when it should", expected.name)