Debian Codegraph @

internal/libyaml/reader_test.go

raw symbols SCIP
1 // Copyright 2025 The go-yaml Project Contributors
2 // SPDX-License-Identifier: Apache-2.0
3
4 // Tests for the input reader.
5 // Verifies BOM detection, encoding handling, and buffered input operations.
6
7 package libyaml
8
9 import (
10 "fmt"
11 "testing"
12
13 "go.yaml.in/yaml/v4/internal/testutil/assert"
14 )
15
16 func TestReader(t *testing.T) {
17 RunTestCases(t, "reader.yaml", map[string]TestHandler{
18 "reader-set-error": runReaderSetErrorTest,
19 "reader-determine-encoding": runReaderDetermineEncodingTest,
20 "reader-update-raw-buffer": runReaderUpdateRawBufferTest,
21 "reader-update-buffer": runReaderUpdateBufferTest,
22 "reader-panic": runReaderPanicTest,
23 })
24 }
25
26 func runReaderSetErrorTest(t *testing.T, tc TestCase) {
27 t.Helper()
28
29 parser := NewParser()
30
31 // args should be [problem, offset, value]
32 assert.Truef(t, len(tc.Args) >= 3, "Args should have at least 3 elements, got %d", len(tc.Args))
33 problem, ok := tc.Args[0].(string)
34 assert.Truef(t, ok, "Args[0] should be string, got %T", tc.Args[0])
35 offset, ok := tc.Args[1].(int)
36 assert.Truef(t, ok, "Args[1] should be int, got %T", tc.Args[1])
37 value, ok := tc.Args[2].(int)
38 assert.Truef(t, ok, "Args[2] should be int, got %T", tc.Args[2])
39
40 // Format message with value if provided
41 message := problem
42 if value != -1 {
43 message = fmt.Sprintf("%s (value: %d)", problem, value)
44 }
45 err := formatReaderError(message, Mark{Index: offset})
46
47 // Check return value: Want is a bool where true means success (no error).
48 want, ok := tc.Want.(bool)
49 assert.Truef(t, ok, "Want should be bool, got %T", tc.Want)
50 if want {
51 assert.NoErrorf(t, err, "setReaderError() returned error: %v", err)
52 } else {
53 assert.NotNilf(t, err, "setReaderError() = nil, want error")
54 }
55
56 // Run field checks
57 runFieldChecks(t, &parser, tc.Checks)
58 }
59
60 func runReaderDetermineEncodingTest(t *testing.T, tc TestCase) {
61 t.Helper()
62
63 parser := NewParser()
64 parser.SetInputString(tc.Input)
65
66 err := parser.determineEncoding()
67
68 // Check return value (defaults to true)
69 want := WantBool(t, tc.Want, true)
70 if want {
71 assert.NoErrorf(t, err, "determineEncoding() returned error: %v", err)
72 } else {
73 assert.NotNilf(t, err, "determineEncoding() = nil, want error")
74 }
75
76 // Run field checks
77 runFieldChecks(t, &parser, tc.Checks)
78 }
79
80 func runReaderUpdateRawBufferTest(t *testing.T, tc TestCase) {
81 t.Helper()
82
83 parser := NewParser()
84 parser.SetInputString(tc.Input)
85
86 // Apply any setup
87 if tc.Setup != nil {
88 applySetup(t, &parser, tc.Setup)
89 }
90
91 err := parser.updateRawBuffer()
92
93 // Check return value (defaults to true)
94 want := WantBool(t, tc.Want, true)
95 if want {
96 assert.NoErrorf(t, err, "updateRawBuffer() returned error: %v", err)
97 } else {
98 assert.NotNilf(t, err, "updateRawBuffer() = nil, want error")
99 }
100
101 // Run field checks
102 runFieldChecks(t, &parser, tc.Checks)
103 }
104
105 func runReaderUpdateBufferTest(t *testing.T, tc TestCase) {
106 t.Helper()
107
108 parser := NewParser()
109 parser.SetInputString(tc.Input)
110
111 // Apply any setup
112 if tc.Setup != nil {
113 applySetup(t, &parser, tc.Setup)
114 }
115
116 // Get the length argument
117 assert.Truef(t, len(tc.Args) >= 1, "Args should have at least 1 element, got %d", len(tc.Args))
118 length, ok := tc.Args[0].(int)
119 assert.Truef(t, ok, "Args[0] should be int, got %T", tc.Args[0])
120
121 err := parser.updateBuffer(length)
122
123 // Check return value (defaults to true)
124 want := WantBool(t, tc.Want, true)
125 if want {
126 assert.NoErrorf(t, err, "updateBuffer(%d) returned error: %v", length, err)
127 } else {
128 assert.NotNilf(t, err, "updateBuffer(%d) = nil, want error", length)
129 }
130
131 // Run field checks
132 runFieldChecks(t, &parser, tc.Checks)
133 }
134
135 func runReaderPanicTest(t *testing.T, tc TestCase) {
136 t.Helper()
137
138 parser := NewParser()
139
140 wantMsg, ok := tc.Want.(string)
141 assert.Truef(t, ok, "Want should be string, got %T", tc.Want)
142
143 assert.PanicMatchesf(t, wantMsg, func() {
144 switch tc.Function {
145 case "updateBuffer":
146 assert.Truef(t, len(tc.Args) >= 1, "Args should have at least 1 element, got %d", len(tc.Args))
147 length, ok := tc.Args[0].(int)
148 assert.Truef(t, ok, "Args[0] should be int, got %T", tc.Args[0])
149 parser.updateBuffer(length)
150 default:
151 t.Fatalf("unknown function: %s", tc.Function)
152 }
153 }, "Expected panic: %s", wantMsg)
154 }
155
156 func applySetup(t *testing.T, parser *Parser, setup any) {
157 t.Helper()
158
159 if setup == nil {
160 return
161 }
162
163 setupMap, ok := setup.(map[string]any)
164 if !ok {
165 t.Fatalf("setup must be a map, got %T", setup)
166 }
167
168 for key, value := range setupMap {
169 switch key {
170 case "eof":
171 boolVal, ok := value.(bool)
172 assert.Truef(t, ok, "setup.eof should be bool, got %T", value)
173 parser.eof = boolVal
174 default:
175 t.Fatalf("unknown setup key: %s", key)
176 }
177 }
178 }