lang: Replace numbered errors with named ones

This makes the tests easier to read and modify without having out of
order numbers. When writing the tests, you'll remember more easily which
section you're erroring in too!
This commit is contained in:
James Shubin
2021-05-07 23:30:48 -04:00
parent 37be9fda9f
commit 63f63955e7
6 changed files with 39 additions and 39 deletions

View File

@@ -546,7 +546,7 @@ func TestAstFunc0(t *testing.T) {
// TestAstFunc1 is a more advanced version which pulls code from physical dirs. // TestAstFunc1 is a more advanced version which pulls code from physical dirs.
func TestAstFunc1(t *testing.T) { func TestAstFunc1(t *testing.T) {
const magicError = "# err: " const magicError = "# err: "
const magicError1 = "err1: " const magicErrorLexParse = "errLexParse: "
const magicErrorFailInit = "errInit: " const magicErrorFailInit = "errInit: "
const magicError2 = "err2: " const magicError2 = "err2: "
const magicError3 = "err3: " const magicError3 = "err3: "
@@ -579,11 +579,11 @@ func TestAstFunc1(t *testing.T) {
} }
type errs struct { type errs struct {
fail1 bool failLexParse bool
failInit bool failInit bool
fail2 bool fail2 bool
fail3 bool fail3 bool
fail4 bool fail4 bool
} }
type test struct { // an individual test type test struct { // an individual test
name string name string
@@ -636,7 +636,7 @@ func TestAstFunc1(t *testing.T) {
// if the graph file has a magic error string, it's a failure // if the graph file has a magic error string, it's a failure
errStr := "" errStr := ""
fail1 := false failLexParse := false
failInit := false failInit := false
fail2 := false fail2 := false
fail3 := false fail3 := false
@@ -645,10 +645,10 @@ func TestAstFunc1(t *testing.T) {
errStr = strings.TrimPrefix(str, magicError) errStr = strings.TrimPrefix(str, magicError)
str = errStr str = errStr
if strings.HasPrefix(str, magicError1) { if strings.HasPrefix(str, magicErrorLexParse) {
errStr = strings.TrimPrefix(str, magicError1) errStr = strings.TrimPrefix(str, magicErrorLexParse)
str = errStr str = errStr
fail1 = true failLexParse = true
} }
if strings.HasPrefix(str, magicErrorFailInit) { if strings.HasPrefix(str, magicErrorFailInit) {
errStr = strings.TrimPrefix(str, magicErrorFailInit) errStr = strings.TrimPrefix(str, magicErrorFailInit)
@@ -679,11 +679,11 @@ func TestAstFunc1(t *testing.T) {
fail: errStr != "", fail: errStr != "",
expstr: str, expstr: str,
errs: errs{ errs: errs{
fail1: fail1, failLexParse: failLexParse,
failInit: failInit, failInit: failInit,
fail2: fail2, fail2: fail2,
fail3: fail3, fail3: fail3,
fail4: fail4, fail4: fail4,
}, },
}) })
//t.Logf("adding: %s", f + "/") //t.Logf("adding: %s", f + "/")
@@ -717,7 +717,7 @@ func TestAstFunc1(t *testing.T) {
t.Run(testName, func(t *testing.T) { t.Run(testName, func(t *testing.T) {
name, path, fail, expstr, errs := tc.name, tc.path, tc.fail, strings.Trim(tc.expstr, "\n"), tc.errs name, path, fail, expstr, errs := tc.name, tc.path, tc.fail, strings.Trim(tc.expstr, "\n"), tc.errs
src := dir + path // location of the test src := dir + path // location of the test
fail1 := errs.fail1 failLexParse := errs.failLexParse
failInit := errs.failInit failInit := errs.failInit
fail2 := errs.fail2 fail2 := errs.fail2
fail3 := errs.fail3 fail3 := errs.fail3
@@ -777,12 +777,12 @@ func TestAstFunc1(t *testing.T) {
reader := bytes.NewReader(output.Main) reader := bytes.NewReader(output.Main)
ast, err := LexParse(reader) ast, err := LexParse(reader)
if (!fail || !fail1) && err != nil { if (!fail || !failLexParse) && err != nil {
t.Errorf("test #%d: FAIL", index) t.Errorf("test #%d: FAIL", index)
t.Errorf("test #%d: lex/parse failed with: %+v", index, err) t.Errorf("test #%d: lex/parse failed with: %+v", index, err)
return return
} }
if fail1 && err != nil { if failLexParse && err != nil {
s := err.Error() // convert to string s := err.Error() // convert to string
if s != expstr { if s != expstr {
t.Errorf("test #%d: FAIL", index) t.Errorf("test #%d: FAIL", index)
@@ -792,7 +792,7 @@ func TestAstFunc1(t *testing.T) {
} }
return // fail happened during lex parse, don't run init/interpolate! return // fail happened during lex parse, don't run init/interpolate!
} }
if fail1 && err == nil { if failLexParse && err == nil {
t.Errorf("test #%d: FAIL", index) t.Errorf("test #%d: FAIL", index)
t.Errorf("test #%d: lex/parse passed, expected fail", index) t.Errorf("test #%d: lex/parse passed, expected fail", index)
return return
@@ -998,7 +998,7 @@ func TestAstFunc1(t *testing.T) {
// stable, static output. // stable, static output.
func TestAstFunc2(t *testing.T) { func TestAstFunc2(t *testing.T) {
const magicError = "# err: " const magicError = "# err: "
const magicError1 = "err1: " const magicErrorLexParse = "errLexParse: "
const magicError9 = "err9: " // TODO: rename const magicError9 = "err9: " // TODO: rename
const magicError8 = "err8: " // TODO: rename const magicError8 = "err8: " // TODO: rename
// TODO: move them all down by one // TODO: move them all down by one
@@ -1035,9 +1035,9 @@ func TestAstFunc2(t *testing.T) {
} }
type errs struct { type errs struct {
fail1 bool failLexParse bool
fail9 bool // TODO: rename fail9 bool // TODO: rename
fail8 bool // TODO: rename fail8 bool // TODO: rename
// TODO: move them all down by one // TODO: move them all down by one
fail2 bool fail2 bool
fail3 bool fail3 bool
@@ -1096,7 +1096,7 @@ func TestAstFunc2(t *testing.T) {
// if the graph file has a magic error string, it's a failure // if the graph file has a magic error string, it's a failure
errStr := "" errStr := ""
fail1 := false failLexParse := false
fail9 := false // TODO: rename fail9 := false // TODO: rename
fail8 := false // TODO: rename fail8 := false // TODO: rename
// TODO: move them all down by one // TODO: move them all down by one
@@ -1109,10 +1109,10 @@ func TestAstFunc2(t *testing.T) {
errStr = strings.TrimPrefix(str, magicError) errStr = strings.TrimPrefix(str, magicError)
str = errStr str = errStr
if strings.HasPrefix(str, magicError1) { if strings.HasPrefix(str, magicErrorLexParse) {
errStr = strings.TrimPrefix(str, magicError1) errStr = strings.TrimPrefix(str, magicErrorLexParse)
str = errStr str = errStr
fail1 = true failLexParse = true
} }
if strings.HasPrefix(str, magicError9) { // TODO: rename if strings.HasPrefix(str, magicError9) { // TODO: rename
errStr = strings.TrimPrefix(str, magicError9) errStr = strings.TrimPrefix(str, magicError9)
@@ -1159,9 +1159,9 @@ func TestAstFunc2(t *testing.T) {
fail: errStr != "", fail: errStr != "",
expstr: str, expstr: str,
errs: errs{ errs: errs{
fail1: fail1, failLexParse: failLexParse,
fail9: fail9, // TODO: rename fail9: fail9, // TODO: rename
fail8: fail8, // TODO: rename fail8: fail8, // TODO: rename
// TODO: move them all down by one // TODO: move them all down by one
fail2: fail2, fail2: fail2,
fail3: fail3, fail3: fail3,
@@ -1201,7 +1201,7 @@ func TestAstFunc2(t *testing.T) {
t.Run(testName, func(t *testing.T) { t.Run(testName, func(t *testing.T) {
name, path, fail, expstr, errs := tc.name, tc.path, tc.fail, strings.Trim(tc.expstr, "\n"), tc.errs name, path, fail, expstr, errs := tc.name, tc.path, tc.fail, strings.Trim(tc.expstr, "\n"), tc.errs
src := dir + path // location of the test src := dir + path // location of the test
fail1 := errs.fail1 failLexParse := errs.failLexParse
fail9 := errs.fail9 // TODO: rename fail9 := errs.fail9 // TODO: rename
fail8 := errs.fail8 // TODO: rename fail8 := errs.fail8 // TODO: rename
// TODO: move them all down by one // TODO: move them all down by one
@@ -1279,12 +1279,12 @@ func TestAstFunc2(t *testing.T) {
reader := bytes.NewReader(output.Main) reader := bytes.NewReader(output.Main)
ast, err := LexParse(reader) ast, err := LexParse(reader)
if (!fail || !fail1) && err != nil { if (!fail || !failLexParse) && err != nil {
t.Errorf("test #%d: FAIL", index) t.Errorf("test #%d: FAIL", index)
t.Errorf("test #%d: lex/parse failed with: %+v", index, err) t.Errorf("test #%d: lex/parse failed with: %+v", index, err)
return return
} }
if fail1 && err != nil { if failLexParse && err != nil {
s := err.Error() // convert to string s := err.Error() // convert to string
if s != expstr { if s != expstr {
t.Errorf("test #%d: FAIL", index) t.Errorf("test #%d: FAIL", index)
@@ -1294,7 +1294,7 @@ func TestAstFunc2(t *testing.T) {
} }
return // fail happened during lex parse, don't run init/interpolate! return // fail happened during lex parse, don't run init/interpolate!
} }
if fail1 && err == nil { if failLexParse && err == nil {
t.Errorf("test #%d: FAIL", index) t.Errorf("test #%d: FAIL", index)
t.Errorf("test #%d: lex/parse passed, expected fail", index) t.Errorf("test #%d: lex/parse passed, expected fail", index)
return return

View File

@@ -1 +1 @@
# err: err1: parser: `syntax error: unexpected COLON` @2:8 # err: errLexParse: parser: `syntax error: unexpected COLON` @2:8

View File

@@ -1 +1 @@
# err: err1: parser: `syntax error: unexpected $end, expecting EQUALS` @1:6 # err: errLexParse: parser: `syntax error: unexpected $end, expecting EQUALS` @1:6

View File

@@ -1 +1 @@
# err: err1: can't set return type in parser: `can't set return type in parser: duplicate struct field of `x int`` @1:24 # err: errLexParse: can't set return type in parser: `can't set return type in parser: duplicate struct field of `x int`` @1:24

View File

@@ -1 +1 @@
# err: err1: can't set return type in parser: `can't set return type in parser: duplicate struct field of `x int`` @1:24 # err: errLexParse: can't set return type in parser: `can't set return type in parser: duplicate struct field of `x int`` @1:24

View File

@@ -1 +1 @@
# err: err1: parser: `syntax error: unexpected $end, expecting EQUALS` @1:6 # err: errLexParse: parser: `syntax error: unexpected $end, expecting EQUALS` @1:6