Debian Codegraph @

internal/libyaml/yaml_test.go

raw symbols SCIP
1 // Copyright 2025 The go-yaml Project Contributors
2 // SPDX-License-Identifier: Apache-2.0
3
4 // Tests for core libyaml types.
5 // Verifies Event, Token, and other fundamental type operations.
6
7 package libyaml
8
9 import (
10 "testing"
11
12 "go.yaml.in/yaml/v4/internal/testutil/assert"
13 )
14
15 func TestYAML(t *testing.T) {
16 RunTestCases(t, "yaml.yaml", map[string]TestHandler{
17 "enum-string": runEnumStringTest,
18 "style-accessor": runStyleAccessorTest,
19 })
20 }
21
22 // runEnumStringTest tests the String() method of various enum types.
23 //
24 //nolint:thelper // because this function is the real test
25 func runEnumStringTest(t *testing.T, tc TestCase) {
26 // Parse enum array: [Type, Value]
27 if len(tc.Enum) != 2 {
28 t.Fatalf("enum must be [Type, Value], got %v", tc.Enum)
29 }
30 enumType, ok := tc.Enum[0].(string)
31 if !ok {
32 t.Fatalf("enum type must be string, got %T", tc.Enum[0])
33 }
34
35 // Value can be int or string constant
36 var enumValue int
37 switch v := tc.Enum[1].(type) {
38 case int:
39 enumValue = v
40 case string:
41 // Parse as constant - this will be resolved by the constant lookup
42 enumValue = resolveConstant(t, v)
43 default:
44 t.Fatalf("enum value must be int or string, got %T", tc.Enum[1])
45 }
46
47 var got string
48 switch enumType {
49 case "ScalarStyle":
50 got = ScalarStyle(enumValue).String()
51 case "TokenType":
52 got = TokenType(enumValue).String()
53 case "EventType":
54 got = EventType(enumValue).String()
55 case "ParserState":
56 got = ParserState(enumValue).String()
57 default:
58 t.Fatalf("unknown enum type: %s", enumType)
59 }
60
61 // Want can be either a string or a single-element sequence
62 var want string
63 switch v := tc.Want.(type) {
64 case string:
65 want = v
66 case []any:
67 if len(v) > 0 {
68 var ok bool
69 want, ok = v[0].(string)
70 if !ok {
71 t.Fatalf("want[0] must be string, got %T", v[0])
72 }
73 } else {
74 t.Fatalf("Want slice is empty, expected at least one element")
75 }
76 default:
77 t.Fatalf("want must be a string or sequence, got %T", tc.Want)
78 }
79 assert.Equalf(t, want, got, "%s(%d).String() = %q, want %q", enumType, enumValue, got, want)
80 }
81
82 // runStyleAccessorTest tests the style accessor methods of Event.
83 //
84 //nolint:thelper // because this function is the real test
85 func runStyleAccessorTest(t *testing.T, tc TestCase) {
86 // Parse test array: [Method, STYLE]
87 if len(tc.StyleTest) != 2 {
88 t.Fatalf("test must be [Method, STYLE], got %v", tc.StyleTest)
89 }
90 method, ok := tc.StyleTest[0].(string)
91 if !ok {
92 t.Fatalf("method must be string, got %T", tc.StyleTest[0])
93 }
94
95 // Style value can be int or string constant
96 var styleValue int
97 switch v := tc.StyleTest[1].(type) {
98 case int:
99 styleValue = v
100 case string:
101 styleValue = resolveConstant(t, v)
102 default:
103 t.Fatalf("style value must be int or string, got %T", tc.StyleTest[1])
104 }
105
106 event := Event{Style: Style(styleValue)}
107
108 var got int
109 switch method {
110 case "ScalarStyle":
111 got = int(event.ScalarStyle())
112 case "SequenceStyle":
113 got = int(event.SequenceStyle())
114 case "MappingStyle":
115 got = int(event.MappingStyle())
116 default:
117 t.Fatalf("unknown accessor: %s", method)
118 }
119
120 assert.Equalf(t, styleValue, got, "Event.%s() = %v, want %v", method, got, styleValue)
121 }