MCPcopy Create free account
hub / github.com/github/gh-aw / TestFormatYAMLError

Function TestFormatYAMLError

pkg/parser/yaml_error_test.go:15–97  ·  view source on GitHub ↗

TestFormatYAMLError tests the new FormatYAMLError function that uses yaml.FormatError()

(t *testing.T)

Source from the content-addressed store, hash-verified

13
14// TestFormatYAMLError tests the new FormatYAMLError function that uses yaml.FormatError()
15func TestFormatYAMLError(t *testing.T) {
16 tests := []struct {
17 name string
18 yamlContent string
19 frontmatterLineOffset int
20 expectedLineCol string // Expected [line:col] format in output
21 expectSourceContext bool // Should contain source code lines with | markers
22 expectVisualPointer bool // Should contain visual ^ pointer
23 }{
24 {
25 name: "invalid mapping with offset 1",
26 yamlContent: "invalid: yaml: syntax",
27 frontmatterLineOffset: 1,
28 expectedLineCol: "[1:10]",
29 expectSourceContext: true,
30 expectVisualPointer: true,
31 },
32 {
33 name: "invalid mapping with offset 5",
34 yamlContent: "invalid: yaml: syntax",
35 frontmatterLineOffset: 5,
36 expectedLineCol: "[5:10]",
37 expectSourceContext: true,
38 expectVisualPointer: true,
39 },
40 {
41 name: "indentation error",
42 yamlContent: "name: test\n invalid_indentation: here",
43 frontmatterLineOffset: 3,
44 expectedLineCol: "[3:",
45 expectSourceContext: true,
46 expectVisualPointer: true,
47 },
48 {
49 name: "duplicate key",
50 yamlContent: "name: test\nname: duplicate",
51 frontmatterLineOffset: 2,
52 expectedLineCol: "[3:1]",
53 expectSourceContext: true,
54 expectVisualPointer: true,
55 },
56 }
57
58 for _, tt := range tests {
59 t.Run(tt.name, func(t *testing.T) {
60 // Generate an actual goccy/go-yaml error
61 var result map[string]any
62 err := yaml.Unmarshal([]byte(tt.yamlContent), &result)
63
64 if err == nil {
65 t.Errorf("Expected YAML parsing to fail for content: %q", tt.yamlContent)
66 return
67 }
68
69 // Format the error with the new function
70 formatted := FormatYAMLError(err, tt.frontmatterLineOffset, tt.yamlContent)
71
72 // Check for expected [line:col] format

Callers

nothing calls this directly

Calls 3

FormatYAMLErrorFunction · 0.85
RunMethod · 0.45
ErrorfMethod · 0.45

Tested by

no test coverage detected