forked from influxdata/flux
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcompile_internal_test.go
120 lines (115 loc) · 2.78 KB
/
compile_internal_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
package flux
import (
"errors"
"testing"
"github.com/google/go-cmp/cmp"
"github.com/influxdata/flux/ast"
"github.com/influxdata/flux/interpreter"
"github.com/influxdata/flux/values"
)
func TestValidatePackageBuiltins(t *testing.T) {
testCases := []struct {
name string
pkg *interpreter.Package
astPkg *ast.Package
err error
}{
{
name: "no errors",
pkg: interpreter.NewPackageWithValues("test", values.NewObjectWithValues(map[string]values.Value{
"foo": values.NewInt(0),
})),
astPkg: &ast.Package{
Files: []*ast.File{{
Body: []ast.Statement{
&ast.BuiltinStatement{
ID: &ast.Identifier{Name: "foo"},
},
},
}},
},
},
{
name: "extra values",
pkg: interpreter.NewPackageWithValues("test", values.NewObjectWithValues(map[string]values.Value{
"foo": values.NewInt(0),
})),
astPkg: &ast.Package{},
err: errors.New("missing builtin values [], extra builtin values [foo]"),
},
{
name: "missing values",
pkg: interpreter.NewPackageWithValues("test", values.NewObject()),
astPkg: &ast.Package{
Files: []*ast.File{{
Body: []ast.Statement{
&ast.BuiltinStatement{
ID: &ast.Identifier{Name: "foo"},
},
},
}},
},
err: errors.New("missing builtin values [foo], extra builtin values []"),
},
{
name: "missing and values",
pkg: interpreter.NewPackageWithValues("test", values.NewObjectWithValues(map[string]values.Value{
"foo": values.NewInt(0),
"bar": values.NewInt(0),
})),
astPkg: &ast.Package{
Files: []*ast.File{{
Body: []ast.Statement{
&ast.BuiltinStatement{
ID: &ast.Identifier{Name: "foo"},
},
&ast.BuiltinStatement{
ID: &ast.Identifier{Name: "baz"},
},
},
}},
},
err: errors.New("missing builtin values [baz], extra builtin values [bar]"),
},
}
for _, tc := range testCases {
tc := tc
t.Run(tc.name, func(t *testing.T) {
err := validatePackageBuiltins(tc.pkg, tc.astPkg)
switch {
case err == nil && tc.err == nil:
// Test passes
case err == nil && tc.err != nil:
t.Errorf("expected error %v", tc.err)
case err != nil && tc.err == nil:
t.Errorf("unexpected error %v", err)
case err != nil && tc.err != nil:
if err.Error() != tc.err.Error() {
t.Errorf("differing error messages -want/+got:\n%s", cmp.Diff(tc.err.Error(), err.Error()))
}
// else test passes
}
})
}
}
func Test_options(t *testing.T) {
pkg := &ast.Package{
Files: []*ast.File{
{
Body: []ast.Statement{
&ast.VariableAssignment{},
&ast.OptionStatement{},
},
},
{
Body: []ast.Statement{
&ast.VariableAssignment{},
},
},
},
}
actual := options(pkg)
if len(actual.Files) != 1 || len(actual.Files[0].Body) != 1 {
t.Fail()
}
}