Initial Commit
This commit is contained in:
1625 files changed
+43449
No files matched your search
@@ -0,0 +1,479 @@
|
||||
# Tests of `std::yaml`: what scalars resolve to, block and flow scalars, aliases and
|
||||
# merges, streams of documents, errors, the reader, and the writer.
|
||||
import std::io;
|
||||
import std::serial;
|
||||
import std::testing;
|
||||
import std::yaml;
|
||||
|
||||
|
||||
# The kind of error parsing text fails with, at a line and column, or `false`.
|
||||
def yaml_fails_at(text: strview, kind: yaml::error_kind, line: u32, column: u32): bool {
|
||||
match yaml::parse(text) {
|
||||
value(&_) => return false;
|
||||
error(e) => return e.kind == kind && e.line == line && e.column == column;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
# The kind of error parsing text fails with, or `nothing`.
|
||||
def yaml_failure(text: strview, given: yaml::limits = yaml::limits {}): maybe<yaml::error_kind> {
|
||||
match yaml::parse(text, given) {
|
||||
value(&_) => return nothing {};
|
||||
error(e) => return e.kind;
|
||||
}
|
||||
return nothing {};
|
||||
}
|
||||
|
||||
# The text a document's root is written again as.
|
||||
def yaml_rewritten(text: strview, indent: u32 = 2): string {
|
||||
val doc := yaml::parse(text) else (e) { return e.describe(); };
|
||||
var out := yaml::writer::new(indent);
|
||||
yaml::encode(&doc.root, &mut out);
|
||||
val written := out.finish() else (e) { return e.describe(); };
|
||||
return string::from(written, nothing {});
|
||||
}
|
||||
|
||||
@serial struct two_keys { @serial var a: i64; @serial var b: i64; }
|
||||
|
||||
@serial struct service {
|
||||
@serial var image: string;
|
||||
@serial var ports: vector<u16>;
|
||||
@serial var restart: maybe<string>;
|
||||
}
|
||||
|
||||
@test def yaml_resolves_plain_scalars_by_the_core_schema(t: &mut tester): void {
|
||||
val doc := yaml::parse("""
|
||||
- null
|
||||
- Null
|
||||
- ~
|
||||
-
|
||||
- true
|
||||
- False
|
||||
- 12
|
||||
- -3
|
||||
- +5
|
||||
- 0o17
|
||||
- 0x1F
|
||||
- 0777
|
||||
- 1.5
|
||||
- .5
|
||||
- 1e3
|
||||
- .inf
|
||||
- -.Inf
|
||||
- .nan
|
||||
- yes
|
||||
- 1:30
|
||||
- '12'
|
||||
- "true"
|
||||
- 1.10
|
||||
""") else (_) {
|
||||
t.expects(false);
|
||||
return;
|
||||
};
|
||||
val items := doc.root;
|
||||
t.expects(items.length == 23);
|
||||
for index in 0u..4u {
|
||||
t.expects(items.at(index).some.is_null);
|
||||
}
|
||||
t.expects(items.at(4).some.as_bool().value && !items.at(5).some.as_bool().value);
|
||||
t.expects(items.at(6).some.as_i64().value == 12 && items.at(7).some.as_i64().value == -3 && items.at(8).some.as_i64().value == 5);
|
||||
t.expects(items.at(9).some.as_u64().value == 15 && items.at(10).some.as_u64().value == 31 && items.at(11).some.as_u64().value == 777);
|
||||
t.expects(items.at(12).some.as_f64().value == 1.5 && items.at(13).some.as_f64().value == 0.5);
|
||||
t.expects(items.at(14).some.as_i64().value == 1000 && items.at(12).some.as_i64() is error);
|
||||
t.expects(items.at(15).some.as_f64().value > 1e308 && items.at(16).some.as_f64().value < -1e308);
|
||||
val nan := items.at(17).some.as_f64().value;
|
||||
t.expects(nan != nan);
|
||||
|
||||
# YAML 1.1's words and sexagesimals are text, and quotes make text.
|
||||
t.expects(items.at(18).some.as_text().value == "yes" && items.at(19).some.as_text().value == "1:30");
|
||||
t.expects(items.at(20).some.as_text().value == "12" && items.at(21).some.as_text().value == "true");
|
||||
|
||||
# Text is read only from text, as in JSON: `1.10` is a number.
|
||||
t.expects(items.at(22).some.kind == yaml::kind::number && items.at(22).some.as_text() is error);
|
||||
t.expects(items.at(22).some.as_text().error.kind == yaml::error_kind::wrong_kind);
|
||||
|
||||
# Any scalar reads as the text written, where its kind is wanted as text.
|
||||
t.expects(items.at(22).some.as_scalar().value == "1.10" && items.at(4).some.as_scalar().value == "true");
|
||||
t.expects(items.at(10).some.as_scalar().value == "0x1F" && items.at(14).some.as_scalar().value == "1e3");
|
||||
t.expects(items.at(0).some.as_scalar().value == "" && items.at(21).some.as_scalar().value == "true");
|
||||
t.expects(items.as_scalar() is error);
|
||||
}
|
||||
|
||||
@test def yaml_tags_decide_what_scalars_are(t: &mut tester): void {
|
||||
val doc := yaml::parse("""
|
||||
%TAG !e! tag:example.com,2000:
|
||||
---
|
||||
a: !!str 123
|
||||
b: !!int "123"
|
||||
c: !!float 1
|
||||
d: ! 12
|
||||
e: !!null ""
|
||||
f: !local x
|
||||
g: !e!widget%21 w
|
||||
h: !<tag:yaml.org,2002:bool> true
|
||||
""") else (_) {
|
||||
t.expects(false);
|
||||
return;
|
||||
};
|
||||
val root := doc.root;
|
||||
t.expects(root.get("a").some.as_text().value == "123" && root.get("b").some.as_i64().value == 123);
|
||||
t.expects(root.get("c").some.as_f64().value == 1.0 && root.get("d").some.as_text().value == "12");
|
||||
t.expects(root.get("e").some.is_null && root.get("f").some.tag.some == "!local");
|
||||
t.expects(root.get("g").some.tag.some == "tag:example.com,2000:widget!" && root.get("h").some.as_bool().value);
|
||||
t.expects(root.get("a").some.tag.some == "tag:yaml.org,2002:str" && root.get("d").some.tag.some == "!");
|
||||
|
||||
t.expects(yaml_failure("!!int abc").some == yaml::error_kind::mistagged);
|
||||
t.expects(yaml_failure("!!bool yes").some == yaml::error_kind::mistagged);
|
||||
t.expects(yaml_failure("!!seq {a: 1}").some == yaml::error_kind::mistagged);
|
||||
t.expects(yaml_failure("!x!y z").some == yaml::error_kind::unknown_handle);
|
||||
}
|
||||
|
||||
@test def yaml_folds_and_chomps_block_scalars(t: &mut tester): void {
|
||||
val doc := yaml::parse("""
|
||||
clip: |
|
||||
one
|
||||
two
|
||||
|
||||
strip: |-
|
||||
one
|
||||
keep: |+
|
||||
one
|
||||
|
||||
|
||||
folded: >
|
||||
one
|
||||
two
|
||||
|
||||
three
|
||||
indented
|
||||
four
|
||||
indicated: |2
|
||||
two more
|
||||
at two
|
||||
""") else (e) {
|
||||
t.expects(false);
|
||||
return;
|
||||
};
|
||||
val root := doc.root;
|
||||
t.expects(root.get("clip").some.as_text().value == "one\ntwo\n");
|
||||
t.expects(root.get("strip").some.as_text().value == "one");
|
||||
t.expects(root.get("keep").some.as_text().value == "one\n\n\n");
|
||||
t.expects(root.get("folded").some.as_text().value == "one two\nthree\n indented\nfour\n");
|
||||
t.expects(root.get("indicated").some.as_text().value == " two more\nat two\n");
|
||||
}
|
||||
|
||||
@test def yaml_reads_flow_scalars_and_their_escapes(t: &mut tester): void {
|
||||
val doc := yaml::parse("""
|
||||
- "tab\\t nul\\0 bell\\a esc\\e nel\\N nbsp\\_ ls\\L ps\\P"
|
||||
- "hex\\x41 u\\u00e9 U\\U0001F600 slash\\/ quote\\" back\\\\"
|
||||
- 'it''s'
|
||||
- "folded
|
||||
over
|
||||
|
||||
lines"
|
||||
- plain words
|
||||
folded too
|
||||
- "joined\\
|
||||
without a space"
|
||||
""") else (_) {
|
||||
t.expects(false);
|
||||
return;
|
||||
};
|
||||
val items := doc.root;
|
||||
t.expects(items.at(0).some.as_text().value == "tab\t nul\0 bell\x07 esc\x1B nel\xC2\x85 nbsp\xC2\xA0 ls\xE2\x80\xA8 ps\xE2\x80\xA9");
|
||||
t.expects(items.at(1).some.as_text().value == "hexA ué U😀 slash/ quote\" back\\");
|
||||
t.expects(items.at(2).some.as_text().value == "it's");
|
||||
t.expects(items.at(3).some.as_text().value == "folded over\nlines");
|
||||
t.expects(items.at(4).some.as_text().value == "plain words folded too");
|
||||
t.expects(items.at(5).some.as_text().value == "joinedwithout a space");
|
||||
|
||||
t.expects(yaml_failure("\"bad \\q escape\"").some == yaml::error_kind::invalid_escape);
|
||||
t.expects(yaml_failure("\"\\uD800\"").some == yaml::error_kind::invalid_escape);
|
||||
}
|
||||
|
||||
@test def yaml_aliases_name_anchored_nodes(t: &mut tester): void {
|
||||
val doc := yaml::parse("""
|
||||
base: &base
|
||||
name: shared
|
||||
size: 3
|
||||
copy: *base
|
||||
list: &list [1, 2]
|
||||
again: *list
|
||||
""") else (_) {
|
||||
t.expects(false);
|
||||
return;
|
||||
};
|
||||
val root := doc.root;
|
||||
t.expects(root.get("copy").some.kind == yaml::kind::mapping && root.get("copy").some.get("size").some.as_i64().value == 3);
|
||||
t.expects(root.get("again").some.at(1).some.as_i64().value == 2 && root.get("again").some.length == 2);
|
||||
|
||||
t.expects(yaml_failure("a: *nowhere").some == yaml::error_kind::unknown_anchor);
|
||||
t.expects(yaml_failure("&a [1, *a]").some == yaml::error_kind::recursive_alias);
|
||||
|
||||
# The billion laughs stand for far more nodes than the limit.
|
||||
val laughs := """
|
||||
a: &a ["lol","lol","lol","lol","lol","lol","lol","lol","lol"]
|
||||
b: &b [*a,*a,*a,*a,*a,*a,*a,*a,*a]
|
||||
c: &c [*b,*b,*b,*b,*b,*b,*b,*b,*b]
|
||||
d: &d [*c,*c,*c,*c,*c,*c,*c,*c,*c]
|
||||
e: &e [*d,*d,*d,*d,*d,*d,*d,*d,*d]
|
||||
f: &f [*e,*e,*e,*e,*e,*e,*e,*e,*e]
|
||||
g: &g [*f,*f,*f,*f,*f,*f,*f,*f,*f]
|
||||
""";
|
||||
t.expects(yaml_failure(laughs).some == yaml::error_kind::too_many_aliases);
|
||||
t.expects(yaml_failure("a: &a [1]\nb: [*a, *a]", yaml::limits { aliases = 3 }).some == yaml::error_kind::too_many_aliases);
|
||||
t.expects(yaml_failure("a: &a [1]\nb: [*a, *a]", yaml::limits { aliases = 4 }) is none);
|
||||
}
|
||||
|
||||
@test def yaml_merge_keys_take_members_a_mapping_lacks(t: &mut tester): void {
|
||||
val doc := yaml::parse("""
|
||||
defaults: &defaults
|
||||
image: base
|
||||
restart: always
|
||||
ports: [80]
|
||||
extra: &extra
|
||||
restart: never
|
||||
user: root
|
||||
web:
|
||||
<<: *defaults
|
||||
image: web
|
||||
worker:
|
||||
<<: [*extra, *defaults]
|
||||
literal:
|
||||
"<<": not merged
|
||||
""") else (_) {
|
||||
t.expects(false);
|
||||
return;
|
||||
};
|
||||
val root := doc.root;
|
||||
val web := root.get("web").some;
|
||||
t.expects(web.length == 3 && web.get("image").some.as_text().value == "web");
|
||||
t.expects(web.get("restart").some.as_text().value == "always" && web.get("<<") is none);
|
||||
|
||||
# Its own members come first, then those merged in, an earlier mapping before a later.
|
||||
var names := string::new(nothing {});
|
||||
for each in root.get("worker").some.members() {
|
||||
names.append(each.name);
|
||||
names.push(' ');
|
||||
}
|
||||
t.expects(names.view() == "restart user image ports ");
|
||||
t.expects(root.get("worker").some.get("restart").some.as_text().value == "never");
|
||||
t.expects(root.get("literal").some.get("<<").some.as_text().value == "not merged");
|
||||
|
||||
val read := yaml::decode::<service>(web, serial::options { strict = true }) else (_) {
|
||||
t.expects(false);
|
||||
return;
|
||||
};
|
||||
t.expects(read.image.view() == "web" && read.ports.length == 1 && read.restart.some.view() == "always");
|
||||
}
|
||||
|
||||
@test def yaml_keeps_repeated_keys(t: &mut tester): void {
|
||||
val doc := yaml::parse("a: 1\nb: 2\na: 3\n") else (_) {
|
||||
t.expects(false);
|
||||
return;
|
||||
};
|
||||
t.expects(doc.root.length == 3 && doc.root.get("a").some.as_i64().value == 1);
|
||||
var total: i64 = 0;
|
||||
for each in doc.root.get_all("a") {
|
||||
total += each.as_i64().value;
|
||||
}
|
||||
t.expects(total == 4);
|
||||
|
||||
t.expects(yaml::from_text::<two_keys>("a: 1\nb: 2\na: 3\n").value.a == 1);
|
||||
t.expects(yaml::from_text::<two_keys>("a: 1\nb: 2\na: 3\n", serial::options { strict = true }).error.kind
|
||||
== yaml::error_kind::repeated_member);
|
||||
}
|
||||
|
||||
@test def yaml_streams_hold_documents(t: &mut tester): void {
|
||||
val doc := yaml::parse("--- first\n--- [second]\n...\n# only a comment\n--- {third: 3}\n") else (_) {
|
||||
t.expects(false);
|
||||
return;
|
||||
};
|
||||
t.expects(doc.document_count == 3 && doc.root.as_text().value == "first");
|
||||
var kinds := vector::<yaml::kind>::new();
|
||||
for each in doc.documents() {
|
||||
kinds.append(each.kind);
|
||||
}
|
||||
t.expects(kinds.length == 3 && kinds[1] == yaml::kind::sequence && kinds[2] == yaml::kind::mapping);
|
||||
|
||||
val empty := yaml::parse("# nothing but a comment\n") else (_) {
|
||||
t.expects(false);
|
||||
return;
|
||||
};
|
||||
t.expects(empty.document_count == 0 && empty.root.is_null);
|
||||
|
||||
match yaml::from_text::<service>("image: a\nports: []\n---\nimage: b\nports: []\n") {
|
||||
value(&_) => t.expects(false);
|
||||
error(e) => t.expects(e.kind == yaml::error_kind::more_documents && e.line == 3);
|
||||
}
|
||||
}
|
||||
|
||||
@test def yaml_errors_say_where_they_are(t: &mut tester): void {
|
||||
t.expects(yaml_fails_at("a:\n\tb: 1\n", yaml::error_kind::tab_indent, 2, 2));
|
||||
t.expects(yaml_fails_at("a: [1, 2\n", yaml::error_kind::unexpected_end, 2, 1));
|
||||
t.expects(yaml_fails_at("a: 1\nb\nc: 2\n", yaml::error_kind::missing_colon, 2, 1));
|
||||
t.expects(yaml_fails_at("key: \"text\"# comment\n", yaml::error_kind::unexpected_byte, 1, 12));
|
||||
t.expects(yaml_fails_at("ok: \x01\n", yaml::error_kind::invalid_character, 1, 5));
|
||||
t.expects(yaml_fails_at("ok: \xC0\x80\n", yaml::error_kind::invalid_utf8, 1, 5));
|
||||
t.expects(yaml_fails_at("%YAML 2.0\n--- a\n", yaml::error_kind::unsupported_version, 1, 1));
|
||||
t.expects(yaml_failure("%YAML 1.2\n%YAML 1.2\n--- a\n").some == yaml::error_kind::invalid_directive);
|
||||
|
||||
# Collections nest no deeper than the limit.
|
||||
t.expects(yaml_failure("[[[[1]]]]", yaml::limits { depth = 3 }).some == yaml::error_kind::too_deep);
|
||||
t.expects(yaml_failure("[[[[1]]]]", yaml::limits { depth = 4 }) is none);
|
||||
|
||||
match yaml::parse("a:\n - 1\n - 2\n") {
|
||||
value(&_) => t.expects(false);
|
||||
error(e) => t.expects(e.line == 3 && e.describe().view().starts_with("3:"));
|
||||
}
|
||||
}
|
||||
|
||||
@test def yaml_reader_gives_events(t: &mut tester): void {
|
||||
var events := yaml::reader::new("--- &a {x: [1, 'two']}\n...\n");
|
||||
var seen := string::new(nothing {});
|
||||
while events.next() is some(read) {
|
||||
match read.kind {
|
||||
begin_document => seen.append(read.explicit ? "+DOC--- " : "+DOC ");
|
||||
end_document => seen.append(read.explicit ? "-DOC... " : "-DOC ");
|
||||
begin_mapping => seen.append($"+MAP&{read.anchor} ".view());
|
||||
end_mapping => seen.append("-MAP ");
|
||||
begin_sequence => seen.append(read.style == yaml::style::flow ? "+SEQ[] " : "+SEQ ");
|
||||
end_sequence => seen.append("-SEQ ");
|
||||
scalar => seen.append($"={read.text}{read.style == yaml::style::single ? "'" : ""} ".view());
|
||||
alias => seen.append($"*{read.text} ".view());
|
||||
}
|
||||
}
|
||||
t.expects(seen.view() == "+DOC--- +MAP&a =x +SEQ[] =1 =two' -SEQ -MAP -DOC... " && events.error is none);
|
||||
|
||||
var skipping := yaml::reader::new("[{a: [1, 2]}, last]");
|
||||
_ = skipping.next();
|
||||
_ = skipping.next();
|
||||
t.expects(skipping.skip());
|
||||
t.expects(skipping.next().some.text == "last");
|
||||
}
|
||||
|
||||
@test def yaml_writer_writes_block_style(t: &mut tester): void {
|
||||
var w := yaml::writer::new();
|
||||
w.begin_object();
|
||||
w.member("name"); w.text("sakura");
|
||||
w.member("version"); w.text("1.10");
|
||||
w.member("targets"); w.begin_array(); w.text("x64"); w.text("vm"); w.end_array();
|
||||
w.member("notes"); w.text("Built nightly.\nShipped weekly.\n");
|
||||
w.end_object();
|
||||
t.expects(w.finish().value == "name: sakura\nversion: \"1.10\"\ntargets:\n - x64\n - vm\nnotes: |\n Built nightly.\n Shipped weekly.\n");
|
||||
|
||||
# Text that would read as something else, or not as written, is quoted.
|
||||
w.clear();
|
||||
w.begin_array();
|
||||
for word in ["NO", "yes", "~", "null", "2026-10-06", "1_000", "- x", "a: b", "a #b", " pad", "", "<<", "x:", "a#b", "plain text"] {
|
||||
w.text(word);
|
||||
}
|
||||
w.end_array();
|
||||
t.expects(w.finish().value == """
|
||||
- "NO"
|
||||
- "yes"
|
||||
- "~"
|
||||
- "null"
|
||||
- "2026-10-06"
|
||||
- "1_000"
|
||||
- "- x"
|
||||
- "a: b"
|
||||
- "a #b"
|
||||
- " pad"
|
||||
- ""
|
||||
- "<<"
|
||||
- "x:"
|
||||
- a#b
|
||||
- plain text
|
||||
""");
|
||||
|
||||
# Collections nest, empty ones are written in place, and numbers read back as they were.
|
||||
w.clear();
|
||||
w.begin_object();
|
||||
w.member("list");
|
||||
w.begin_array();
|
||||
w.begin_object(); w.member("a"); w.signed(1); w.member("b"); w.real(2.5); w.end_object();
|
||||
w.begin_array(); w.boolean(true); w.empty(); w.end_array();
|
||||
w.begin_object(); w.end_object();
|
||||
w.end_array();
|
||||
w.member("none"); w.begin_array(); w.end_array();
|
||||
w.member("floats"); w.begin_array(); w.real(3.0); w.real(-1.0 / 0.0); w.real(0.0 / 0.0); w.unsigned(18446744073709551615); w.end_array();
|
||||
w.member("ends"); w.text(" two\nlines\n\n");
|
||||
w.end_object();
|
||||
w.document();
|
||||
w.text("second\tdocument");
|
||||
t.expects(w.finish().value == """
|
||||
list:
|
||||
- a: 1
|
||||
b: 2.5
|
||||
- - true
|
||||
- null
|
||||
- {}
|
||||
none: []
|
||||
floats:
|
||||
- 3.0
|
||||
- -.inf
|
||||
- .nan
|
||||
- 18446744073709551615
|
||||
ends: |2+
|
||||
two
|
||||
lines
|
||||
|
||||
---
|
||||
"second\\tdocument"
|
||||
""");
|
||||
|
||||
# Four spaces a level.
|
||||
t.expects(yaml_rewritten("a: {b: [1, {c: 2}]}", 4).view() == "a:\n b:\n - 1\n - c: 2\n");
|
||||
|
||||
# Text that is not UTF-8 is written as `null`, and finishing gives why.
|
||||
w.clear();
|
||||
w.text("bad\xFF");
|
||||
t.expects(w.finish().error.kind == yaml::error_kind::invalid_utf8 && w._text.view() == "null\n");
|
||||
}
|
||||
|
||||
@test def yaml_writer_passes_its_text_to_streams(t: &mut tester): void {
|
||||
var services := vector::<service>::new();
|
||||
for i in 0..400 {
|
||||
var ports := vector::<u16>::new();
|
||||
ports.append((8000 + i) as u16);
|
||||
services.append(service { image = $"image-{i}", ports = move(ports) });
|
||||
}
|
||||
val whole := yaml::to_text(&services) else (_) {
|
||||
t.expects(false);
|
||||
return;
|
||||
};
|
||||
t.expects(whole.length > 16384);
|
||||
|
||||
var said := string::new(nothing {});
|
||||
yaml::write_to(&services, &mut said) else (_) {
|
||||
t.expects(false);
|
||||
return;
|
||||
};
|
||||
t.expects(said.view() == whole.view());
|
||||
|
||||
val back := yaml::from_text::<vector<service>>(said.view()) else (_) {
|
||||
t.expects(false);
|
||||
return;
|
||||
};
|
||||
t.expects(back.length == 400 && back[399].ports[0] == 8399 && back[7].image.view() == "image-7" && back[7].restart is none);
|
||||
}
|
||||
|
||||
@test def yaml_documents_round_trip_through_the_writer(t: &mut tester): void {
|
||||
val text := """
|
||||
anchors:
|
||||
- &x {deep: [1, "two", 3.5, null, true]}
|
||||
- *x
|
||||
"quoted key": 'single'
|
||||
multi: "line one\\nline two\\n"
|
||||
folded: >
|
||||
a
|
||||
b
|
||||
? complex
|
||||
: value
|
||||
""";
|
||||
val first := yaml_rewritten(text);
|
||||
val second := yaml_rewritten(first.view());
|
||||
t.expects(first.view() == second.view());
|
||||
t.expects(first.view().starts_with("anchors:\n - deep:\n - 1\n - two\n - 3.5\n - null\n - true\n - deep:"));
|
||||
}
|
||||
Reference in new issue
Block a user