test: Clean up and improve golang tests

This adds some consistency to the tests and properly catches difficult
scenarios in some of the lexparse tests.
This commit is contained in:
James Shubin
2018-11-18 20:10:05 -05:00
parent 958d3f6094
commit 48beea3884
8 changed files with 244 additions and 233 deletions

View File

@@ -68,7 +68,7 @@ func TestInstance1(t *testing.T) {
fail bool
expect map[string]string
}
values := []test{}
testCases := []test{}
{
code := util.Code(`
@@ -79,7 +79,7 @@ func TestInstance1(t *testing.T) {
state => "exists",
}
`)
values = append(values, test{
testCases = append(testCases, test{
name: "hello world",
code: code,
fail: false,
@@ -89,9 +89,9 @@ func TestInstance1(t *testing.T) {
})
}
for index, test := range values { // run all the tests
t.Run(fmt.Sprintf("test #%d (%s)", index, test.name), func(t *testing.T) {
code, fail, expect := test.code, test.fail, test.expect
for index, tc := range testCases { // run all the tests
t.Run(fmt.Sprintf("test #%d (%s)", index, tc.name), func(t *testing.T) {
code, fail, expect := tc.code, tc.fail, tc.expect
m := Instance{
Hostname: "h1",
@@ -151,7 +151,7 @@ func TestCluster1(t *testing.T) {
hosts []string
expect map[string]map[string]string // hostname, file, contents
}
values := []test{}
testCases := []test{}
{
code := util.Code(`
@@ -162,7 +162,7 @@ func TestCluster1(t *testing.T) {
state => "exists",
}
`)
values = append(values, test{
testCases = append(testCases, test{
name: "simple pair",
code: code,
fail: false,
@@ -186,7 +186,7 @@ func TestCluster1(t *testing.T) {
state => "exists",
}
`)
values = append(values, test{
testCases = append(testCases, test{
name: "hello world",
code: code,
fail: false,
@@ -205,9 +205,9 @@ func TestCluster1(t *testing.T) {
})
}
for index, test := range values { // run all the tests
t.Run(fmt.Sprintf("test #%d (%s)", index, test.name), func(t *testing.T) {
code, fail, hosts, expect := test.code, test.fail, test.hosts, test.expect
for index, tc := range testCases { // run all the tests
t.Run(fmt.Sprintf("test #%d (%s)", index, tc.name), func(t *testing.T) {
code, fail, hosts, expect := tc.code, tc.fail, tc.hosts, tc.expect
c := Cluster{
Hostnames: hosts,

View File

@@ -130,6 +130,10 @@ func TestInterpolate0(t *testing.T) {
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

View File

@@ -77,11 +77,11 @@ func TestAstFunc0(t *testing.T) {
scope *interfaces.Scope
graph *pgraph.Graph
}
values := []test{}
testCases := []test{}
{
graph, _ := pgraph.NewGraph("g")
values = append(values, test{ // 0
testCases = append(testCases, test{ // 0
"nil",
``,
false,
@@ -91,7 +91,7 @@ func TestAstFunc0(t *testing.T) {
}
{
graph, _ := pgraph.NewGraph("g")
values = append(values, test{
testCases = append(testCases, test{
name: "scope only",
code: ``,
fail: false,
@@ -105,7 +105,7 @@ func TestAstFunc0(t *testing.T) {
e1 := edge("x")
graph.AddVertex(&v1, &v2)
graph.AddEdge(&v1, &v2, &e1)
values = append(values, test{
testCases = append(testCases, test{
name: "two vars",
code: `
$x = 42
@@ -118,7 +118,7 @@ func TestAstFunc0(t *testing.T) {
}
{
graph, _ := pgraph.NewGraph("g")
values = append(values, test{
testCases = append(testCases, test{
name: "self-referential vars",
code: `
$x = $y
@@ -136,7 +136,7 @@ func TestAstFunc0(t *testing.T) {
graph.AddEdge(&v1, &v2, &e1)
graph.AddEdge(&v2, &v3, &e2)
graph.AddEdge(&v3, &v4, &e3)
values = append(values, test{
testCases = append(testCases, test{
name: "chained vars",
code: `
test "t" {
@@ -156,7 +156,7 @@ func TestAstFunc0(t *testing.T) {
graph.AddVertex(&v1, &v2)
e1 := edge("b")
graph.AddEdge(&v1, &v2, &e1)
values = append(values, test{
testCases = append(testCases, test{
name: "simple bool",
code: `
if $b {
@@ -175,7 +175,7 @@ func TestAstFunc0(t *testing.T) {
graph.AddEdge(&v2, &v5, &e1)
graph.AddEdge(&v3, &v5, &e2)
graph.AddEdge(&v4, &v5, &e3)
values = append(values, test{
testCases = append(testCases, test{
name: "simple operator",
code: `
test "t" {
@@ -203,7 +203,7 @@ func TestAstFunc0(t *testing.T) {
graph.AddEdge(&v2, &v8, &e4)
graph.AddEdge(&v7, &v8, &e5)
graph.AddEdge(&v6, &v8, &e6)
values = append(values, test{
testCases = append(testCases, test{
name: "simple operators",
code: `
test "t" {
@@ -230,7 +230,7 @@ func TestAstFunc0(t *testing.T) {
graph.AddEdge(&v5, &v8, &e3)
graph.AddEdge(&v8, &v6, &e5)
values = append(values, test{
testCases = append(testCases, test{
name: "nested resource and scoped var",
code: `
if true {
@@ -246,7 +246,7 @@ func TestAstFunc0(t *testing.T) {
})
}
{
values = append(values, test{
testCases = append(testCases, test{
name: "out of scope error",
code: `
# should be out of scope, and a compile error!
@@ -260,7 +260,7 @@ func TestAstFunc0(t *testing.T) {
})
}
{
values = append(values, test{
testCases = append(testCases, test{
name: "variable re-declaration error",
code: `
# this should fail b/c of variable re-declaration
@@ -280,7 +280,7 @@ func TestAstFunc0(t *testing.T) {
// only one edge! (cool)
graph.AddEdge(&v1, &v4, &e1)
values = append(values, test{
testCases = append(testCases, test{
name: "variable shadowing",
code: `
# this should be okay, because var is shadowed
@@ -306,7 +306,7 @@ func TestAstFunc0(t *testing.T) {
// only one edge! (cool)
graph.AddEdge(&v2, &v4, &e1)
values = append(values, test{
testCases = append(testCases, test{
name: "variable shadowing inner",
code: `
# this should be okay, because var is shadowed
@@ -335,7 +335,7 @@ func TestAstFunc0(t *testing.T) {
// graph.AddEdge(&v1, &v3, &e1)
// graph.AddEdge(&v2, &v4, &e2)
//
// values = append(values, test{
// testCases = append(testCases, test{
// name: "variable shadowing both",
// code: `
// # this should be okay, because var is shadowed
@@ -355,7 +355,7 @@ func TestAstFunc0(t *testing.T) {
// })
//}
{
values = append(values, test{
testCases = append(testCases, test{
name: "variable re-declaration and type change error",
code: `
# this should fail b/c of variable re-declaration
@@ -367,8 +367,8 @@ func TestAstFunc0(t *testing.T) {
}
names := []string{}
for index, test := range values { // run all the tests
name, code, fail, scope, exp := test.name, test.code, test.fail, test.scope, test.graph
for index, tc := range testCases { // run all the tests
name, code, fail, scope, exp := tc.name, tc.code, tc.fail, tc.scope, tc.graph
if name == "" {
name = "<sub test not named>"
@@ -380,7 +380,7 @@ func TestAstFunc0(t *testing.T) {
names = append(names, name)
//if index != 3 { // hack to run a subset (useful for debugging)
//if test.name != "simple operators" {
//if tc.name != "simple operators" {
// continue
//}
@@ -495,11 +495,11 @@ func TestAstInterpret0(t *testing.T) {
fail bool
graph *pgraph.Graph
}
values := []test{}
testCases := []test{}
{
graph, _ := pgraph.NewGraph("g")
values = append(values, test{ // 0
testCases = append(testCases, test{ // 0
"nil",
``,
false,
@@ -507,7 +507,7 @@ func TestAstInterpret0(t *testing.T) {
})
}
{
values = append(values, test{
testCases = append(testCases, test{
name: "wrong res field type",
code: `
test "t1" {
@@ -530,7 +530,7 @@ func TestAstInterpret0(t *testing.T) {
int8ptrptrptr := &int8ptrptr
x.Int8PtrPtrPtr = &int8ptrptrptr
graph.AddVertex(t1)
values = append(values, test{
testCases = append(testCases, test{
name: "resource with three pointer fields",
code: `
test "t1" {
@@ -550,7 +550,7 @@ func TestAstInterpret0(t *testing.T) {
stringptr := "wow"
x.StringPtr = &stringptr
graph.AddVertex(t1)
values = append(values, test{
testCases = append(testCases, test{
name: "resource with simple string pointer field",
code: `
test "t1" {
@@ -582,7 +582,7 @@ func TestAstInterpret0(t *testing.T) {
Notify: false,
}
graph.AddEdge(t1, t2, edge)
values = append(values, test{
testCases = append(testCases, test{
name: "two resources and send/recv edge",
code: `
test "t1" {
@@ -599,8 +599,8 @@ func TestAstInterpret0(t *testing.T) {
}
names := []string{}
for index, test := range values { // run all the tests
name, code, fail, exp := test.name, test.code, test.fail, test.graph
for index, tc := range testCases { // run all the tests
name, code, fail, exp := tc.name, tc.code, tc.fail, tc.graph
if name == "" {
name = "<sub test not named>"
@@ -612,7 +612,7 @@ func TestAstInterpret0(t *testing.T) {
names = append(names, name)
//if index != 3 { // hack to run a subset (useful for debugging)
//if test.name != "nil" {
//if tc.name != "nil" {
// continue
//}

View File

@@ -305,11 +305,11 @@ func TestInterpretMany(t *testing.T) {
fail bool
graph *pgraph.Graph
}
values := []test{}
testCases := []test{}
{
graph, _ := pgraph.NewGraph("g")
values = append(values, test{ // 0
testCases = append(testCases, test{ // 0
"nil",
``,
false,
@@ -318,7 +318,7 @@ func TestInterpretMany(t *testing.T) {
}
{
graph, _ := pgraph.NewGraph("g")
values = append(values, test{ // 1
testCases = append(testCases, test{ // 1
name: "empty",
code: ``,
fail: false,
@@ -332,7 +332,7 @@ func TestInterpretMany(t *testing.T) {
i := int64(42 + 13)
x.Int64Ptr = &i
graph.AddVertex(x)
values = append(values, test{
testCases = append(testCases, test{
name: "simple addition",
code: `
test "t" {
@@ -350,7 +350,7 @@ func TestInterpretMany(t *testing.T) {
i := int64(42 + 13 + 99)
x.Int64Ptr = &i
graph.AddVertex(x)
values = append(values, test{
testCases = append(testCases, test{
name: "triple addition",
code: `
test "t" {
@@ -368,7 +368,7 @@ func TestInterpretMany(t *testing.T) {
i := int64(42 + 13 - 99)
x.Int64Ptr = &i
graph.AddVertex(x)
values = append(values, test{
testCases = append(testCases, test{
name: "triple addition/subtraction",
code: `
test "t" {
@@ -386,7 +386,7 @@ func TestInterpretMany(t *testing.T) {
s1 := "hello"
x1.StringPtr = &s1
graph.AddVertex(x1)
values = append(values, test{
testCases = append(testCases, test{
name: "single include",
code: `
class c1($a, $b) {
@@ -410,7 +410,7 @@ func TestInterpretMany(t *testing.T) {
x1.StringPtr = &s1
x2.StringPtr = &s2
graph.AddVertex(x1, x2)
values = append(values, test{
testCases = append(testCases, test{
name: "double include",
code: `
class c1($a, $b) {
@@ -435,7 +435,7 @@ func TestInterpretMany(t *testing.T) {
//x1.StringPtr = &s1
//x2.Int64Ptr = &i2
//graph.AddVertex(x1, x2)
values = append(values, test{
testCases = append(testCases, test{
name: "double include different types error",
code: `
class c1($a, $b) {
@@ -466,7 +466,7 @@ func TestInterpretMany(t *testing.T) {
x1.StringPtr = &s1
x2.StringPtr = &s2
graph.AddVertex(x1, x2)
values = append(values, test{
testCases = append(testCases, test{
name: "double include different types allowed",
code: `
class c1($a, $b) {
@@ -494,7 +494,7 @@ func TestInterpretMany(t *testing.T) {
// x1.StringPtr = &s1
// x2.StringPtr = &s2
// graph.AddVertex(x1, x2)
// values = append(values, test{
// testCases = append(testCases, test{
// name: "double include different printf types allowed",
// code: `
// import "fmt"
@@ -520,7 +520,7 @@ func TestInterpretMany(t *testing.T) {
x1.StringPtr = &s1
x2.StringPtr = &s2
graph.AddVertex(x1, x2)
values = append(values, test{
testCases = append(testCases, test{
name: "double include with variable in parent scope",
code: `
$foo = "hey"
@@ -546,7 +546,7 @@ func TestInterpretMany(t *testing.T) {
x1.StringPtr = &s1
x2.StringPtr = &s2
graph.AddVertex(x1, x2)
values = append(values, test{
testCases = append(testCases, test{
name: "double include with out of order variable in parent scope",
code: `
include c1("t1")
@@ -569,7 +569,7 @@ func TestInterpretMany(t *testing.T) {
s1 := "hello"
x1.StringPtr = &s1
graph.AddVertex(x1)
values = append(values, test{
testCases = append(testCases, test{
name: "duplicate include identical",
code: `
include c1("t1", "hello")
@@ -591,7 +591,7 @@ func TestInterpretMany(t *testing.T) {
s1 := "hello"
x1.StringPtr = &s1
graph.AddVertex(x1)
values = append(values, test{
testCases = append(testCases, test{
name: "duplicate include non-identical",
code: `
include c1("t1", "hello")
@@ -613,7 +613,7 @@ func TestInterpretMany(t *testing.T) {
})
}
{
values = append(values, test{
testCases = append(testCases, test{
name: "duplicate include incompatible",
code: `
include c1("t1", "hello")
@@ -628,7 +628,7 @@ func TestInterpretMany(t *testing.T) {
})
}
{
values = append(values, test{
testCases = append(testCases, test{
name: "class wrong number of args 1",
code: `
include c1("hello") # missing second arg
@@ -642,7 +642,7 @@ func TestInterpretMany(t *testing.T) {
})
}
{
values = append(values, test{
testCases = append(testCases, test{
name: "class wrong number of args 2",
code: `
include c1("hello", 42) # added second arg
@@ -665,7 +665,7 @@ func TestInterpretMany(t *testing.T) {
x1.StringPtr = &s1
x2.StringPtr = &s2
graph.AddVertex(x1, x2)
values = append(values, test{
testCases = append(testCases, test{
name: "nested classes 1",
code: `
import "fmt"
@@ -693,7 +693,7 @@ func TestInterpretMany(t *testing.T) {
})
}
{
values = append(values, test{
testCases = append(testCases, test{
name: "nested classes out of scope 1",
code: `
import "fmt"
@@ -722,7 +722,7 @@ func TestInterpretMany(t *testing.T) {
// TODO: recursive classes are not currently supported (should they be?)
//{
// graph, _ := pgraph.NewGraph("g")
// values = append(values, test{
// testCases = append(testCases, test{
// name: "recursive classes 0",
// code: `
// include c1(0) # start at zero
@@ -744,7 +744,7 @@ func TestInterpretMany(t *testing.T) {
// s0 := "count is 3"
// x0.StringPtr = &s0
// graph.AddVertex(x0)
// values = append(values, test{
// testCases = append(testCases, test{
// name: "recursive classes 1",
// code: `
// import "fmt"
@@ -782,7 +782,7 @@ func TestInterpretMany(t *testing.T) {
// x2.StringPtr = &s2
// x3.StringPtr = &s3
// graph.AddVertex(x0, x1, x2, x3)
// values = append(values, test{
// testCases = append(testCases, test{
// name: "recursive classes 2",
// code: `
// import "fmt"
@@ -810,7 +810,7 @@ func TestInterpretMany(t *testing.T) {
//}
// TODO: remove this test if we ever support recursive classes
{
values = append(values, test{
testCases = append(testCases, test{
name: "recursive classes fail 1",
code: `
import "fmt"
@@ -831,7 +831,7 @@ func TestInterpretMany(t *testing.T) {
}
// TODO: remove this test if we ever support recursive classes
{
values = append(values, test{
testCases = append(testCases, test{
name: "recursive classes fail 2",
code: `
import "fmt"
@@ -861,8 +861,8 @@ func TestInterpretMany(t *testing.T) {
}
names := []string{}
for index, test := range values { // run all the tests
name, code, fail, exp := test.name, test.code, test.fail, test.graph
for index, tc := range testCases { // run all the tests
name, code, fail, exp := tc.name, tc.code, tc.fail, tc.graph
if name == "" {
name = "<sub test not named>"
@@ -874,7 +874,7 @@ func TestInterpretMany(t *testing.T) {
names = append(names, name)
//if index != 3 { // hack to run a subset (useful for debugging)
//if test.name != "nil" {
//if tc.name != "nil" {
// continue
//}

View File

@@ -41,10 +41,10 @@ func TestLexParse0(t *testing.T) {
fail bool
exp interfaces.Stmt
}
values := []test{}
testCases := []test{}
{
values = append(values, test{
testCases = append(testCases, test{
"nil",
``,
false,
@@ -52,7 +52,7 @@ func TestLexParse0(t *testing.T) {
})
}
{
values = append(values, test{
testCases = append(testCases, test{
name: "simple assignment",
code: `$rewsna = -42`,
fail: false,
@@ -69,7 +69,7 @@ func TestLexParse0(t *testing.T) {
})
}
{
values = append(values, test{
testCases = append(testCases, test{
name: "one res",
code: `noop "n1" {}`,
fail: false,
@@ -77,14 +77,14 @@ func TestLexParse0(t *testing.T) {
})
}
{
values = append(values, test{
testCases = append(testCases, test{
name: "res with keyword",
code: `false "n1" {}`, // false is a special keyword
fail: true,
})
}
{
values = append(values, test{
testCases = append(testCases, test{
name: "bad escaping",
code: `
test "t1" {
@@ -95,7 +95,7 @@ func TestLexParse0(t *testing.T) {
})
}
{
values = append(values, test{
testCases = append(testCases, test{
name: "int overflow",
code: `
test "t1" {
@@ -106,7 +106,7 @@ func TestLexParse0(t *testing.T) {
})
}
{
values = append(values, test{
testCases = append(testCases, test{
name: "overflow after lexer",
code: `
test "t1" {
@@ -118,7 +118,7 @@ func TestLexParse0(t *testing.T) {
})
}
{
values = append(values, test{
testCases = append(testCases, test{
name: "one res with param",
code: `
test "t1" {
@@ -130,7 +130,7 @@ func TestLexParse0(t *testing.T) {
})
}
{
values = append(values, test{
testCases = append(testCases, test{
name: "one res with elvis",
code: `
test "t1" {
@@ -145,7 +145,7 @@ func TestLexParse0(t *testing.T) {
}
{
// TODO: skip trailing comma requirement on one-liners
values = append(values, test{
testCases = append(testCases, test{
name: "two lists",
code: `
$somelist = [42, 0, -13,]
@@ -162,7 +162,7 @@ func TestLexParse0(t *testing.T) {
})
}
{
values = append(values, test{
testCases = append(testCases, test{
name: "one map",
code: `
$somemap = {
@@ -175,7 +175,7 @@ func TestLexParse0(t *testing.T) {
})
}
{
values = append(values, test{
testCases = append(testCases, test{
name: "another map",
code: `
$somemap = {
@@ -189,7 +189,7 @@ func TestLexParse0(t *testing.T) {
}
// TODO: alternate possible syntax ?
//{
// values = append(values, test{ // ?
// testCases = append(testCases, test{ // ?
// code: `
// $somestruct = struct{
// foo: "foo1";
@@ -201,7 +201,7 @@ func TestLexParse0(t *testing.T) {
// })
//}
{
values = append(values, test{
testCases = append(testCases, test{
name: "one struct",
code: `
$somestruct = struct{
@@ -214,7 +214,7 @@ func TestLexParse0(t *testing.T) {
})
}
{
values = append(values, test{
testCases = append(testCases, test{
name: "struct with nested struct",
code: `
$somestruct = struct{
@@ -232,7 +232,7 @@ func TestLexParse0(t *testing.T) {
}
// types
{
values = append(values, test{
testCases = append(testCases, test{
name: "some lists",
code: `
$intlist []int = [42, -0, 13,]
@@ -243,7 +243,7 @@ func TestLexParse0(t *testing.T) {
})
}
{
values = append(values, test{
testCases = append(testCases, test{
name: "maps 1",
code: `
# make sure the "str:" part doesn't match a single ident
@@ -257,7 +257,7 @@ func TestLexParse0(t *testing.T) {
})
}
{
values = append(values, test{
testCases = append(testCases, test{
name: "maps 2",
code: `
$mapstrintlist map{str: []int} = {
@@ -271,7 +271,7 @@ func TestLexParse0(t *testing.T) {
})
}
{
values = append(values, test{
testCases = append(testCases, test{
name: "maps and lists",
code: `
$strmap map{str: int} = {
@@ -289,7 +289,7 @@ func TestLexParse0(t *testing.T) {
})
}
{
values = append(values, test{
testCases = append(testCases, test{
name: "some structs",
code: `
$structx struct{a int; b bool; c str} = struct{
@@ -308,7 +308,7 @@ func TestLexParse0(t *testing.T) {
})
}
{
values = append(values, test{
testCases = append(testCases, test{
name: "res with floats",
code: `
test "t1" {
@@ -325,7 +325,7 @@ func TestLexParse0(t *testing.T) {
// ULP away from the largest floating point number of the given size,
// ParseFloat returns f = ±Inf, err.Err = ErrRange.
//{
// values = append(values, test{
// testCases = append(testCases, test{
// name: "overflowing float",
// code: `
// test "t1" {
@@ -336,7 +336,7 @@ func TestLexParse0(t *testing.T) {
// })
//}
{
values = append(values, test{
testCases = append(testCases, test{
name: "res and addition",
code: `
test "t1" {
@@ -359,7 +359,7 @@ func TestLexParse0(t *testing.T) {
},
},
}
values = append(values, test{
testCases = append(testCases, test{
name: "func call 1",
code: `
$x1 = foo1()
@@ -387,7 +387,7 @@ func TestLexParse0(t *testing.T) {
},
},
}
values = append(values, test{
testCases = append(testCases, test{
name: "func call 2",
code: `
$x1 = foo1(13, "hello")
@@ -408,7 +408,7 @@ func TestLexParse0(t *testing.T) {
},
},
}
values = append(values, test{
testCases = append(testCases, test{
name: "func call dotted 1",
code: `
$x1 = pkg.foo1()
@@ -436,7 +436,7 @@ func TestLexParse0(t *testing.T) {
},
},
}
values = append(values, test{
testCases = append(testCases, test{
name: "func call dotted 2",
code: `
$x1 = pkg.foo1(true, "hello")
@@ -446,7 +446,7 @@ func TestLexParse0(t *testing.T) {
})
}
{
values = append(values, test{
testCases = append(testCases, test{
name: "func call dotted invalid 1",
code: `
$x1 = .pkg.foo1(true, "hello")
@@ -455,7 +455,7 @@ func TestLexParse0(t *testing.T) {
})
}
{
values = append(values, test{
testCases = append(testCases, test{
name: "func call dotted invalid 2",
code: `
$x1 = pkg.foo1.(true, "hello")
@@ -464,7 +464,7 @@ func TestLexParse0(t *testing.T) {
})
}
{
values = append(values, test{
testCases = append(testCases, test{
name: "func call dotted invalid 3",
code: `
$x1 = .pkg.foo1.(true, "hello")
@@ -473,7 +473,7 @@ func TestLexParse0(t *testing.T) {
})
}
{
values = append(values, test{
testCases = append(testCases, test{
name: "func call dotted invalid 4",
code: `
$x1 = pkg..foo1(true, "hello")
@@ -492,7 +492,7 @@ func TestLexParse0(t *testing.T) {
},
},
}
values = append(values, test{
testCases = append(testCases, test{
name: "dotted var 1",
code: `
$x1 = $pkg.foo1
@@ -512,7 +512,7 @@ func TestLexParse0(t *testing.T) {
},
},
}
values = append(values, test{
testCases = append(testCases, test{
name: "dotted var 2",
code: `
$x1 = $pkg.foo1.bar
@@ -522,7 +522,7 @@ func TestLexParse0(t *testing.T) {
})
}
{
values = append(values, test{
testCases = append(testCases, test{
name: "invalid dotted var 1",
code: `
$x1 = $.pkg.foo1.bar
@@ -531,7 +531,7 @@ func TestLexParse0(t *testing.T) {
})
}
{
values = append(values, test{
testCases = append(testCases, test{
name: "invalid dotted var 2",
code: `
$x1 = $pkg.foo1.bar.
@@ -540,7 +540,7 @@ func TestLexParse0(t *testing.T) {
})
}
{
values = append(values, test{
testCases = append(testCases, test{
name: "invalid dotted var 3",
code: `
$x1 = $.pkg.foo1.bar.
@@ -549,7 +549,7 @@ func TestLexParse0(t *testing.T) {
})
}
{
values = append(values, test{
testCases = append(testCases, test{
name: "invalid dotted var 4",
code: `
$x1 = $pkg..foo1.bar
@@ -587,7 +587,7 @@ func TestLexParse0(t *testing.T) {
},
},
}
values = append(values, test{
testCases = append(testCases, test{
name: "addition",
code: `
test "t1" {
@@ -639,7 +639,7 @@ func TestLexParse0(t *testing.T) {
},
},
}
values = append(values, test{
testCases = append(testCases, test{
name: "multiple float addition",
code: `
test "t1" {
@@ -691,7 +691,7 @@ func TestLexParse0(t *testing.T) {
},
},
}
values = append(values, test{
testCases = append(testCases, test{
name: "order of operations lucky",
code: `
test "t1" {
@@ -743,7 +743,7 @@ func TestLexParse0(t *testing.T) {
},
},
}
values = append(values, test{
testCases = append(testCases, test{
name: "order of operations needs left precedence",
code: `
test "t1" {
@@ -795,7 +795,7 @@ func TestLexParse0(t *testing.T) {
},
},
}
values = append(values, test{
testCases = append(testCases, test{
name: "order of operations parens",
code: `
test "t1" {
@@ -847,7 +847,7 @@ func TestLexParse0(t *testing.T) {
},
},
}
values = append(values, test{
testCases = append(testCases, test{
name: "order of operations bools",
code: `
test "t1" {
@@ -899,7 +899,7 @@ func TestLexParse0(t *testing.T) {
},
},
}
values = append(values, test{
testCases = append(testCases, test{
name: "order of operations bools reversed",
code: `
test "t1" {
@@ -948,7 +948,7 @@ func TestLexParse0(t *testing.T) {
},
},
}
values = append(values, test{
testCases = append(testCases, test{
name: "order of operations with not",
code: `
test "t1" {
@@ -1000,7 +1000,7 @@ func TestLexParse0(t *testing.T) {
},
},
}
values = append(values, test{
testCases = append(testCases, test{
name: "order of operations logical",
code: `
test "t1" {
@@ -1062,7 +1062,7 @@ func TestLexParse0(t *testing.T) {
},
},
}
values = append(values, test{
testCases = append(testCases, test{
name: "edge stmt",
code: `
test "t1" {
@@ -1079,7 +1079,7 @@ func TestLexParse0(t *testing.T) {
})
}
{
values = append(values, test{
testCases = append(testCases, test{
name: "parser set type incompatibility str",
code: `
$x int = "hello" # type should be str to work
@@ -1091,7 +1091,7 @@ func TestLexParse0(t *testing.T) {
})
}
{
values = append(values, test{
testCases = append(testCases, test{
name: "parser set type incompatibility int",
code: `
$x int = "hello" # value should be int to work
@@ -1131,7 +1131,7 @@ func TestLexParse0(t *testing.T) {
},
},
}
values = append(values, test{
testCases = append(testCases, test{
name: "simple class 1",
code: `
class c1 {
@@ -1174,7 +1174,7 @@ func TestLexParse0(t *testing.T) {
},
},
}
values = append(values, test{
testCases = append(testCases, test{
name: "simple dotted class 1",
code: `
# a dotted identifier only occurs via an imported class
@@ -1219,7 +1219,7 @@ func TestLexParse0(t *testing.T) {
},
},
}
values = append(values, test{
testCases = append(testCases, test{
name: "simple dotted class 2",
code: `
# a dotted identifier only occurs via an imported class
@@ -1236,7 +1236,7 @@ func TestLexParse0(t *testing.T) {
})
}
{
values = append(values, test{
testCases = append(testCases, test{
name: "simple dotted invalid class 1",
code: `
# a dotted identifier only occurs via an imported class
@@ -1247,7 +1247,7 @@ func TestLexParse0(t *testing.T) {
})
}
{
values = append(values, test{
testCases = append(testCases, test{
name: "simple dotted invalid class 2",
code: `
# a dotted identifier only occurs via an imported class
@@ -1258,7 +1258,7 @@ func TestLexParse0(t *testing.T) {
})
}
{
values = append(values, test{
testCases = append(testCases, test{
name: "simple dotted invalid include 1",
code: `
class .foo.c1 {
@@ -1268,7 +1268,7 @@ func TestLexParse0(t *testing.T) {
})
}
{
values = append(values, test{
testCases = append(testCases, test{
name: "simple dotted invalid include 2",
code: `
class foo.c1. {
@@ -1278,7 +1278,7 @@ func TestLexParse0(t *testing.T) {
})
}
{
values = append(values, test{
testCases = append(testCases, test{
name: "simple dotted invalid include 3",
code: `
class .foo.c1. {
@@ -1288,7 +1288,7 @@ func TestLexParse0(t *testing.T) {
})
}
{
values = append(values, test{
testCases = append(testCases, test{
name: "simple dotted invalid include 4",
code: `
class foo..c1 {
@@ -1320,7 +1320,7 @@ func TestLexParse0(t *testing.T) {
},
},
}
values = append(values, test{
testCases = append(testCases, test{
name: "simple class with args 0",
code: `
class x() {
@@ -1334,7 +1334,7 @@ func TestLexParse0(t *testing.T) {
})
}
{
values = append(values, test{
testCases = append(testCases, test{
name: "simple class underscore failure",
code: `
class x_() {
@@ -1401,7 +1401,7 @@ func TestLexParse0(t *testing.T) {
},
},
}
values = append(values, test{
testCases = append(testCases, test{
name: "simple class with args 1",
code: `
class c1($a, $b) {
@@ -1474,7 +1474,7 @@ func TestLexParse0(t *testing.T) {
},
},
}
values = append(values, test{
testCases = append(testCases, test{
name: "simple class with typed args 1",
code: `
class c1($a str, $b) {
@@ -1498,7 +1498,7 @@ func TestLexParse0(t *testing.T) {
},
},
}
values = append(values, test{
testCases = append(testCases, test{
name: "simple import 1",
code: `
import "foo1"
@@ -1516,7 +1516,7 @@ func TestLexParse0(t *testing.T) {
},
},
}
values = append(values, test{
testCases = append(testCases, test{
name: "simple import 2",
code: `
import "foo1" as bar
@@ -1542,7 +1542,7 @@ func TestLexParse0(t *testing.T) {
},
},
}
values = append(values, test{
testCases = append(testCases, test{
name: "simple import 3",
code: `
import "foo1"
@@ -1562,7 +1562,7 @@ func TestLexParse0(t *testing.T) {
},
},
}
values = append(values, test{
testCases = append(testCases, test{
name: "simple import 4",
code: `
import "foo1" as *
@@ -1594,7 +1594,7 @@ func TestLexParse0(t *testing.T) {
},
},
}
values = append(values, test{
testCases = append(testCases, test{
name: "simple import inside class 1",
code: `
class c1 {
@@ -1620,7 +1620,7 @@ func TestLexParse0(t *testing.T) {
},
},
}
values = append(values, test{
testCases = append(testCases, test{
name: "simple function stmt 1",
code: `
func f1() {
@@ -1661,7 +1661,7 @@ func TestLexParse0(t *testing.T) {
},
},
}
values = append(values, test{
testCases = append(testCases, test{
name: "simple function stmt 2",
code: `
func f2() int {
@@ -1712,7 +1712,7 @@ func TestLexParse0(t *testing.T) {
},
},
}
values = append(values, test{
testCases = append(testCases, test{
name: "simple function stmt 3",
code: `
func f3($a int, $b) int {
@@ -1758,7 +1758,7 @@ func TestLexParse0(t *testing.T) {
},
},
}
values = append(values, test{
testCases = append(testCases, test{
name: "simple function stmt 4",
code: `
func f4($x str) str {
@@ -1771,68 +1771,71 @@ func TestLexParse0(t *testing.T) {
}
names := []string{}
for index, test := range values { // run all the tests
name, code, fail, exp := test.name, test.code, test.fail, test.exp
if name == "" {
name = "<sub test not named>"
}
if util.StrInList(name, names) {
t.Errorf("test #%d: duplicate sub test name of: %s", index, name)
for index, tc := range testCases { // run all the tests
if tc.name == "" {
t.Errorf("test #%d: not named", index)
continue
}
names = append(names, name)
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)
//if index != 3 { // hack to run a subset (useful for debugging)
//if (index != 20 && index != 21) {
//if test.name != "nil" {
//if tc.name != "nil" {
// continue
//}
t.Logf("\n\ntest #%d (%s) ----------------\n\n", index, name)
t.Run(fmt.Sprintf("test #%d (%s)", index, tc.name), func(t *testing.T) {
name, code, fail, exp := tc.name, tc.code, tc.fail, tc.exp
str := strings.NewReader(code)
ast, err := LexParse(str)
t.Logf("\n\ntest #%d (%s) ----------------\n\n", index, name)
if !fail && err != nil {
t.Errorf("test #%d: lex/parse failed with: %+v", index, err)
continue
}
if fail && err == nil {
t.Errorf("test #%d: lex/parse passed, expected fail", index)
continue
}
str := strings.NewReader(code)
ast, err := LexParse(str)
if !fail && ast == nil {
t.Errorf("test #%d: lex/parse was nil", index)
continue
}
if !fail && err != nil {
t.Errorf("test #%d: lex/parse failed with: %+v", index, err)
return
}
if fail && err == nil {
t.Errorf("test #%d: lex/parse passed, expected fail", index)
return
}
if exp != nil {
if !reflect.DeepEqual(ast, exp) {
// double check because DeepEqual is different since the func exists
diff := pretty.Compare(ast, exp)
if diff != "" { // bonus
t.Errorf("test #%d: AST did not match expected", index)
// TODO: consider making our own recursive print function
t.Logf("test #%d: actual: \n\n%s\n", index, spew.Sdump(ast))
t.Logf("test #%d: expected: \n\n%s", index, spew.Sdump(exp))
if !fail && ast == nil {
t.Errorf("test #%d: lex/parse was nil", index)
return
}
// more details, for tricky cases:
diffable := &pretty.Config{
Diffable: true,
IncludeUnexported: true,
//PrintStringers: false,
//PrintTextMarshalers: false,
//SkipZeroFields: false,
if exp != nil {
if !reflect.DeepEqual(ast, exp) {
// double check because DeepEqual is different since the func exists
diff := pretty.Compare(ast, exp)
if diff != "" { // bonus
t.Errorf("test #%d: AST did not match expected", index)
// TODO: consider making our own recursive print function
t.Logf("test #%d: actual: \n\n%s\n", index, spew.Sdump(ast))
t.Logf("test #%d: expected: \n\n%s", index, spew.Sdump(exp))
// more details, for tricky cases:
diffable := &pretty.Config{
Diffable: true,
IncludeUnexported: true,
//PrintStringers: false,
//PrintTextMarshalers: false,
//SkipZeroFields: false,
}
t.Logf("test #%d: actual: \n\n%s\n", index, diffable.Sprint(ast))
t.Logf("test #%d: expected: \n\n%s", index, diffable.Sprint(exp))
t.Logf("test #%d: diff:\n%s", index, diff)
return
}
t.Logf("test #%d: actual: \n\n%s\n", index, diffable.Sprint(ast))
t.Logf("test #%d: expected: \n\n%s", index, diffable.Sprint(exp))
t.Logf("test #%d: diff:\n%s", index, diff)
continue
}
}
}
})
}
}

View File

@@ -51,7 +51,7 @@ func TestType0(t *testing.T) {
}
func TestType1(t *testing.T) {
values := map[string]*Type{
testCases := map[string]*Type{
"": nil, // error
"nope": nil, // error
@@ -664,7 +664,7 @@ func TestType1(t *testing.T) {
},
}
for str, val := range values { // run all the tests
for str, val := range testCases { // run all the tests
// for debugging
//if str != "func(str, int) bool" {
@@ -700,7 +700,7 @@ func TestType1(t *testing.T) {
func TestType2(t *testing.T) {
// mapping from golang representation to our expected equivalent
values := map[string]*Type{
testCases := map[string]*Type{
// basic types
"bool": {
Kind: KindBool,
@@ -1180,7 +1180,7 @@ func TestType2(t *testing.T) {
},
}
for str, typ := range values { // run all the tests
for str, typ := range testCases { // run all the tests
// check the type
reflected := typ.Reflect()
@@ -1194,7 +1194,7 @@ func TestType2(t *testing.T) {
func TestType3(t *testing.T) {
// functions with named types...
values := map[string]*Type{
testCases := map[string]*Type{
"func(input str) bool": {
Kind: KindFunc,
Map: map[string]*Type{
@@ -1266,7 +1266,7 @@ func TestType3(t *testing.T) {
},
}
for str, val := range values { // run all the tests
for str, val := range testCases { // run all the tests
// for debugging
//if str != "func(aaa str, bb int) bool" {

View File

@@ -26,7 +26,7 @@ import (
)
func TestPrint1(t *testing.T) {
values := map[Value]string{
testCases := map[Value]string{
&BoolValue{V: true}: "true",
&BoolValue{V: false}: "false",
&StrValue{V: ""}: `""`,
@@ -82,28 +82,28 @@ func TestPrint1(t *testing.T) {
}
d0 := NewMap(NewType("map{str: int}"))
values[d0] = `{}`
testCases[d0] = `{}`
d1 := NewMap(NewType("map{str: int}"))
d1.Add(&StrValue{V: "answer"}, &IntValue{V: 42})
values[d1] = `{"answer": 42}`
testCases[d1] = `{"answer": 42}`
d2 := NewMap(NewType("map{str: int}"))
d2.Add(&StrValue{V: "answer"}, &IntValue{V: 42})
d2.Add(&StrValue{V: "hello"}, &IntValue{V: 13})
values[d2] = `{"answer": 42, "hello": 13}`
testCases[d2] = `{"answer": 42, "hello": 13}`
s0 := NewStruct(NewType("struct{}"))
values[s0] = `struct{}`
testCases[s0] = `struct{}`
s1 := NewStruct(NewType("struct{answer int}"))
values[s1] = `struct{answer: 0}`
testCases[s1] = `struct{answer: 0}`
s2 := NewStruct(NewType("struct{answer int; truth bool; hello str}"))
values[s2] = `struct{answer: 0; truth: false; hello: ""}`
testCases[s2] = `struct{answer: 0; truth: false; hello: ""}`
s3 := NewStruct(NewType("struct{answer int; truth bool; hello str; nested []int}"))
values[s3] = `struct{answer: 0; truth: false; hello: ""; nested: []}`
testCases[s3] = `struct{answer: 0; truth: false; hello: ""; nested: []}`
s4 := NewStruct(NewType("struct{answer int; truth bool; hello str; nested []int}"))
if err := s4.Set("answer", &IntValue{V: 42}); err != nil {
@@ -114,9 +114,9 @@ func TestPrint1(t *testing.T) {
t.Errorf("struct could not set key, error: %v", err)
return
}
values[s4] = `struct{answer: 42; truth: true; hello: ""; nested: []}`
testCases[s4] = `struct{answer: 42; truth: true; hello: ""; nested: []}`
for v, exp := range values { // run all the tests
for v, exp := range testCases { // run all the tests
if s := v.String(); s != exp {
t.Errorf("value representation of `%s` did not match expected: `%s`", s, exp)
}
@@ -125,7 +125,7 @@ func TestPrint1(t *testing.T) {
func TestReflectValue1(t *testing.T) {
// value string representations in golang can be ambiguous, see below...
values := map[Value]string{
testCases := map[Value]string{
&BoolValue{V: true}: "true",
&BoolValue{V: false}: "false",
&StrValue{V: ""}: ``,
@@ -197,26 +197,26 @@ func TestReflectValue1(t *testing.T) {
}
d0 := NewMap(NewType("map{str: int}"))
values[d0] = `map[]`
testCases[d0] = `map[]`
d1 := NewMap(NewType("map{str: int}"))
d1.Add(&StrValue{V: "answer"}, &IntValue{V: 42})
values[d1] = `map[answer:42]`
testCases[d1] = `map[answer:42]`
// multiple key maps are tested below since they have multiple outputs
// TODO: https://github.com/golang/go/issues/21095
s0 := NewStruct(NewType("struct{}"))
values[s0] = `{}`
testCases[s0] = `{}`
s1 := NewStruct(NewType("struct{Answer int}"))
values[s1] = `{Answer:0}`
testCases[s1] = `{Answer:0}`
s2 := NewStruct(NewType("struct{Answer int; Truth bool; Hello str}"))
values[s2] = `{Answer:0 Truth:false Hello:}`
testCases[s2] = `{Answer:0 Truth:false Hello:}`
s3 := NewStruct(NewType("struct{Answer int; Truth bool; Hello str; Nested []int}"))
values[s3] = `{Answer:0 Truth:false Hello: Nested:[]}`
testCases[s3] = `{Answer:0 Truth:false Hello: Nested:[]}`
s4 := NewStruct(NewType("struct{Answer int; Truth bool; Hello str; Nested []int}"))
if err := s4.Set("Answer", &IntValue{V: 42}); err != nil {
@@ -227,9 +227,9 @@ func TestReflectValue1(t *testing.T) {
t.Errorf("struct could not set key, error: %v", err)
return
}
values[s4] = `{Answer:42 Truth:true Hello: Nested:[]}`
testCases[s4] = `{Answer:42 Truth:true Hello: Nested:[]}`
for v, exp := range values { // run all the tests
for v, exp := range testCases { // run all the tests
//t.Logf("expected: %s", exp)
if v == nil {
t.Logf("nil: %s", exp)
@@ -249,7 +249,7 @@ func TestSort1(t *testing.T) {
values []Value
sorted []Value
}
values := []test{
testCases := []test{
{
[]Value{},
[]Value{},
@@ -443,8 +443,8 @@ func TestSort1(t *testing.T) {
// FIXME: add map and struct sorting tests
}
for index, test := range values { // run all the tests
v1, v2 := test.values, test.sorted
for index, tc := range testCases { // run all the tests
v1, v2 := tc.values, tc.sorted
sort.Sort(ValueSlice(v1)) // sort it :)
if l1, l2 := len(v1), len(v2); l1 != l2 {

View File

@@ -36,11 +36,11 @@ func TestUnification1(t *testing.T) {
fail bool
expect map[interfaces.Expr]*types.Type
}
values := []test{}
testCases := []test{}
// this causes a panic, so it can't be used
//{
// values = append(values, test{
// testCases = append(testCases, test{
// "nil",
// nil,
// true, // expect error
@@ -63,7 +63,7 @@ func TestUnification1(t *testing.T) {
},
},
}
values = append(values, test{
testCases = append(testCases, test{
name: "one res",
ast: stmt,
fail: false,
@@ -97,7 +97,7 @@ func TestUnification1(t *testing.T) {
},
},
}
values = append(values, test{
testCases = append(testCases, test{
name: "list of strings",
ast: stmt,
fail: false,
@@ -137,7 +137,7 @@ func TestUnification1(t *testing.T) {
},
},
}
values = append(values, test{
testCases = append(testCases, test{
name: "map of int->float",
ast: stmt,
fail: false,
@@ -179,7 +179,7 @@ func TestUnification1(t *testing.T) {
},
},
}
values = append(values, test{
testCases = append(testCases, test{
name: "simple struct",
ast: stmt,
fail: false,
@@ -227,7 +227,7 @@ func TestUnification1(t *testing.T) {
},
},
}
values = append(values, test{
testCases = append(testCases, test{
name: "func call",
ast: stmt,
fail: false,
@@ -282,7 +282,7 @@ func TestUnification1(t *testing.T) {
},
},
}
values = append(values, test{
testCases = append(testCases, test{
name: "func call, multiple ints",
ast: stmt,
fail: false,
@@ -338,7 +338,7 @@ func TestUnification1(t *testing.T) {
},
},
}
values = append(values, test{
testCases = append(testCases, test{
name: "func call, multiple floats",
ast: stmt,
fail: false,
@@ -372,7 +372,7 @@ func TestUnification1(t *testing.T) {
},
},
}
values = append(values, test{
testCases = append(testCases, test{
name: "assign from func call or two ints",
ast: stmt,
fail: false,
@@ -402,7 +402,7 @@ func TestUnification1(t *testing.T) {
},
},
}
values = append(values, test{
testCases = append(testCases, test{
name: "simple template",
ast: stmt,
fail: false,
@@ -439,7 +439,7 @@ func TestUnification1(t *testing.T) {
},
},
}
values = append(values, test{
testCases = append(testCases, test{
name: "complex template",
ast: stmt,
fail: false,
@@ -470,7 +470,7 @@ func TestUnification1(t *testing.T) {
},
},
}
values = append(values, test{
testCases = append(testCases, test{
name: "single fact unification",
ast: stmt,
fail: true,
@@ -505,7 +505,7 @@ func TestUnification1(t *testing.T) {
},
},
}
values = append(values, test{
testCases = append(testCases, test{
name: "function, wrong arg count",
ast: stmt,
fail: true,
@@ -513,14 +513,18 @@ func TestUnification1(t *testing.T) {
}
names := []string{}
for index, test := range values { // run all the tests
if util.StrInList(test.name, names) {
t.Errorf("test #%d: duplicate sub test name of: %s", index, test.name)
for index, tc := range testCases { // run all the tests
if tc.name == "" {
t.Errorf("test #%d: not named", index)
continue
}
names = append(names, test.name)
t.Run(fmt.Sprintf("test #%d (%s)", index, test.name), func(t *testing.T) {
ast, fail, expect := test.ast, test.fail, test.expect
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 := tc.ast, tc.fail, tc.expect
//str := strings.NewReader(code)
//ast, err := LexParse(str)