v/vlib/x/json2/decoder2/tests/decode_string_test.v
Larsimusrex a11de7263f
Some checks failed
Graphics CI / gg-regressions (push) Waiting to run
vlib modules CI / build-module-docs (push) Waiting to run
Shy and PV CI / v-compiles-puzzle-vibes (push) Waiting to run
Sanitized CI / sanitize-undefined-clang (push) Waiting to run
Sanitized CI / sanitize-undefined-gcc (push) Waiting to run
Sanitized CI / tests-sanitize-address-clang (push) Waiting to run
Sanitized CI / sanitize-address-msvc (push) Waiting to run
Sanitized CI / sanitize-address-gcc (push) Waiting to run
Sanitized CI / sanitize-memory-clang (push) Waiting to run
sdl CI / v-compiles-sdl-examples (push) Waiting to run
Time CI / time-linux (push) Waiting to run
Time CI / time-macos (push) Waiting to run
Time CI / time-windows (push) Waiting to run
toml CI / toml-module-pass-external-test-suites (push) Waiting to run
Tools CI / tools-linux (clang) (push) Waiting to run
Tools CI / tools-linux (gcc) (push) Waiting to run
Tools CI / tools-linux (tcc) (push) Waiting to run
Tools CI / tools-macos (clang) (push) Waiting to run
Tools CI / tools-windows (gcc) (push) Waiting to run
Tools CI / tools-windows (msvc) (push) Waiting to run
Tools CI / tools-windows (tcc) (push) Waiting to run
Tools CI / tools-docker-ubuntu-musl (push) Waiting to run
vab CI / vab-compiles-v-examples (push) Waiting to run
vab CI / v-compiles-os-android (push) Waiting to run
json decoder benchmark CI / json-encode-benchmark (push) Has been cancelled
json encoder benchmark CI / json-encode-benchmark (push) Has been cancelled
decoder2: improve checker with better EOF detection (#25075)
2025-08-09 16:38:58 +03:00

64 lines
1.9 KiB
V

import x.json2.decoder2 as json
fn test_json_escape_low_chars() {
assert json.decode[string](r'"\u001b"')! == '\u001b'
assert json.decode[string](r'"\u000f"')! == '\u000f'
assert json.decode[string](r'" "')! == '\u0020'
assert json.decode[string](r'"\u0000"')! == '\u0000'
}
fn test_json_string() {
assert json.decode[string](r'"te\u2714st"')! == 'test'
assert json.decode[string](r'"te✔st"')! == 'test'
assert json.decode[string]('""')! == ''
}
fn test_json_string_emoji() {
assert json.decode[string](r'"🐈"')! == '🐈'
assert json.decode[string](r'"💀"')! == '💀'
assert json.decode[string](r'"🐈💀"')! == '🐈💀'
}
fn test_json_string_non_ascii() {
assert json.decode[string](r'"\u3072\u3089\u304c\u306a"')! == ''
assert json.decode[string]('"a\\u3072b\\u3089c\\u304cd\\u306ae fgh"')! == 'abcde fgh'
assert json.decode[string]('"\\u3072\\u3089\\u304c\\u306a"')! == ''
}
fn test_utf8_strings_are_not_modified() {
assert json.decode[string]('"ü"')! == 'ü'
assert json.decode[string]('"Schilddrüsenerkrankungen"')! == 'Schilddrüsenerkrankungen'
}
fn test_json_string_invalid_escapes() {
mut has_error := false
json.decode[string](r'"\x"') or {
if err is json.JsonDecodeError {
assert err.line == 1
assert err.character == 3
assert err.message == 'Syntax: unknown escape sequence'
}
has_error = true
} // Invalid escape
assert has_error, 'Expected error'
has_error = false
json.decode[string](r'"\u123"') or {
if err is json.JsonDecodeError {
assert err.line == 1
assert err.character == 3
assert err.message == 'Syntax: short unicode escape sequence \\u123'
}
has_error = true
} // Incomplete Unicode
assert has_error, 'Expected error'
}
fn test_json_string_whitespace() {
// Test strings with whitespace
assert json.decode[string]('" "')! == ' '
assert json.decode[string]('"\t\n\r"')! == '\t\n\r'
}