|
| 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 | +// CaseT is an interface that defines the methods for a test case. |
| 27 | +type CaseT interface { |
| 28 | + // Name returns the name of the running (sub-) test or benchmark. |
| 29 | + // |
| 30 | + // The name will include the name of the test along with the names of |
| 31 | + // any nested sub-tests. If two sibling sub-tests have the same name, |
| 32 | + // Name will append a suffix to guarantee the returned name is unique. |
| 33 | + Name() string |
| 34 | + |
| 35 | + // Fail marks the function as having failed but continues execution. |
| 36 | + Fail() |
| 37 | + |
| 38 | + // Failed reports whether the function has failed. |
| 39 | + Failed() bool |
| 40 | + |
| 41 | + // FailNow marks the function as having failed and stops its execution |
| 42 | + // by calling runtime.Goexit (which then runs all deferred calls in the |
| 43 | + // current goroutine). |
| 44 | + // Execution will continue at the next test or benchmark. |
| 45 | + // FailNow must be called from the goroutine running the |
| 46 | + // test or benchmark function, not from other goroutines |
| 47 | + // created during the test. Calling FailNow does not stop |
| 48 | + // those other goroutines. |
| 49 | + FailNow() |
| 50 | + |
| 51 | + // Log formats its arguments using default formatting, analogous to Println, |
| 52 | + // and records the text in the error log. For tests, the text will be printed only if |
| 53 | + // the test fails or the -test.v flag is set. For benchmarks, the text is always |
| 54 | + // printed to avoid having performance depend on the value of the -test.v flag. |
| 55 | + Log(args ...any) |
| 56 | + |
| 57 | + // Logf formats its arguments according to the format, analogous to Printf, and |
| 58 | + // records the text in the error log. A final newline is added if not provided. For |
| 59 | + // tests, the text will be printed only if the test fails or the -test.v flag is |
| 60 | + // set. For benchmarks, the text is always printed to avoid having performance |
| 61 | + // depend on the value of the -test.v flag. |
| 62 | + Logf(format string, args ...any) |
| 63 | + |
| 64 | + // Errorln is equivalent to Log followed by Fail. |
| 65 | + Errorln(args ...any) |
| 66 | + |
| 67 | + // Errorf is equivalent to Logf followed by Fail. |
| 68 | + Errorf(format string, args ...any) |
| 69 | + |
| 70 | + // Fatal is equivalent to Log followed by FailNow. |
| 71 | + Fatal(args ...any) |
| 72 | + |
| 73 | + // Fatalf is equivalent to Logf followed by FailNow. |
| 74 | + Fatalf(format string, args ...any) |
| 75 | + |
| 76 | + // Skip is equivalent to Log followed by SkipNow. |
| 77 | + Skip(args ...any) |
| 78 | + |
| 79 | + // Skipf is equivalent to Logf followed by SkipNow. |
| 80 | + Skipf(format string, args ...any) |
| 81 | + |
| 82 | + // SkipNow marks the test as having been skipped and stops its execution |
| 83 | + // by calling runtime.Goexit. |
| 84 | + // If a test fails (see Error, Errorf, Fail) and is then skipped, |
| 85 | + // it is still considered to have failed. |
| 86 | + // Execution will continue at the next test or benchmark. See also FailNow. |
| 87 | + // SkipNow must be called from the goroutine running the test, not from |
| 88 | + // other goroutines created during the test. Calling SkipNow does not stop |
| 89 | + // those other goroutines. |
| 90 | + SkipNow() |
| 91 | + |
| 92 | + // Skipped reports whether the test was skipped. |
| 93 | + Skipped() bool |
| 94 | + |
| 95 | + // Helper marks the calling function as a test helper function. |
| 96 | + // When printing file and line information, that function will be skipped. |
| 97 | + // Helper may be called simultaneously from multiple goroutines. |
| 98 | + Helper() |
| 99 | + |
| 100 | + // Cleanup registers a function to be called when the test (or subtest) and all its |
| 101 | + // subtests complete. Cleanup functions will be called in last added, |
| 102 | + // first called order. |
| 103 | + Cleanup(f func()) |
| 104 | + |
| 105 | + // TempDir returns a temporary directory for the test to use. |
| 106 | + // The directory is automatically removed by Cleanup when the test and |
| 107 | + // all its subtests complete. |
| 108 | + // Each subsequent call to t.TempDir returns a unique directory; |
| 109 | + // if the directory creation fails, TempDir terminates the test by calling Fatal. |
| 110 | + TempDir() string |
| 111 | + |
| 112 | + // Run runs f as a subtest of t called name. |
| 113 | + // |
| 114 | + // Run may be called simultaneously from multiple goroutines, but all such calls |
| 115 | + // must return before the outer test function for t returns. |
| 116 | + Run(name string, f func()) bool |
| 117 | + |
| 118 | + // Deadline reports the time at which the test binary will have |
| 119 | + // exceeded the timeout specified by the -timeout flag. |
| 120 | + // |
| 121 | + // The ok result is false if the -timeout flag indicates “no timeout” (0). |
| 122 | + Deadline() (deadline time.Time, ok bool) |
| 123 | +} |
| 124 | + |
| 125 | +// ----------------------------------------------------------------------------- |
| 126 | + |
| 127 | +// TestingT is a wrapper for testing.T. |
| 128 | +// It implements the CaseT interface and provides methods to manage test state |
| 129 | +// and support formatted test logs. |
| 130 | +type TestingT struct { |
| 131 | + *testing.T |
| 132 | +} |
| 133 | + |
| 134 | +// NewT creates a testing object. |
| 135 | +func NewT(t *testing.T) TestingT { |
| 136 | + return TestingT{t} |
| 137 | +} |
| 138 | + |
| 139 | +// Errorln is equivalent to Log followed by Fail. |
| 140 | +func (p TestingT) Errorln(args ...any) { |
| 141 | + t := p.T |
| 142 | + t.Helper() |
| 143 | + t.Error(args...) |
| 144 | +} |
| 145 | + |
| 146 | +// Run runs f as a subtest of t called name. |
| 147 | +// |
| 148 | +// Run may be called simultaneously from multiple goroutines, but all such calls |
| 149 | +// must return before the outer test function for t returns. |
| 150 | +func (p TestingT) Run(name string, doSth func()) bool { |
| 151 | + return p.T.Run(name, func(t *testing.T) { |
| 152 | + doSth() |
| 153 | + }) |
| 154 | +} |
| 155 | + |
| 156 | +// ----------------------------------------------------------------------------- |
0 commit comments