69 lines
2.1 KiB
Plaintext
69 lines
2.1 KiB
Plaintext
# JSON's conformance, by Nicolas Seriot's JSONTestSuite, whose parsing cases are in
|
|
# `tests/json/parsing` under its MIT license. Each `y_` case must be accepted, and each
|
|
# `n_` case refused. RFC 8259 leaves the `i_` cases to the parser.
|
|
import std::io;
|
|
import std::json;
|
|
import std::testing;
|
|
|
|
|
|
# The `i_` cases that are accepted: numbers too large or too small for any type,
|
|
# which reading them as one refuses, and nesting within the limit. Every other is
|
|
# refused: text that is not UTF-8, half of a surrogate pair, UTF-16, and a byte
|
|
# order mark before the value.
|
|
const accepted: span<strview> = [
|
|
"i_number_double_huge_neg_exp.json",
|
|
"i_number_huge_exp.json",
|
|
"i_number_neg_int_huge_exp.json",
|
|
"i_number_pos_double_huge_exp.json",
|
|
"i_number_real_neg_overflow.json",
|
|
"i_number_real_pos_overflow.json",
|
|
"i_number_real_underflow.json",
|
|
"i_number_too_big_neg_int.json",
|
|
"i_number_too_big_pos_int.json",
|
|
"i_number_very_big_negative_int.json",
|
|
"i_structure_500_nested_arrays.json",
|
|
];
|
|
|
|
def is_accepted(name: strview): bool {
|
|
for each in accepted {
|
|
if each == name {
|
|
return true;
|
|
}
|
|
}
|
|
return false;
|
|
}
|
|
|
|
@test def json_conforms_to_its_test_suite(t: &mut tester): void {
|
|
val folder := path::parse("tests/json/parsing") else (_) {
|
|
t.expects(false);
|
|
return;
|
|
};
|
|
var listing := io::directory::open(&folder) else (e) {
|
|
print("The suite is read from `tests/json/parsing`, so the tests run from the root of the repository.\n");
|
|
t.expects(false);
|
|
return;
|
|
};
|
|
|
|
var checked: u64 = 0;
|
|
while listing.next() is some(each) {
|
|
val name: strview = each.name;
|
|
if !name.ends_with(".json") {
|
|
continue;
|
|
}
|
|
|
|
val at := folder.join(name);
|
|
val text := io::read_file(&at) else (_) {
|
|
t.expects(false);
|
|
continue;
|
|
};
|
|
val parsed := json::parse(text.span) is value;
|
|
val wanted := name[0] == 'y' || (name[0] == 'i' && is_accepted(name));
|
|
if parsed != wanted {
|
|
print($"{name} is {parsed ? "accepted" : "refused"}\n");
|
|
}
|
|
t.expects(parsed == wanted);
|
|
checked++;
|
|
}
|
|
t.expects(checked == 318);
|
|
}
|