Initial Commit

This commit is contained in:
SoraKatadzuma committed 2026-12-06 13:39:23 -06:00
commit b1e8b0939d
1624 files changed
+43436

No files matched your search

+373
View File
@@ -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);
}
+12
View File
@@ -0,0 +1,12 @@
[
{
"name": "Mark McGwire",
"hr": 65,
"avg": 0.278
},
{
"name": "Sammy Sosa",
"hr": 63,
"avg": 0.288
}
]
+8
View File
@@ -0,0 +1,8 @@
-
name: Mark McGwire
hr: 65
avg: 0.278
-
name: Sammy Sosa
hr: 63
avg: 0.288
+22
View File
@@ -0,0 +1,22 @@
+STR
+DOC
+SEQ
+MAP
=VAL :name
=VAL :Mark McGwire
=VAL :hr
=VAL :65
=VAL :avg
=VAL :0.278
-MAP
+MAP
=VAL :name
=VAL :Sammy Sosa
=VAL :hr
=VAL :63
=VAL :avg
=VAL :0.288
-MAP
-SEQ
-DOC
-STR
View File
Whitespace-only changes.
+3
View File
@@ -0,0 +1,3 @@
foo:
bar
invalid
+5
View File
@@ -0,0 +1,5 @@
+STR
+DOC
+MAP
=VAL :foo
=VAL :bar
+18
View File
@@ -0,0 +1,18 @@
{
"top1": {
"key1": "scalar1"
},
"top2": {
"key2": "scalar2"
},
"top3": {
"scalar1": "scalar3"
},
"top4": {
"scalar2": "scalar4"
},
"top5": "scalar5",
"top6": {
"key6": "scalar6"
}
}
+12
View File
@@ -0,0 +1,12 @@
"top1" :
"key1" : &alias1 scalar1
'top2' :
'key2' : &alias2 scalar2
top3: &node3
*alias1 : scalar3
top4:
*alias2 : scalar4
top5 :
scalar5
top6:
&anchor6 'key6' : scalar6
+33
View File
@@ -0,0 +1,33 @@
+STR
+DOC
+MAP
=VAL "top1
+MAP
=VAL "key1
=VAL &alias1 :scalar1
-MAP
=VAL 'top2
+MAP
=VAL 'key2
=VAL &alias2 :scalar2
-MAP
=VAL :top3
+MAP &node3
=ALI *alias1
=VAL :scalar3
-MAP
=VAL :top4
+MAP
=ALI *alias2
=VAL :scalar4
-MAP
=VAL :top5
=VAL :scalar5
=VAL :top6
+MAP
=VAL &anchor6 'key6
=VAL :scalar6
-MAP
-MAP
-DOC
-STR
+1
View File
@@ -0,0 +1 @@
"text"
+2
View File
@@ -0,0 +1,2 @@
%YAML 1.2
--- text
+5
View File
@@ -0,0 +1,5 @@
+STR
+DOC ---
=VAL :text
-DOC
-STR
+6
View File
@@ -0,0 +1,6 @@
[
"a",
"b",
42,
"d"
]
+4
View File
@@ -0,0 +1,4 @@
- !!str a
- b
- !!int 42
- d
+10
View File
@@ -0,0 +1,10 @@
+STR
+DOC
+SEQ
=VAL <tag:yaml.org,2002:str> :a
=VAL :b
=VAL <tag:yaml.org,2002:int> :42
=VAL :d
-SEQ
-DOC
-STR
View File
Whitespace-only changes.
+3
View File
@@ -0,0 +1,3 @@
this
is
invalid: x
+2
View File
@@ -0,0 +1,2 @@
+STR
+DOC
+7
View File
@@ -0,0 +1,7 @@
{
"a!\"#$%&'()*+,-./09:;<=>?@AZ[\\]^_`az{|}~": "safe",
"?foo": "safe question mark",
":foo": "safe colon",
"-foo": "safe dash",
"this is#not": "a comment"
}
+5
View File
@@ -0,0 +1,5 @@
a!"#$%&'()*+,-./09:;<=>?@AZ[\]^_`az{|}~: safe
?foo: safe question mark
:foo: safe colon
-foo: safe dash
this is#not: a comment
+16
View File
@@ -0,0 +1,16 @@
+STR
+DOC
+MAP
=VAL :a!"#$%&'()*+,-./09:;<=>?@AZ[\\]^_`az{|}~
=VAL :safe
=VAL :?foo
=VAL :safe question mark
=VAL ::foo
=VAL :safe colon
=VAL :-foo
=VAL :safe dash
=VAL :this is#not
=VAL :a comment
-MAP
-DOC
-STR
View File
Whitespace-only changes.
+1
View File
@@ -0,0 +1 @@
--- |0
+2
View File
@@ -0,0 +1,2 @@
+STR
+DOC ---
View File
Whitespace-only changes.
+1
View File
@@ -0,0 +1 @@
--- |10
+2
View File
@@ -0,0 +1,2 @@
+STR
+DOC ---
+1
View File
@@ -0,0 +1 @@
""
+1
View File
@@ -0,0 +1 @@
--- |1-
+5
View File
@@ -0,0 +1,5 @@
+STR
+DOC ---
=VAL |
-DOC
-STR
+1
View File
@@ -0,0 +1 @@
""
+1
View File
@@ -0,0 +1 @@
--- |1+
+5
View File
@@ -0,0 +1,5 @@
+STR
+DOC ---
=VAL |
-DOC
-STR
+2
View File
@@ -0,0 +1,2 @@
: a
: b
+10
View File
@@ -0,0 +1,10 @@
+STR
+DOC
+MAP
=VAL :
=VAL :a
=VAL :
=VAL :b
-MAP
-DOC
-STR
+1
View File
@@ -0,0 +1 @@
"foo"
+4
View File
@@ -0,0 +1,4 @@
%FOO bar baz # Should be ignored
# with a warning.
---
"foo"
+5
View File
@@ -0,0 +1,5 @@
+STR
+DOC ---
=VAL "foo
-DOC
-STR
+4
View File
@@ -0,0 +1,4 @@
{
"key": "value",
"foo": "key"
}
+3
View File
@@ -0,0 +1,3 @@
&a: key: &a value
foo:
*a:
+10
View File
@@ -0,0 +1,10 @@
+STR
+DOC
+MAP
=VAL &a: :key
=VAL &a :value
=VAL :foo
=ALI *a:
-MAP
-DOC
-STR
+5
View File
@@ -0,0 +1,5 @@
{
"Mark McGwire": null,
"Sammy Sosa": null,
"Ken Griff": null
}
+7
View File
@@ -0,0 +1,7 @@
# Sets are represented as a
# Mapping where each key is
# associated with a null value
--- !!set
? Mark McGwire
? Sammy Sosa
? Ken Griff
+12
View File
@@ -0,0 +1,12 @@
+STR
+DOC ---
+MAP <tag:yaml.org,2002:set>
=VAL :Mark McGwire
=VAL :
=VAL :Sammy Sosa
=VAL :
=VAL :Ken Griff
=VAL :
-MAP
-DOC
-STR
+5
View File
@@ -0,0 +1,5 @@
[
1,
-2,
33
]
+4
View File
@@ -0,0 +1,4 @@
---
- !!int 1
- !!int -2
- !!int 33
+9
View File
@@ -0,0 +1,9 @@
+STR
+DOC ---
+SEQ
=VAL <tag:yaml.org,2002:int> :1
=VAL <tag:yaml.org,2002:int> :-2
=VAL <tag:yaml.org,2002:int> :33
-SEQ
-DOC
-STR
+7
View File
@@ -0,0 +1,7 @@
{
"a": "b"
}
[
"c"
]
"d e"
+8
View File
@@ -0,0 +1,8 @@
--- !!map
? a
: b
--- !!seq
- !!str c
--- !!str
d
e
+16
View File
@@ -0,0 +1,16 @@
+STR
+DOC ---
+MAP <tag:yaml.org,2002:map>
=VAL :a
=VAL :b
-MAP
-DOC
+DOC ---
+SEQ <tag:yaml.org,2002:seq>
=VAL <tag:yaml.org,2002:str> :c
-SEQ
-DOC
+DOC ---
=VAL <tag:yaml.org,2002:str> :d e
-DOC
-STR
+3
View File
@@ -0,0 +1,3 @@
{
"plain": "a b\nc"
}
+5
View File
@@ -0,0 +1,5 @@
---
plain: a
b
c
+8
View File
@@ -0,0 +1,8 @@
+STR
+DOC ---
+MAP
=VAL :plain
=VAL :a b\nc
-MAP
-DOC
-STR
+7
View File
@@ -0,0 +1,7 @@
[
[
"s1_i1",
"s1_i2"
],
"s2"
]
+3
View File
@@ -0,0 +1,3 @@
- - s1_i1
- s1_i2
- s2
+11
View File
@@ -0,0 +1,11 @@
+STR
+DOC
+SEQ
+SEQ
=VAL :s1_i1
=VAL :s1_i2
-SEQ
=VAL :s2
-SEQ
-DOC
-STR
+6
View File
@@ -0,0 +1,6 @@
{
"First occurrence": "Foo",
"Second occurrence": "Foo",
"Override anchor": "Bar",
"Reuse anchor": "Bar"
}
+4
View File
@@ -0,0 +1,4 @@
First occurrence: &anchor Foo
Second occurrence: *anchor
Override anchor: &anchor Bar
Reuse anchor: *anchor
+14
View File
@@ -0,0 +1,14 @@
+STR
+DOC
+MAP
=VAL :First occurrence
=VAL &anchor :Foo
=VAL :Second occurrence
=ALI *anchor
=VAL :Override anchor
=VAL &anchor :Bar
=VAL :Reuse anchor
=ALI *anchor
-MAP
-DOC
-STR
View File
Whitespace-only changes.
+3
View File
@@ -0,0 +1,3 @@
---
key: value
... invalid
+7
View File
@@ -0,0 +1,7 @@
+STR
+DOC ---
+MAP
=VAL :key
=VAL :value
-MAP
-DOC ...
+1
View File
@@ -0,0 +1 @@
"k:#foo &a !t s"
+3
View File
@@ -0,0 +1,3 @@
---
k:#foo
&a !t s
+5
View File
@@ -0,0 +1,5 @@
+STR
+DOC ---
=VAL :k:#foo &a !t s
-DOC
-STR
+3
View File
@@ -0,0 +1,3 @@
[
"a"
]
+2
View File
@@ -0,0 +1,2 @@
&sequence
- a
+7
View File
@@ -0,0 +1,7 @@
+STR
+DOC
+SEQ &sequence
=VAL :a
-SEQ
-DOC
-STR
+1
View File
@@ -0,0 +1 @@
"1 leading \ttab"
+2
View File
@@ -0,0 +1,2 @@
"1 leading
\ttab"
+5
View File
@@ -0,0 +1,5 @@
+STR
+DOC
=VAL "1 leading \ttab
-DOC
-STR
+1
View File
@@ -0,0 +1 @@
"2 leading \ttab"
+2
View File
@@ -0,0 +1,2 @@
"2 leading
\ tab"
+5
View File
@@ -0,0 +1,5 @@
+STR
+DOC
=VAL "2 leading \ttab
-DOC
-STR
+1
View File
@@ -0,0 +1 @@
"3 leading tab"
+2
View File
@@ -0,0 +1,2 @@
"3 leading
tab"
+5
View File
@@ -0,0 +1,5 @@
+STR
+DOC
=VAL "3 leading tab
-DOC
-STR
+1
View File
@@ -0,0 +1 @@
"4 leading \t tab"
+2
View File
@@ -0,0 +1,2 @@
"4 leading
\t tab"
+5
View File
@@ -0,0 +1,5 @@
+STR
+DOC
=VAL "4 leading \t tab
-DOC
-STR
+1
View File
@@ -0,0 +1 @@
"5 leading \t tab"
+2
View File
@@ -0,0 +1,2 @@
"5 leading
\ tab"
+5
View File
@@ -0,0 +1,5 @@
+STR
+DOC
=VAL "5 leading \t tab
-DOC
-STR
+1
View File
@@ -0,0 +1 @@
"6 leading tab"
+2
View File
@@ -0,0 +1,2 @@
"6 leading
tab"
+5
View File
@@ -0,0 +1,5 @@
+STR
+DOC
=VAL "6 leading tab
-DOC
-STR
+3
View File
@@ -0,0 +1,3 @@
{
"escaped slash": "a/b"
}
+1
View File
@@ -0,0 +1 @@
escaped slash: "a\/b"
+8
View File
@@ -0,0 +1,8 @@
+STR
+DOC
+MAP
=VAL :escaped slash
=VAL "a/b
-MAP
-DOC
-STR
+5
View File
@@ -0,0 +1,5 @@
{
unquoted : "separate",
http://foo.com,
omitted value:,
}
+12
View File
@@ -0,0 +1,12 @@
+STR
+DOC
+MAP {}
=VAL :unquoted
=VAL "separate
=VAL :http://foo.com
=VAL :
=VAL :omitted value
=VAL :
-MAP
-DOC
-STR
+4
View File
@@ -0,0 +1,4 @@
{
"plain": "This unquoted scalar spans many lines.",
"quoted": "So does this quoted scalar.\n"
}
+6
View File
@@ -0,0 +1,6 @@
plain:
This unquoted scalar
spans many lines.
quoted: "So does this
quoted scalar.\n"
+10
View File
@@ -0,0 +1,10 @@
+STR
+DOC
+MAP
=VAL :plain
=VAL :This unquoted scalar spans many lines.
=VAL :quoted
=VAL "So does this quoted scalar.\n
-MAP
-DOC
-STR
View File
Whitespace-only changes.
+4
View File
@@ -0,0 +1,4 @@
---
a:
b:
c: value
+4
View File
@@ -0,0 +1,4 @@
+STR
+DOC ---
+MAP
=VAL :a
+4
View File
@@ -0,0 +1,4 @@
---
[
[ a, [ [[b,c]]: d, e]]: 23
]
+24
View File
@@ -0,0 +1,24 @@
+STR
+DOC ---
+SEQ []
+MAP {}
+SEQ []
=VAL :a
+SEQ []
+MAP {}
+SEQ []
+SEQ []
=VAL :b
=VAL :c
-SEQ
-SEQ
=VAL :d
-MAP
=VAL :e
-SEQ
-SEQ
=VAL :23
-MAP
-SEQ
-DOC
-STR
Loaded 100 of 1624 files, more files were not shown because too many files have changed in this diff. Show more