Initial Commit
This commit is contained in:
1624 files changed
+43408
No files matched your search
@@ -0,0 +1,373 @@
|
||||
# YAML's conformance, by the YAML test suite's release `data-2022-01-17`, whose cases
|
||||
# are in `tests/yaml/suite` under its MIT license. A case marked as an error must be
|
||||
# refused, and every other's events must be the suite's, its documents what its JSON
|
||||
# holds where it has JSON, and each document must read back as itself once written.
|
||||
import std::io;
|
||||
import std::json;
|
||||
import std::testing;
|
||||
import std::yaml;
|
||||
|
||||
|
||||
# Appends a scalar's text as the suite writes it, with `\`, line breaks, tabs,
|
||||
# carriage returns, and backspaces escaped.
|
||||
def yaml_append_escaped(text: strview, into: &mut string): void {
|
||||
for byte in text {
|
||||
match byte {
|
||||
'\\' => into.append("\\\\");
|
||||
'\n' => into.append("\\n");
|
||||
'\t' => into.append("\\t");
|
||||
'\r' => into.append("\\r");
|
||||
0x08 => into.append("\\b");
|
||||
else => into.push(byte);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
def yaml_append_decorations(read: &yaml::event, into: &mut string): void {
|
||||
if read.anchor.length > 0 {
|
||||
into.append(" &");
|
||||
into.append(read.anchor);
|
||||
}
|
||||
if read.tag.length > 0 {
|
||||
into.append(" <");
|
||||
into.append(read.tag);
|
||||
into.append(">");
|
||||
}
|
||||
}
|
||||
|
||||
# The events of text, as the suite writes them, and whether reading failed.
|
||||
def yaml_events_of(text: strview, into: &mut string): bool {
|
||||
into.append("+STR\n");
|
||||
var events := yaml::reader::new(text);
|
||||
while events.next() is some(read) {
|
||||
match read.kind {
|
||||
begin_document => into.append(read.explicit ? "+DOC ---\n" : "+DOC\n");
|
||||
end_document => into.append(read.explicit ? "-DOC ...\n" : "-DOC\n");
|
||||
begin_mapping => {
|
||||
into.append(read.style == yaml::style::flow ? "+MAP {}" : "+MAP");
|
||||
yaml_append_decorations(&read, into);
|
||||
into.push('\n');
|
||||
}
|
||||
begin_sequence => {
|
||||
into.append(read.style == yaml::style::flow ? "+SEQ []" : "+SEQ");
|
||||
yaml_append_decorations(&read, into);
|
||||
into.push('\n');
|
||||
}
|
||||
end_mapping => into.append("-MAP\n");
|
||||
end_sequence => into.append("-SEQ\n");
|
||||
alias => {
|
||||
into.append("=ALI *");
|
||||
into.append(read.text);
|
||||
into.push('\n');
|
||||
}
|
||||
scalar => {
|
||||
into.append("=VAL");
|
||||
yaml_append_decorations(&read, into);
|
||||
into.append(match read.style {
|
||||
single => " '",
|
||||
double => " \"",
|
||||
literal => " |",
|
||||
folded => " >",
|
||||
else => " :",
|
||||
});
|
||||
yaml_append_escaped(read.text, into);
|
||||
into.push('\n');
|
||||
}
|
||||
}
|
||||
}
|
||||
if events.error is some(e) {
|
||||
into.append($"ERROR {e.describe()}\n");
|
||||
return false;
|
||||
}
|
||||
into.append("-STR\n");
|
||||
return true;
|
||||
}
|
||||
|
||||
def yaml_is_json_space(byte: u8): bool {
|
||||
return byte == ' ' || byte == '\n' || byte == '\t' || byte == '\r';
|
||||
}
|
||||
|
||||
# Splits text holding JSON values one after another into each value's text.
|
||||
def yaml_json_values(text: strview): vector<strview> {
|
||||
var made := vector::<strview>::new();
|
||||
var at: u64 = 0;
|
||||
while true {
|
||||
while at < text.length && yaml_is_json_space(text[at]) {
|
||||
at++;
|
||||
}
|
||||
if at >= text.length {
|
||||
break;
|
||||
}
|
||||
val start := at;
|
||||
var depth: u64 = 0;
|
||||
var quoted := false;
|
||||
while at < text.length {
|
||||
val byte := text[at];
|
||||
if quoted {
|
||||
if byte == '\\' {
|
||||
at += 2;
|
||||
continue;
|
||||
}
|
||||
at++;
|
||||
if byte == '"' {
|
||||
quoted = false;
|
||||
if depth == 0 {
|
||||
break;
|
||||
}
|
||||
}
|
||||
continue;
|
||||
}
|
||||
if byte == '"' {
|
||||
quoted = true;
|
||||
at++;
|
||||
continue;
|
||||
}
|
||||
if byte == '[' || byte == '{' {
|
||||
depth++;
|
||||
at++;
|
||||
continue;
|
||||
}
|
||||
if byte == ']' || byte == '}' {
|
||||
depth--;
|
||||
at++;
|
||||
if depth == 0 {
|
||||
break;
|
||||
}
|
||||
continue;
|
||||
}
|
||||
if depth == 0 && yaml_is_json_space(byte) {
|
||||
break;
|
||||
}
|
||||
at++;
|
||||
}
|
||||
made.append(text.substr(start, at - start));
|
||||
}
|
||||
return move(made);
|
||||
}
|
||||
|
||||
# Writes a YAML value as compact JSON.
|
||||
def yaml_as_json(read: yaml::value): string {
|
||||
var out := json::writer::new();
|
||||
read.encode(&mut out);
|
||||
val text := out.finish() else (e) { return $"<{e.describe()}>"; };
|
||||
return string::from(text, nothing {});
|
||||
}
|
||||
|
||||
# Writes JSON text again compactly.
|
||||
def yaml_rewritten_json(text: strview): string {
|
||||
val doc := json::parse(text) else (e) { return $"<bad json {e.describe()}>"; };
|
||||
var out := json::writer::new();
|
||||
json::encode(&doc.root, &mut out);
|
||||
val written := out.finish() else (e) { return e.describe(); };
|
||||
return string::from(written, nothing {});
|
||||
}
|
||||
|
||||
# Whether a YAML value holds what a JSON value does: objects compared by their
|
||||
# members' names, in any order, and numbers by their values.
|
||||
def yaml_same_as_json(y: yaml::value, j: json::value): bool {
|
||||
match y.kind {
|
||||
empty => return j.kind == json::kind::empty;
|
||||
boolean => return j.kind == json::kind::boolean && y.as_bool().value == j.as_bool().value;
|
||||
text => return j.kind == json::kind::text && y.as_text().value == j.as_text().value;
|
||||
number => {
|
||||
if j.kind != json::kind::number {
|
||||
return false;
|
||||
}
|
||||
if y.as_i64() is value(whole) {
|
||||
return j.as_i64() is value && j.as_i64().value == whole;
|
||||
}
|
||||
return y.as_f64().value == j.as_f64().value;
|
||||
}
|
||||
sequence => {
|
||||
if j.kind != json::kind::array || y.length != j.length {
|
||||
return false;
|
||||
}
|
||||
var index: u64 = 0;
|
||||
for each in y.elements() {
|
||||
if !yaml_same_as_json(each, j.at(index).some) {
|
||||
return false;
|
||||
}
|
||||
index++;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
mapping => {
|
||||
if j.kind != json::kind::object || y.length != j.length {
|
||||
return false;
|
||||
}
|
||||
for each in y.members() {
|
||||
val other := j.get(each.name) else { return false; };
|
||||
if !yaml_same_as_json(each.value, other) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
# Whether each document of the YAML text is what the JSON text holds, value by value.
|
||||
def yaml_matches_json(text: strview, wanted: strview, show: bool): bool {
|
||||
val doc := yaml::parse(text) else (e) {
|
||||
if show { println($"parse failed: {e.describe()}"); }
|
||||
return false;
|
||||
};
|
||||
val values := yaml_json_values(wanted);
|
||||
if values.length != doc.document_count {
|
||||
if show { println($"{doc.document_count} documents, {values.length} JSON values"); }
|
||||
return false;
|
||||
}
|
||||
var index: u64 = 0;
|
||||
for each in doc.documents() {
|
||||
val parsed := json::parse(values[index]) else (_) { return false; };
|
||||
if !yaml_same_as_json(each, parsed.root) {
|
||||
if show { println($"document {index}:\n got {yaml_as_json(each)}\n expected {yaml_rewritten_json(values[index])}"); }
|
||||
return false;
|
||||
}
|
||||
index++;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
# Whether two YAML values hold the same, members in order.
|
||||
def yaml_same(a: yaml::value, b: yaml::value): bool {
|
||||
if a.kind != b.kind {
|
||||
return false;
|
||||
}
|
||||
match a.kind {
|
||||
empty => return true;
|
||||
boolean => return a.as_bool().value == b.as_bool().value;
|
||||
text => return a.as_text().value == b.as_text().value;
|
||||
number => {
|
||||
if a.as_i64() is value(whole) {
|
||||
return b.as_i64() is value && b.as_i64().value == whole;
|
||||
}
|
||||
if a.as_u64() is value(large) {
|
||||
return b.as_u64() is value && b.as_u64().value == large;
|
||||
}
|
||||
val x := a.as_f64().value;
|
||||
val y := b.as_f64().value;
|
||||
return x == y || (x != x && y != y);
|
||||
}
|
||||
sequence => {
|
||||
if a.length != b.length {
|
||||
return false;
|
||||
}
|
||||
var others := b.elements();
|
||||
for each in a.elements() {
|
||||
val other := others.next() else { return false; };
|
||||
if !yaml_same(each, other) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
mapping => {
|
||||
if a.length != b.length {
|
||||
return false;
|
||||
}
|
||||
var others := b.members();
|
||||
for each in a.members() {
|
||||
val other := others.next() else { return false; };
|
||||
if each.name != other.name || !yaml_same(each.value, other.value) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
# Whether each document of the text, written by the writer and read back, holds
|
||||
# what it held: documents with keys that are collections, which the writer cannot
|
||||
# write, are left out.
|
||||
def yaml_round_trips(text: strview, show: bool): bool {
|
||||
val doc := yaml::parse(text) else (_) { return false; };
|
||||
for each in doc.documents() {
|
||||
var out := yaml::writer::new();
|
||||
each.encode(&mut out);
|
||||
val written := out.finish() else (e) {
|
||||
if e.kind == yaml::error_kind::wrong_kind {
|
||||
continue;
|
||||
}
|
||||
if show { println($"writing failed: {e.describe()}"); }
|
||||
return false;
|
||||
};
|
||||
val again := yaml::parse(written) else (e) {
|
||||
if show { println($"written text does not read: {e.describe()}\n{written}"); }
|
||||
return false;
|
||||
};
|
||||
if again.document_count != 1 || !yaml_same(each, again.root) {
|
||||
if show { println($"read back differently:\n{written}"); }
|
||||
return false;
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
@test def yaml_conforms_to_its_test_suite(t: &mut tester): void {
|
||||
val folder := path::parse("tests/yaml/suite") else (_) {
|
||||
t.expects(false);
|
||||
return;
|
||||
};
|
||||
var listing := io::directory::open(&folder) else (e) {
|
||||
print("The suite is read from `tests/yaml/suite`, so the tests run from the root of the repository.\n");
|
||||
t.expects(false);
|
||||
return;
|
||||
};
|
||||
|
||||
var checked: u64 = 0;
|
||||
var compared: u64 = 0;
|
||||
while listing.next() is some(each) {
|
||||
val name: strview = each.name;
|
||||
if name == "LICENSE" {
|
||||
continue;
|
||||
}
|
||||
|
||||
val at := folder.join(name);
|
||||
val text := io::read_file(&at.join("in.yaml")) else (_) {
|
||||
t.expects(false);
|
||||
continue;
|
||||
};
|
||||
checked++;
|
||||
var events := string::new(nothing {});
|
||||
val read := yaml_events_of(text.span, &mut events);
|
||||
if io::exists(&at.join("error")) {
|
||||
val refused := !read || yaml::parse(text.span) is error;
|
||||
if !refused {
|
||||
print($"{name} is accepted\n");
|
||||
}
|
||||
t.expects(refused);
|
||||
continue;
|
||||
}
|
||||
|
||||
val wanted := io::read_file(&at.join("test.event")) else (_) {
|
||||
t.expects(false);
|
||||
continue;
|
||||
};
|
||||
val same_events := read && events.span == wanted.span;
|
||||
if !same_events {
|
||||
print($"{name} gives other events\n");
|
||||
}
|
||||
t.expects(same_events);
|
||||
if !yaml_round_trips(text.span, false) {
|
||||
print($"{name} does not read back as itself\n");
|
||||
t.expects(false);
|
||||
}
|
||||
if io::exists(&at.join("in.json")) {
|
||||
val json_text := io::read_file(&at.join("in.json")) else (_) {
|
||||
t.expects(false);
|
||||
continue;
|
||||
};
|
||||
val matched := yaml_matches_json(text.span, json_text.span, false);
|
||||
if !matched {
|
||||
print($"{name} holds other than its JSON\n");
|
||||
}
|
||||
t.expects(matched);
|
||||
compared++;
|
||||
}
|
||||
}
|
||||
t.expects(checked == 402 && compared == 279);
|
||||
}
|
||||
Reference in new issue
Block a user