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 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174
|
package userfunc
import (
"fmt"
"testing"
"github.com/hashicorp/hcl/v2"
"github.com/hashicorp/hcl/v2/hclsyntax"
"github.com/zclconf/go-cty/cty"
)
func TestDecodeUserFunctions(t *testing.T) {
tests := []struct {
src string
testExpr string
baseCtx *hcl.EvalContext
want cty.Value
diagCount int
}{
{
`
function "greet" {
params = [name]
result = "Hello, ${name}."
}
`,
`greet("Ermintrude")`,
nil,
cty.StringVal("Hello, Ermintrude."),
0,
},
{
`
function "greet" {
params = [name]
result = "Hello, ${name}."
}
`,
`greet()`,
nil,
cty.DynamicVal,
1, // missing value for "name"
},
{
`
function "greet" {
params = [name]
result = "Hello, ${name}."
}
`,
`greet("Ermintrude", "extra")`,
nil,
cty.DynamicVal,
1, // too many arguments
},
{
`
function "add" {
params = [a, b]
result = a + b
}
`,
`add(1, 5)`,
nil,
cty.NumberIntVal(6),
0,
},
{
`
function "argstuple" {
params = []
variadic_param = args
result = args
}
`,
`argstuple("a", true, 1)`,
nil,
cty.TupleVal([]cty.Value{cty.StringVal("a"), cty.True, cty.NumberIntVal(1)}),
0,
},
{
`
function "missing_var" {
params = []
result = nonexist
}
`,
`missing_var()`,
nil,
cty.DynamicVal,
1, // no variable named "nonexist"
},
{
`
function "closure" {
params = []
result = upvalue
}
`,
`closure()`,
&hcl.EvalContext{
Variables: map[string]cty.Value{
"upvalue": cty.True,
},
},
cty.True,
0,
},
{
`
function "neg" {
params = [val]
result = -val
}
function "add" {
params = [a, b]
result = a + b
}
`,
`neg(add(1, 3))`,
nil,
cty.NumberIntVal(-4),
0,
},
{
`
function "neg" {
parrams = [val]
result = -val
}
`,
`null`,
nil,
cty.NullVal(cty.DynamicPseudoType),
2, // missing attribute "params", and unknown attribute "parrams"
},
}
for i, test := range tests {
t.Run(fmt.Sprintf("%02d", i), func(t *testing.T) {
f, diags := hclsyntax.ParseConfig([]byte(test.src), "config", hcl.Pos{Line: 1, Column: 1})
if f == nil || f.Body == nil {
t.Fatalf("got nil file or body")
}
funcs, _, funcsDiags := decodeUserFunctions(f.Body, "function", func() *hcl.EvalContext {
return test.baseCtx
})
diags = append(diags, funcsDiags...)
expr, exprParseDiags := hclsyntax.ParseExpression([]byte(test.testExpr), "testexpr", hcl.Pos{Line: 1, Column: 1})
diags = append(diags, exprParseDiags...)
if expr == nil {
t.Fatalf("parsing test expr returned nil")
}
got, exprDiags := expr.Value(&hcl.EvalContext{
Functions: funcs,
})
diags = append(diags, exprDiags...)
if len(diags) != test.diagCount {
t.Errorf("wrong number of diagnostics %d; want %d", len(diags), test.diagCount)
for _, diag := range diags {
t.Logf("- %s", diag)
}
}
if !got.RawEquals(test.want) {
t.Errorf("wrong result\ngot: %#v\nwant: %#v", got, test.want)
}
})
}
}
|