From bff5c091647fae3b022b3aacccc20f3d056aa492 Mon Sep 17 00:00:00 2001 From: Mitchell Hashimoto Date: Tue, 22 Jul 2014 16:16:46 -0700 Subject: [PATCH] config: test nested function calls --- config/expr_parse_test.go | 51 +++++++++++++++++++++++++++++++++++++-- config/interpolate.go | 8 ++++++ 2 files changed, 57 insertions(+), 2 deletions(-) diff --git a/config/expr_parse_test.go b/config/expr_parse_test.go index 41697d8b62..d115191026 100644 --- a/config/expr_parse_test.go +++ b/config/expr_parse_test.go @@ -49,6 +49,39 @@ func TestExprParse(t *testing.T) { }, false, }, + + { + "lookup(var.foo, lookup(var.baz, var.bar))", + &FunctionInterpolation{ + Func: nil, // Funcs["lookup"] + Args: []Interpolation{ + &VariableInterpolation{ + Variable: &UserVariable{ + Name: "foo", + key: "var.foo", + }, + }, + &FunctionInterpolation{ + Func: nil, // Funcs["lookup"] + Args: []Interpolation{ + &VariableInterpolation{ + Variable: &UserVariable{ + Name: "baz", + key: "var.baz", + }, + }, + &VariableInterpolation{ + Variable: &UserVariable{ + Name: "bar", + key: "var.bar", + }, + }, + }, + }, + }, + }, + false, + }, } for i, tc := range cases { @@ -59,8 +92,22 @@ func TestExprParse(t *testing.T) { // This is jank, but reflect.DeepEqual never has functions // being the same. - if f, ok := actual.(*FunctionInterpolation); ok { - f.Func = nil + f, ok := actual.(*FunctionInterpolation) + if ok { + fs := make([]*FunctionInterpolation, 1) + fs[0] = f + for len(fs) > 0 { + f := fs[0] + fs = fs[1:] + + f.Func = nil + for _, a := range f.Args { + f, ok := a.(*FunctionInterpolation) + if ok { + fs = append(fs, f) + } + } + } } if !reflect.DeepEqual(actual, tc.Result) { diff --git a/config/interpolate.go b/config/interpolate.go index 5250627b4b..cffd58b179 100644 --- a/config/interpolate.go +++ b/config/interpolate.go @@ -98,6 +98,10 @@ func (i *FunctionInterpolation) Interpolate( return i.Func(vs, args...) } +func (i *FunctionInterpolation) GoString() string { + return fmt.Sprintf("*%#v", *i) +} + func (i *FunctionInterpolation) Variables() map[string]InterpolatedVariable { result := make(map[string]InterpolatedVariable) for _, a := range i.Args { @@ -130,6 +134,10 @@ func (i *VariableInterpolation) Interpolate( return v, nil } +func (i *VariableInterpolation) GoString() string { + return fmt.Sprintf("*%#v", *i) +} + func (i *VariableInterpolation) Variables() map[string]InterpolatedVariable { return map[string]InterpolatedVariable{i.Variable.FullKey(): i.Variable} }