This enables imports in mcl code, and is one of last remaining blockers to using mgmt. Now we can start writing standalone modules, and adding standard library functions as needed. There's still lots to do, but this was a big missing piece. It was much harder to get right than I had expected, but I think it's solid! This unfortunately large commit is the result of some wild hacking I've been doing for the past little while. It's the result of a rebase that broke many "wip" commits that tracked my private progress, into something that's not gratuitously messy for our git logs. Since this was a learning and discovery process for me, I've "erased" the confusing git history that wouldn't have helped. I'm happy to discuss the dead-ends, and a small portion of that code was even left in for possible future use. This patch includes: * A change to the cli interface: You now specify the front-end explicitly, instead of leaving it up to the front-end to decide when to "activate". For example, instead of: mgmt run --lang code.mcl we now do: mgmt run lang --lang code.mcl We might rename the --lang flag in the future to avoid the awkward word repetition. Suggestions welcome, but I'm considering "input". One side-effect of this change, is that flags which are "engine" specific now must be specified with "run" before the front-end name. Eg: mgmt run --tmp-prefix lang --lang code.mcl instead of putting --tmp-prefix at the end. We also changed the GAPI slightly, but I've patched all code that used it. This also makes things consistent with the "deploy" command. * The deploys are more robust and let you deploy after a run This has been vastly improved and let's mgmt really run as a smart engine that can handle different workloads. If you don't want to deploy when you've started with `run` or if one comes in, you can use the --no-watch-deploy option to block new deploys. * The import statement exists and works! We now have a working `import` statement. Read the docs, and try it out. I think it's quite elegant how it fits in with `SetScope`. Have a look. As a result, we now have some built-in functions available in modules. This also adds the metadata.yaml entry-point for all modules. Have a look at the examples or the tests. The bulk of the patch is to support this. * Improved lang input parsing code: I re-wrote the parsing that determined what ran when we passed different things to --lang. Deciding between running an mcl file or raw code is now handled in a more intelligent, and re-usable way. See the inputs.go file if you want to have a look. One casualty is that you can't stream code from stdin *directly* to the front-end, it's encapsulated into a deploy first. You can still use stdin though! I doubt anyone will notice this change. * The scope was extended to include functions and classes: Go forth and import lovely code. All these exist in scopes now, and can be re-used! * Function calls actually use the scope now. Glad I got this sorted out. * There is import cycle detection for modules! Yes, this is another dag. I think that's #4. I guess they're useful. * A ton of tests and new test infra was added! This should make it much easier to add new tests that run mcl code. Have a look at TestAstFunc1 to see how to add more of these. As usual, I'll try to keep these commits smaller in the future!
761 lines
16 KiB
Go
761 lines
16 KiB
Go
// Mgmt
|
|
// Copyright (C) 2013-2018+ James Shubin and the project contributors
|
|
// Written by James Shubin <james@shubin.ca> and the project contributors
|
|
//
|
|
// This program is free software: you can redistribute it and/or modify
|
|
// it under the terms of the GNU General Public License as published by
|
|
// the Free Software Foundation, either version 3 of the License, or
|
|
// (at your option) any later version.
|
|
//
|
|
// This program is distributed in the hope that it will be useful,
|
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
// GNU General Public License for more details.
|
|
//
|
|
// You should have received a copy of the GNU General Public License
|
|
// along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
// +build !root
|
|
|
|
package lang
|
|
|
|
import (
|
|
"fmt"
|
|
"testing"
|
|
|
|
"github.com/purpleidea/mgmt/lang/funcs"
|
|
"github.com/purpleidea/mgmt/lang/interfaces"
|
|
"github.com/purpleidea/mgmt/lang/types"
|
|
"github.com/purpleidea/mgmt/lang/unification"
|
|
"github.com/purpleidea/mgmt/util"
|
|
)
|
|
|
|
func TestUnification1(t *testing.T) {
|
|
type test struct { // an individual test
|
|
name string
|
|
ast interfaces.Stmt // raw AST
|
|
fail bool
|
|
expect map[interfaces.Expr]*types.Type
|
|
experr error // expected error if fail == true (nil ignores it)
|
|
}
|
|
testCases := []test{}
|
|
|
|
// this causes a panic, so it can't be used
|
|
//{
|
|
// testCases = append(testCases, test{
|
|
// "nil",
|
|
// nil,
|
|
// true, // expect error
|
|
// nil, // no AST
|
|
// })
|
|
//}
|
|
{
|
|
expr := &ExprStr{V: "hello"}
|
|
stmt := &StmtProg{
|
|
Prog: []interfaces.Stmt{
|
|
&StmtRes{
|
|
Kind: "test",
|
|
Name: &ExprStr{V: "t1"},
|
|
Contents: []StmtResContents{
|
|
&StmtResField{
|
|
Field: "str",
|
|
Value: expr,
|
|
},
|
|
},
|
|
},
|
|
},
|
|
}
|
|
testCases = append(testCases, test{
|
|
name: "one res",
|
|
ast: stmt,
|
|
fail: false,
|
|
expect: map[interfaces.Expr]*types.Type{
|
|
expr: types.TypeStr,
|
|
},
|
|
})
|
|
}
|
|
{
|
|
v1 := &ExprStr{}
|
|
v2 := &ExprStr{}
|
|
v3 := &ExprStr{}
|
|
expr := &ExprList{
|
|
Elements: []interfaces.Expr{
|
|
v1,
|
|
v2,
|
|
v3,
|
|
},
|
|
}
|
|
stmt := &StmtProg{
|
|
Prog: []interfaces.Stmt{
|
|
&StmtRes{
|
|
Kind: "test",
|
|
Name: &ExprStr{V: "test"},
|
|
Contents: []StmtResContents{
|
|
&StmtResField{
|
|
Field: "slicestring",
|
|
Value: expr,
|
|
},
|
|
},
|
|
},
|
|
},
|
|
}
|
|
testCases = append(testCases, test{
|
|
name: "list of strings",
|
|
ast: stmt,
|
|
fail: false,
|
|
expect: map[interfaces.Expr]*types.Type{
|
|
v1: types.TypeStr,
|
|
v2: types.TypeStr,
|
|
v3: types.TypeStr,
|
|
expr: types.NewType("[]str"),
|
|
},
|
|
})
|
|
}
|
|
{
|
|
k1 := &ExprInt{}
|
|
k2 := &ExprInt{}
|
|
k3 := &ExprInt{}
|
|
v1 := &ExprFloat{}
|
|
v2 := &ExprFloat{}
|
|
v3 := &ExprFloat{}
|
|
expr := &ExprMap{
|
|
KVs: []*ExprMapKV{
|
|
{Key: k1, Val: v1},
|
|
{Key: k2, Val: v2},
|
|
{Key: k3, Val: v3},
|
|
},
|
|
}
|
|
stmt := &StmtProg{
|
|
Prog: []interfaces.Stmt{
|
|
&StmtRes{
|
|
Kind: "test",
|
|
Name: &ExprStr{V: "test"},
|
|
Contents: []StmtResContents{
|
|
&StmtResField{
|
|
Field: "mapintfloat",
|
|
Value: expr,
|
|
},
|
|
},
|
|
},
|
|
},
|
|
}
|
|
testCases = append(testCases, test{
|
|
name: "map of int->float",
|
|
ast: stmt,
|
|
fail: false,
|
|
expect: map[interfaces.Expr]*types.Type{
|
|
k1: types.TypeInt,
|
|
k2: types.TypeInt,
|
|
k3: types.TypeInt,
|
|
v1: types.TypeFloat,
|
|
v2: types.TypeFloat,
|
|
v3: types.TypeFloat,
|
|
expr: types.NewType("map{int: float}"),
|
|
},
|
|
})
|
|
}
|
|
{
|
|
b := &ExprBool{}
|
|
s := &ExprStr{}
|
|
i := &ExprInt{}
|
|
f := &ExprFloat{}
|
|
expr := &ExprStruct{
|
|
Fields: []*ExprStructField{
|
|
{Name: "somebool", Value: b},
|
|
{Name: "somestr", Value: s},
|
|
{Name: "someint", Value: i},
|
|
{Name: "somefloat", Value: f},
|
|
},
|
|
}
|
|
stmt := &StmtProg{
|
|
Prog: []interfaces.Stmt{
|
|
&StmtRes{
|
|
Kind: "test",
|
|
Name: &ExprStr{V: "test"},
|
|
Contents: []StmtResContents{
|
|
&StmtResField{
|
|
Field: "mixedstruct",
|
|
Value: expr,
|
|
},
|
|
},
|
|
},
|
|
},
|
|
}
|
|
testCases = append(testCases, test{
|
|
name: "simple struct",
|
|
ast: stmt,
|
|
fail: false,
|
|
expect: map[interfaces.Expr]*types.Type{
|
|
b: types.TypeBool,
|
|
s: types.TypeStr,
|
|
i: types.TypeInt,
|
|
f: types.TypeFloat,
|
|
expr: types.NewType("struct{somebool bool; somestr str; someint int; somefloat float}"),
|
|
},
|
|
})
|
|
}
|
|
{
|
|
// test "n1" {
|
|
// int64ptr => 13 + 42,
|
|
//}
|
|
expr := &ExprCall{
|
|
Name: operatorFuncName,
|
|
Args: []interfaces.Expr{
|
|
&ExprStr{
|
|
V: "+",
|
|
},
|
|
&ExprInt{
|
|
V: 13,
|
|
},
|
|
|
|
&ExprInt{
|
|
V: 42,
|
|
},
|
|
},
|
|
}
|
|
stmt := &StmtProg{
|
|
Prog: []interfaces.Stmt{
|
|
&StmtRes{
|
|
Kind: "test",
|
|
Name: &ExprStr{
|
|
V: "n1",
|
|
},
|
|
Contents: []StmtResContents{
|
|
&StmtResField{
|
|
Field: "int64ptr",
|
|
Value: expr, // func
|
|
},
|
|
},
|
|
},
|
|
},
|
|
}
|
|
testCases = append(testCases, test{
|
|
name: "func call",
|
|
ast: stmt,
|
|
fail: false,
|
|
expect: map[interfaces.Expr]*types.Type{
|
|
expr: types.NewType("int"),
|
|
},
|
|
})
|
|
}
|
|
{
|
|
//test "n1" {
|
|
// int64ptr => 13 + 42 - 4,
|
|
//}
|
|
innerFunc := &ExprCall{
|
|
Name: operatorFuncName,
|
|
Args: []interfaces.Expr{
|
|
&ExprStr{
|
|
V: "-",
|
|
},
|
|
&ExprInt{
|
|
V: 42,
|
|
},
|
|
&ExprInt{
|
|
V: 4,
|
|
},
|
|
},
|
|
}
|
|
expr := &ExprCall{
|
|
Name: operatorFuncName,
|
|
Args: []interfaces.Expr{
|
|
&ExprStr{
|
|
V: "+",
|
|
},
|
|
&ExprInt{
|
|
V: 13,
|
|
},
|
|
innerFunc, // nested func, can we unify?
|
|
},
|
|
}
|
|
stmt := &StmtProg{
|
|
Prog: []interfaces.Stmt{
|
|
&StmtRes{
|
|
Kind: "test",
|
|
Name: &ExprStr{
|
|
V: "n1",
|
|
},
|
|
Contents: []StmtResContents{
|
|
&StmtResField{
|
|
Field: "int64ptr",
|
|
Value: expr,
|
|
},
|
|
},
|
|
},
|
|
},
|
|
}
|
|
testCases = append(testCases, test{
|
|
name: "func call, multiple ints",
|
|
ast: stmt,
|
|
fail: false,
|
|
expect: map[interfaces.Expr]*types.Type{
|
|
innerFunc: types.NewType("int"),
|
|
expr: types.NewType("int"),
|
|
},
|
|
})
|
|
}
|
|
{
|
|
//test "n1" {
|
|
// float32 => -25.38789 + 32.6 + 13.7,
|
|
//}
|
|
innerFunc := &ExprCall{
|
|
Name: operatorFuncName,
|
|
Args: []interfaces.Expr{
|
|
&ExprStr{
|
|
V: "+",
|
|
},
|
|
&ExprFloat{
|
|
V: 32.6,
|
|
},
|
|
&ExprFloat{
|
|
V: 13.7,
|
|
},
|
|
},
|
|
}
|
|
expr := &ExprCall{
|
|
Name: operatorFuncName,
|
|
Args: []interfaces.Expr{
|
|
&ExprStr{
|
|
V: "+",
|
|
},
|
|
&ExprFloat{
|
|
V: -25.38789,
|
|
},
|
|
innerFunc, // nested func, can we unify?
|
|
},
|
|
}
|
|
stmt := &StmtProg{
|
|
Prog: []interfaces.Stmt{
|
|
&StmtRes{
|
|
Kind: "test",
|
|
Name: &ExprStr{
|
|
V: "n1",
|
|
},
|
|
Contents: []StmtResContents{
|
|
&StmtResField{
|
|
Field: "float32",
|
|
Value: expr,
|
|
},
|
|
},
|
|
},
|
|
},
|
|
}
|
|
testCases = append(testCases, test{
|
|
name: "func call, multiple floats",
|
|
ast: stmt,
|
|
fail: false,
|
|
expect: map[interfaces.Expr]*types.Type{
|
|
innerFunc: types.NewType("float"),
|
|
expr: types.NewType("float"),
|
|
},
|
|
})
|
|
}
|
|
{
|
|
//$x = 42 - 13
|
|
innerFunc := &ExprCall{
|
|
Name: operatorFuncName,
|
|
Args: []interfaces.Expr{
|
|
&ExprStr{
|
|
V: "-",
|
|
},
|
|
&ExprInt{
|
|
V: 42,
|
|
},
|
|
&ExprInt{
|
|
V: 13,
|
|
},
|
|
},
|
|
}
|
|
stmt := &StmtProg{
|
|
Prog: []interfaces.Stmt{
|
|
&StmtBind{
|
|
Ident: "x",
|
|
Value: innerFunc,
|
|
},
|
|
},
|
|
}
|
|
testCases = append(testCases, test{
|
|
name: "assign from func call or two ints",
|
|
ast: stmt,
|
|
fail: false,
|
|
expect: map[interfaces.Expr]*types.Type{
|
|
innerFunc: types.NewType("int"),
|
|
},
|
|
})
|
|
}
|
|
{
|
|
//$x = template("hello", 42)
|
|
innerFunc := &ExprCall{
|
|
Name: "template",
|
|
Args: []interfaces.Expr{
|
|
&ExprStr{
|
|
V: "hello",
|
|
},
|
|
&ExprInt{
|
|
V: 42,
|
|
},
|
|
},
|
|
}
|
|
stmt := &StmtProg{
|
|
Prog: []interfaces.Stmt{
|
|
&StmtBind{
|
|
Ident: "x",
|
|
Value: innerFunc,
|
|
},
|
|
},
|
|
}
|
|
testCases = append(testCases, test{
|
|
name: "simple template",
|
|
ast: stmt,
|
|
fail: false,
|
|
expect: map[interfaces.Expr]*types.Type{
|
|
innerFunc: types.NewType("str"),
|
|
},
|
|
})
|
|
}
|
|
{
|
|
//$v = 42
|
|
//$x = template("hello", $v) # redirect var for harder unification
|
|
innerFunc := &ExprCall{
|
|
Name: "template",
|
|
Args: []interfaces.Expr{
|
|
&ExprStr{
|
|
V: "hello", // whatever...
|
|
},
|
|
&ExprVar{
|
|
Name: "x",
|
|
},
|
|
},
|
|
}
|
|
stmt := &StmtProg{
|
|
Prog: []interfaces.Stmt{
|
|
&StmtBind{
|
|
Ident: "v",
|
|
Value: &ExprInt{
|
|
V: 42,
|
|
},
|
|
},
|
|
&StmtBind{
|
|
Ident: "x",
|
|
Value: innerFunc,
|
|
},
|
|
},
|
|
}
|
|
testCases = append(testCases, test{
|
|
name: "complex template",
|
|
ast: stmt,
|
|
fail: false,
|
|
expect: map[interfaces.Expr]*types.Type{
|
|
innerFunc: types.NewType("str"),
|
|
},
|
|
})
|
|
}
|
|
{
|
|
//test "t1" {
|
|
// stringptr => datetime(), # bad (str vs. int)
|
|
//}
|
|
expr := &ExprCall{
|
|
Name: "datetime",
|
|
Args: []interfaces.Expr{},
|
|
}
|
|
stmt := &StmtProg{
|
|
Prog: []interfaces.Stmt{
|
|
&StmtRes{
|
|
Kind: "test",
|
|
Name: &ExprStr{V: "t1"},
|
|
Contents: []StmtResContents{
|
|
&StmtResField{
|
|
Field: "stringptr",
|
|
Value: expr,
|
|
},
|
|
},
|
|
},
|
|
},
|
|
}
|
|
testCases = append(testCases, test{
|
|
name: "single fact unification",
|
|
ast: stmt,
|
|
fail: true,
|
|
})
|
|
}
|
|
{
|
|
//test "t1" {
|
|
// stringptr => getenv("GOPATH", "bug"), # bad (two args vs. one)
|
|
//}
|
|
expr := &ExprCall{
|
|
Name: "getenv",
|
|
Args: []interfaces.Expr{
|
|
&ExprStr{
|
|
V: "GOPATH",
|
|
},
|
|
&ExprStr{
|
|
V: "bug",
|
|
},
|
|
},
|
|
}
|
|
stmt := &StmtProg{
|
|
Prog: []interfaces.Stmt{
|
|
&StmtRes{
|
|
Kind: "test",
|
|
Name: &ExprStr{V: "t1"},
|
|
Contents: []StmtResContents{
|
|
&StmtResField{
|
|
Field: "stringptr",
|
|
Value: expr,
|
|
},
|
|
},
|
|
},
|
|
},
|
|
}
|
|
testCases = append(testCases, test{
|
|
name: "function, wrong arg count",
|
|
ast: stmt,
|
|
fail: true,
|
|
})
|
|
}
|
|
// XXX: add these tests when we fix the bug!
|
|
//{
|
|
// //test "t1" {
|
|
// // import "fmt"
|
|
// // stringptr => fmt.printf("hello %s and %s", "one"), # bad
|
|
// //}
|
|
// expr := &ExprCall{
|
|
// Name: "fmt.printf",
|
|
// Args: []interfaces.Expr{
|
|
// &ExprStr{
|
|
// V: "hello %s and %s",
|
|
// },
|
|
// &ExprStr{
|
|
// V: "one",
|
|
// },
|
|
// },
|
|
// }
|
|
// stmt := &StmtProg{
|
|
// Prog: []interfaces.Stmt{
|
|
// &StmtImport{
|
|
// Name: "fmt",
|
|
// },
|
|
// &StmtRes{
|
|
// Kind: "test",
|
|
// Name: &ExprStr{V: "t1"},
|
|
// Contents: []StmtResContents{
|
|
// &StmtResField{
|
|
// Field: "stringptr",
|
|
// Value: expr,
|
|
// },
|
|
// },
|
|
// },
|
|
// },
|
|
// }
|
|
// testCases = append(testCases, test{
|
|
// name: "function, missing arg for printf",
|
|
// ast: stmt,
|
|
// fail: true,
|
|
// })
|
|
//}
|
|
//{
|
|
// //test "t1" {
|
|
// // import "fmt"
|
|
// // stringptr => fmt.printf("hello %s and %s", "one", "two", "three"), # bad
|
|
// //}
|
|
// expr := &ExprCall{
|
|
// Name: "fmt.printf",
|
|
// Args: []interfaces.Expr{
|
|
// &ExprStr{
|
|
// V: "hello %s and %s",
|
|
// },
|
|
// &ExprStr{
|
|
// V: "one",
|
|
// },
|
|
// &ExprStr{
|
|
// V: "two",
|
|
// },
|
|
// &ExprStr{
|
|
// V: "three",
|
|
// },
|
|
// },
|
|
// }
|
|
// stmt := &StmtProg{
|
|
// Prog: []interfaces.Stmt{
|
|
// &StmtImport{
|
|
// Name: "fmt",
|
|
// },
|
|
// &StmtRes{
|
|
// Kind: "test",
|
|
// Name: &ExprStr{V: "t1"},
|
|
// Contents: []StmtResContents{
|
|
// &StmtResField{
|
|
// Field: "stringptr",
|
|
// Value: expr,
|
|
// },
|
|
// },
|
|
// },
|
|
// },
|
|
// }
|
|
// testCases = append(testCases, test{
|
|
// name: "function, extra arg for printf",
|
|
// ast: stmt,
|
|
// fail: true,
|
|
// })
|
|
//}
|
|
{
|
|
//test "t1" {
|
|
// import "fmt"
|
|
// stringptr => fmt.printf("hello %s and %s", "one", "two"),
|
|
//}
|
|
expr := &ExprCall{
|
|
Name: "fmt.printf",
|
|
Args: []interfaces.Expr{
|
|
&ExprStr{
|
|
V: "hello %s and %s",
|
|
},
|
|
&ExprStr{
|
|
V: "one",
|
|
},
|
|
&ExprStr{
|
|
V: "two",
|
|
},
|
|
},
|
|
}
|
|
stmt := &StmtProg{
|
|
Prog: []interfaces.Stmt{
|
|
&StmtImport{
|
|
Name: "fmt",
|
|
},
|
|
&StmtRes{
|
|
Kind: "test",
|
|
Name: &ExprStr{V: "t1"},
|
|
Contents: []StmtResContents{
|
|
&StmtResField{
|
|
Field: "stringptr",
|
|
Value: expr,
|
|
},
|
|
},
|
|
},
|
|
},
|
|
}
|
|
testCases = append(testCases, test{
|
|
name: "function, regular printf unification",
|
|
ast: stmt,
|
|
fail: false,
|
|
expect: map[interfaces.Expr]*types.Type{
|
|
expr: types.NewType("str"),
|
|
},
|
|
})
|
|
}
|
|
|
|
names := []string{}
|
|
for index, tc := range testCases { // run all the tests
|
|
if tc.name == "" {
|
|
t.Errorf("test #%d: not named", index)
|
|
continue
|
|
}
|
|
if util.StrInList(tc.name, names) {
|
|
t.Errorf("test #%d: duplicate sub test name of: %s", index, tc.name)
|
|
continue
|
|
}
|
|
names = append(names, tc.name)
|
|
t.Run(fmt.Sprintf("test #%d (%s)", index, tc.name), func(t *testing.T) {
|
|
ast, fail, expect, experr := tc.ast, tc.fail, tc.expect, tc.experr
|
|
|
|
//str := strings.NewReader(code)
|
|
//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)
|
|
|
|
data := &interfaces.Data{
|
|
Debug: true,
|
|
Logf: func(format string, v ...interface{}) {
|
|
t.Logf(fmt.Sprintf("test #%d", index)+": ast: "+format, v...)
|
|
},
|
|
}
|
|
// some of this might happen *after* interpolate in SetScope or Unify...
|
|
if err := ast.Init(data); err != nil {
|
|
t.Errorf("test #%d: FAIL", index)
|
|
t.Errorf("test #%d: could not init and validate AST: %+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)
|
|
// return
|
|
//}
|
|
//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
|
|
//"hostname": &ExprStr{V: obj.Hostname},
|
|
},
|
|
// all the built-in top-level, core functions enter here...
|
|
Functions: funcs.LookupPrefix(""),
|
|
}
|
|
// propagate the scope down through the AST...
|
|
if err := ast.SetScope(scope); err != nil {
|
|
t.Errorf("test #%d: FAIL", index)
|
|
t.Errorf("test #%d: set scope failed with: %+v", index, err)
|
|
return
|
|
}
|
|
|
|
// 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: 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 fail && experr != nil && err != experr { // test for specific error!
|
|
t.Errorf("test #%d: FAIL", index)
|
|
t.Errorf("test #%d: expected fail, got wrong error", index)
|
|
t.Errorf("test #%d: got error: %+v", index, err)
|
|
t.Errorf("test #%d: exp error: %+v", index, experr)
|
|
}
|
|
|
|
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)
|
|
t.Logf("test #%d: got: %+v", index, typ)
|
|
t.Logf("test #%d: exp: %+v", index, exptyp)
|
|
failed = true
|
|
break
|
|
}
|
|
}
|
|
if failed {
|
|
return
|
|
}
|
|
})
|
|
}
|
|
}
|