Initial Commit
This commit is contained in:
1625 files changed
+43592
No files matched your search
@@ -0,0 +1,439 @@
|
||||
# Tests of `std::xml`: its text, the reader, the document type, and the document.
|
||||
import std::io;
|
||||
import std::xml;
|
||||
import std::testing;
|
||||
import system;
|
||||
|
||||
|
||||
# Whether parsing the text fails with the kind of error, at the line and column.
|
||||
def fails_at(text: strview, kind: xml::error_kind, line: u32, column: u32): bool {
|
||||
match xml::parse(text) {
|
||||
value(&_) => return false;
|
||||
error(e) => return e.kind == kind && e.line == line && e.column == column;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
# Whether parsing the text fails with the kind of error, wherever.
|
||||
def fails_with(text: strview, kind: xml::error_kind): bool {
|
||||
match xml::parse(text) {
|
||||
value(&_) => return false;
|
||||
error(e) => return e.kind == kind;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
# The tokens a reader gives, each as its kind and what it names or holds.
|
||||
def tokens_of(text: strview): string {
|
||||
var tokens := xml::reader::new(text);
|
||||
var made := string::new(nothing {});
|
||||
while tokens.next() is some(read) {
|
||||
match read.kind {
|
||||
start => made.append($"<{read.name}{read.empty ? "/" : ""}>");
|
||||
end => made.append($"</{read.name}>");
|
||||
text => made.append($"{read.cdata ? "cdata" : "text"}[{read.text}]");
|
||||
comment => made.append($"comment[{read.text}]");
|
||||
instruction => made.append($"?{read.name}[{read.text}]");
|
||||
}
|
||||
}
|
||||
if tokens.error is some(e) {
|
||||
made.append($" error {e.describe()}");
|
||||
}
|
||||
return made;
|
||||
}
|
||||
|
||||
# The root element's text, or the error parsing gives.
|
||||
def root_text(text: strview): string {
|
||||
val doc := xml::parse(text) else (e) { return e.describe(); };
|
||||
return doc.root.text();
|
||||
}
|
||||
|
||||
# The value of the root's attribute, or the error parsing gives.
|
||||
def root_attribute(text: strview, name: strview): string {
|
||||
val doc := xml::parse(text) else (e) { return e.describe(); };
|
||||
val value := doc.root.attribute(name) else { return string::from("(none)", nothing {}); };
|
||||
return string::from(value, nothing {});
|
||||
}
|
||||
|
||||
@test def xml_escapes_text_and_attributes(t: &mut tester): void {
|
||||
var text := string::new(nothing {});
|
||||
xml::escape("a < b && c > d \"q\"\r\n\t", &mut text);
|
||||
t.expects(text.span == "a < b && c > d \"q\" \n\t");
|
||||
|
||||
var value := string::new(nothing {});
|
||||
xml::escape("say \"hi\"\n\tthen <go>", &mut value, true);
|
||||
t.expects(value.span == "say "hi" 	then <go>");
|
||||
|
||||
# What is escaped reads back as it was.
|
||||
var wrapped := string::from("<a x=\"", nothing {});
|
||||
wrapped.append(value.span);
|
||||
wrapped.append("\">");
|
||||
wrapped.append(text.span);
|
||||
wrapped.append("</a>");
|
||||
t.expects(root_attribute(wrapped.span, "x").span == "say \"hi\"\n\tthen <go>");
|
||||
t.expects(root_text(wrapped.span).span == "a < b && c > d \"q\"\r\n\t");
|
||||
}
|
||||
|
||||
@test def xml_reader_gives_tokens(t: &mut tester): void {
|
||||
t.expects(tokens_of("<?xml version=\"1.0\"?><!-- c --><a><b/>x<![CDATA[<y>]]><<?go now?></a>").span
|
||||
== "comment[ c ]<a><b/></b>text[x]cdata[<y>]text[<]?go[now]</a>");
|
||||
|
||||
# Whitespace outside the root element is no token, and within it is.
|
||||
t.expects(tokens_of("\n<a>\n <b> </b>\n</a>\n").span == "<a>text[\n ]<b>text[ ]</b>text[\n]</a>");
|
||||
|
||||
# Line breaks of every kind are `\n`.
|
||||
t.expects(tokens_of("<a>1\r\n2\r3<!--\r\n--></a>").span == "<a>text[1\n2\n3]comment[\n]</a>");
|
||||
|
||||
var tokens := xml::reader::new("<a>\n <b c='d'/></a>");
|
||||
val first := tokens.next().some;
|
||||
t.expects(first.kind == xml::token_kind::start && first.offset == 0 && tokens.depth == 1);
|
||||
_ = tokens.next();
|
||||
val second := tokens.next().some;
|
||||
t.expects(second.name == "b" && second.empty && second.offset == 6 && tokens.attributes.length == 1);
|
||||
t.expects(tokens.attributes[0].name == "c" && tokens.attributes[0].value == "d" && tokens.attributes[0].offset == 9);
|
||||
val closing := tokens.next().some;
|
||||
t.expects(closing.kind == xml::token_kind::end && closing.name == "b" && tokens.depth == 1);
|
||||
}
|
||||
|
||||
@test def xml_reader_skips_elements(t: &mut tester): void {
|
||||
var tokens := xml::reader::new("<a><skip><x><y/></x>text</skip><keep/></a>");
|
||||
_ = tokens.next();
|
||||
_ = tokens.next();
|
||||
t.expects(tokens.skip());
|
||||
val next := tokens.next().some;
|
||||
t.expects(next.kind == xml::token_kind::start && next.name == "keep");
|
||||
}
|
||||
|
||||
@test def xml_reader_stops_at_the_first_error(t: &mut tester): void {
|
||||
var tokens := xml::reader::new("<a><b></c></a>");
|
||||
var count := 0;
|
||||
while tokens.next() is some(_) {
|
||||
count++;
|
||||
}
|
||||
t.expects(count == 2);
|
||||
t.expects(tokens.error.some.kind == xml::error_kind::mismatched_tag && tokens.error.some.column == 7);
|
||||
t.expects(tokens.next() is none);
|
||||
}
|
||||
|
||||
@test def xml_declaration_is_read(t: &mut tester): void {
|
||||
val doc := xml::parse("\xEF\xBB\xBF<?xml version='1.0' encoding=\"utf-8\" standalone='yes' ?><a/>") else (e) {
|
||||
t.expects(false);
|
||||
return;
|
||||
};
|
||||
t.expects(doc.version == "1.0" && doc.encoding == "utf-8" && doc.standalone);
|
||||
|
||||
val bare := xml::parse("<a/>") else (e) { t.expects(false); return; };
|
||||
t.expects(bare.version == "" && bare.encoding == "" && !bare.standalone);
|
||||
|
||||
# Another encoding is read while the text is ASCII, which it is the same as.
|
||||
t.expects(xml::parse("<?xml version='1.0' encoding='ISO-8859-1'?><a>plain</a>") is value);
|
||||
t.expects(fails_at("<?xml version='1.0' encoding='ISO-8859-1'?><a>caf\xE9</a>", xml::error_kind::unsupported_encoding, 1, 50));
|
||||
t.expects(fails_at("\xFF\xFE<\x00a\x00/\x00>\x00", xml::error_kind::unsupported_encoding, 1, 1));
|
||||
|
||||
t.expects(fails_at(" <?xml version='1.0'?><a/>", xml::error_kind::invalid_declaration, 1, 2));
|
||||
t.expects(fails_at("<?xml version='2.0'?><a/>", xml::error_kind::invalid_declaration, 1, 20));
|
||||
t.expects(fails_at("<?xml encoding='UTF-8'?><a/>", xml::error_kind::invalid_declaration, 1, 7));
|
||||
t.expects(fails_at("<?xml version='1.0'standalone='yes'?><a/>", xml::error_kind::invalid_declaration, 1, 20));
|
||||
t.expects(fails_at("<?xml version='1.0' standalone='maybe'?><a/>", xml::error_kind::invalid_declaration, 1, 39));
|
||||
t.expects(fails_at("<a><?XML no?></a>", xml::error_kind::invalid_declaration, 1, 4));
|
||||
t.expects(xml::parse("<?xml-stylesheet href='a.css'?><a/>") is value);
|
||||
}
|
||||
|
||||
@test def xml_errors_say_where(t: &mut tester): void {
|
||||
t.expects(fails_at("", xml::error_kind::no_root, 1, 1));
|
||||
t.expects(fails_at("<!-- only -->\n", xml::error_kind::no_root, 2, 1));
|
||||
t.expects(fails_at("<a>", xml::error_kind::unexpected_end, 1, 4));
|
||||
t.expects(fails_at("<a>\n</b>", xml::error_kind::mismatched_tag, 2, 1));
|
||||
t.expects(fails_at("<a x='1' x=\"2\"/>", xml::error_kind::repeated_attribute, 1, 10));
|
||||
t.expects(fails_at("<a x='1'y='2'/>", xml::error_kind::unexpected_byte, 1, 9));
|
||||
t.expects(fails_at("<a x=1/>", xml::error_kind::unexpected_byte, 1, 6));
|
||||
t.expects(fails_at("<a x/>", xml::error_kind::unexpected_byte, 1, 5));
|
||||
t.expects(fails_at("<a x='<'/>", xml::error_kind::unexpected_byte, 1, 7));
|
||||
t.expects(fails_at("<a>]]></a>", xml::error_kind::unexpected_byte, 1, 4));
|
||||
t.expects(fails_at("<a><!-- a -- b --></a>", xml::error_kind::unexpected_byte, 1, 11));
|
||||
t.expects(fails_at("<a><!-- a ---></a>", xml::error_kind::unexpected_byte, 1, 11));
|
||||
t.expects(fails_at("<a/>text", xml::error_kind::text_outside_root, 1, 5));
|
||||
t.expects(fails_at("text<a/>", xml::error_kind::text_outside_root, 1, 1));
|
||||
t.expects(fails_at("<a/><b/>", xml::error_kind::second_root, 1, 5));
|
||||
t.expects(fails_at("<a>&foo;</a>", xml::error_kind::undeclared_entity, 1, 4));
|
||||
t.expects(fails_at("<a>�</a>", xml::error_kind::invalid_character, 1, 4));
|
||||
t.expects(fails_at("<a>�</a>", xml::error_kind::invalid_character, 1, 4));
|
||||
t.expects(fails_at("<a>�</a>", xml::error_kind::invalid_character, 1, 4));
|
||||
t.expects(fails_at("<a>�</a>", xml::error_kind::invalid_character, 1, 4));
|
||||
t.expects(fails_at("<a>& b</a>", xml::error_kind::invalid_reference, 1, 4));
|
||||
t.expects(fails_at("<a>&#x;</a>", xml::error_kind::invalid_reference, 1, 4));
|
||||
t.expects(fails_at("<a>&</a>", xml::error_kind::invalid_reference, 1, 4));
|
||||
t.expects(fails_at("<a>\x01</a>", xml::error_kind::invalid_character, 1, 4));
|
||||
t.expects(fails_at("<a>\xEF\xBF\xBE</a>", xml::error_kind::invalid_character, 1, 4));
|
||||
t.expects(fails_at("<a>\n\xC0\x80</a>", xml::error_kind::invalid_utf8, 2, 1));
|
||||
t.expects(fails_at("<a><!DOCTYPE a></a>", xml::error_kind::unexpected_byte, 1, 4));
|
||||
t.expects(fails_at("<a/><!DOCTYPE a>", xml::error_kind::unexpected_byte, 1, 5));
|
||||
t.expects(fails_at("<!DOCTYPE a><!DOCTYPE a><a/>", xml::error_kind::unexpected_byte, 1, 13));
|
||||
t.expects(fails_at("<a><![CDATA[x</a>", xml::error_kind::unexpected_end, 1, 18));
|
||||
t.expects(fails_at("<1a/>", xml::error_kind::unexpected_byte, 1, 2));
|
||||
t.expects(xml::parse("<a></a >") is value);
|
||||
}
|
||||
|
||||
@test def xml_names_may_be_any_language(t: &mut tester): void {
|
||||
val doc := xml::parse("<名前 属性='値'><é.x-y/></名前>") else (e) { t.expects(false); return; };
|
||||
t.expects(doc.root.name == "名前" && doc.root.attribute("属性").unwrap() == "値");
|
||||
t.expects(doc.root.child("é.x-y") is some);
|
||||
t.expects(xml::parse("<a\xC2\xB7/>") is value);
|
||||
t.expects(fails_at("<\xC2\xB7a/>", xml::error_kind::unexpected_byte, 1, 2));
|
||||
}
|
||||
|
||||
@test def xml_attributes_are_normalized(t: &mut tester): void {
|
||||
# Each whitespace character is a space, a line break of the document's own one
|
||||
# however written, and a reference's character as it is.
|
||||
t.expects(root_attribute("<a x='1\t2\n3\r\n4\r5	6 7'/>", "x").span == "1 2 3 4 5\t6\n7");
|
||||
t.expects(root_attribute("<a x=\"it's\" y='say \"hi\"'/>", "y").span == "say \"hi\"");
|
||||
t.expects(root_attribute("<a x='<>&'"AB'/>", "x").span == "<>&'\"AB");
|
||||
}
|
||||
|
||||
@test def xml_entities_are_expanded(t: &mut tester): void {
|
||||
val doctype := """
|
||||
<!DOCTYPE a [
|
||||
<!ENTITY name "world">
|
||||
<!ENTITY greet "hello, &name;!">
|
||||
<!ENTITY markup "<b>bold &name;</b>">
|
||||
<!ENTITY spaced "one two">
|
||||
<!ENTITY lt2 "&#60;">
|
||||
]>
|
||||
""";
|
||||
var text := string::from(doctype, nothing {});
|
||||
text.append("<a x='&greet; &spaced;'>&greet; &markup; <2;</a>");
|
||||
val doc := xml::parse(text.span) else (e) { println(e.describe()); t.expects(false); return; };
|
||||
t.expects(doc.root.attribute("x").unwrap() == "hello, world! one two");
|
||||
t.expects(doc.root.text().span == "hello, world! bold world <");
|
||||
val bold := doc.root.child("b") else { t.expects(false); return; };
|
||||
t.expects(bold.text().span == "bold world");
|
||||
|
||||
# A character reference within an entity's value is replaced as it is declared,
|
||||
# so `&#60;` stands for `<`, a `<` read as text where it is used.
|
||||
t.expects(root_text("<!DOCTYPE a [<!ENTITY e '&#60;b/>'>]><a>&e;</a>").span == "<b/>");
|
||||
t.expects(root_text("<!DOCTYPE a [<!ENTITY e '<b/>'>]><a>&e;</a>").span == "");
|
||||
|
||||
# Text tokens go on across where an entity's text begins and ends.
|
||||
t.expects(tokens_of("<!DOCTYPE a [<!ENTITY e 'middle'>]><a>before &e; after</a>").span
|
||||
== "<a>text[before middle after]</a>");
|
||||
|
||||
# A token within an entity is where the entity is referred to.
|
||||
var tokens := xml::reader::new("<!DOCTYPE a [<!ENTITY e '<b/>'>]><a> &e;</a>");
|
||||
_ = tokens.next();
|
||||
_ = tokens.next();
|
||||
val inner := tokens.next().some;
|
||||
t.expects(inner.name == "b" && inner.offset == 38);
|
||||
}
|
||||
|
||||
@test def xml_entities_are_checked(t: &mut tester): void {
|
||||
t.expects(fails_at("<!DOCTYPE a [<!ENTITY e '&e;'>]><a>&e;</a>", xml::error_kind::recursive_entity, 1, 36));
|
||||
t.expects(fails_at("<!DOCTYPE a [<!ENTITY e '&f;'><!ENTITY f '&e;'>]><a x='&e;'/>", xml::error_kind::recursive_entity, 1, 56));
|
||||
t.expects(fails_at("<!DOCTYPE a [<!ENTITY e '<b>'>]><a>&e;</b></a>", xml::error_kind::unbalanced_entity, 1, 36));
|
||||
t.expects(fails_at("<!DOCTYPE a [<!ENTITY e '</a><a>'>]><a>&e;</a>", xml::error_kind::unbalanced_entity, 1, 40));
|
||||
t.expects(fails_at("<!DOCTYPE a [<!ENTITY e SYSTEM 'e.xml'>]><a>&e;</a>", xml::error_kind::external_entity, 1, 45));
|
||||
t.expects(fails_at("<!DOCTYPE a [<!NOTATION n SYSTEM 'n'><!ENTITY e SYSTEM 'e.png' NDATA n>]><a>&e;</a>",
|
||||
xml::error_kind::external_entity, 1, 77));
|
||||
t.expects(fails_at("<!DOCTYPE a [<!ENTITY e '<'>]><a x='&e;'/>", xml::error_kind::unexpected_byte, 1, 37));
|
||||
t.expects(fails_at("<!DOCTYPE a [<!ENTITY e 'a�'>]><a/>", xml::error_kind::invalid_character, 1, 27));
|
||||
t.expects(fails_at("<!DOCTYPE a [<!ENTITY e '&'>]><a/>", xml::error_kind::invalid_reference, 1, 26));
|
||||
t.expects(fails_at("<!DOCTYPE a [<!ENTITY e '&'>]><a>&e;</a>", xml::error_kind::invalid_reference, 1, 38));
|
||||
t.expects(fails_at("<!DOCTYPE a [<!ENTITY e 'x'>]><a>&e</a>", xml::error_kind::invalid_reference, 1, 34));
|
||||
|
||||
# Entities standing for more text than the limit stop, rather than exhaust memory.
|
||||
var laughs := string::from("<!DOCTYPE a [<!ENTITY l0 'ha'>", nothing {});
|
||||
for level in 1..12 {
|
||||
laughs.append($"<!ENTITY l{level} '&l{level - 1};&l{level - 1};&l{level - 1};&l{level - 1};'>");
|
||||
}
|
||||
laughs.append("]><a>&l11;</a>");
|
||||
t.expects(fails_with(laughs.span, xml::error_kind::too_much_expansion));
|
||||
t.expects(xml::parse(laughs.span, xml::options { expansion = 0x10000000 }) is value);
|
||||
}
|
||||
|
||||
@test def xml_document_type_declares_attributes(t: &mut tester): void {
|
||||
val text := """
|
||||
<!DOCTYPE a [
|
||||
<!ELEMENT a (b | c)*>
|
||||
<!ELEMENT b EMPTY>
|
||||
<!ELEMENT c (#PCDATA | b)*>
|
||||
<!ATTLIST a
|
||||
kind (one | two) "one"
|
||||
tokens NMTOKENS " x y "
|
||||
given NMTOKENS #IMPLIED
|
||||
fixed CDATA #FIXED " as is "
|
||||
needed ID #REQUIRED>
|
||||
<!ATTLIST a kind CDATA "ignored">
|
||||
]>
|
||||
<a given=" p q " needed="n"/>
|
||||
""";
|
||||
val doc := xml::parse(text) else (e) { println(e.describe()); t.expects(false); return; };
|
||||
val root := doc.root;
|
||||
t.expects(root.attribute("kind").unwrap() == "one");
|
||||
t.expects(root.attribute("tokens").unwrap() == "x y");
|
||||
t.expects(root.attribute("given").unwrap() == "p q");
|
||||
t.expects(root.attribute("fixed").unwrap() == " as is ");
|
||||
var specified := 0;
|
||||
for each in root.attributes() {
|
||||
if each.specified {
|
||||
specified++;
|
||||
}
|
||||
}
|
||||
t.expects(specified == 2 && root.attributes().count() == 5);
|
||||
t.expects(doc.doctype == "a");
|
||||
}
|
||||
|
||||
@test def xml_document_type_is_checked(t: &mut tester): void {
|
||||
t.expects(xml::parse("<!DOCTYPE a SYSTEM 'a.dtd'><a/>") is value);
|
||||
t.expects(xml::parse("<!DOCTYPE a PUBLIC '-//A//DTD A 1.0//EN' 'a.dtd' [<!-- c --><?pi?>]><a/>") is value);
|
||||
t.expects(xml::parse("<!DOCTYPE a [<!ELEMENT a ((b, c?)+ | (#PCDATA))>]><a/>") is error);
|
||||
t.expects(xml::parse("<!DOCTYPE a [<!ELEMENT a ((b, c?)+ | d*)>]><a/>") is value);
|
||||
t.expects(xml::parse("<!DOCTYPE a [<!NOTATION n PUBLIC 'n'>]><a/>") is value);
|
||||
t.expects(fails_at("<!DOCTYPE a [<!ELEMENT a (b | c, d)>]><a/>", xml::error_kind::unexpected_byte, 1, 32));
|
||||
t.expects(fails_at("<!DOCTYPE a [<!ELEMENT a (#PCDATA | b)>]><a/>", xml::error_kind::unexpected_byte, 1, 39));
|
||||
t.expects(fails_at("<!DOCTYPE a [<!ATTLIST a x BOGUS #IMPLIED>]><a/>", xml::error_kind::unexpected_byte, 1, 28));
|
||||
t.expects(fails_at("<!DOCTYPE a [<!ENTITY e '%p;'>]><a/>", xml::error_kind::unexpected_byte, 1, 26));
|
||||
t.expects(fails_at("<!DOCTYPE a PUBLIC 'a{' 'a'><a/>", xml::error_kind::unexpected_byte, 1, 22));
|
||||
t.expects(fails_at("<!DOCTYPE a [<!BOGUS>]><a/>", xml::error_kind::unexpected_byte, 1, 14));
|
||||
t.expects(fails_at("<!DOCTYPE a [", xml::error_kind::unexpected_end, 1, 14));
|
||||
|
||||
# A parameter entity between declarations is read where it is referred to.
|
||||
t.expects(root_attribute("<!DOCTYPE a [<!ENTITY % decls '<!ATTLIST a x CDATA \"from\">'>%decls;]><a/>", "x").span == "from");
|
||||
|
||||
# After one not read, the declarations that follow are left out, since it may
|
||||
# declare the same names first.
|
||||
t.expects(root_attribute("<!DOCTYPE a [%outside;<!ATTLIST a x CDATA 'left out'>]><a/>", "x").span == "(none)");
|
||||
t.expects(fails_with("<?xml version='1.0' standalone='yes'?><!DOCTYPE a [%outside;]><a/>", xml::error_kind::undeclared_entity));
|
||||
}
|
||||
|
||||
@test def xml_namespaces_are_resolved(t: &mut tester): void {
|
||||
val text := """
|
||||
<root xmlns="urn:default" xmlns:p="urn:p">
|
||||
<p:item p:id="1" plain="2" xml:lang="en"/>
|
||||
<inner xmlns="">
|
||||
<bare/>
|
||||
</inner>
|
||||
<q:other xmlns:q="urn:p"/>
|
||||
</root>
|
||||
""";
|
||||
val doc := xml::parse(text) else (e) { println(e.describe()); t.expects(false); return; };
|
||||
val root := doc.root;
|
||||
t.expects(root.namespace == "urn:default" && root.prefix == "" && root.local == "root");
|
||||
|
||||
val item := root.child("p:item") else { t.expects(false); return; };
|
||||
t.expects(item.namespace == "urn:p" && item.prefix == "p" && item.local == "item");
|
||||
t.expects(item.attribute("urn:p", "id").unwrap() == "1");
|
||||
t.expects(item.attribute("", "plain").unwrap() == "2");
|
||||
t.expects(item.attribute(xml::xml_namespace, "lang").unwrap() == "en");
|
||||
t.expects(item.attribute("urn:default", "plain") is none);
|
||||
|
||||
val inner := root.child("inner") else { t.expects(false); return; };
|
||||
t.expects(inner.namespace == "" && inner.child("bare").unwrap().namespace == "");
|
||||
t.expects(root.child("q:other").unwrap().namespace == "urn:p");
|
||||
|
||||
# Without namespaces, names are as written.
|
||||
val plain := xml::parse("<a:b c:d='e'/>", xml::options { namespaces = false }) else (e) { t.expects(false); return; };
|
||||
t.expects(plain.root.name == "a:b" && plain.root.local == "a:b" && plain.root.namespace == "");
|
||||
}
|
||||
|
||||
@test def xml_namespaces_are_checked(t: &mut tester): void {
|
||||
t.expects(fails_at("<p:a/>", xml::error_kind::unbound_prefix, 1, 2));
|
||||
t.expects(fails_at("<a p:x='1'/>", xml::error_kind::unbound_prefix, 1, 4));
|
||||
t.expects(fails_at("<a xmlns:p=''/>", xml::error_kind::invalid_namespace, 1, 4));
|
||||
t.expects(fails_at("<a xmlns:xmlns='urn:x'/>", xml::error_kind::invalid_namespace, 1, 4));
|
||||
t.expects(fails_at("<a xmlns:xml='urn:x'/>", xml::error_kind::invalid_namespace, 1, 4));
|
||||
t.expects(fails_at("<a xmlns:p='http://www.w3.org/XML/1998/namespace'/>", xml::error_kind::invalid_namespace, 1, 4));
|
||||
t.expects(fails_at("<xmlns:a/>", xml::error_kind::invalid_namespace, 1, 2));
|
||||
t.expects(fails_at("<a:b:c xmlns:a='urn:a'/>", xml::error_kind::invalid_name, 1, 2));
|
||||
t.expects(fails_at("<a: xmlns:a='urn:a'/>", xml::error_kind::invalid_name, 1, 2));
|
||||
t.expects(fails_at("<a xmlns:p='urn:x' xmlns:q='urn:x' p:x='1' q:x='2'/>", xml::error_kind::repeated_attribute, 1, 44));
|
||||
t.expects(fails_at("<!DOCTYPE a [<!ENTITY a:b 'x'>]><a/>", xml::error_kind::invalid_name, 1, 23));
|
||||
t.expects(fails_at("<a><?p:i?></a>", xml::error_kind::invalid_name, 1, 6));
|
||||
t.expects(xml::parse("<a xml:space='preserve' xmlns:xml='http://www.w3.org/XML/1998/namespace'/>") is value);
|
||||
}
|
||||
|
||||
@test def xml_many_attributes_are_checked(t: &mut tester): void {
|
||||
var many := string::from("<a xmlns:p='urn:x' xmlns:q='urn:x'", nothing {});
|
||||
for index in 0..40 {
|
||||
many.append($" n{index}='{index}'");
|
||||
}
|
||||
var repeated := string::copy_of(&many);
|
||||
many.append("/>");
|
||||
t.expects(xml::parse(many.span) is value);
|
||||
|
||||
repeated.append(" n17='again'/>");
|
||||
t.expects(fails_with(repeated.span, xml::error_kind::repeated_attribute));
|
||||
|
||||
var bound := string::from("<a xmlns:p='urn:x' xmlns:q='urn:x'", nothing {});
|
||||
for index in 0..20 {
|
||||
bound.append($" p:n{index}='{index}'");
|
||||
}
|
||||
bound.append(" q:n5='again'/>");
|
||||
t.expects(fails_with(bound.span, xml::error_kind::repeated_attribute));
|
||||
}
|
||||
|
||||
@test def xml_nesting_is_limited(t: &mut tester): void {
|
||||
t.expects(xml::parse("<a><b><c/></b></a>", xml::options { depth = 3 }) is value);
|
||||
match xml::parse("<a><b><c><d/></c></b></a>", xml::options { depth = 3 }) {
|
||||
value(&_) => t.expects(false);
|
||||
error(e) => t.expects(e.kind == xml::error_kind::too_deep && e.column == 10);
|
||||
}
|
||||
|
||||
var deep := string::new(nothing {});
|
||||
for _ in 0..600 { deep.append("<a>"); }
|
||||
for _ in 0..600 { deep.append("</a>"); }
|
||||
t.expects(fails_with(deep.span, xml::error_kind::too_deep));
|
||||
t.expects(xml::parse(deep.span, xml::options { depth = 1000 }) is value);
|
||||
}
|
||||
|
||||
@test def xml_document_holds_every_node(t: &mut tester): void {
|
||||
val text := "<?xml version='1.0'?>\n<!-- before -->\n<list a='1'>\n <item>one</item>\n <!-- between -->\n <item>t<![CDATA[w]]>o</item>\n <?keep it?>\n</list>\n<!-- after -->\n";
|
||||
val doc := xml::parse(text) else (e) { t.expects(false); return; };
|
||||
|
||||
var top := string::new(nothing {});
|
||||
for each in doc.children() {
|
||||
top.append($"{each.kind}:{each.name}{each.text};");
|
||||
}
|
||||
t.expects(top.span == "comment: before ;element:list;comment: after ;");
|
||||
|
||||
val list := doc.root;
|
||||
t.expects(list.place.line == 3 && list.place.column == 1 && list.parent is none);
|
||||
var kinds := string::new(nothing {});
|
||||
for each in list.children() {
|
||||
kinds.append($"{each.kind} ");
|
||||
}
|
||||
t.expects(kinds.span == "text element text comment text element text instruction text ");
|
||||
|
||||
var items := string::new(nothing {});
|
||||
for item in list.elements() {
|
||||
items.append(item.text().span);
|
||||
items.append(",");
|
||||
t.expects(item.parent.unwrap().name == "list");
|
||||
}
|
||||
t.expects(items.span == "one,two,");
|
||||
t.expects(list.elements().count() == 2);
|
||||
|
||||
# Text and a CDATA section beside it are one node.
|
||||
val second := list.elements().nth(1) else { t.expects(false); return; };
|
||||
t.expects(second.children().count() == 1 && second.place.line == 6);
|
||||
t.expects(list.child("missing") is none && list.attribute("missing") is none);
|
||||
t.expects(list.attribute("a").unwrap() == "1");
|
||||
}
|
||||
|
||||
@test def xml_document_outlives_its_text(t: &mut tester): void {
|
||||
var text := string::from("<a x='<y'>&z</a>", nothing {});
|
||||
var doc := xml::parse(text.span) else (e) { t.expects(false); return; };
|
||||
text.clear();
|
||||
text.append("garbage that was the text");
|
||||
t.expects(doc.root.name == "a" && doc.root.attribute("x").unwrap() == "<y" && doc.root.text().span == "&z");
|
||||
|
||||
var moved := move(&mut doc);
|
||||
t.expects(moved.root.name == "a");
|
||||
}
|
||||
|
||||
@test def xml_errors_describe_themselves(t: &mut tester): void {
|
||||
match xml::parse("<a>\n <b></c>\n</a>") {
|
||||
value(&_) => t.expects(false);
|
||||
error(e) => t.expects(e.describe().span == "2:6: An end tag names another element than the one open.");
|
||||
}
|
||||
val place := xml::place_of("a\r\nb\rc\nd", 7);
|
||||
t.expects(place.line == 4 && place.column == 1);
|
||||
}
|
||||
Reference in new issue
Block a user