Files
std/tests/json/scanning.sakura
T
2026-10-27 16:22:18 -05:00

40 lines
1.4 KiB
Plaintext

# Tests of JSON's scanning of text, which an executable does sixteen bytes at a
# time: a string read alike wherever its bytes fall, so on either side of where
# each sixteen ends.
import std::json;
import std::testing;
@test def json_strings_read_alike_wherever_their_bytes_fall(t: &mut tester): void {
for place in 0..40u {
var before := string::new(nothing {});
for i in 0..place {
before.push(('a' as u64 + i % 26) as u8);
}
# Escapes, among them a quote, and a character past ASCII.
for middle in ["\\n", "é", "\\\"", "\\u0041"] {
var text := string::from("[\"", nothing {});
text.append(before.view());
text.append(middle);
text.append("0123456789abcdefghij\"]");
t.expects(json::parse(text.view()) is value);
}
# A control character stops it where it is.
var bad := string::from("[\"", nothing {});
bad.append(before.view());
bad.push(0x01);
bad.append("0123456789abcdefghij\"]");
match json::parse(bad.view()) {
value(&_) => t.expects(false);
error(e) => t.expects(e.kind == json::error_kind::unexpected_byte && e.offset == 2 + place as u64);
}
# Spacing of any length between tokens.
var spaced := string::from("[", nothing {});
for i in 0..place {
spaced.push(i % 3 == 0 ? '\n' : ' ');
}
spaced.append("1 ,\t2]");
t.expects(json::parse(spaced.view()) is value);
}
}