Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Use subtests for table driven tests #64

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 23 additions & 20 deletions ast/ast_optimize_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package ast

import (
"fmt"
"reflect"
"testing"
// "github.com/pmezard/go-difflib/difflib"
Expand Down Expand Up @@ -340,25 +341,27 @@ var cases = []struct {

func TestOptimize(t *testing.T) {
for i := range cases {
Optimize(cases[i].in)
if !reflect.DeepEqual(cases[i].in, cases[i].out) {
t.Errorf("%d: not equal", i)
// dumpin := goon.Sdump(cases[i].in)
// dumpout := goon.Sdump(cases[i].out)
// fmt.Println("=== want:")
// fmt.Println(dumpout)
// fmt.Println("=== got:")
// fmt.Println(dumpin)
// fmt.Println("=== diff:")
// diff := difflib.UnifiedDiff{
// A: difflib.SplitLines(dumpout),
// B: difflib.SplitLines(dumpin),
// FromFile: "want",
// ToFile: "got",
// Context: 3,
// }
// text, _ := difflib.GetUnifiedDiffString(diff)
// fmt.Println(text)
}
t.Run(fmt.Sprint(i), func(t *testing.T) {
Optimize(cases[i].in)
if !reflect.DeepEqual(cases[i].in, cases[i].out) {
t.Errorf("%d: not equal", i)
// dumpin := goon.Sdump(cases[i].in)
// dumpout := goon.Sdump(cases[i].out)
// fmt.Println("=== want:")
// fmt.Println(dumpout)
// fmt.Println("=== got:")
// fmt.Println(dumpin)
// fmt.Println("=== diff:")
// diff := difflib.UnifiedDiff{
// A: difflib.SplitLines(dumpout),
// B: difflib.SplitLines(dumpin),
// FromFile: "want",
// ToFile: "got",
// Context: 3,
// }
// text, _ := difflib.GetUnifiedDiffString(diff)
// fmt.Println(text)
}
})
}
}
58 changes: 30 additions & 28 deletions ast/ast_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,39 +69,41 @@ var expRanges = []string{

func TestCharClassParse(t *testing.T) {
for i, c := range charClasses {
m := NewCharClassMatcher(Pos{}, c)
t.Run(c, func(t *testing.T) {
m := NewCharClassMatcher(Pos{}, c)

ic := strings.HasSuffix(c, "i")
if m.IgnoreCase != ic {
t.Errorf("%q: want ignore case: %t, got %t", c, ic, m.IgnoreCase)
}
iv := c[1] == '^'
if m.Inverted != iv {
t.Errorf("%q: want inverted: %t, got %t", c, iv, m.Inverted)
}
ic := strings.HasSuffix(c, "i")
if m.IgnoreCase != ic {
t.Errorf("%q: want ignore case: %t, got %t", c, ic, m.IgnoreCase)
}
iv := c[1] == '^'
if m.Inverted != iv {
t.Errorf("%q: want inverted: %t, got %t", c, iv, m.Inverted)
}

if n := utf8.RuneCountInString(expChars[i]); len(m.Chars) != n {
t.Errorf("%q: want %d chars, got %d", c, n, len(m.Chars))
} else if string(m.Chars) != expChars[i] {
t.Errorf("%q: want %q, got %q", c, expChars[i], string(m.Chars))
}
if n := utf8.RuneCountInString(expChars[i]); len(m.Chars) != n {
t.Errorf("%q: want %d chars, got %d", c, n, len(m.Chars))
} else if string(m.Chars) != expChars[i] {
t.Errorf("%q: want %q, got %q", c, expChars[i], string(m.Chars))
}

if n := utf8.RuneCountInString(expRanges[i]); len(m.Ranges) != n {
t.Errorf("%q: want %d chars, got %d", c, n, len(m.Ranges))
} else if string(m.Ranges) != expRanges[i] {
t.Errorf("%q: want %q, got %q", c, expRanges[i], string(m.Ranges))
}
if n := utf8.RuneCountInString(expRanges[i]); len(m.Ranges) != n {
t.Errorf("%q: want %d chars, got %d", c, n, len(m.Ranges))
} else if string(m.Ranges) != expRanges[i] {
t.Errorf("%q: want %q, got %q", c, expRanges[i], string(m.Ranges))
}

if n := len(expUnicodeClasses[i]); len(m.UnicodeClasses) != n {
t.Errorf("%q: want %d Unicode classes, got %d", c, n, len(m.UnicodeClasses))
} else if n > 0 {
want := expUnicodeClasses[i]
got := m.UnicodeClasses
for j, wantClass := range want {
if wantClass != got[j] {
t.Errorf("%q: range table %d: want %v, got %v", c, j, wantClass, got[j])
if n := len(expUnicodeClasses[i]); len(m.UnicodeClasses) != n {
t.Errorf("%q: want %d Unicode classes, got %d", c, n, len(m.UnicodeClasses))
} else if n > 0 {
want := expUnicodeClasses[i]
got := m.UnicodeClasses
for j, wantClass := range want {
if wantClass != got[j] {
t.Errorf("%q: range table %d: want %v, got %v", c, j, wantClass, got[j])
}
}
}
}
})
}
}
48 changes: 26 additions & 22 deletions bootstrap/parser_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,17 +53,19 @@ var parseExpRes = []string{
func TestParseValid(t *testing.T) {
p := NewParser()
for i, c := range parseValidCases {
g, err := p.Parse("", strings.NewReader(c))
if err != nil {
t.Errorf("%d: got error %v", i, err)
continue
}
t.Run(c, func(t *testing.T) {
g, err := p.Parse("", strings.NewReader(c))
if err != nil {
t.Errorf("%d: got error %v", i, err)
return
}

want := parseExpRes[i]
got := g.String()
if want != got {
t.Errorf("%d: want \n%s\n, got \n%s\n", i, want, got)
}
want := parseExpRes[i]
got := g.String()
if want != got {
t.Errorf("%d: want \n%s\n, got \n%s\n", i, want, got)
}
})
}
}

Expand All @@ -80,18 +82,20 @@ var parseExpErrs = [][]string{
func TestParseInvalid(t *testing.T) {
p := NewParser()
for i, c := range parseInvalidCases {
_, err := p.Parse("", strings.NewReader(c))
el := *(err.(*errList))
if len(el) != len(parseExpErrs[i]) {
t.Errorf("%d: want %d errors, got %d", i, len(parseExpErrs[i]), len(el))
continue
}
for j, err := range el {
want := parseExpErrs[i][j]
got := err.Error()
if want != got {
t.Errorf("%d: error %d: want %q, got %q", i, j, want, got)
t.Run(c, func(t *testing.T) {
_, err := p.Parse("", strings.NewReader(c))
el := *(err.(*errList))
if len(el) != len(parseExpErrs[i]) {
t.Errorf("%d: want %d errors, got %d", i, len(parseExpErrs[i]), len(el))
return
}
for j, err := range el {
want := parseExpErrs[i][j]
got := err.Error()
if want != got {
t.Errorf("%d: error %d: want %q, got %q", i, j, want, got)
}
}
}
})
}
}
82 changes: 43 additions & 39 deletions bootstrap/scan_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -218,33 +218,35 @@ func TestScanValid(t *testing.T) {
var s Scanner
var errh errsink
for i, c := range scanValidCases {
errh.reset()
s.Init("", strings.NewReader(c), errh.add)
t.Run(c, func(t *testing.T) {
errh.reset()
s.Init("", strings.NewReader(c), errh.add)

j := 0
for {
tok, ok := s.Scan()
if j < len(scanExpTokens[i]) {
got := tok.String()
want := scanExpTokens[i][j]
if got != want {
t.Errorf("%d: token %d: want %q, got %q", i, j, want, got)
j := 0
for {
tok, ok := s.Scan()
if j < len(scanExpTokens[i]) {
got := tok.String()
want := scanExpTokens[i][j]
if got != want {
t.Errorf("%d: token %d: want %q, got %q", i, j, want, got)
}
} else {
t.Errorf("%d: want %d tokens, got #%d", i, len(scanExpTokens[i]), j+1)
}
} else {
t.Errorf("%d: want %d tokens, got #%d", i, len(scanExpTokens[i]), j+1)
}
if !ok {
if j < len(scanExpTokens[i])-1 {
t.Errorf("%d: wand %d tokens, got only %d", i, len(scanExpTokens[i]), j+1)
if !ok {
if j < len(scanExpTokens[i])-1 {
t.Errorf("%d: wand %d tokens, got only %d", i, len(scanExpTokens[i]), j+1)
}
break
}
break
j++
}
if len(errh.errs) != 0 {
t.Errorf("%d: want no error, got %d", i, len(errh.errs))
t.Log(errh.errs)
}
j++
}
if len(errh.errs) != 0 {
t.Errorf("%d: want no error, got %d", i, len(errh.errs))
t.Log(errh.errs)
}
})
}
}

Expand Down Expand Up @@ -336,23 +338,25 @@ func TestScanInvalid(t *testing.T) {
var s Scanner
var errh errsink
for i, c := range scanInvalidCases {
errh.reset()
s.Init("", strings.NewReader(c), errh.add)
for {
if _, ok := s.Scan(); !ok {
break
t.Run(c, func(t *testing.T) {
errh.reset()
s.Init("", strings.NewReader(c), errh.add)
for {
if _, ok := s.Scan(); !ok {
break
}
}
}
if len(errh.errs) != len(scanExpErrs[i]) {
t.Errorf("%d: want %d errors, got %d", i, len(scanExpErrs[i]), len(errh.errs))
continue
}
for j := range errh.errs {
want := scanExpErrs[i][j]
got := errh.StringAt(j)
if want != got {
t.Errorf("%d: error %d: want %q, got %q", i, j, want, got)
if len(errh.errs) != len(scanExpErrs[i]) {
t.Errorf("%d: want %d errors, got %d", i, len(scanExpErrs[i]), len(errh.errs))
return
}
for j := range errh.errs {
want := scanExpErrs[i][j]
got := errh.StringAt(j)
if want != got {
t.Errorf("%d: error %d: want %q, got %q", i, j, want, got)
}
}
}
})
}
}
64 changes: 34 additions & 30 deletions examples/calculator/calculator_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,19 +48,21 @@ var validCases = map[string]int{

func TestValidCases(t *testing.T) {
for tc, exp := range validCases {
got, err := Parse("", []byte(tc))
if err != nil {
t.Errorf("%q: want no error, got %v", tc, err)
continue
}
goti, ok := got.(int)
if !ok {
t.Errorf("%q: want type %T, got %T", tc, exp, got)
continue
}
if exp != goti {
t.Errorf("%q: want %d, got %d", tc, exp, goti)
}
t.Run(tc, func(t *testing.T) {
got, err := Parse("", []byte(tc))
if err != nil {
t.Errorf("%q: want no error, got %v", tc, err)
return
}
goti, ok := got.(int)
if !ok {
t.Errorf("%q: want type %T, got %T", tc, exp, got)
return
}
if exp != goti {
t.Errorf("%q: want %d, got %d", tc, exp, goti)
}
})
}
}

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

func TestInvalidCases(t *testing.T) {
for tc, exp := range invalidCases {
got, err := Parse("", []byte(tc))
if err == nil {
t.Errorf("%q: want error, got none (%v)", tc, got)
continue
}
el, ok := err.(errList)
if !ok {
t.Errorf("%q: want error type %T, got %T", tc, &errList{}, err)
continue
}
for _, e := range el {
if _, ok := e.(*parserError); !ok {
t.Errorf("%q: want all individual errors to be %T, got %T (%[3]v)", tc, &parserError{}, e)
t.Run(tc, func(t *testing.T) {
got, err := Parse("", []byte(tc))
if err == nil {
t.Errorf("%q: want error, got none (%v)", tc, got)
return
}
}
if exp != err.Error() {
t.Errorf("%q: want \n%s\n, got \n%s\n", tc, exp, err)
}
el, ok := err.(errList)
if !ok {
t.Errorf("%q: want error type %T, got %T", tc, &errList{}, err)
return
}
for _, e := range el {
if _, ok := e.(*parserError); !ok {
t.Errorf("%q: want all individual errors to be %T, got %T (%[3]v)", tc, &parserError{}, e)
}
}
if exp != err.Error() {
t.Errorf("%q: want \n%s\n, got \n%s\n", tc, exp, err)
}
})
}
}

Expand Down
Loading