| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
package libyaml |
| 9 |
|
| 10 |
import ( |
| 11 |
"bytes" |
| 12 |
"testing" |
| 13 |
|
| 14 |
"go.yaml.in/yaml/v4/internal/testutil/assert" |
| 15 |
) |
| 16 |
|
| 17 |
func TestScanner(t *testing.T) { |
| 18 |
RunTestCases(t, "scanner.yaml", map[string]TestHandler{ |
| 19 |
"scan-tokens": runScanTokensTest, |
| 20 |
"scan-tokens-detailed": runScanTokensDetailedTest, |
| 21 |
"scan-error": runScanErrorTest, |
| 22 |
"char-predicate": runCharPredicateTest, |
| 23 |
"char-convert": runCharConvertTest, |
| 24 |
}) |
| 25 |
} |
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
func runScanTokensTest(t *testing.T, tc TestCase) { |
| 31 |
types, ok := scanTokens(tc.Yaml) |
| 32 |
assert.Truef(t, ok, "scanTokens() failed") |
| 33 |
|
| 34 |
|
| 35 |
wantSlice, ok := tc.Want.([]any) |
| 36 |
assert.Truef(t, ok, "Want should be []interface{}") |
| 37 |
|
| 38 |
var expected []TokenType |
| 39 |
for _, item := range wantSlice { |
| 40 |
tokenStr, ok := item.(string) |
| 41 |
assert.Truef(t, ok, "Want item should be string") |
| 42 |
expected = append(expected, ParseTokenType(t, tokenStr)) |
| 43 |
} |
| 44 |
|
| 45 |
assert.Equalf(t, len(expected), len(types), "scanTokens() got %d tokens, want %d", len(types), len(expected)) |
| 46 |
for i, tt := range expected { |
| 47 |
assert.Equalf(t, tt, types[i], "token[%d] = %v, want %v", i, types[i], tt) |
| 48 |
} |
| 49 |
} |
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
func runScanTokensDetailedTest(t *testing.T, tc TestCase) { |
| 55 |
tokens, ok := scanTokensDetailed(tc.Yaml) |
| 56 |
assert.Truef(t, ok, "scanTokensDetailed() failed") |
| 57 |
|
| 58 |
assert.Equalf(t, len(tc.WantTokens), len(tokens), "scanTokensDetailed() got %d tokens, want %d", len(tokens), len(tc.WantTokens)) |
| 59 |
|
| 60 |
for i, wantSpec := range tc.WantTokens { |
| 61 |
token := tokens[i] |
| 62 |
wantType := ParseTokenType(t, wantSpec.Type) |
| 63 |
|
| 64 |
assert.Equalf(t, wantType, token.Type, "token[%d].Type = %v, want %v", i, token.Type, wantType) |
| 65 |
|
| 66 |
|
| 67 |
if wantSpec.Value != "" { |
| 68 |
assert.Truef(t, bytes.Equal(token.Value, []byte(wantSpec.Value)), |
| 69 |
"token[%d].Value = %q, want %q", i, token.Value, wantSpec.Value) |
| 70 |
} |
| 71 |
|
| 72 |
if wantSpec.Style != "" { |
| 73 |
wantStyle := ParseScalarStyle(t, wantSpec.Style) |
| 74 |
assert.Equalf(t, wantStyle, token.Style, "token[%d].Style = %v, want %v", i, token.Style, wantStyle) |
| 75 |
} |
| 76 |
} |
| 77 |
} |
| 78 |
|
| 79 |
|
| 80 |
|
| 81 |
|
| 82 |
func runScanErrorTest(t *testing.T, tc TestCase) { |
| 83 |
parser := NewParser() |
| 84 |
parser.SetInputString([]byte(tc.Yaml)) |
| 85 |
|
| 86 |
var token Token |
| 87 |
var scanErr error |
| 88 |
for scanErr == nil && token.Type != STREAM_END_TOKEN { |
| 89 |
scanErr = parser.Scan(&token) |
| 90 |
} |
| 91 |
|
| 92 |
|
| 93 |
|
| 94 |
|
| 95 |
wantError := true |
| 96 |
if tc.Want != nil { |
| 97 |
switch v := tc.Want.(type) { |
| 98 |
case bool: |
| 99 |
wantError = v |
| 100 |
case []any: |
| 101 |
if len(v) > 0 { |
| 102 |
if boolVal, ok := v[0].(bool); ok { |
| 103 |
wantError = boolVal |
| 104 |
} |
| 105 |
} |
| 106 |
} |
| 107 |
} |
| 108 |
if wantError { |
| 109 |
assert.Truef(t, scanErr != nil, "Expected scanner error, but got none") |
| 110 |
|
| 111 |
if tc.Like != "" { |
| 112 |
assert.ErrorMatchesf(t, tc.Like, scanErr, "") |
| 113 |
} |
| 114 |
} else { |
| 115 |
assert.Truef(t, scanErr == nil, "Expected no scanner error, but got %v", scanErr) |
| 116 |
} |
| 117 |
} |
| 118 |
|
| 119 |
|
| 120 |
|
| 121 |
|
| 122 |
func runCharPredicateTest(t *testing.T, tc TestCase) { |
| 123 |
t.Helper() |
| 124 |
|
| 125 |
input := tc.Input |
| 126 |
index := tc.Index |
| 127 |
|
| 128 |
|
| 129 |
want := WantBool(t, tc.Want, true) |
| 130 |
|
| 131 |
var got bool |
| 132 |
switch tc.Function { |
| 133 |
case "isAlpha": |
| 134 |
got = isAlpha(input, index) |
| 135 |
case "isFlowIndicator": |
| 136 |
got = isFlowIndicator(input, index) |
| 137 |
case "isAnchorChar": |
| 138 |
got = isAnchorChar(input, index) |
| 139 |
case "isColon": |
| 140 |
got = isColon(input, index) |
| 141 |
case "isTagURIChar": |
| 142 |
|
| 143 |
verbatim := false |
| 144 |
if len(tc.Args) >= 1 { |
| 145 |
v, ok := tc.Args[0].(bool) |
| 146 |
assert.Truef(t, ok, "Args[0] should be bool, got %T", tc.Args[0]) |
| 147 |
verbatim = v |
| 148 |
} |
| 149 |
got = isTagURIChar(input, index, verbatim) |
| 150 |
case "isDigit": |
| 151 |
got = isDigit(input, index) |
| 152 |
case "isHex": |
| 153 |
got = isHex(input, index) |
| 154 |
case "isASCII": |
| 155 |
got = isASCII(input, index) |
| 156 |
case "isPrintable": |
| 157 |
got = isPrintable(input, index) |
| 158 |
case "isZeroChar": |
| 159 |
got = isZeroChar(input, index) |
| 160 |
case "isBOM": |
| 161 |
got = isBOM(input, index) |
| 162 |
case "isSpace": |
| 163 |
got = isSpace(input, index) |
| 164 |
case "isTab": |
| 165 |
got = isTab(input, index) |
| 166 |
case "isBlank": |
| 167 |
got = isBlank(input, index) |
| 168 |
case "isLineBreak": |
| 169 |
got = isLineBreak(input, index) |
| 170 |
case "isCRLF": |
| 171 |
got = isCRLF(input, index) |
| 172 |
case "isBreakOrZero": |
| 173 |
got = isBreakOrZero(input, index) |
| 174 |
case "isSpaceOrZero": |
| 175 |
got = isSpaceOrZero(input, index) |
| 176 |
case "isBlankOrZero": |
| 177 |
got = isBlankOrZero(input, index) |
| 178 |
default: |
| 179 |
t.Fatalf("unknown function: %s", tc.Function) |
| 180 |
} |
| 181 |
|
| 182 |
assert.Equalf(t, want, got, "%s(%q, %d) = %v, want %v", tc.Function, input, index, got, want) |
| 183 |
} |
| 184 |
|
| 185 |
func runCharConvertTest(t *testing.T, tc TestCase) { |
| 186 |
t.Helper() |
| 187 |
|
| 188 |
input := tc.Input |
| 189 |
index := tc.Index |
| 190 |
want, ok := tc.Want.(int) |
| 191 |
assert.Truef(t, ok, "Want should be int, got %T", tc.Want) |
| 192 |
|
| 193 |
var got int |
| 194 |
switch tc.Function { |
| 195 |
case "asDigit": |
| 196 |
got = asDigit(input, index) |
| 197 |
case "asHex": |
| 198 |
got = asHex(input, index) |
| 199 |
case "width": |
| 200 |
|
| 201 |
got = width(input[index]) |
| 202 |
default: |
| 203 |
t.Fatalf("unknown function: %s", tc.Function) |
| 204 |
} |
| 205 |
|
| 206 |
assert.Equalf(t, want, got, "%s(%q, %d) = %d, want %d", tc.Function, input, index, got, want) |
| 207 |
} |