github.com/goplus/yap@v0.8.1/test/case.go (about) 1 /* 2 * Copyright (c) 2024 The GoPlus Authors (goplus.org). All rights reserved. 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 package test 18 19 import ( 20 "testing" 21 "time" 22 ) 23 24 // ----------------------------------------------------------------------------- 25 26 type CaseT interface { 27 // Name returns the name of the running (sub-) test or benchmark. 28 // 29 // The name will include the name of the test along with the names of 30 // any nested sub-tests. If two sibling sub-tests have the same name, 31 // Name will append a suffix to guarantee the returned name is unique. 32 Name() string 33 34 // Fail marks the function as having failed but continues execution. 35 Fail() 36 37 // Failed reports whether the function has failed. 38 Failed() bool 39 40 // FailNow marks the function as having failed and stops its execution 41 // by calling runtime.Goexit (which then runs all deferred calls in the 42 // current goroutine). 43 // Execution will continue at the next test or benchmark. 44 // FailNow must be called from the goroutine running the 45 // test or benchmark function, not from other goroutines 46 // created during the test. Calling FailNow does not stop 47 // those other goroutines. 48 FailNow() 49 50 // Log formats its arguments using default formatting, analogous to Println, 51 // and records the text in the error log. For tests, the text will be printed only if 52 // the test fails or the -test.v flag is set. For benchmarks, the text is always 53 // printed to avoid having performance depend on the value of the -test.v flag. 54 Log(args ...any) 55 56 // Logf formats its arguments according to the format, analogous to Printf, and 57 // records the text in the error log. A final newline is added if not provided. For 58 // tests, the text will be printed only if the test fails or the -test.v flag is 59 // set. For benchmarks, the text is always printed to avoid having performance 60 // depend on the value of the -test.v flag. 61 Logf(format string, args ...any) 62 63 // Errorln is equivalent to Log followed by Fail. 64 Errorln(args ...any) 65 66 // Errorf is equivalent to Logf followed by Fail. 67 Errorf(format string, args ...any) 68 69 // Fatal is equivalent to Log followed by FailNow. 70 Fatal(args ...any) 71 72 // Fatalf is equivalent to Logf followed by FailNow. 73 Fatalf(format string, args ...any) 74 75 // Skip is equivalent to Log followed by SkipNow. 76 Skip(args ...any) 77 78 // Skipf is equivalent to Logf followed by SkipNow. 79 Skipf(format string, args ...any) 80 81 // SkipNow marks the test as having been skipped and stops its execution 82 // by calling runtime.Goexit. 83 // If a test fails (see Error, Errorf, Fail) and is then skipped, 84 // it is still considered to have failed. 85 // Execution will continue at the next test or benchmark. See also FailNow. 86 // SkipNow must be called from the goroutine running the test, not from 87 // other goroutines created during the test. Calling SkipNow does not stop 88 // those other goroutines. 89 SkipNow() 90 91 // Skipped reports whether the test was skipped. 92 Skipped() bool 93 94 // Helper marks the calling function as a test helper function. 95 // When printing file and line information, that function will be skipped. 96 // Helper may be called simultaneously from multiple goroutines. 97 Helper() 98 99 // Cleanup registers a function to be called when the test (or subtest) and all its 100 // subtests complete. Cleanup functions will be called in last added, 101 // first called order. 102 Cleanup(f func()) 103 104 // TempDir returns a temporary directory for the test to use. 105 // The directory is automatically removed by Cleanup when the test and 106 // all its subtests complete. 107 // Each subsequent call to t.TempDir returns a unique directory; 108 // if the directory creation fails, TempDir terminates the test by calling Fatal. 109 TempDir() string 110 111 // Run runs f as a subtest of t called name. 112 // 113 // Run may be called simultaneously from multiple goroutines, but all such calls 114 // must return before the outer test function for t returns. 115 Run(name string, f func()) bool 116 117 // Deadline reports the time at which the test binary will have 118 // exceeded the timeout specified by the -timeout flag. 119 // 120 // The ok result is false if the -timeout flag indicates “no timeout” (0). 121 Deadline() (deadline time.Time, ok bool) 122 } 123 124 // ----------------------------------------------------------------------------- 125 126 type TestingT struct { 127 *testing.T 128 } 129 130 // NewT creates a testing object. 131 func NewT(t *testing.T) TestingT { 132 return TestingT{t} 133 } 134 135 // Errorln is equivalent to Log followed by Fail. 136 func (p TestingT) Errorln(args ...any) { 137 t := p.T 138 t.Helper() 139 t.Error(args...) 140 } 141 142 // Run runs f as a subtest of t called name. 143 // 144 // Run may be called simultaneously from multiple goroutines, but all such calls 145 // must return before the outer test function for t returns. 146 func (p TestingT) Run(name string, doSth func()) bool { 147 return p.T.Run(name, func(t *testing.T) { 148 doSth() 149 }) 150 } 151 152 // -----------------------------------------------------------------------------