Skip to content

Commit 5a6a0b9

Browse files
committed
General improvements.
Removed NilError. Renamed ErrorsNotFatal to just NotFatal. Comment changes. Test case changes.
1 parent 406ae6a commit 5a6a0b9

File tree

7 files changed

+73
-77
lines changed

7 files changed

+73
-77
lines changed

cmd.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,8 @@ func (c *Cmd) WantCode(expected int) {
9292
}
9393

9494
// Chdir sets the working directory where the command will be run.
95+
// Chdir(""), the default, is equivalent to Chdir("."); it uses
96+
// the current directory.
9597
func (c *Cmd) Chdir(path string) {
9698
c.dir = path
9799
}
@@ -197,7 +199,7 @@ func (c *Cmd) Run(t Reporter, input string) {
197199
if out.Len() == 0 {
198200
t.Error("no output")
199201
} else {
200-
// Don't use t.Error("output\n" + out.String()); the output usually ends with a newline,
202+
// Don't use t.Error("output:\n" + out.String()); the output usually ends with a newline,
201203
// and t.Error always adds another newline.
202204
t.Errorf("output:\n%s", out.String())
203205
}

cmd_test.go

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -127,13 +127,13 @@ exit code: 0
127127

128128
st.Reset()
129129
c.CheckStdout(nil)
130-
c.Run(&st, "whatever")
130+
c.Run(&st, "greenery")
131131
st.Expect(t, true, true, `unexpected output
132132
command: /bin/sh -c read x; echo a $x b
133133
input:
134-
whatever
134+
greenery
135135
output:
136-
a whatever b
136+
a greenery b
137137
no error output
138138
exit code: 0
139139
`)
@@ -180,14 +180,14 @@ exit code: 99
180180

181181
st.Reset()
182182
c.CheckStderr(nil)
183-
c.Run(&st, "something")
183+
c.Run(&st, "tropical")
184184
st.Expect(t, true, true, `unexpected error output
185185
command: /bin/sh -c read x; if [ "$x" != nothing ]; then echo $x >&2; exit 99; fi
186186
input:
187-
something
187+
tropical
188188
no output
189189
error output:
190-
something
190+
tropical
191191
exit code: 99
192192
`)
193193

@@ -349,8 +349,12 @@ func TestCmdChdir(t *testing.T) {
349349

350350
path := filepath.Join(tmp, "somefile")
351351
f, e := os.Create(path)
352-
NilError(t, e)
353-
NilError(t, f.Close())
352+
if e == nil {
353+
e = f.Close()
354+
}
355+
if e != nil {
356+
t.Fatal(e)
357+
}
354358

355359
c := Command("/bin/ls")
356360
c.Chdir(tmp)

go.mod

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
module github.com/pat42smith/gotest
22

3-
go 1.18
3+
go 1.21

gotest.go

Lines changed: 23 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// Copyright 2022 Patrick Smith
1+
// Copyright 2022-2023 Patrick Smith
22
// Use of this source code is subject to the MIT-style license in the LICENSE file.
33

44
// Package gotest provides support for writing test cases in Go.
@@ -10,7 +10,7 @@
1010
//
1111
// By default, these functions use FailNow, Fatal, or Fatalf to report errors,
1212
// so they terminate the running test on the first error. This behavior may
13-
// be changed with the ErrorsNotFatal wrapper.
13+
// be changed with the NotFatal wrapper.
1414
package gotest
1515

1616
// Type Reporter is an interface satisfied by the testing.T, .B, and .F types.
@@ -20,7 +20,8 @@ package gotest
2020
// can properly mark themselves as helper functions.
2121
//
2222
// In future, more methods from the intersection of T, B, and F may be added.
23-
// Be warned that code using Reporter in unusual ways may break if this happens.
23+
// Be warned that this may break code containing types designed to implement Reporter;
24+
// you create such types at your own risk.
2425
type Reporter interface {
2526
Error(args ...any)
2627
Errorf(format string, args ...any)
@@ -38,7 +39,7 @@ type Reporter interface {
3839
func Require(t Reporter, condition bool) {
3940
t.Helper()
4041
if !condition {
41-
t.Fatal("Test requirement failed")
42+
t.Fatal("Required condition failed")
4243
}
4344
}
4445

@@ -50,19 +51,12 @@ func Expect[T comparable](t Reporter, expected, actual T) {
5051
}
5152
}
5253

53-
// NilError fails and terminates the test if passed a non-nil error.
54-
func NilError(t Reporter, e error) {
55-
t.Helper()
56-
if e != nil {
57-
t.Fatal(e)
58-
}
59-
}
60-
6154
// Function panics runs f and reports whether it panics.
6255
//
6356
// If f panics, panics returns true and the value passed to panic.
6457
// Otherwise, panics returns (false, nil).
65-
// If f calls panic(nil), panics will correctly return (true, nil).
58+
// If f calls panic(nil), panics will return (true, nil) in Go 1.20 and
59+
// earlier. It will return true and a non-nil value in 1.21 and later.
6660
func panics(f func()) (panicked bool, with any) {
6761
defer func() {
6862
if panicked {
@@ -78,6 +72,8 @@ func panics(f func()) (panicked bool, with any) {
7872
//
7973
// If f does not panic, MustPanic terminates the running test with an error.
8074
// If f does panic, MustPanic returns the value that was passed to panic.
75+
// However, if f calls panic(nil), MustPanic will return a non-nil value
76+
// in Go 1.21 and later versions.
8177
func MustPanic(t Reporter, f func()) any {
8278
t.Helper()
8379
panicked, with := panics(f)
@@ -87,22 +83,25 @@ func MustPanic(t Reporter, f func()) any {
8783
return with
8884
}
8985

90-
// ErrorsNotFatal wraps a Reporter, and redirects fatal errors to non-terminating errors.
91-
type ErrorsNotFatal struct {
86+
// NotFatal wraps a Reporter, and redirects fatal errors to non-terminating errors.
87+
type NotFatal struct {
9288
Reporter
9389
}
9490

95-
// ErrorsNotFatal.FailNow marks a test failed, but does not not terminate it.
96-
func (enf ErrorsNotFatal) FailNow() {
97-
enf.Fail()
91+
// NotFatal.FailNow marks a test failed, but does not not terminate the test;
92+
// it is equivalent to nf.Fail().
93+
func (nf NotFatal) FailNow() {
94+
nf.Fail()
9895
}
9996

100-
// ErrorsNotFatal.Fatal reports an error without terminating the test.
101-
func (enf ErrorsNotFatal) Fatal(args ...any) {
102-
enf.Error(args...)
97+
// NotFatal.Fatal reports an error without terminating the test;
98+
// it is equivalent to nf.Error(args...).
99+
func (nf NotFatal) Fatal(args ...any) {
100+
nf.Error(args...)
103101
}
104102

105-
// ErrorsNotFatal.Fatal reports an error without terminating the test.
106-
func (enf ErrorsNotFatal) Fatalf(format string, args ...any) {
107-
enf.Errorf(format, args...)
103+
// NotFatal.Fatal reports an error without terminating the test;
104+
// it is equivalent to nf.Errorf(format, args...).
105+
func (nf NotFatal) Fatalf(format string, args ...any) {
106+
nf.Errorf(format, args...)
108107
}

gotest_test.go

Lines changed: 21 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,9 @@
44
package gotest
55

66
import (
7-
"fmt"
87
"os"
98
"path/filepath"
10-
"strconv"
9+
"runtime"
1110
"strings"
1211
"testing"
1312
)
@@ -18,16 +17,7 @@ func TestRequire(t *testing.T) {
1817
st.Expect(t, false, false, "")
1918

2019
Require(&st, false)
21-
st.Expect(t, true, true, "Test requirement failed\n")
22-
}
23-
24-
func TestNilError(t *testing.T) {
25-
var st StubReporter
26-
NilError(&st, nil)
27-
st.Expect(t, false, false, "")
28-
29-
NilError(&st, fmt.Errorf("oops"))
30-
st.Expect(t, true, true, "oops\n")
20+
st.Expect(t, true, true, "Required condition failed\n")
3121
}
3222

3323
func TestExpect(t *testing.T) {
@@ -39,36 +29,28 @@ func TestExpect(t *testing.T) {
3929
st.Expect(t, true, true, "Expected a but actual value was b\n")
4030

4131
// This should not compile, as the arguments have different types: Expect(&st, 7, "7")
42-
testprogram := `package main_test
32+
testprogram := `package foo
4333
import "github.com/pat42smith/gotest"
4434
import "testing"
4535
46-
func TestExpect(t *testing.T) {
36+
func Try(t *testing.T) {
4737
gotest.Expect(t, 7, "7")
4838
}`
4939

50-
here, e := os.Getwd()
51-
NilError(t, e)
52-
here = strconv.Quote(here)
53-
gomod := `module main
54-
go 1.18
55-
require github.com/pat42smith/gotest v0.0.0
56-
replace github.com/pat42smith/gotest => ` + here + "\n"
57-
5840
tmp := t.TempDir()
59-
testfile := filepath.Join(tmp, "main_test.go")
60-
NilError(t, os.WriteFile(testfile, []byte(testprogram), 0444))
61-
modfile := filepath.Join(tmp, "go.mod")
62-
NilError(t, os.WriteFile(modfile, []byte(gomod), 0444))
41+
testfile := filepath.Join(tmp, "foo.go")
42+
if e := os.WriteFile(testfile, []byte(testprogram), 0444); e != nil {
43+
t.Fatal(e)
44+
}
6345

64-
cmd := Command("go", "test")
65-
cmd.Chdir(tmp)
46+
cmd := Command("go", "build", testfile)
6647
cmd.CheckStderr(func(actual string) bool {
67-
return strings.Contains(actual, `default type string of "7" does not match inferred type int for T`)
48+
return strings.Contains(actual, `mismatched types untyped int and untyped string (cannot infer T)`)
6849
})
50+
cmd.Run(t, "")
6951
}
7052

71-
func Testpanics(t *testing.T) {
53+
func TestPanics(t *testing.T) {
7254
p, w := panics(func() {})
7355
Expect(t, false, p)
7456
Require(t, w == nil) // Can't use Expect(t, nil, w) because w (type any) does not implement comparable.
@@ -79,13 +61,15 @@ func Testpanics(t *testing.T) {
7961
Expect(t, true, p)
8062
Require(t, w == 97)
8163

82-
// panic(nil) is a special case that appears to the recover function as if no panic happened.
83-
// So be sure to check this case.
64+
// panic(nil) is a special case that, in older versions of Go, appeared to the recover
65+
// function as if no panic happened. Since Go 1.21, it causes a runtime panic.
8466
p, w = panics(func() {
8567
panic(nil)
8668
})
8769
Expect(t, true, p)
88-
Require(t, w == nil)
70+
if _, ok := w.(*runtime.PanicNilError); !ok {
71+
t.Fatalf("result of panics after panic(nil) is a %T; expected *runtime.PanicNilError", w)
72+
}
8973
}
9074

9175
func TestMustPanic(t *testing.T) {
@@ -101,14 +85,14 @@ func TestMustPanic(t *testing.T) {
10185
Require(t, x == nil)
10286
}
10387

104-
func TestErrorsNotFatal(t *testing.T) {
88+
func TestNotFatal(t *testing.T) {
10589
var st1, st2, st3 StubReporter
106-
ErrorsNotFatal{&st1}.FailNow()
90+
NotFatal{&st1}.FailNow()
10791
st1.Expect(t, true, false, "")
10892

109-
ErrorsNotFatal{&st2}.Fatal("problem")
93+
NotFatal{&st2}.Fatal("problem")
11094
st2.Expect(t, true, false, "problem\n")
11195

112-
ErrorsNotFatal{&st3}.Fatalf("<%s>", "uh oh")
96+
NotFatal{&st3}.Fatalf("<%s>", "uh oh")
11397
st3.Expect(t, true, false, "<uh oh>\n")
11498
}

stubreporter.go

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -107,22 +107,21 @@ func (sr *StubReporter) Fatalf(format string, args ...any) {
107107
// Expect verifies the status of the StubReporter.
108108
//
109109
// The failed, killed, and log parameters are compared to the StubReporter status.
110-
// If they do not match, an error is reported to t; the message will include
111-
// the when parameter.
110+
// If they do not match, an error is reported to t.
112111
func (sr *StubReporter) Expect(t Reporter, failed, killed bool, log string) {
113112
t.Helper()
114113
ok := true
115114
if sr.Failed() != failed {
116115
ok = false
117-
if !failed {
116+
if sr.Failed() {
118117
t.Error("StubReporter marked failed")
119118
} else {
120119
t.Error("StubReporter marked not failed")
121120
}
122121
}
123122
if sr.Killed() != killed {
124123
ok = false
125-
if !killed {
124+
if sr.Killed() {
126125
t.Error("StubReporter marked killed")
127126
} else {
128127
t.Error("StubReporter marked not killed")

stubreporter_test.go

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,16 @@ import (
1111
func TestStubHelper(t *testing.T) {
1212
var sr StubReporter
1313
sr.Helper()
14-
// If it returned, we're fine.
14+
sr.Expect(t, false, false, "")
15+
16+
sr.failed = true
17+
sr.killed = true
18+
fmt.Fprintln(&sr.log, "boo")
19+
sr.Helper()
20+
sr.Expect(t, true, true, "boo\n")
1521
}
1622

17-
func TestStubSRExpect(t *testing.T) {
23+
func TestStubExpect(t *testing.T) {
1824
for bits := 0; bits < 0100; bits++ {
1925
var sr StubReporter
2026
if bits&1 != 0 {
@@ -115,6 +121,8 @@ func TestStubReset(t *testing.T) {
115121
sr.Expect(t, true, true, "boo\n")
116122
sr.Reset()
117123
sr.Expect(t, false, false, "")
124+
sr.Reset()
125+
sr.Expect(t, false, false, "")
118126
}
119127

120128
func TestStubMessages(t *testing.T) {

0 commit comments

Comments
 (0)