| 1 | // This file contains just a few generic helpers which are used by the |
| 2 | // other test files. |
| 3 | |
| 4 | package check_testscip-go gomod github.com/pingcap/check . `github.com/pingcap/check_test`/ |
| 5 | |
| 6 | import ( |
| 7 | "flagscip-go gomod github.com/golang/go/src go1.13 flag/" |
| 8 | "fmtscip-go gomod github.com/golang/go/src go1.13 fmt/" |
| 9 | "osscip-go gomod github.com/golang/go/src go1.13 os/" |
| 10 | "regexpscip-go gomod github.com/golang/go/src go1.13 regexp/" |
| 11 | "runtimescip-go gomod github.com/golang/go/src go1.13 runtime/" |
| 12 | "testingscip-go gomod github.com/golang/go/src go1.13 testing/" |
| 13 | "timescip-go gomod github.com/golang/go/src go1.13 time/" |
| 14 | |
| 15 | "github.com/pingcap/checkscip-go gomod github.com/pingcap/check . `github.com/pingcap/check`/" |
| 16 | ) |
| 17 | |
| 18 | // We count the number of suites run at least to get a vague hint that the |
| 19 | // test suite is behaving as it should. Otherwise a bug introduced at the |
| 20 | // very core of the system could go unperceived. |
| 21 | const suitesRunExpectedscip-go gomod github.com/pingcap/check . `github.com/pingcap/check_test`/suitesRunExpected. = 8 |
| 22 | |
| 23 | var suitesRunscip-go gomod github.com/pingcap/check . `github.com/pingcap/check_test`/suitesRun. int = 0 |
| 24 | |
| 25 | func Testscip-go gomod github.com/pingcap/check . `github.com/pingcap/check_test`/Test().(t *testingscip-go gomod github.com/golang/go/src go1.13 testing/.Tscip-go gomod github.com/golang/go/src go1.13 testing/T#) { |
| 26 | checkscip-go gomod github.com/pingcap/check . `github.com/pingcap/check`/.TestingTscip-go gomod github.com/pingcap/check . `github.com/pingcap/check`/TestingT().(t) |
| 27 | if suitesRunscip-go gomod github.com/pingcap/check . `github.com/pingcap/check_test`/suitesRun. != suitesRunExpectedscip-go gomod github.com/pingcap/check . `github.com/pingcap/check_test`/suitesRunExpected. && flagscip-go gomod github.com/golang/go/src go1.13 flag/.Lookupscip-go gomod github.com/golang/go/src go1.13 flag/Lookup().("check.g").Valuescip-go gomod github.com/golang/go/src go1.13 flag/Flag#Value..Stringscip-go gomod github.com/golang/go/src go1.13 flag/Value#String().() == "" { |
| 28 | criticalscip-go gomod github.com/pingcap/check . `github.com/pingcap/check_test`/critical().(fmtscip-go gomod github.com/golang/go/src go1.13 fmt/.Sprintfscip-go gomod github.com/golang/go/src go1.13 fmt/Sprintf().("Expected %d suites to run rather than %d", |
| 29 | suitesRunExpectedscip-go gomod github.com/pingcap/check . `github.com/pingcap/check_test`/suitesRunExpected., suitesRunscip-go gomod github.com/pingcap/check . `github.com/pingcap/check_test`/suitesRun.)) |
| 30 | } |
| 31 | } |
| 32 | |
| 33 | // ----------------------------------------------------------------------- |
| 34 | // Helper functions. |
| 35 | |
| 36 | // Break down badly. This is used in test cases which can't yet assume |
| 37 | // that the fundamental bits are working. |
| 38 | func criticalscip-go gomod github.com/pingcap/check . `github.com/pingcap/check_test`/critical().(error string) { |
| 39 | fmtscip-go gomod github.com/golang/go/src go1.13 fmt/.Fprintlnscip-go gomod github.com/golang/go/src go1.13 fmt/Fprintln().(osscip-go gomod github.com/golang/go/src go1.13 os/.Stderrscip-go gomod github.com/golang/go/src go1.13 os/Stderr., "CRITICAL: "+error) |
| 40 | osscip-go gomod github.com/golang/go/src go1.13 os/.Exitscip-go gomod github.com/golang/go/src go1.13 os/Exit().(1) |
| 41 | } |
| 42 | |
| 43 | // Return the file line where it's called. |
| 44 | func getMyLinescip-go gomod github.com/pingcap/check . `github.com/pingcap/check_test`/getMyLine().() int { |
| 45 | if _, _, line, ok := runtimescip-go gomod github.com/golang/go/src go1.13 runtime/.Callerscip-go gomod github.com/golang/go/src go1.13 runtime/Caller().(1); ok { |
| 46 | return line |
| 47 | } |
| 48 | return -1 |
| 49 | } |
| 50 | |
| 51 | // ----------------------------------------------------------------------- |
| 52 | // Helper type implementing a basic io.Writer for testing output. |
| 53 | |
| 54 | // Type implementing the io.Writer interface for analyzing output. |
| 55 | type Stringscip-go gomod github.com/pingcap/check . `github.com/pingcap/check_test`/String# struct { |
| 56 | valuescip-go gomod github.com/pingcap/check . `github.com/pingcap/check_test`/String#value. string |
| 57 | } |
| 58 | |
| 59 | // The only function required by the io.Writer interface. Will append |
| 60 | // written data to the String.value string. |
| 61 | func (s *Stringscip-go gomod github.com/pingcap/check . `github.com/pingcap/check_test`/String#) Writescip-go gomod github.com/pingcap/check . `github.com/pingcap/check_test`/String#Write().(p []byte) (n int, err error) { |
| 62 | s.valuescip-go gomod github.com/pingcap/check . `github.com/pingcap/check_test`/String#value. += string(p) |
| 63 | return len(p), nil |
| 64 | } |
| 65 | |
| 66 | // Trivial wrapper to test errors happening on a different file |
| 67 | // than the test itself. |
| 68 | func checkEqualWrapperscip-go gomod github.com/pingcap/check . `github.com/pingcap/check_test`/checkEqualWrapper().(c *checkscip-go gomod github.com/pingcap/check . `github.com/pingcap/check`/.Cscip-go gomod github.com/pingcap/check . `github.com/pingcap/check`/C#, obtained, expected interface{}) (result bool, line int) { |
| 69 | return c.Checkscip-go gomod github.com/pingcap/check . `github.com/pingcap/check`/C#Check().(obtained, checkscip-go gomod github.com/pingcap/check . `github.com/pingcap/check`/.Equalsscip-go gomod github.com/pingcap/check . `github.com/pingcap/check`/Equals., expected), getMyLinescip-go gomod github.com/pingcap/check . `github.com/pingcap/check_test`/getMyLine().() |
| 70 | } |
| 71 | |
| 72 | // ----------------------------------------------------------------------- |
| 73 | // Helper suite for testing basic fail behavior. |
| 74 | |
| 75 | type FailHelperscip-go gomod github.com/pingcap/check . `github.com/pingcap/check_test`/FailHelper# struct { |
| 76 | testLinescip-go gomod github.com/pingcap/check . `github.com/pingcap/check_test`/FailHelper#testLine. int |
| 77 | } |
| 78 | |
| 79 | func (s *FailHelperscip-go gomod github.com/pingcap/check . `github.com/pingcap/check_test`/FailHelper#) TestLogAndFailscip-go gomod github.com/pingcap/check . `github.com/pingcap/check_test`/FailHelper#TestLogAndFail().(c *checkscip-go gomod github.com/pingcap/check . `github.com/pingcap/check`/.Cscip-go gomod github.com/pingcap/check . `github.com/pingcap/check`/C#) { |
| 80 | s.testLinescip-go gomod github.com/pingcap/check . `github.com/pingcap/check_test`/FailHelper#testLine. = getMyLinescip-go gomod github.com/pingcap/check . `github.com/pingcap/check_test`/getMyLine().() - 1 |
| 81 | c.Logscip-go gomod github.com/pingcap/check . `github.com/pingcap/check`/C#Log().("Expected failure!") |
| 82 | c.Failscip-go gomod github.com/pingcap/check . `github.com/pingcap/check`/C#Fail().() |
| 83 | } |
| 84 | |
| 85 | // ----------------------------------------------------------------------- |
| 86 | // Helper suite for testing basic success behavior. |
| 87 | |
| 88 | type SuccessHelperscip-go gomod github.com/pingcap/check . `github.com/pingcap/check_test`/SuccessHelper# struct{} |
| 89 | |
| 90 | func (s *SuccessHelperscip-go gomod github.com/pingcap/check . `github.com/pingcap/check_test`/SuccessHelper#) TestLogAndSucceedscip-go gomod github.com/pingcap/check . `github.com/pingcap/check_test`/SuccessHelper#TestLogAndSucceed().(c *checkscip-go gomod github.com/pingcap/check . `github.com/pingcap/check`/.Cscip-go gomod github.com/pingcap/check . `github.com/pingcap/check`/C#) { |
| 91 | c.Logscip-go gomod github.com/pingcap/check . `github.com/pingcap/check`/C#Log().("Expected success!") |
| 92 | } |
| 93 | |
| 94 | // ----------------------------------------------------------------------- |
| 95 | // Helper suite for testing ordering and behavior of fixture. |
| 96 | |
| 97 | type FixtureHelperscip-go gomod github.com/pingcap/check . `github.com/pingcap/check_test`/FixtureHelper# struct { |
| 98 | callsscip-go gomod github.com/pingcap/check . `github.com/pingcap/check_test`/FixtureHelper#calls. []string |
| 99 | panicOnscip-go gomod github.com/pingcap/check . `github.com/pingcap/check_test`/FixtureHelper#panicOn. string |
| 100 | skipscip-go gomod github.com/pingcap/check . `github.com/pingcap/check_test`/FixtureHelper#skip. bool |
| 101 | skipOnNscip-go gomod github.com/pingcap/check . `github.com/pingcap/check_test`/FixtureHelper#skipOnN. int |
| 102 | sleepOnscip-go gomod github.com/pingcap/check . `github.com/pingcap/check_test`/FixtureHelper#sleepOn. string |
| 103 | sleepscip-go gomod github.com/pingcap/check . `github.com/pingcap/check_test`/FixtureHelper#sleep. timescip-go gomod github.com/golang/go/src go1.13 time/.Durationscip-go gomod github.com/golang/go/src go1.13 time/Duration# |
| 104 | bytesscip-go gomod github.com/pingcap/check . `github.com/pingcap/check_test`/FixtureHelper#bytes. int64 |
| 105 | } |
| 106 | |
| 107 | func (s *FixtureHelperscip-go gomod github.com/pingcap/check . `github.com/pingcap/check_test`/FixtureHelper#) tracescip-go gomod github.com/pingcap/check . `github.com/pingcap/check_test`/FixtureHelper#trace().(name string, c *checkscip-go gomod github.com/pingcap/check . `github.com/pingcap/check`/.Cscip-go gomod github.com/pingcap/check . `github.com/pingcap/check`/C#) { |
| 108 | s.callsscip-go gomod github.com/pingcap/check . `github.com/pingcap/check_test`/FixtureHelper#calls. = append(s.callsscip-go gomod github.com/pingcap/check . `github.com/pingcap/check_test`/FixtureHelper#calls., name) |
| 109 | if name == s.panicOnscip-go gomod github.com/pingcap/check . `github.com/pingcap/check_test`/FixtureHelper#panicOn. { |
| 110 | panic(name) |
| 111 | } |
| 112 | if s.sleepscip-go gomod github.com/pingcap/check . `github.com/pingcap/check_test`/FixtureHelper#sleep. > 0 && s.sleepOnscip-go gomod github.com/pingcap/check . `github.com/pingcap/check_test`/FixtureHelper#sleepOn. == name { |
| 113 | timescip-go gomod github.com/golang/go/src go1.13 time/.Sleepscip-go gomod github.com/golang/go/src go1.13 time/Sleep().(s.sleepscip-go gomod github.com/pingcap/check . `github.com/pingcap/check_test`/FixtureHelper#sleep.) |
| 114 | } |
| 115 | if s.skipscip-go gomod github.com/pingcap/check . `github.com/pingcap/check_test`/FixtureHelper#skip. && s.skipOnNscip-go gomod github.com/pingcap/check . `github.com/pingcap/check_test`/FixtureHelper#skipOnN. == len(s.callsscip-go gomod github.com/pingcap/check . `github.com/pingcap/check_test`/FixtureHelper#calls.)-1 { |
| 116 | c.Skipscip-go gomod github.com/pingcap/check . `github.com/pingcap/check`/C#Skip().("skipOnN == n") |
| 117 | } |
| 118 | } |
| 119 | |
| 120 | func (s *FixtureHelperscip-go gomod github.com/pingcap/check . `github.com/pingcap/check_test`/FixtureHelper#) SetUpSuitescip-go gomod github.com/pingcap/check . `github.com/pingcap/check_test`/FixtureHelper#SetUpSuite().(c *checkscip-go gomod github.com/pingcap/check . `github.com/pingcap/check`/.Cscip-go gomod github.com/pingcap/check . `github.com/pingcap/check`/C#) { |
| 121 | s.tracescip-go gomod github.com/pingcap/check . `github.com/pingcap/check_test`/FixtureHelper#trace().("SetUpSuite", c) |
| 122 | } |
| 123 | |
| 124 | func (s *FixtureHelperscip-go gomod github.com/pingcap/check . `github.com/pingcap/check_test`/FixtureHelper#) TearDownSuitescip-go gomod github.com/pingcap/check . `github.com/pingcap/check_test`/FixtureHelper#TearDownSuite().(c *checkscip-go gomod github.com/pingcap/check . `github.com/pingcap/check`/.Cscip-go gomod github.com/pingcap/check . `github.com/pingcap/check`/C#) { |
| 125 | s.tracescip-go gomod github.com/pingcap/check . `github.com/pingcap/check_test`/FixtureHelper#trace().("TearDownSuite", c) |
| 126 | } |
| 127 | |
| 128 | func (s *FixtureHelperscip-go gomod github.com/pingcap/check . `github.com/pingcap/check_test`/FixtureHelper#) SetUpTestscip-go gomod github.com/pingcap/check . `github.com/pingcap/check_test`/FixtureHelper#SetUpTest().(c *checkscip-go gomod github.com/pingcap/check . `github.com/pingcap/check`/.Cscip-go gomod github.com/pingcap/check . `github.com/pingcap/check`/C#) { |
| 129 | s.tracescip-go gomod github.com/pingcap/check . `github.com/pingcap/check_test`/FixtureHelper#trace().("SetUpTest", c) |
| 130 | } |
| 131 | |
| 132 | func (s *FixtureHelperscip-go gomod github.com/pingcap/check . `github.com/pingcap/check_test`/FixtureHelper#) TearDownTestscip-go gomod github.com/pingcap/check . `github.com/pingcap/check_test`/FixtureHelper#TearDownTest().(c *checkscip-go gomod github.com/pingcap/check . `github.com/pingcap/check`/.Cscip-go gomod github.com/pingcap/check . `github.com/pingcap/check`/C#) { |
| 133 | s.tracescip-go gomod github.com/pingcap/check . `github.com/pingcap/check_test`/FixtureHelper#trace().("TearDownTest", c) |
| 134 | } |
| 135 | |
| 136 | func (s *FixtureHelperscip-go gomod github.com/pingcap/check . `github.com/pingcap/check_test`/FixtureHelper#) Test1scip-go gomod github.com/pingcap/check . `github.com/pingcap/check_test`/FixtureHelper#Test1().(c *checkscip-go gomod github.com/pingcap/check . `github.com/pingcap/check`/.Cscip-go gomod github.com/pingcap/check . `github.com/pingcap/check`/C#) { |
| 137 | s.tracescip-go gomod github.com/pingcap/check . `github.com/pingcap/check_test`/FixtureHelper#trace().("Test1", c) |
| 138 | } |
| 139 | |
| 140 | func (s *FixtureHelperscip-go gomod github.com/pingcap/check . `github.com/pingcap/check_test`/FixtureHelper#) Test2scip-go gomod github.com/pingcap/check . `github.com/pingcap/check_test`/FixtureHelper#Test2().(c *checkscip-go gomod github.com/pingcap/check . `github.com/pingcap/check`/.Cscip-go gomod github.com/pingcap/check . `github.com/pingcap/check`/C#) { |
| 141 | s.tracescip-go gomod github.com/pingcap/check . `github.com/pingcap/check_test`/FixtureHelper#trace().("Test2", c) |
| 142 | } |
| 143 | |
| 144 | func (s *FixtureHelperscip-go gomod github.com/pingcap/check . `github.com/pingcap/check_test`/FixtureHelper#) Benchmark1scip-go gomod github.com/pingcap/check . `github.com/pingcap/check_test`/FixtureHelper#Benchmark1().(c *checkscip-go gomod github.com/pingcap/check . `github.com/pingcap/check`/.Cscip-go gomod github.com/pingcap/check . `github.com/pingcap/check`/C#) { |
| 145 | s.tracescip-go gomod github.com/pingcap/check . `github.com/pingcap/check_test`/FixtureHelper#trace().("Benchmark1", c) |
| 146 | for i := 0; i < c.Nscip-go gomod github.com/pingcap/check . `github.com/pingcap/check`/timer#N.; i++ { |
| 147 | timescip-go gomod github.com/golang/go/src go1.13 time/.Sleepscip-go gomod github.com/golang/go/src go1.13 time/Sleep().(s.sleepscip-go gomod github.com/pingcap/check . `github.com/pingcap/check_test`/FixtureHelper#sleep.) |
| 148 | } |
| 149 | } |
| 150 | |
| 151 | func (s *FixtureHelperscip-go gomod github.com/pingcap/check . `github.com/pingcap/check_test`/FixtureHelper#) Benchmark2scip-go gomod github.com/pingcap/check . `github.com/pingcap/check_test`/FixtureHelper#Benchmark2().(c *checkscip-go gomod github.com/pingcap/check . `github.com/pingcap/check`/.Cscip-go gomod github.com/pingcap/check . `github.com/pingcap/check`/C#) { |
| 152 | s.tracescip-go gomod github.com/pingcap/check . `github.com/pingcap/check_test`/FixtureHelper#trace().("Benchmark2", c) |
| 153 | c.SetBytesscip-go gomod github.com/pingcap/check . `github.com/pingcap/check`/C#SetBytes().(1024) |
| 154 | for i := 0; i < c.Nscip-go gomod github.com/pingcap/check . `github.com/pingcap/check`/timer#N.; i++ { |
| 155 | timescip-go gomod github.com/golang/go/src go1.13 time/.Sleepscip-go gomod github.com/golang/go/src go1.13 time/Sleep().(s.sleepscip-go gomod github.com/pingcap/check . `github.com/pingcap/check_test`/FixtureHelper#sleep.) |
| 156 | } |
| 157 | } |
| 158 | |
| 159 | func (s *FixtureHelperscip-go gomod github.com/pingcap/check . `github.com/pingcap/check_test`/FixtureHelper#) Benchmark3scip-go gomod github.com/pingcap/check . `github.com/pingcap/check_test`/FixtureHelper#Benchmark3().(c *checkscip-go gomod github.com/pingcap/check . `github.com/pingcap/check`/.Cscip-go gomod github.com/pingcap/check . `github.com/pingcap/check`/C#) { |
| 160 | var x []int64 |
| 161 | s.tracescip-go gomod github.com/pingcap/check . `github.com/pingcap/check_test`/FixtureHelper#trace().("Benchmark3", c) |
| 162 | for i := 0; i < c.Nscip-go gomod github.com/pingcap/check . `github.com/pingcap/check`/timer#N.; i++ { |
| 163 | timescip-go gomod github.com/golang/go/src go1.13 time/.Sleepscip-go gomod github.com/golang/go/src go1.13 time/Sleep().(s.sleepscip-go gomod github.com/pingcap/check . `github.com/pingcap/check_test`/FixtureHelper#sleep.) |
| 164 | x = make([]int64, 5) |
| 165 | _ = x |
| 166 | } |
| 167 | } |
| 168 | |
| 169 | // ----------------------------------------------------------------------- |
| 170 | // Helper which checks the state of the test and ensures that it matches |
| 171 | // the given expectations. Depends on c.Errorf() working, so shouldn't |
| 172 | // be used to test this one function. |
| 173 | |
| 174 | type expectedStatescip-go gomod github.com/pingcap/check . `github.com/pingcap/check_test`/expectedState# struct { |
| 175 | namescip-go gomod github.com/pingcap/check . `github.com/pingcap/check_test`/expectedState#name. string |
| 176 | resultscip-go gomod github.com/pingcap/check . `github.com/pingcap/check_test`/expectedState#result. interface{} |
| 177 | failedscip-go gomod github.com/pingcap/check . `github.com/pingcap/check_test`/expectedState#failed. bool |
| 178 | logscip-go gomod github.com/pingcap/check . `github.com/pingcap/check_test`/expectedState#log. string |
| 179 | } |
| 180 | |
| 181 | // Verify the state of the test. Note that since this also verifies if |
| 182 | // the test is supposed to be in a failed state, no other checks should |
| 183 | // be done in addition to what is being tested. |
| 184 | func checkStatescip-go gomod github.com/pingcap/check . `github.com/pingcap/check_test`/checkState().(c *checkscip-go gomod github.com/pingcap/check . `github.com/pingcap/check`/.Cscip-go gomod github.com/pingcap/check . `github.com/pingcap/check`/C#, result interface{}, expected *expectedStatescip-go gomod github.com/pingcap/check . `github.com/pingcap/check_test`/expectedState#) { |
| 185 | failed := c.Failedscip-go gomod github.com/pingcap/check . `github.com/pingcap/check`/C#Failed().() |
| 186 | c.Succeedscip-go gomod github.com/pingcap/check . `github.com/pingcap/check`/C#Succeed().() |
| 187 | log := c.GetTestLogscip-go gomod github.com/pingcap/check . `github.com/pingcap/check`/C#GetTestLog().() |
| 188 | matched, matchError := regexpscip-go gomod github.com/golang/go/src go1.13 regexp/.MatchStringscip-go gomod github.com/golang/go/src go1.13 regexp/MatchString().("^"+expected.logscip-go gomod github.com/pingcap/check . `github.com/pingcap/check_test`/expectedState#log.+"$", log) |
| 189 | if matchError != nil { |
| 190 | c.Errorfscip-go gomod github.com/pingcap/check . `github.com/pingcap/check`/C#Errorf().("Error in matching expression used in testing %s", |
| 191 | expected.namescip-go gomod github.com/pingcap/check . `github.com/pingcap/check_test`/expectedState#name.) |
| 192 | } else if !matched { |
| 193 | c.Errorfscip-go gomod github.com/pingcap/check . `github.com/pingcap/check`/C#Errorf().("%s logged:\n----------\n%s----------\n\nExpected:\n----------\n%s\n----------", |
| 194 | expected.namescip-go gomod github.com/pingcap/check . `github.com/pingcap/check_test`/expectedState#name., log, expected.logscip-go gomod github.com/pingcap/check . `github.com/pingcap/check_test`/expectedState#log.) |
| 195 | } |
| 196 | if result != expected.resultscip-go gomod github.com/pingcap/check . `github.com/pingcap/check_test`/expectedState#result. { |
| 197 | c.Errorfscip-go gomod github.com/pingcap/check . `github.com/pingcap/check`/C#Errorf().("%s returned %#v rather than %#v", |
| 198 | expected.namescip-go gomod github.com/pingcap/check . `github.com/pingcap/check_test`/expectedState#name., result, expected.resultscip-go gomod github.com/pingcap/check . `github.com/pingcap/check_test`/expectedState#result.) |
| 199 | } |
| 200 | if failed != expected.failedscip-go gomod github.com/pingcap/check . `github.com/pingcap/check_test`/expectedState#failed. { |
| 201 | if failed { |
| 202 | c.Errorfscip-go gomod github.com/pingcap/check . `github.com/pingcap/check`/C#Errorf().("%s has failed when it shouldn't", expected.namescip-go gomod github.com/pingcap/check . `github.com/pingcap/check_test`/expectedState#name.) |
| 203 | } else { |
| 204 | c.Errorfscip-go gomod github.com/pingcap/check . `github.com/pingcap/check`/C#Errorf().("%s has not failed when it should", expected.namescip-go gomod github.com/pingcap/check . `github.com/pingcap/check_test`/expectedState#name.) |
| 205 | } |
| 206 | } |
| 207 | } |