Skip to content

Commit 58f5644

Browse files
committed
Use subtests for table driven tests
1 parent 21f14ed commit 58f5644

File tree

22 files changed

+498
-421
lines changed

22 files changed

+498
-421
lines changed

ast/ast_optimize_test.go

Lines changed: 23 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package ast
22

33
import (
4+
"fmt"
45
"reflect"
56
"testing"
67
// "github.com/pmezard/go-difflib/difflib"
@@ -340,25 +341,27 @@ var cases = []struct {
340341

341342
func TestOptimize(t *testing.T) {
342343
for i := range cases {
343-
Optimize(cases[i].in)
344-
if !reflect.DeepEqual(cases[i].in, cases[i].out) {
345-
t.Errorf("%d: not equal", i)
346-
// dumpin := goon.Sdump(cases[i].in)
347-
// dumpout := goon.Sdump(cases[i].out)
348-
// fmt.Println("=== want:")
349-
// fmt.Println(dumpout)
350-
// fmt.Println("=== got:")
351-
// fmt.Println(dumpin)
352-
// fmt.Println("=== diff:")
353-
// diff := difflib.UnifiedDiff{
354-
// A: difflib.SplitLines(dumpout),
355-
// B: difflib.SplitLines(dumpin),
356-
// FromFile: "want",
357-
// ToFile: "got",
358-
// Context: 3,
359-
// }
360-
// text, _ := difflib.GetUnifiedDiffString(diff)
361-
// fmt.Println(text)
362-
}
344+
t.Run(fmt.Sprint(i), func(t *testing.T) {
345+
Optimize(cases[i].in)
346+
if !reflect.DeepEqual(cases[i].in, cases[i].out) {
347+
t.Errorf("%d: not equal", i)
348+
// dumpin := goon.Sdump(cases[i].in)
349+
// dumpout := goon.Sdump(cases[i].out)
350+
// fmt.Println("=== want:")
351+
// fmt.Println(dumpout)
352+
// fmt.Println("=== got:")
353+
// fmt.Println(dumpin)
354+
// fmt.Println("=== diff:")
355+
// diff := difflib.UnifiedDiff{
356+
// A: difflib.SplitLines(dumpout),
357+
// B: difflib.SplitLines(dumpin),
358+
// FromFile: "want",
359+
// ToFile: "got",
360+
// Context: 3,
361+
// }
362+
// text, _ := difflib.GetUnifiedDiffString(diff)
363+
// fmt.Println(text)
364+
}
365+
})
363366
}
364367
}

ast/ast_test.go

Lines changed: 30 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -69,39 +69,41 @@ var expRanges = []string{
6969

7070
func TestCharClassParse(t *testing.T) {
7171
for i, c := range charClasses {
72-
m := NewCharClassMatcher(Pos{}, c)
72+
t.Run(c, func(t *testing.T) {
73+
m := NewCharClassMatcher(Pos{}, c)
7374

74-
ic := strings.HasSuffix(c, "i")
75-
if m.IgnoreCase != ic {
76-
t.Errorf("%q: want ignore case: %t, got %t", c, ic, m.IgnoreCase)
77-
}
78-
iv := c[1] == '^'
79-
if m.Inverted != iv {
80-
t.Errorf("%q: want inverted: %t, got %t", c, iv, m.Inverted)
81-
}
75+
ic := strings.HasSuffix(c, "i")
76+
if m.IgnoreCase != ic {
77+
t.Errorf("%q: want ignore case: %t, got %t", c, ic, m.IgnoreCase)
78+
}
79+
iv := c[1] == '^'
80+
if m.Inverted != iv {
81+
t.Errorf("%q: want inverted: %t, got %t", c, iv, m.Inverted)
82+
}
8283

83-
if n := utf8.RuneCountInString(expChars[i]); len(m.Chars) != n {
84-
t.Errorf("%q: want %d chars, got %d", c, n, len(m.Chars))
85-
} else if string(m.Chars) != expChars[i] {
86-
t.Errorf("%q: want %q, got %q", c, expChars[i], string(m.Chars))
87-
}
84+
if n := utf8.RuneCountInString(expChars[i]); len(m.Chars) != n {
85+
t.Errorf("%q: want %d chars, got %d", c, n, len(m.Chars))
86+
} else if string(m.Chars) != expChars[i] {
87+
t.Errorf("%q: want %q, got %q", c, expChars[i], string(m.Chars))
88+
}
8889

89-
if n := utf8.RuneCountInString(expRanges[i]); len(m.Ranges) != n {
90-
t.Errorf("%q: want %d chars, got %d", c, n, len(m.Ranges))
91-
} else if string(m.Ranges) != expRanges[i] {
92-
t.Errorf("%q: want %q, got %q", c, expRanges[i], string(m.Ranges))
93-
}
90+
if n := utf8.RuneCountInString(expRanges[i]); len(m.Ranges) != n {
91+
t.Errorf("%q: want %d chars, got %d", c, n, len(m.Ranges))
92+
} else if string(m.Ranges) != expRanges[i] {
93+
t.Errorf("%q: want %q, got %q", c, expRanges[i], string(m.Ranges))
94+
}
9495

95-
if n := len(expUnicodeClasses[i]); len(m.UnicodeClasses) != n {
96-
t.Errorf("%q: want %d Unicode classes, got %d", c, n, len(m.UnicodeClasses))
97-
} else if n > 0 {
98-
want := expUnicodeClasses[i]
99-
got := m.UnicodeClasses
100-
for j, wantClass := range want {
101-
if wantClass != got[j] {
102-
t.Errorf("%q: range table %d: want %v, got %v", c, j, wantClass, got[j])
96+
if n := len(expUnicodeClasses[i]); len(m.UnicodeClasses) != n {
97+
t.Errorf("%q: want %d Unicode classes, got %d", c, n, len(m.UnicodeClasses))
98+
} else if n > 0 {
99+
want := expUnicodeClasses[i]
100+
got := m.UnicodeClasses
101+
for j, wantClass := range want {
102+
if wantClass != got[j] {
103+
t.Errorf("%q: range table %d: want %v, got %v", c, j, wantClass, got[j])
104+
}
103105
}
104106
}
105-
}
107+
})
106108
}
107109
}

bootstrap/parser_test.go

Lines changed: 26 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -53,17 +53,19 @@ var parseExpRes = []string{
5353
func TestParseValid(t *testing.T) {
5454
p := NewParser()
5555
for i, c := range parseValidCases {
56-
g, err := p.Parse("", strings.NewReader(c))
57-
if err != nil {
58-
t.Errorf("%d: got error %v", i, err)
59-
continue
60-
}
56+
t.Run(c, func(t *testing.T) {
57+
g, err := p.Parse("", strings.NewReader(c))
58+
if err != nil {
59+
t.Errorf("%d: got error %v", i, err)
60+
return
61+
}
6162

62-
want := parseExpRes[i]
63-
got := g.String()
64-
if want != got {
65-
t.Errorf("%d: want \n%s\n, got \n%s\n", i, want, got)
66-
}
63+
want := parseExpRes[i]
64+
got := g.String()
65+
if want != got {
66+
t.Errorf("%d: want \n%s\n, got \n%s\n", i, want, got)
67+
}
68+
})
6769
}
6870
}
6971

@@ -80,18 +82,20 @@ var parseExpErrs = [][]string{
8082
func TestParseInvalid(t *testing.T) {
8183
p := NewParser()
8284
for i, c := range parseInvalidCases {
83-
_, err := p.Parse("", strings.NewReader(c))
84-
el := *(err.(*errList))
85-
if len(el) != len(parseExpErrs[i]) {
86-
t.Errorf("%d: want %d errors, got %d", i, len(parseExpErrs[i]), len(el))
87-
continue
88-
}
89-
for j, err := range el {
90-
want := parseExpErrs[i][j]
91-
got := err.Error()
92-
if want != got {
93-
t.Errorf("%d: error %d: want %q, got %q", i, j, want, got)
85+
t.Run(c, func(t *testing.T) {
86+
_, err := p.Parse("", strings.NewReader(c))
87+
el := *(err.(*errList))
88+
if len(el) != len(parseExpErrs[i]) {
89+
t.Errorf("%d: want %d errors, got %d", i, len(parseExpErrs[i]), len(el))
90+
return
91+
}
92+
for j, err := range el {
93+
want := parseExpErrs[i][j]
94+
got := err.Error()
95+
if want != got {
96+
t.Errorf("%d: error %d: want %q, got %q", i, j, want, got)
97+
}
9498
}
95-
}
99+
})
96100
}
97101
}

bootstrap/scan_test.go

Lines changed: 43 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -218,33 +218,35 @@ func TestScanValid(t *testing.T) {
218218
var s Scanner
219219
var errh errsink
220220
for i, c := range scanValidCases {
221-
errh.reset()
222-
s.Init("", strings.NewReader(c), errh.add)
221+
t.Run(c, func(t *testing.T) {
222+
errh.reset()
223+
s.Init("", strings.NewReader(c), errh.add)
223224

224-
j := 0
225-
for {
226-
tok, ok := s.Scan()
227-
if j < len(scanExpTokens[i]) {
228-
got := tok.String()
229-
want := scanExpTokens[i][j]
230-
if got != want {
231-
t.Errorf("%d: token %d: want %q, got %q", i, j, want, got)
225+
j := 0
226+
for {
227+
tok, ok := s.Scan()
228+
if j < len(scanExpTokens[i]) {
229+
got := tok.String()
230+
want := scanExpTokens[i][j]
231+
if got != want {
232+
t.Errorf("%d: token %d: want %q, got %q", i, j, want, got)
233+
}
234+
} else {
235+
t.Errorf("%d: want %d tokens, got #%d", i, len(scanExpTokens[i]), j+1)
232236
}
233-
} else {
234-
t.Errorf("%d: want %d tokens, got #%d", i, len(scanExpTokens[i]), j+1)
235-
}
236-
if !ok {
237-
if j < len(scanExpTokens[i])-1 {
238-
t.Errorf("%d: wand %d tokens, got only %d", i, len(scanExpTokens[i]), j+1)
237+
if !ok {
238+
if j < len(scanExpTokens[i])-1 {
239+
t.Errorf("%d: wand %d tokens, got only %d", i, len(scanExpTokens[i]), j+1)
240+
}
241+
break
239242
}
240-
break
243+
j++
244+
}
245+
if len(errh.errs) != 0 {
246+
t.Errorf("%d: want no error, got %d", i, len(errh.errs))
247+
t.Log(errh.errs)
241248
}
242-
j++
243-
}
244-
if len(errh.errs) != 0 {
245-
t.Errorf("%d: want no error, got %d", i, len(errh.errs))
246-
t.Log(errh.errs)
247-
}
249+
})
248250
}
249251
}
250252

@@ -336,23 +338,25 @@ func TestScanInvalid(t *testing.T) {
336338
var s Scanner
337339
var errh errsink
338340
for i, c := range scanInvalidCases {
339-
errh.reset()
340-
s.Init("", strings.NewReader(c), errh.add)
341-
for {
342-
if _, ok := s.Scan(); !ok {
343-
break
341+
t.Run(c, func(t *testing.T) {
342+
errh.reset()
343+
s.Init("", strings.NewReader(c), errh.add)
344+
for {
345+
if _, ok := s.Scan(); !ok {
346+
break
347+
}
344348
}
345-
}
346-
if len(errh.errs) != len(scanExpErrs[i]) {
347-
t.Errorf("%d: want %d errors, got %d", i, len(scanExpErrs[i]), len(errh.errs))
348-
continue
349-
}
350-
for j := range errh.errs {
351-
want := scanExpErrs[i][j]
352-
got := errh.StringAt(j)
353-
if want != got {
354-
t.Errorf("%d: error %d: want %q, got %q", i, j, want, got)
349+
if len(errh.errs) != len(scanExpErrs[i]) {
350+
t.Errorf("%d: want %d errors, got %d", i, len(scanExpErrs[i]), len(errh.errs))
351+
return
352+
}
353+
for j := range errh.errs {
354+
want := scanExpErrs[i][j]
355+
got := errh.StringAt(j)
356+
if want != got {
357+
t.Errorf("%d: error %d: want %q, got %q", i, j, want, got)
358+
}
355359
}
356-
}
360+
})
357361
}
358362
}

examples/calculator/calculator_test.go

Lines changed: 34 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -48,19 +48,21 @@ var validCases = map[string]int{
4848

4949
func TestValidCases(t *testing.T) {
5050
for tc, exp := range validCases {
51-
got, err := Parse("", []byte(tc))
52-
if err != nil {
53-
t.Errorf("%q: want no error, got %v", tc, err)
54-
continue
55-
}
56-
goti, ok := got.(int)
57-
if !ok {
58-
t.Errorf("%q: want type %T, got %T", tc, exp, got)
59-
continue
60-
}
61-
if exp != goti {
62-
t.Errorf("%q: want %d, got %d", tc, exp, goti)
63-
}
51+
t.Run(tc, func(t *testing.T) {
52+
got, err := Parse("", []byte(tc))
53+
if err != nil {
54+
t.Errorf("%q: want no error, got %v", tc, err)
55+
return
56+
}
57+
goti, ok := got.(int)
58+
if !ok {
59+
t.Errorf("%q: want type %T, got %T", tc, exp, got)
60+
return
61+
}
62+
if exp != goti {
63+
t.Errorf("%q: want %d, got %d", tc, exp, goti)
64+
}
65+
})
6466
}
6567
}
6668

@@ -88,24 +90,26 @@ var invalidCases = map[string]string{
8890

8991
func TestInvalidCases(t *testing.T) {
9092
for tc, exp := range invalidCases {
91-
got, err := Parse("", []byte(tc))
92-
if err == nil {
93-
t.Errorf("%q: want error, got none (%v)", tc, got)
94-
continue
95-
}
96-
el, ok := err.(errList)
97-
if !ok {
98-
t.Errorf("%q: want error type %T, got %T", tc, &errList{}, err)
99-
continue
100-
}
101-
for _, e := range el {
102-
if _, ok := e.(*parserError); !ok {
103-
t.Errorf("%q: want all individual errors to be %T, got %T (%[3]v)", tc, &parserError{}, e)
93+
t.Run(tc, func(t *testing.T) {
94+
got, err := Parse("", []byte(tc))
95+
if err == nil {
96+
t.Errorf("%q: want error, got none (%v)", tc, got)
97+
return
10498
}
105-
}
106-
if exp != err.Error() {
107-
t.Errorf("%q: want \n%s\n, got \n%s\n", tc, exp, err)
108-
}
99+
el, ok := err.(errList)
100+
if !ok {
101+
t.Errorf("%q: want error type %T, got %T", tc, &errList{}, err)
102+
return
103+
}
104+
for _, e := range el {
105+
if _, ok := e.(*parserError); !ok {
106+
t.Errorf("%q: want all individual errors to be %T, got %T (%[3]v)", tc, &parserError{}, e)
107+
}
108+
}
109+
if exp != err.Error() {
110+
t.Errorf("%q: want \n%s\n, got \n%s\n", tc, exp, err)
111+
}
112+
})
109113
}
110114
}
111115

0 commit comments

Comments
 (0)