| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
package libyaml |
| 8 |
|
| 9 |
import ( |
| 10 |
"errors" |
| 11 |
"testing" |
| 12 |
|
| 13 |
"go.yaml.in/yaml/v4/internal/testutil/assert" |
| 14 |
) |
| 15 |
|
| 16 |
func TestErrors(t *testing.T) { |
| 17 |
RunTestCases(t, "errors.yaml", map[string]TestHandler{ |
| 18 |
"load-error": runLoadErrorTest, |
| 19 |
"dump-error": runDumpErrorTest, |
| 20 |
"emitter-error": runEmitterYAMLErrorTest, |
| 21 |
"writer-error": runWriterYAMLErrorTest, |
| 22 |
"load-errors": runLoadErrorsTest, |
| 23 |
"load-errors-as": runLoadErrorsAsTest, |
| 24 |
"load-errors-is": runLoadErrorsIsTest, |
| 25 |
"type-error": runTypeYAMLErrorTest, |
| 26 |
}) |
| 27 |
} |
| 28 |
|
| 29 |
func runLoadErrorTest(t *testing.T, tc TestCase) { |
| 30 |
t.Helper() |
| 31 |
|
| 32 |
|
| 33 |
errorSpec, ok := tc.From.(map[string]any) |
| 34 |
assert.Truef(t, ok, "from should be map[string]any, got %T", tc.From) |
| 35 |
|
| 36 |
err := buildLoadError(t, errorSpec) |
| 37 |
got := err.Error() |
| 38 |
want, ok := tc.Want.(string) |
| 39 |
assert.Truef(t, ok, "want should be string, got %T", tc.Want) |
| 40 |
|
| 41 |
assert.Equalf(t, want, got, "error message mismatch") |
| 42 |
|
| 43 |
|
| 44 |
if stageStr, ok := errorSpec["stage"].(string); ok { |
| 45 |
assert.Equalf(t, Stage(stageStr), err.Stage, "Stage mismatch") |
| 46 |
} |
| 47 |
|
| 48 |
|
| 49 |
if tc.Also == "unwrap" { |
| 50 |
unwrapped := err.Unwrap() |
| 51 |
if err.err != nil { |
| 52 |
assert.NotNilf(t, unwrapped, "Unwrap() should return non-nil when Err is set") |
| 53 |
assert.Equalf(t, err.err.Error(), unwrapped.Error(), "Unwrap() error message mismatch") |
| 54 |
} else { |
| 55 |
if unwrapped != nil { |
| 56 |
t.Fatalf("Unwrap() should return nil when Err is not set, got %v", unwrapped) |
| 57 |
} |
| 58 |
} |
| 59 |
} |
| 60 |
} |
| 61 |
|
| 62 |
func runDumpErrorTest(t *testing.T, tc TestCase) { |
| 63 |
t.Helper() |
| 64 |
|
| 65 |
errorSpec, ok := tc.From.(map[string]any) |
| 66 |
assert.Truef(t, ok, "from should be map[string]any, got %T", tc.From) |
| 67 |
|
| 68 |
err := buildDumpError(t, errorSpec) |
| 69 |
if err == nil { |
| 70 |
t.Fatal("buildDumpError returned nil") |
| 71 |
} |
| 72 |
got := err.Error() |
| 73 |
want, ok := tc.Want.(string) |
| 74 |
assert.Truef(t, ok, "want should be string, got %T", tc.Want) |
| 75 |
|
| 76 |
assert.Equalf(t, want, got, "error message mismatch") |
| 77 |
|
| 78 |
|
| 79 |
if stageVal, hasStage := errorSpec["stage"]; hasStage { |
| 80 |
stageStr, ok := stageVal.(string) |
| 81 |
assert.Truef(t, ok, "stage should be string, got %T", stageVal) |
| 82 |
if ok { |
| 83 |
assert.Equalf(t, Stage(stageStr), err.Stage, "Stage mismatch") |
| 84 |
} |
| 85 |
} |
| 86 |
|
| 87 |
|
| 88 |
if tc.Also == "unwrap" { |
| 89 |
unwrapped := err.Unwrap() |
| 90 |
if err.err != nil { |
| 91 |
assert.NotNilf(t, unwrapped, "Unwrap() should return non-nil when err is set") |
| 92 |
assert.Equalf(t, err.err.Error(), unwrapped.Error(), "Unwrap() error message mismatch") |
| 93 |
} else { |
| 94 |
if unwrapped != nil { |
| 95 |
t.Fatalf("Unwrap() should return nil when err is not set, got %v", unwrapped) |
| 96 |
} |
| 97 |
} |
| 98 |
} |
| 99 |
} |
| 100 |
|
| 101 |
func buildDumpError(t *testing.T, spec map[string]any) *DumpError { |
| 102 |
t.Helper() |
| 103 |
|
| 104 |
err := &DumpError{ |
| 105 |
Stage: Stage(getString(t, spec, "stage")), |
| 106 |
Message: getString(t, spec, "message"), |
| 107 |
} |
| 108 |
|
| 109 |
|
| 110 |
if errMsg, ok := spec["err"].(string); ok { |
| 111 |
err.err = errors.New(errMsg) |
| 112 |
} |
| 113 |
|
| 114 |
return err |
| 115 |
} |
| 116 |
|
| 117 |
func runEmitterYAMLErrorTest(t *testing.T, tc TestCase) { |
| 118 |
t.Helper() |
| 119 |
|
| 120 |
errorSpec, ok := tc.From.(map[string]any) |
| 121 |
assert.Truef(t, ok, "from should be map[string]any, got %T", tc.From) |
| 122 |
|
| 123 |
message := getString(t, errorSpec, "message") |
| 124 |
err := EmitterError{Message: message} |
| 125 |
|
| 126 |
got := err.Error() |
| 127 |
want, ok := tc.Want.(string) |
| 128 |
assert.Truef(t, ok, "want should be string, got %T", tc.Want) |
| 129 |
assert.Equalf(t, want, got, "error message mismatch") |
| 130 |
} |
| 131 |
|
| 132 |
func runWriterYAMLErrorTest(t *testing.T, tc TestCase) { |
| 133 |
t.Helper() |
| 134 |
|
| 135 |
errorSpec, ok := tc.From.(map[string]any) |
| 136 |
assert.Truef(t, ok, "from should be map[string]any, got %T", tc.From) |
| 137 |
|
| 138 |
message := getString(t, errorSpec, "message") |
| 139 |
err := WriterError{Err: errors.New(message)} |
| 140 |
|
| 141 |
got := err.Error() |
| 142 |
want, ok := tc.Want.(string) |
| 143 |
assert.Truef(t, ok, "want should be string, got %T", tc.Want) |
| 144 |
assert.Equalf(t, want, got, "error message mismatch") |
| 145 |
|
| 146 |
|
| 147 |
if tc.Also == "unwrap" { |
| 148 |
unwrapped := err.Unwrap() |
| 149 |
assert.NotNilf(t, unwrapped, "Unwrap() should return non-nil") |
| 150 |
assert.Equalf(t, message, unwrapped.Error(), "Unwrap() error message mismatch") |
| 151 |
} |
| 152 |
} |
| 153 |
|
| 154 |
func runLoadErrorsTest(t *testing.T, tc TestCase) { |
| 155 |
t.Helper() |
| 156 |
|
| 157 |
errorSpec, ok := tc.From.(map[string]any) |
| 158 |
assert.Truef(t, ok, "from should be map[string]any, got %T", tc.From) |
| 159 |
|
| 160 |
errList := buildLoadErrorList(t, errorSpec) |
| 161 |
err := &LoadErrors{Errors: errList} |
| 162 |
|
| 163 |
got := err.Error() |
| 164 |
want, ok := tc.Want.(string) |
| 165 |
assert.Truef(t, ok, "want should be string, got %T", tc.Want) |
| 166 |
|
| 167 |
assert.Equalf(t, want, got, "error message mismatch") |
| 168 |
} |
| 169 |
|
| 170 |
func runLoadErrorsAsTest(t *testing.T, tc TestCase) { |
| 171 |
t.Helper() |
| 172 |
|
| 173 |
errorSpec, ok := tc.From.(map[string]any) |
| 174 |
assert.Truef(t, ok, "from should be map[string]any, got %T", tc.From) |
| 175 |
|
| 176 |
errList := buildLoadErrorList(t, errorSpec) |
| 177 |
err := &LoadErrors{Errors: errList} |
| 178 |
|
| 179 |
switch tc.As { |
| 180 |
case "TypeError": |
| 181 |
var target *TypeError |
| 182 |
gotAs := errors.As(err, &target) |
| 183 |
assert.Equalf(t, tc.WantAs, gotAs, "errors.As result mismatch") |
| 184 |
|
| 185 |
if tc.WantAs && target != nil { |
| 186 |
assert.Equalf(t, len(tc.WantMessages), len(target.Errors), "TypeError.Errors length mismatch") |
| 187 |
for i, wantMsg := range tc.WantMessages { |
| 188 |
wantStr, ok := wantMsg.(string) |
| 189 |
assert.Truef(t, ok, "want_messages[%d] should be string, got %T", i, wantMsg) |
| 190 |
assert.Equalf(t, wantStr, target.Errors[i], "TypeError.Errors[%d] mismatch", i) |
| 191 |
} |
| 192 |
} |
| 193 |
|
| 194 |
default: |
| 195 |
t.Fatalf("unknown as type: %s", tc.As) |
| 196 |
} |
| 197 |
} |
| 198 |
|
| 199 |
func runLoadErrorsIsTest(t *testing.T, tc TestCase) { |
| 200 |
t.Helper() |
| 201 |
|
| 202 |
errorSpec, ok := tc.From.(map[string]any) |
| 203 |
assert.Truef(t, ok, "from should be map[string]any, got %T", tc.From) |
| 204 |
|
| 205 |
errList := buildLoadErrorList(t, errorSpec) |
| 206 |
err := &LoadErrors{Errors: errList} |
| 207 |
|
| 208 |
|
| 209 |
gotIs := false |
| 210 |
for _, cerr := range err.Errors { |
| 211 |
if cerr.err != nil && cerr.err.Error() == tc.Is { |
| 212 |
gotIs = true |
| 213 |
break |
| 214 |
} |
| 215 |
} |
| 216 |
|
| 217 |
assert.Equalf(t, tc.WantIs, gotIs, "errors.Is result mismatch") |
| 218 |
} |
| 219 |
|
| 220 |
func runTypeYAMLErrorTest(t *testing.T, tc TestCase) { |
| 221 |
t.Helper() |
| 222 |
|
| 223 |
errorSpec, ok := tc.From.(map[string]any) |
| 224 |
assert.Truef(t, ok, "from should be map[string]any, got %T", tc.From) |
| 225 |
|
| 226 |
errorMsgs := getStringSlice(t, errorSpec, "errors") |
| 227 |
err := &TypeError{Errors: errorMsgs} |
| 228 |
|
| 229 |
got := err.Error() |
| 230 |
want, ok := tc.Want.(string) |
| 231 |
assert.Truef(t, ok, "want should be string, got %T", tc.Want) |
| 232 |
|
| 233 |
assert.Equalf(t, want, got, "error message mismatch") |
| 234 |
} |
| 235 |
|
| 236 |
|
| 237 |
|
| 238 |
func buildLoadError(t *testing.T, spec map[string]any) *LoadError { |
| 239 |
t.Helper() |
| 240 |
|
| 241 |
err := &LoadError{ |
| 242 |
Stage: Stage(getString(t, spec, "stage")), |
| 243 |
Mark: buildMark(t, spec, "mark"), |
| 244 |
Message: getString(t, spec, "message"), |
| 245 |
} |
| 246 |
|
| 247 |
|
| 248 |
if contextMsg, ok := spec["context_message"].(string); ok { |
| 249 |
err.ContextMsg = contextMsg |
| 250 |
err.ContextMark = buildMark(t, spec, "context_mark") |
| 251 |
} |
| 252 |
|
| 253 |
|
| 254 |
if errMsg, ok := spec["err"].(string); ok { |
| 255 |
err.err = errors.New(errMsg) |
| 256 |
} |
| 257 |
|
| 258 |
return err |
| 259 |
} |
| 260 |
|
| 261 |
func buildMark(t *testing.T, spec map[string]any, key string) Mark { |
| 262 |
t.Helper() |
| 263 |
|
| 264 |
markSpec, ok := spec[key].(map[string]any) |
| 265 |
if !ok { |
| 266 |
return Mark{} |
| 267 |
} |
| 268 |
|
| 269 |
return Mark{ |
| 270 |
Line: getInt(t, markSpec, "line"), |
| 271 |
Column: getInt(t, markSpec, "column"), |
| 272 |
Index: getInt(t, markSpec, "index"), |
| 273 |
} |
| 274 |
} |
| 275 |
|
| 276 |
func buildLoadErrorList(t *testing.T, spec map[string]any) []*LoadError { |
| 277 |
t.Helper() |
| 278 |
|
| 279 |
errorsSpec, ok := spec["errors"].([]any) |
| 280 |
if !ok { |
| 281 |
return nil |
| 282 |
} |
| 283 |
|
| 284 |
var result []*LoadError |
| 285 |
for _, errSpec := range errorsSpec { |
| 286 |
errMap, ok := errSpec.(map[string]any) |
| 287 |
assert.Truef(t, ok, "error spec should be map[string]any") |
| 288 |
|
| 289 |
line := getInt(t, errMap, "line") |
| 290 |
message := getString(t, errMap, "message") |
| 291 |
|
| 292 |
result = append(result, &LoadError{ |
| 293 |
Stage: ConstructorStage, |
| 294 |
Mark: Mark{Line: line}, |
| 295 |
Message: message, |
| 296 |
err: errors.New(message), |
| 297 |
}) |
| 298 |
} |
| 299 |
|
| 300 |
return result |
| 301 |
} |
| 302 |
|
| 303 |
func getString(t *testing.T, spec map[string]any, key string) string { |
| 304 |
t.Helper() |
| 305 |
v, ok := spec[key] |
| 306 |
if !ok { |
| 307 |
return "" |
| 308 |
} |
| 309 |
s, ok := v.(string) |
| 310 |
assert.Truef(t, ok, "%s should be string, got %T", key, v) |
| 311 |
return s |
| 312 |
} |
| 313 |
|
| 314 |
func getInt(t *testing.T, spec map[string]any, key string) int { |
| 315 |
t.Helper() |
| 316 |
v, ok := spec[key] |
| 317 |
if !ok { |
| 318 |
return 0 |
| 319 |
} |
| 320 |
i, ok := v.(int) |
| 321 |
assert.Truef(t, ok, "%s should be int, got %T", key, v) |
| 322 |
return i |
| 323 |
} |
| 324 |
|
| 325 |
func getBool(t *testing.T, spec map[string]any, key string) bool { |
| 326 |
t.Helper() |
| 327 |
v, ok := spec[key] |
| 328 |
if !ok { |
| 329 |
return false |
| 330 |
} |
| 331 |
b, ok := v.(bool) |
| 332 |
assert.Truef(t, ok, "%s should be bool, got %T", key, v) |
| 333 |
return b |
| 334 |
} |
| 335 |
|
| 336 |
func getStringSlice(t *testing.T, spec map[string]any, key string) []string { |
| 337 |
t.Helper() |
| 338 |
v, ok := spec[key] |
| 339 |
if !ok { |
| 340 |
return nil |
| 341 |
} |
| 342 |
slice, ok := v.([]any) |
| 343 |
assert.Truef(t, ok, "%s should be []any, got %T", key, v) |
| 344 |
|
| 345 |
var result []string |
| 346 |
for i, item := range slice { |
| 347 |
s, ok := item.(string) |
| 348 |
assert.Truef(t, ok, "%s[%d] should be string, got %T", key, i, item) |
| 349 |
result = append(result, s) |
| 350 |
} |
| 351 |
return result |
| 352 |
} |