test: Refactor unification_test to subtests
Testsuite for unification now uses subtests feature.
This commit is contained in:
@@ -475,93 +475,82 @@ func TestUnification1(t *testing.T) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
for index, test := range values { // run all the tests
|
for index, test := range values { // run all the tests
|
||||||
name, ast, fail, expect := test.name, test.ast, test.fail, test.expect
|
t.Run(fmt.Sprintf("test #%d (%s)", index, test.name), func(t *testing.T) {
|
||||||
|
ast, fail, expect := test.ast, test.fail, test.expect
|
||||||
|
|
||||||
if name == "" {
|
//str := strings.NewReader(code)
|
||||||
name = "<sub test not named>"
|
//ast, err := LexParse(str)
|
||||||
}
|
//if err != nil {
|
||||||
|
// t.Errorf("test #%d: lex/parse failed with: %+v", index, err)
|
||||||
|
// return
|
||||||
|
//}
|
||||||
|
// TODO: print out the AST's so that we can see the types
|
||||||
|
t.Logf("\n\ntest #%d: AST (before): %+v\n", index, ast)
|
||||||
|
|
||||||
//if index != 3 { // hack to run a subset (useful for debugging)
|
// skip interpolation in this test so that the node pointers
|
||||||
//if test.name != "nil" {
|
// aren't changed and so we can compare directly to expected
|
||||||
// continue
|
//astInterpolated, err := ast.Interpolate() // interpolate strings in ast
|
||||||
//}
|
//if err != nil {
|
||||||
|
// t.Errorf("test #%d: interpolate failed with: %+v", index, err)
|
||||||
|
// return
|
||||||
|
//}
|
||||||
|
//t.Logf("test #%d: astInterpolated: %+v", index, astInterpolated)
|
||||||
|
|
||||||
t.Logf("\n\ntest #%d (%s) ----------------\n\n", index, name)
|
// top-level, built-in, initial global scope
|
||||||
|
scope := &interfaces.Scope{
|
||||||
//str := strings.NewReader(code)
|
Variables: map[string]interfaces.Expr{
|
||||||
//ast, err := LexParse(str)
|
"purpleidea": &ExprStr{V: "hello world!"}, // james says hi
|
||||||
//if err != nil {
|
},
|
||||||
// t.Errorf("test #%d: lex/parse failed with: %+v", index, err)
|
}
|
||||||
// continue
|
// propagate the scope down through the AST...
|
||||||
//}
|
if err := ast.SetScope(scope); err != nil {
|
||||||
// TODO: print out the AST's so that we can see the types
|
t.Errorf("test #%d: FAIL", index)
|
||||||
t.Logf("\n\ntest #%d: AST (before): %+v\n", index, ast)
|
t.Errorf("test #%d: set scope failed with: %+v", index, err)
|
||||||
|
return
|
||||||
// skip interpolation in this test so that the node pointers
|
|
||||||
// aren't changed and so we can compare directly to expected
|
|
||||||
//astInterpolated, err := ast.Interpolate() // interpolate strings in ast
|
|
||||||
//if err != nil {
|
|
||||||
// t.Errorf("test #%d: interpolate failed with: %+v", index, err)
|
|
||||||
// continue
|
|
||||||
//}
|
|
||||||
//t.Logf("test #%d: astInterpolated: %+v", index, astInterpolated)
|
|
||||||
|
|
||||||
// top-level, built-in, initial global scope
|
|
||||||
scope := &interfaces.Scope{
|
|
||||||
Variables: map[string]interfaces.Expr{
|
|
||||||
"purpleidea": &ExprStr{V: "hello world!"}, // james says hi
|
|
||||||
},
|
|
||||||
}
|
|
||||||
// propagate the scope down through the AST...
|
|
||||||
if err := ast.SetScope(scope); err != nil {
|
|
||||||
t.Errorf("test #%d: set scope failed with: %+v", index, err)
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
|
|
||||||
// apply type unification
|
|
||||||
logf := func(format string, v ...interface{}) {
|
|
||||||
t.Logf(fmt.Sprintf("test #%d", index)+": unification: "+format, v...)
|
|
||||||
}
|
|
||||||
err := unification.Unify(ast, unification.SimpleInvariantSolverLogger(logf))
|
|
||||||
|
|
||||||
// TODO: print out the AST's so that we can see the types
|
|
||||||
t.Logf("\n\ntest #%d: AST (after): %+v\n", index, ast)
|
|
||||||
|
|
||||||
if !fail && err != nil {
|
|
||||||
t.Errorf("test #%d: unification failed with: %+v", index, err)
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
if fail && err == nil {
|
|
||||||
t.Errorf("test #%d: unification passed, expected fail", index)
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
if fail {
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
// continue the test
|
|
||||||
|
|
||||||
if expect == nil {
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
// TODO: do this in sorted order
|
|
||||||
var failed bool
|
|
||||||
for expr, exptyp := range expect {
|
|
||||||
typ, err := expr.Type() // lookup type
|
|
||||||
if err != nil {
|
|
||||||
t.Errorf("test #%d: type lookup of %+v failed with: %+v", index, expr, err)
|
|
||||||
failed = true
|
|
||||||
break
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if err := typ.Cmp(exptyp); err != nil {
|
// apply type unification
|
||||||
t.Errorf("test #%d: type cmp failed with: %+v", index, err)
|
logf := func(format string, v ...interface{}) {
|
||||||
failed = true
|
t.Logf(fmt.Sprintf("test #%d", index)+": unification: "+format, v...)
|
||||||
break
|
|
||||||
}
|
}
|
||||||
}
|
err := unification.Unify(ast, unification.SimpleInvariantSolverLogger(logf))
|
||||||
if failed {
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
|
|
||||||
|
// TODO: print out the AST's so that we can see the types
|
||||||
|
t.Logf("\n\ntest #%d: AST (after): %+v\n", index, ast)
|
||||||
|
|
||||||
|
if !fail && err != nil {
|
||||||
|
t.Errorf("test #%d: FAIL", index)
|
||||||
|
t.Errorf("test #%d: unification failed with: %+v", index, err)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
if fail && err == nil {
|
||||||
|
t.Errorf("test #%d: FAIL", index)
|
||||||
|
t.Errorf("test #%d: unification passed, expected fail", index)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
if expect == nil { // test done early
|
||||||
|
return
|
||||||
|
}
|
||||||
|
// TODO: do this in sorted order
|
||||||
|
var failed bool
|
||||||
|
for expr, exptyp := range expect {
|
||||||
|
typ, err := expr.Type() // lookup type
|
||||||
|
if err != nil {
|
||||||
|
t.Errorf("test #%d: type lookup of %+v failed with: %+v", index, expr, err)
|
||||||
|
failed = true
|
||||||
|
break
|
||||||
|
}
|
||||||
|
|
||||||
|
if err := typ.Cmp(exptyp); err != nil {
|
||||||
|
t.Errorf("test #%d: type cmp failed with: %+v", index, err)
|
||||||
|
failed = true
|
||||||
|
break
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if failed {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
})
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user