1+ // Copyright 2009 The Go Authors. All rights reserved.
2+ // Use of this source code is governed by a BSD-style
3+ // license that can be found in the LICENSE file.
4+ //
5+ // This file has been modified for use by the TinyGo compiler.
6+ // src: https://github.com/golang/go/blob/61bb56ad/src/testing/testing.go
7+
8+ // Package testing provides support for automated testing of Go packages.
19package testing
210
311import (
@@ -7,13 +15,143 @@ import (
715 "os"
816)
917
10- // T is a test helper.
11- type T struct {
12- name string
18+ // common holds the elements common between T and B and
19+ // captures common methods such as Errorf.
20+ type common struct {
1321 output io.Writer
1422
15- // flags the test as having failed when non-zero
16- failed int
23+ failed bool // Test or benchmark has failed.
24+ skipped bool // Test of benchmark has been skipped.
25+ finished bool // Test function has completed.
26+ name string // Name of test or benchmark.
27+ }
28+
29+ // TB is the interface common to T and B.
30+ type TB interface {
31+ Error (args ... interface {})
32+ Errorf (format string , args ... interface {})
33+ Fail ()
34+ FailNow ()
35+ Failed () bool
36+ Fatal (args ... interface {})
37+ Fatalf (format string , args ... interface {})
38+ Log (args ... interface {})
39+ Logf (format string , args ... interface {})
40+ Name () string
41+ Skip (args ... interface {})
42+ SkipNow ()
43+ Skipf (format string , args ... interface {})
44+ Skipped () bool
45+ // Helper()
46+ }
47+
48+ var _ TB = (* T )(nil )
49+ var _ TB = (* B )(nil )
50+
51+ // T is a type passed to Test functions to manage test state and support formatted test logs.
52+ // Logs are accumulated during execution and dumped to standard output when done.
53+ //
54+ type T struct {
55+ common
56+ }
57+
58+ // Name returns the name of the running test or benchmark.
59+ func (c * common ) Name () string {
60+ return c .name
61+ }
62+
63+ // Fail marks the function as having failed but continues execution.
64+ func (c * common ) Fail () {
65+ c .failed = true
66+ }
67+
68+ // Failed reports whether the function has failed.
69+ func (c * common ) Failed () bool {
70+ failed := c .failed
71+ return failed
72+ }
73+
74+ // FailNow marks the function as having failed and stops its execution
75+ // by calling runtime.Goexit (which then runs all deferred calls in the
76+ // current goroutine).
77+ func (c * common ) FailNow () {
78+ c .Fail ()
79+
80+ c .finished = true
81+ c .Error ("FailNow is incomplete, requires runtime.Goexit()" )
82+ }
83+
84+ // log generates the output.
85+ func (c * common ) log (s string ) {
86+ // This doesn't print the same as in upstream go, but works for now.
87+ fmt .Fprintf (c .output , "\t " )
88+ fmt .Fprintln (c .output , s )
89+ }
90+
91+ // Log formats its arguments using default formatting, analogous to Println,
92+ // and records the text in the error log. For tests, the text will be printed only if
93+ // the test fails or the -test.v flag is set. For benchmarks, the text is always
94+ // printed to avoid having performance depend on the value of the -test.v flag.
95+ func (c * common ) Log (args ... interface {}) { c .log (fmt .Sprintln (args ... )) }
96+
97+ // Logf formats its arguments according to the format, analogous to Printf, and
98+ // records the text in the error log. A final newline is added if not provided. For
99+ // tests, the text will be printed only if the test fails or the -test.v flag is
100+ // set. For benchmarks, the text is always printed to avoid having performance
101+ // depend on the value of the -test.v flag.
102+ func (c * common ) Logf (format string , args ... interface {}) { c .log (fmt .Sprintf (format , args ... )) }
103+
104+ // Error is equivalent to Log followed by Fail.
105+ func (c * common ) Error (args ... interface {}) {
106+ c .log (fmt .Sprintln (args ... ))
107+ c .Fail ()
108+ }
109+
110+ // Errorf is equivalent to Logf followed by Fail.
111+ func (c * common ) Errorf (format string , args ... interface {}) {
112+ c .log (fmt .Sprintf (format , args ... ))
113+ c .Fail ()
114+ }
115+
116+ // Fatal is equivalent to Log followed by FailNow.
117+ func (c * common ) Fatal (args ... interface {}) {
118+ c .log (fmt .Sprintln (args ... ))
119+ c .FailNow ()
120+ }
121+
122+ // Fatalf is equivalent to Logf followed by FailNow.
123+ func (c * common ) Fatalf (format string , args ... interface {}) {
124+ c .log (fmt .Sprintf (format , args ... ))
125+ c .FailNow ()
126+ }
127+
128+ // Skip is equivalent to Log followed by SkipNow.
129+ func (c * common ) Skip (args ... interface {}) {
130+ c .log (fmt .Sprintln (args ... ))
131+ c .SkipNow ()
132+ }
133+
134+ // Skipf is equivalent to Logf followed by SkipNow.
135+ func (c * common ) Skipf (format string , args ... interface {}) {
136+ c .log (fmt .Sprintf (format , args ... ))
137+ c .SkipNow ()
138+ }
139+
140+ // SkipNow marks the test as having been skipped and stops its execution
141+ // by calling runtime.Goexit.
142+ func (c * common ) SkipNow () {
143+ c .skip ()
144+ c .finished = true
145+ c .Error ("SkipNow is incomplete, requires runtime.Goexit()" )
146+ }
147+
148+ func (c * common ) skip () {
149+ c .skipped = true
150+ }
151+
152+ // Skipped reports whether the test was skipped.
153+ func (c * common ) Skipped () bool {
154+ return c .skipped
17155}
18156
19157// TestToCall is a reference to a test that should be called during a test suite run.
@@ -35,21 +173,25 @@ func (m *M) Run() int {
35173 failures := 0
36174 for _ , test := range m .Tests {
37175 t := & T {
38- name : test .Name ,
39- output : & bytes.Buffer {},
176+ common : common {
177+ name : test .Name ,
178+ output : & bytes.Buffer {},
179+ },
40180 }
41181
42182 fmt .Printf ("=== RUN %s\n " , test .Name )
43183 test .Func (t )
44184
45- if t .failed == 0 {
46- fmt .Printf ("--- PASS: %s\n " , test .Name )
47- } else {
185+ if t .failed {
48186 fmt .Printf ("--- FAIL: %s\n " , test .Name )
187+ } else {
188+ fmt .Printf ("--- PASS: %s\n " , test .Name )
49189 }
50190 fmt .Println (t .output )
51191
52- failures += t .failed
192+ if t .failed {
193+ failures ++
194+ }
53195 }
54196
55197 if failures > 0 {
@@ -62,16 +204,3 @@ func (m *M) Run() int {
62204func TestMain (m * M ) {
63205 os .Exit (m .Run ())
64206}
65-
66- // Error is equivalent to Log followed by Fail
67- func (t * T ) Error (args ... interface {}) {
68- // This doesn't print the same as in upstream go, but works good enough
69- // TODO: buffer test output like go does
70- fmt .Fprintf (t .output , "\t " )
71- fmt .Fprintln (t .output , args ... )
72- t .Fail ()
73- }
74-
75- func (t * T ) Fail () {
76- t .failed = 1
77- }
0 commit comments