(t *testing.T)
| 200 | } |
| 201 | |
| 202 | func TestCanFunc(t *testing.T) { |
| 203 | tests := map[string]struct { |
| 204 | expr string |
| 205 | vars map[string]cty.Value |
| 206 | want cty.Value |
| 207 | }{ |
| 208 | "succeeds": { |
| 209 | `can(1)`, |
| 210 | nil, |
| 211 | cty.True, |
| 212 | }, |
| 213 | "fails": { |
| 214 | `can(nope)`, |
| 215 | nil, |
| 216 | cty.False, |
| 217 | }, |
| 218 | "simple unknown": { |
| 219 | `can(unknown)`, |
| 220 | map[string]cty.Value{ |
| 221 | "unknown": cty.UnknownVal(cty.Number), |
| 222 | }, |
| 223 | cty.UnknownVal(cty.Bool), |
| 224 | }, |
| 225 | "traversal through unknown": { |
| 226 | `can(unknown.foo)`, |
| 227 | map[string]cty.Value{ |
| 228 | "unknown": cty.UnknownVal(cty.Map(cty.Number)), |
| 229 | }, |
| 230 | cty.UnknownVal(cty.Bool), |
| 231 | }, |
| 232 | "deep unknown": { |
| 233 | `can(has_unknown)`, |
| 234 | map[string]cty.Value{ |
| 235 | "has_unknown": cty.ListVal([]cty.Value{cty.UnknownVal(cty.Bool)}), |
| 236 | }, |
| 237 | cty.UnknownVal(cty.Bool), |
| 238 | }, |
| 239 | } |
| 240 | |
| 241 | for k, test := range tests { |
| 242 | t.Run(k, func(t *testing.T) { |
| 243 | expr, diags := hclsyntax.ParseExpression([]byte(test.expr), "test.hcl", hcl.Pos{Line: 1, Column: 1}) |
| 244 | if diags.HasErrors() { |
| 245 | t.Fatalf("unexpected problems: %s", diags.Error()) |
| 246 | } |
| 247 | |
| 248 | ctx := &hcl.EvalContext{ |
| 249 | Variables: test.vars, |
| 250 | Functions: map[string]function.Function{ |
| 251 | "can": CanFunc, |
| 252 | }, |
| 253 | } |
| 254 | |
| 255 | got, err := expr.Value(ctx) |
| 256 | if err != nil { |
| 257 | t.Errorf("unexpected error\ngot: %s\nwant: <nil>", err) |
| 258 | } |
| 259 | if !test.want.RawEquals(got) { |
nothing calls this directly
no test coverage detected