~niemeyer/gocheck/old-trunk

« back to all changes in this revision

Viewing changes to gocheck_test.go

  • Committer: Gustavo Niemeyer
  • Date: 2011-04-14 11:45:36 UTC
  • Revision ID: gustavo@niemeyer.net-20110414114536-ug726tol52c7seat
gofmt

Show diffs side-by-side

added added

removed removed

Lines of Context:
5
5
 
6
6
 
7
7
import (
8
 
    "launchpad.net/gocheck"
9
 
    "testing"
10
 
    "runtime"
11
 
    "regexp"
12
 
    "flag"
13
 
    "fmt"
14
 
    "os"
 
8
        "launchpad.net/gocheck"
 
9
        "testing"
 
10
        "runtime"
 
11
        "regexp"
 
12
        "flag"
 
13
        "fmt"
 
14
        "os"
15
15
)
16
16
 
17
17
 
23
23
var suitesRun int = 0
24
24
 
25
25
func Test(t *testing.T) {
26
 
    gocheck.TestingT(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))
31
 
    }
 
26
        gocheck.TestingT(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))
 
31
        }
32
32
}
33
33
 
34
34
 
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)
42
 
    os.Exit(1)
 
41
        fmt.Fprintln(os.Stderr, "CRITICAL: "+error)
 
42
        os.Exit(1)
43
43
}
44
44
 
45
45
 
46
46
// Return the file line where it's called.
47
47
func getMyLine() int {
48
 
    if _, _, line, ok := runtime.Caller(1); ok {
49
 
        return line
50
 
    }
51
 
    return -1
 
48
        if _, _, line, ok := runtime.Caller(1); ok {
 
49
                return line
 
50
        }
 
51
        return -1
52
52
}
53
53
 
54
54
 
57
57
 
58
58
// Type implementing the io.Writer interface for analyzing output.
59
59
type String struct {
60
 
    value string
 
60
        value string
61
61
}
62
62
 
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) {
66
 
    s.value += string(p)
67
 
    return len(p), nil
 
66
        s.value += string(p)
 
67
        return len(p), nil
68
68
}
69
69
 
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()
74
74
}
75
75
 
76
76
 
78
78
// Helper suite for testing basic fail behavior.
79
79
 
80
80
type FailHelper struct {
81
 
    testLine int
 
81
        testLine int
82
82
}
83
83
 
84
84
func (s *FailHelper) TestLogAndFail(c *gocheck.C) {
85
 
    s.testLine = getMyLine() - 1
86
 
    c.Log("Expected failure!")
87
 
    c.Fail()
 
85
        s.testLine = getMyLine() - 1
 
86
        c.Log("Expected failure!")
 
87
        c.Fail()
88
88
}
89
89
 
90
90
 
94
94
type SuccessHelper struct{}
95
95
 
96
96
func (s *SuccessHelper) TestLogAndSucceed(c *gocheck.C) {
97
 
    c.Log("Expected success!")
 
97
        c.Log("Expected success!")
98
98
}
99
99
 
100
100
 
102
102
// Helper suite for testing ordering and behavior of fixture.
103
103
 
104
104
type FixtureHelper struct {
105
 
    calls   [64]string
106
 
    n       int
107
 
    panicOn string
108
 
    skip    bool
109
 
    skipOnN int
 
105
        calls   [64]string
 
106
        n       int
 
107
        panicOn string
 
108
        skip    bool
 
109
        skipOnN int
110
110
}
111
111
 
112
112
func (s *FixtureHelper) trace(name string, c *gocheck.C) {
113
 
    n := s.n
114
 
    s.calls[n] = name
115
 
    s.n += 1
116
 
    if name == s.panicOn {
117
 
        panic(name)
118
 
    }
119
 
    if s.skip && s.skipOnN == n {
120
 
        c.Skip("skipOnN == n")
121
 
    }
 
113
        n := s.n
 
114
        s.calls[n] = name
 
115
        s.n += 1
 
116
        if name == s.panicOn {
 
117
                panic(name)
 
118
        }
 
119
        if s.skip && s.skipOnN == n {
 
120
                c.Skip("skipOnN == n")
 
121
        }
122
122
}
123
123
 
124
124
func (s *FixtureHelper) SetUpSuite(c *gocheck.C) {
125
 
    s.trace("SetUpSuite", c)
 
125
        s.trace("SetUpSuite", c)
126
126
}
127
127
 
128
128
func (s *FixtureHelper) TearDownSuite(c *gocheck.C) {
129
 
    s.trace("TearDownSuite", c)
 
129
        s.trace("TearDownSuite", c)
130
130
}
131
131
 
132
132
func (s *FixtureHelper) SetUpTest(c *gocheck.C) {
133
 
    s.trace("SetUpTest", c)
 
133
        s.trace("SetUpTest", c)
134
134
}
135
135
 
136
136
func (s *FixtureHelper) TearDownTest(c *gocheck.C) {
137
 
    s.trace("TearDownTest", c)
 
137
        s.trace("TearDownTest", c)
138
138
}
139
139
 
140
140
func (s *FixtureHelper) Test1(c *gocheck.C) {
141
 
    s.trace("Test1", c)
 
141
        s.trace("Test1", c)
142
142
}
143
143
 
144
144
func (s *FixtureHelper) Test2(c *gocheck.C) {
145
 
    s.trace("Test2", c)
 
145
        s.trace("Test2", c)
146
146
}
147
147
 
148
148
 
152
152
// be used to test this one function.
153
153
 
154
154
type expectedState struct {
155
 
    name   string
156
 
    result interface{}
157
 
    failed bool
158
 
    log    string
 
155
        name   string
 
156
        result interface{}
 
157
        failed bool
 
158
        log    string
159
159
}
160
160
 
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) {
165
 
    failed := c.Failed()
166
 
    c.Succeed()
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",
171
 
            expected.name)
172
 
    } else if !matched {
173
 
        c.Errorf("%s logged %#v which doesn't match %#v",
174
 
            expected.name, log, expected.log)
175
 
    }
176
 
    if result != expected.result {
177
 
        c.Errorf("%s returned %#v rather than %#v",
178
 
            expected.name, result, expected.result)
179
 
    }
180
 
    if failed != expected.failed {
181
 
        if failed {
182
 
            c.Errorf("%s has failed when it shouldn't", expected.name)
183
 
        } else {
184
 
            c.Errorf("%s has not failed when it should", expected.name)
185
 
        }
186
 
    }
 
165
        failed := c.Failed()
 
166
        c.Succeed()
 
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",
 
171
                        expected.name)
 
172
        } else if !matched {
 
173
                c.Errorf("%s logged %#v which doesn't match %#v",
 
174
                        expected.name, log, expected.log)
 
175
        }
 
176
        if result != expected.result {
 
177
                c.Errorf("%s returned %#v rather than %#v",
 
178
                        expected.name, result, expected.result)
 
179
        }
 
180
        if failed != expected.failed {
 
181
                if failed {
 
182
                        c.Errorf("%s has failed when it shouldn't", expected.name)
 
183
                } else {
 
184
                        c.Errorf("%s has not failed when it should", expected.name)
 
185
                }
 
186
        }
187
187
}