lang: Improve test case with more specific errors

This commit is contained in:
James Shubin
2019-03-09 17:14:13 -05:00
parent de43569fa2
commit 170e56b34a
4 changed files with 75 additions and 20 deletions

View File

@@ -528,6 +528,10 @@ 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 magicError2 = "err2: "
const magicError3 = "err3: "
const magicError4 = "err4: "
const magicEmpty = "# empty!" const magicEmpty = "# empty!"
dir, err := util.TestDirFull() dir, err := util.TestDirFull()
if err != nil { if err != nil {
@@ -545,12 +549,19 @@ func TestAstFunc1(t *testing.T) {
Functions: funcs.LookupPrefix(""), Functions: funcs.LookupPrefix(""),
} }
type errs struct {
fail1 bool
fail2 bool
fail3 bool
fail4 bool
}
type test struct { // an individual test type test struct { // an individual test
name string name string
path string // relative sub directory path inside tests dir path string // relative sub directory path inside tests dir
fail bool fail bool
//graph *pgraph.Graph //graph *pgraph.Graph
expstr string // expected graph in string format expstr string // expected graph in string format
errs errs
} }
testCases := []test{} testCases := []test{}
//{ //{
@@ -595,9 +606,34 @@ 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
fail2 := false
fail3 := false
fail4 := false
if strings.HasPrefix(str, magicError) { if strings.HasPrefix(str, magicError) {
errStr = strings.TrimPrefix(str, magicError) errStr = strings.TrimPrefix(str, magicError)
str = errStr 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 // add automatic test case
@@ -606,6 +642,12 @@ func TestAstFunc1(t *testing.T) {
path: f + "/", path: f + "/",
fail: errStr != "", fail: errStr != "",
expstr: str, expstr: str,
errs: errs{
fail1: fail1,
fail2: fail2,
fail3: fail3,
fail4: fail4,
},
}) })
//t.Logf("adding: %s", f + "/") //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) { 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 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) 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) t.Errorf("test #%d: lex/parse failed with: %+v", index, err)
return return
} }
if fail && err != nil { if fail1 && err != nil {
// TODO: %+v instead? // TODO: %+v instead?
s := fmt.Sprintf("%s", err) // convert to string s := fmt.Sprintf("%s", err) // convert to string
if s != expstr { if s != expstr {
@@ -701,6 +747,12 @@ 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 {
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) t.Logf("test #%d: AST: %+v", index, ast)
importGraph, err := pgraph.NewGraph("importGraph") 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) t.Errorf("test #%d: could not set scope: %+v", index, err)
return return
} }
if fail && err != nil { if fail2 && err != nil {
// TODO: %+v instead? // TODO: %+v instead?
s := fmt.Sprintf("%s", err) // convert to string s := fmt.Sprintf("%s", err) // convert to string
if s != expstr { if s != expstr {
@@ -760,6 +812,11 @@ func TestAstFunc1(t *testing.T) {
} }
return // fail happened during set scope, don't run unification! 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 // apply type unification
xlogf := func(format string, v ...interface{}) { 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) t.Errorf("test #%d: could not unify types: %+v", index, err)
return return
} }
// maybe it will fail during graph below instead? if fail3 && err != nil {
//if fail && err == nil {
// t.Errorf("test #%d: FAIL", index)
// t.Errorf("test #%d: unification passed, expected fail", index)
// continue
//}
if fail && err != nil {
// TODO: %+v instead? // TODO: %+v instead?
s := fmt.Sprintf("%s", err) // convert to string s := fmt.Sprintf("%s", err) // convert to string
if s != expstr { if s != expstr {
@@ -788,6 +839,11 @@ func TestAstFunc1(t *testing.T) {
} }
return // fail happened during unification, don't run Graph! 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 // build the function graph
graph, err := iast.Graph() graph, err := iast.Graph()
@@ -797,13 +853,7 @@ func TestAstFunc1(t *testing.T) {
t.Errorf("test #%d: functions failed with: %+v", index, err) t.Errorf("test #%d: functions failed with: %+v", index, err)
return return
} }
if fail && err == nil { if fail4 && err != nil { // can't process graph if it's 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
// TODO: %+v instead? // TODO: %+v instead?
s := fmt.Sprintf("%s", err) // convert to string s := fmt.Sprintf("%s", err) // convert to string
if s != expstr { if s != expstr {
@@ -814,6 +864,11 @@ func TestAstFunc1(t *testing.T) {
} }
return 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) t.Logf("test #%d: graph: %+v", index, graph)
str := strings.Trim(graph.Sprint(), "\n") // text format of graph str := strings.Trim(graph.Sprint(), "\n") // text format of graph

View File

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

View File

@@ -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)

View File

@@ -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)