From 170e56b34a56996feecaaeaeb576b2c09405ab24 Mon Sep 17 00:00:00 2001 From: James Shubin Date: Sat, 9 Mar 2019 17:14:13 -0500 Subject: [PATCH] lang: Improve test case with more specific errors --- lang/interpret_test.go | 89 +++++++++++++++---- lang/interpret_test/TestAstFunc1/fail1.graph | 2 +- lang/interpret_test/TestAstFunc1/fail2.graph | 2 +- .../interpret_test/TestAstFunc1/unused1.graph | 2 +- 4 files changed, 75 insertions(+), 20 deletions(-) diff --git a/lang/interpret_test.go b/lang/interpret_test.go index a9e0fa6d..97c0472b 100644 --- a/lang/interpret_test.go +++ b/lang/interpret_test.go @@ -528,6 +528,10 @@ func TestAstFunc0(t *testing.T) { // TestAstFunc1 is a more advanced version which pulls code from physical dirs. func TestAstFunc1(t *testing.T) { const magicError = "# err: " + const magicError1 = "err1: " + const magicError2 = "err2: " + const magicError3 = "err3: " + const magicError4 = "err4: " const magicEmpty = "# empty!" dir, err := util.TestDirFull() if err != nil { @@ -545,12 +549,19 @@ func TestAstFunc1(t *testing.T) { Functions: funcs.LookupPrefix(""), } + type errs struct { + fail1 bool + fail2 bool + fail3 bool + fail4 bool + } type test struct { // an individual test name string path string // relative sub directory path inside tests dir fail bool //graph *pgraph.Graph expstr string // expected graph in string format + errs errs } testCases := []test{} //{ @@ -595,9 +606,34 @@ func TestAstFunc1(t *testing.T) { // if the graph file has a magic error string, it's a failure errStr := "" + fail1 := false + fail2 := false + fail3 := false + fail4 := false if strings.HasPrefix(str, magicError) { errStr = strings.TrimPrefix(str, magicError) str = errStr + + if strings.HasPrefix(str, magicError1) { + errStr = strings.TrimPrefix(str, magicError1) + str = errStr + fail1 = true + } + if strings.HasPrefix(str, magicError2) { + errStr = strings.TrimPrefix(str, magicError2) + str = errStr + fail2 = true + } + if strings.HasPrefix(str, magicError3) { + errStr = strings.TrimPrefix(str, magicError3) + str = errStr + fail3 = true + } + if strings.HasPrefix(str, magicError4) { + errStr = strings.TrimPrefix(str, magicError4) + str = errStr + fail4 = true + } } // add automatic test case @@ -606,6 +642,12 @@ func TestAstFunc1(t *testing.T) { path: f + "/", fail: errStr != "", expstr: str, + errs: errs{ + fail1: fail1, + fail2: fail2, + fail3: fail3, + fail4: fail4, + }, }) //t.Logf("adding: %s", f + "/") } @@ -628,8 +670,12 @@ func TestAstFunc1(t *testing.T) { //} t.Run(fmt.Sprintf("test #%d (%s)", index, tc.name), func(t *testing.T) { - name, path, fail, expstr := tc.name, tc.path, tc.fail, strings.Trim(tc.expstr, "\n") + 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 + fail1 := errs.fail1 + fail2 := errs.fail2 + fail3 := errs.fail3 + fail4 := errs.fail4 t.Logf("\n\ntest #%d (%s) ----------------\npath: %s\n\n", index, name, src) @@ -690,7 +736,7 @@ func TestAstFunc1(t *testing.T) { t.Errorf("test #%d: lex/parse failed with: %+v", index, err) return } - if fail && err != nil { + if fail1 && err != nil { // TODO: %+v instead? s := fmt.Sprintf("%s", err) // convert to string if s != expstr { @@ -701,6 +747,12 @@ func TestAstFunc1(t *testing.T) { } return // fail happened during lex parse, don't run init/interpolate! } + if fail1 && err == nil { + t.Errorf("test #%d: FAIL", index) + t.Errorf("test #%d: lex/parse passed, expected fail", index) + return + } + t.Logf("test #%d: AST: %+v", index, ast) importGraph, err := pgraph.NewGraph("importGraph") @@ -749,7 +801,7 @@ func TestAstFunc1(t *testing.T) { t.Errorf("test #%d: could not set scope: %+v", index, err) return } - if fail && err != nil { + if fail2 && err != nil { // TODO: %+v instead? s := fmt.Sprintf("%s", err) // convert to string if s != expstr { @@ -760,6 +812,11 @@ func TestAstFunc1(t *testing.T) { } return // fail happened during set scope, don't run unification! } + if fail2 && err == nil { + t.Errorf("test #%d: FAIL", index) + t.Errorf("test #%d: set scope passed, expected fail", index) + return + } // apply type unification xlogf := func(format string, v ...interface{}) { @@ -771,13 +828,7 @@ func TestAstFunc1(t *testing.T) { t.Errorf("test #%d: could not unify types: %+v", index, err) return } - // maybe it will fail during graph below instead? - //if fail && err == nil { - // t.Errorf("test #%d: FAIL", index) - // t.Errorf("test #%d: unification passed, expected fail", index) - // continue - //} - if fail && err != nil { + if fail3 && err != nil { // TODO: %+v instead? s := fmt.Sprintf("%s", err) // convert to string if s != expstr { @@ -788,6 +839,11 @@ func TestAstFunc1(t *testing.T) { } return // fail happened during unification, don't run Graph! } + if fail3 && err == nil { + t.Errorf("test #%d: FAIL", index) + t.Errorf("test #%d: unification passed, expected fail", index) + return + } // build the function graph graph, err := iast.Graph() @@ -797,13 +853,7 @@ func TestAstFunc1(t *testing.T) { t.Errorf("test #%d: functions failed with: %+v", index, err) return } - if fail && err == nil { - t.Errorf("test #%d: FAIL", index) - t.Errorf("test #%d: functions passed, expected fail", index) - return - } - - if fail { // can't process graph if it's nil + if fail4 && err != nil { // can't process graph if it's nil // TODO: %+v instead? s := fmt.Sprintf("%s", err) // convert to string if s != expstr { @@ -814,6 +864,11 @@ func TestAstFunc1(t *testing.T) { } return } + if fail4 && err == nil { + t.Errorf("test #%d: FAIL", index) + t.Errorf("test #%d: functions passed, expected fail", index) + return + } t.Logf("test #%d: graph: %+v", index, graph) str := strings.Trim(graph.Sprint(), "\n") // text format of graph diff --git a/lang/interpret_test/TestAstFunc1/fail1.graph b/lang/interpret_test/TestAstFunc1/fail1.graph index 52028abf..b7c36b7b 100644 --- a/lang/interpret_test/TestAstFunc1/fail1.graph +++ b/lang/interpret_test/TestAstFunc1/fail1.graph @@ -1 +1 @@ -# err: parser: `syntax error: unexpected COLON` @2:8 +# err: err1: parser: `syntax error: unexpected COLON` @2:8 diff --git a/lang/interpret_test/TestAstFunc1/fail2.graph b/lang/interpret_test/TestAstFunc1/fail2.graph index 456bf1cb..8cb726c2 100644 --- a/lang/interpret_test/TestAstFunc1/fail2.graph +++ b/lang/interpret_test/TestAstFunc1/fail2.graph @@ -1 +1 @@ -# err: can't unify, invariant illogicality with equality: base kind does not match (2 != 3) +# err: err3: can't unify, invariant illogicality with equality: base kind does not match (2 != 3) diff --git a/lang/interpret_test/TestAstFunc1/unused1.graph b/lang/interpret_test/TestAstFunc1/unused1.graph index 0832494b..5f5fa7b3 100644 --- a/lang/interpret_test/TestAstFunc1/unused1.graph +++ b/lang/interpret_test/TestAstFunc1/unused1.graph @@ -1 +1 @@ -# err: import scope `something.mcl` failed: local import of `something.mcl` failed: module contains unused statements: found stmt: res(print) +# err: err2: import scope `something.mcl` failed: local import of `something.mcl` failed: module contains unused statements: found stmt: res(print)