config: tests for the parser

This commit is contained in:
Mitchell Hashimoto 2014-07-22 16:08:19 -07:00
parent de1c23617a
commit 7dfd4f5a3c
3 changed files with 70 additions and 73 deletions

70
config/expr_parse_test.go Normal file
View File

@ -0,0 +1,70 @@
package config
import (
"reflect"
"testing"
)
func TestExprParse(t *testing.T) {
cases := []struct {
Input string
Result Interpolation
Error bool
}{
{
"foo",
nil,
true,
},
{
"var.foo",
&VariableInterpolation{
Variable: &UserVariable{
Name: "foo",
key: "var.foo",
},
},
false,
},
{
"lookup(var.foo, var.bar)",
&FunctionInterpolation{
Func: nil, // Funcs["lookup"]
Args: []Interpolation{
&VariableInterpolation{
Variable: &UserVariable{
Name: "foo",
key: "var.foo",
},
},
&VariableInterpolation{
Variable: &UserVariable{
Name: "bar",
key: "var.bar",
},
},
},
},
false,
},
}
for i, tc := range cases {
actual, err := ExprParse(tc.Input)
if (err != nil) != tc.Error {
t.Fatalf("%d. Error: %s", i, err)
}
// This is jank, but reflect.DeepEqual never has functions
// being the same.
if f, ok := actual.(*FunctionInterpolation); ok {
f.Func = nil
}
if !reflect.DeepEqual(actual, tc.Result) {
t.Fatalf("%d bad: %#v", i, actual)
}
}
}

View File

@ -1,9 +0,0 @@
package config
import (
"testing"
)
func TestExprParse(t *testing.T) {
exprParse(&exprLex{input: `lookup(var.foo)`})
}

View File

@ -6,70 +6,6 @@ import (
"testing"
)
/*
func TestNewInterpolation(t *testing.T) {
cases := []struct {
Input string
Result Interpolation
Error bool
}{
{
"foo",
nil,
true,
},
{
"var.foo",
&VariableInterpolation{
Variable: &UserVariable{
Name: "foo",
key: "var.foo",
},
key: "var.foo",
},
false,
},
{
"lookup(var.foo, var.bar)",
&FunctionInterpolation{
Func: nil, // Funcs["lookup"]
Args: []InterpolatedVariable{
&UserVariable{
Name: "foo",
key: "var.foo",
},
&UserVariable{
Name: "bar",
key: "var.bar",
},
},
key: "lookup(var.foo, var.bar)",
},
false,
},
}
for i, tc := range cases {
actual, err := NewInterpolation(tc.Input)
if (err != nil) != tc.Error {
t.Fatalf("%d. Error: %s", i, err)
}
// This is jank, but reflect.DeepEqual never has functions
// being the same.
if f, ok := actual.(*FunctionInterpolation); ok {
f.Func = nil
}
if !reflect.DeepEqual(actual, tc.Result) {
t.Fatalf("%d bad: %#v", i, actual)
}
}
}
*/
func TestNewInterpolatedVariable(t *testing.T) {
cases := []struct {
Input string