diff --git a/hclpack/structure.go b/hclpack/structure.go index 2721d26..4c72be5 100644 --- a/hclpack/structure.go +++ b/hclpack/structure.go @@ -36,7 +36,9 @@ func (b *Body) Content(schema *hcl.BodySchema) (*hcl.BodyContent, hcl.Diagnostic // so callers can type-assert to obtain a child Body in order to serialize it // separately if needed. func (b *Body) PartialContent(schema *hcl.BodySchema) (*hcl.BodyContent, hcl.Body, hcl.Diagnostics) { - remain := &Body{} + remain := &Body{ + MissingItemRange_: b.MissingItemRange_, + } content, diags := b.content(schema, remain) return content, remain, diags } diff --git a/hclpack/structure_test.go b/hclpack/structure_test.go index 78b1037..e99bd2b 100644 --- a/hclpack/structure_test.go +++ b/hclpack/structure_test.go @@ -164,3 +164,43 @@ func TestBodyContent(t *testing.T) { } } + +func TestBodyPartialContent(t *testing.T) { + tests := map[string]struct { + Body *Body + Schema *hcl.BodySchema + WantRemain hcl.Body + }{ + "missing range": { + &Body{ + MissingItemRange_: hcl.Range{ + Filename: "file.hcl", + Start: hcl.Pos{Line: 3, Column: 2}, + End: hcl.Pos{Line: 3, Column: 2}, + }, + }, + &hcl.BodySchema{}, + &Body{ + MissingItemRange_: hcl.Range{ + Filename: "file.hcl", + Start: hcl.Pos{Line: 3, Column: 2}, + End: hcl.Pos{Line: 3, Column: 2}, + }, + }, + }, + } + + for name, test := range tests { + t.Run(name, func(t *testing.T) { + _, gotRemain, diags := test.Body.PartialContent(test.Schema) + for _, diag := range diags { + t.Errorf("unexpected diagnostic: %s", diag.Error()) + } + + if !cmp.Equal(test.WantRemain, gotRemain) { + t.Errorf("wrong remaining result\n%s", cmp.Diff(test.WantRemain, gotRemain)) + } + }) + } + +}