Skip to content

Commit 3960231

Browse files
committed
test(engine): assert 2-space indent in encodeYAMLNodeIndented happy path
Previous shape claimed in the docstring to "guard against a refactor that swaps the indent" but only checked for the substring "key: value" — flat top-level mapping has no leading spaces regardless of indent setting, so the assertion was indent-blind. Switch to a nested mapping and assert "\n inner: leaf" with the 2-space prefix, plus an explicit reject of the "\n inner: leaf" 4-space variant (yaml.v3's default when SetIndent is not called). Now a regression that drops SetIndent(2) actually fails this test. Signed-off-by: Aleksei Sviridkin <f@lex.la>
1 parent 43b8ab3 commit 3960231

1 file changed

Lines changed: 33 additions & 8 deletions

File tree

pkg/engine/contract_yaml_encoder_test.go

Lines changed: 33 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -70,16 +70,28 @@ func TestEncodeYAMLNodeIndented_WrapsEncodeError(t *testing.T) {
7070
}
7171
}
7272

73-
// TestEncodeYAMLNodeIndented_HappyPath pins the success contract:
74-
// a small mapping node encodes to 2-space-indented YAML. Guards
75-
// against a refactor that swaps the indent or breaks the encode
76-
// pipeline.
73+
// TestEncodeYAMLNodeIndented_HappyPath pins the success contract
74+
// AND the 2-space indent the helper installs via SetIndent(2). A
75+
// nested mapping is used because indentation only manifests at
76+
// nesting depth ≥ 1: top-level keys never carry leading spaces
77+
// regardless of the indent setting, so a flat mapping cannot
78+
// distinguish 2-space from 4-space output. The nested shape pins
79+
// both the canonical " inner: leaf" line and the absence of any
80+
// 4-space-indented variant — guarding against a refactor that
81+
// swaps SetIndent(2) for SetIndent(4) or drops the call entirely
82+
// (yaml.v3 default is 4).
7783
func TestEncodeYAMLNodeIndented_HappyPath(t *testing.T) {
7884
node := &yaml.Node{
7985
Kind: yaml.MappingNode,
8086
Content: []*yaml.Node{
81-
{Kind: yaml.ScalarNode, Value: "key"},
82-
{Kind: yaml.ScalarNode, Value: "value"},
87+
{Kind: yaml.ScalarNode, Value: "outer"},
88+
{
89+
Kind: yaml.MappingNode,
90+
Content: []*yaml.Node{
91+
{Kind: yaml.ScalarNode, Value: "inner"},
92+
{Kind: yaml.ScalarNode, Value: "leaf"},
93+
},
94+
},
8395
},
8496
}
8597

@@ -89,8 +101,21 @@ func TestEncodeYAMLNodeIndented_HappyPath(t *testing.T) {
89101
}
90102

91103
got := buf.String()
92-
if !strings.Contains(got, "key: value") {
93-
t.Errorf("missing key: value in encoded output: %q", got)
104+
105+
// The nested key must appear with exactly two leading spaces.
106+
// "\n inner: leaf" — anchored on the preceding newline so a
107+
// stray " inner: leaf" (4-space indent) does not satisfy
108+
// the substring match by accident.
109+
const want2Space = "\n inner: leaf"
110+
if !strings.Contains(got, want2Space) {
111+
t.Errorf("encoded output missing expected 2-space indented %q in:\n%s", want2Space, got)
112+
}
113+
114+
// Reject a 4-space match outright. yaml.v3's default indent
115+
// is 4, so this catches the regression that drops
116+
// SetIndent(2).
117+
if strings.Contains(got, "\n inner: leaf") {
118+
t.Errorf("encoded output uses 4-space indent (yaml.v3 default), want 2-space:\n%s", got)
94119
}
95120
}
96121

0 commit comments

Comments
 (0)