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

Function TestExpandWithForEachCheck

ext/dynblock/expand_body_test.go:342–423  ·  view source on GitHub ↗
(t *testing.T)

Source from the content-addressed store, hash-verified

340}
341
342func TestExpandWithForEachCheck(t *testing.T) {
343 forEachExpr := hcltest.MockExprLiteral(cty.MapValEmpty(cty.String).Mark("boop"))
344 evalCtx := &hcl.EvalContext{}
345 srcContent := &hcl.BodyContent{
346 Blocks: hcl.Blocks{
347 {
348 Type: "dynamic",
349 Labels: []string{"foo"},
350 LabelRanges: []hcl.Range{{}},
351 Body: hcltest.MockBody(&hcl.BodyContent{
352 Attributes: hcltest.MockAttrs(map[string]hcl.Expression{
353 "for_each": forEachExpr,
354 }),
355 Blocks: hcl.Blocks{
356 {
357 Type: "content",
358 Body: hcltest.MockBody(&hcl.BodyContent{}),
359 },
360 },
361 }),
362 },
363 },
364 }
365 srcBody := hcltest.MockBody(srcContent)
366
367 hookCalled := false
368 var gotV cty.Value
369 var gotEvalCtx *hcl.EvalContext
370
371 expBody := Expand(
372 srcBody, evalCtx,
373 OptCheckForEach(func(v cty.Value, e hcl.Expression, ec *hcl.EvalContext) hcl.Diagnostics {
374 hookCalled = true
375 gotV = v
376 gotEvalCtx = ec
377 return hcl.Diagnostics{
378 &hcl.Diagnostic{
379 Severity: hcl.DiagError,
380 Summary: "Bad for_each",
381 Detail: "I don't like it.",
382 Expression: e,
383 EvalContext: ec,
384 Extra: "diagnostic extra",
385 },
386 }
387 }),
388 )
389
390 _, diags := expBody.Content(&hcl.BodySchema{
391 Blocks: []hcl.BlockHeaderSchema{
392 {
393 Type: "foo",
394 },
395 },
396 })
397 if !diags.HasErrors() {
398 t.Fatal("succeeded; want an error")
399 }

Callers

nothing calls this directly

Calls 8

MockExprLiteralFunction · 0.92
MockBodyFunction · 0.92
MockAttrsFunction · 0.92
ExpandFunction · 0.85
OptCheckForEachFunction · 0.85
HasErrorsMethod · 0.80
ContentMethod · 0.65
ErrorMethod · 0.45

Tested by

no test coverage detected