Skip to content

Commit 847a6b0

Browse files
authored
Merge pull request #117 from xushiwei/q
new package: x/test
2 parents b0e18c3 + 3dfff9b commit 847a6b0

File tree

8 files changed

+988
-2
lines changed

8 files changed

+988
-2
lines changed

.github/codecov.yml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
coverage:
2+
ignore:
3+
- "test"
4+
- "ts"

.github/workflows/go.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ jobs:
1010
Test:
1111
strategy:
1212
matrix:
13-
go-version: [1.17.x, 1.18.x, 1.20.x, 1.21.x]
13+
go-version: [1.18.x, 1.20.x, 1.21.x]
1414
os: [ubuntu-latest, windows-latest, macos-latest]
1515
runs-on: ${{ matrix.os }}
1616
steps:

go.mod

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
module github.com/qiniu/x
22

3-
go 1.13
3+
go 1.18
44

55
retract (
66
v7.0.0+incompatible

test/_match_test.go

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
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+
)
22+
23+
func TestBasic(t *testing.T) {
24+
}

test/case.go

Lines changed: 156 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,156 @@
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+
// -----------------------------------------------------------------------------

test/logt/logt.go

Lines changed: 167 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,167 @@
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(xsw):
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+
}

0 commit comments

Comments
 (0)