MCPcopy
hub / github.com/hashicorp/hcl / TestTryFunc

Function TestTryFunc

ext/tryfunc/tryfunc_test.go:15–200  ·  view source on GitHub ↗
(t *testing.T)

Source from the content-addressed store, hash-verified

13)
14
15func TestTryFunc(t *testing.T) {
16 tests := map[string]struct {
17 expr string
18 vars map[string]cty.Value
19 want cty.Value
20 wantErr string
21 }{
22 "one argument succeeds": {
23 `try(1)`,
24 nil,
25 cty.NumberIntVal(1),
26 ``,
27 },
28 "one marked argument succeeds": {
29 `try(sensitive)`,
30 map[string]cty.Value{
31 "sensitive": cty.StringVal("secret").Mark("porpoise"),
32 },
33 cty.StringVal("secret").Mark("porpoise"),
34 ``,
35 },
36 "two arguments, first succeeds": {
37 `try(1, 2)`,
38 nil,
39 cty.NumberIntVal(1),
40 ``,
41 },
42 "two arguments, first fails": {
43 `try(nope, 2)`,
44 nil,
45 cty.NumberIntVal(2),
46 ``,
47 },
48 "two arguments, first depends on unknowns": {
49 `try(unknown, 2)`,
50 map[string]cty.Value{
51 "unknown": cty.UnknownVal(cty.Number),
52 },
53 cty.DynamicVal, // can't proceed until first argument is known
54 ``,
55 },
56 "two arguments, first succeeds and second depends on unknowns": {
57 `try(1, unknown)`,
58 map[string]cty.Value{
59 "unknown": cty.UnknownVal(cty.Number),
60 },
61 cty.NumberIntVal(1), // we know 1st succeeds, so it doesn't matter that 2nd is unknown
62 ``,
63 },
64 "two arguments, first depends on unknowns deeply": {
65 `try(has_unknowns, 2)`,
66 map[string]cty.Value{
67 "has_unknowns": cty.ListVal([]cty.Value{cty.UnknownVal(cty.Bool)}),
68 },
69 cty.DynamicVal, // can't proceed until first argument is wholly known
70 ``,
71 },
72 "two arguments, first traverses through an unkown": {

Callers

nothing calls this directly

Calls 5

ParseExpressionFunction · 0.92
RunMethod · 0.80
HasErrorsMethod · 0.80
ValueMethod · 0.65
ErrorMethod · 0.45

Tested by

no test coverage detected