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