# 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($""); 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.view() == "a < b && c > d \"q\" \n\t"); var value := string::new(nothing {}); xml::escape("say \"hi\"\n\tthen ", &mut value, true); t.expects(value.view() == "say "hi" then <go>"); # What is escaped reads back as it was. var wrapped := string::from(""); wrapped.append(text.view()); wrapped.append(""); t.expects(root_attribute(wrapped.view(), "x").view() == "say \"hi\"\n\tthen "); t.expects(root_text(wrapped.view()).view() == "a < b && c > d \"q\"\r\n\t"); } @test def xml_reader_gives_tokens(t: &mut tester): void { t.expects(tokens_of("x]]><").view() == "comment[ c ]text[x]cdata[]text[<]?go[now]"); # Whitespace outside the root element is no token, and within it is. t.expects(tokens_of("\n\n \n\n").view() == "text[\n ]text[ ]text[\n]"); # Line breaks of every kind are `\n`. t.expects(tokens_of("1\r\n2\r3").view() == "text[1\n2\n3]comment[\n]"); var tokens := xml::reader::new("\n "); 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("text"); _ = 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(""); 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") else (e) { t.expects(false); return; }; t.expects(doc.version == "1.0" && doc.encoding == "utf-8" && doc.standalone); val bare := xml::parse("") 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("plain") is value); t.expects(fails_at("caf\xE9", 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::error_kind::invalid_declaration, 1, 2)); t.expects(fails_at("", xml::error_kind::invalid_declaration, 1, 20)); t.expects(fails_at("", xml::error_kind::invalid_declaration, 1, 7)); t.expects(fails_at("", xml::error_kind::invalid_declaration, 1, 20)); t.expects(fails_at("", xml::error_kind::invalid_declaration, 1, 39)); t.expects(fails_at("", xml::error_kind::invalid_declaration, 1, 4)); t.expects(xml::parse("") 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("\n", xml::error_kind::no_root, 2, 1)); t.expects(fails_at("", xml::error_kind::unexpected_end, 1, 4)); t.expects(fails_at("\n", xml::error_kind::mismatched_tag, 2, 1)); t.expects(fails_at("", xml::error_kind::repeated_attribute, 1, 10)); t.expects(fails_at("", xml::error_kind::unexpected_byte, 1, 9)); t.expects(fails_at("", xml::error_kind::unexpected_byte, 1, 6)); t.expects(fails_at("", xml::error_kind::unexpected_byte, 1, 5)); t.expects(fails_at("", xml::error_kind::unexpected_byte, 1, 7)); t.expects(fails_at("]]>", xml::error_kind::unexpected_byte, 1, 4)); t.expects(fails_at("", xml::error_kind::unexpected_byte, 1, 11)); t.expects(fails_at("", xml::error_kind::unexpected_byte, 1, 11)); t.expects(fails_at("text", xml::error_kind::text_outside_root, 1, 5)); t.expects(fails_at("text", xml::error_kind::text_outside_root, 1, 1)); t.expects(fails_at("", xml::error_kind::second_root, 1, 5)); t.expects(fails_at("&foo;", xml::error_kind::undeclared_entity, 1, 4)); t.expects(fails_at("�", xml::error_kind::invalid_character, 1, 4)); t.expects(fails_at("�", xml::error_kind::invalid_character, 1, 4)); t.expects(fails_at("�", xml::error_kind::invalid_character, 1, 4)); t.expects(fails_at("�", xml::error_kind::invalid_character, 1, 4)); t.expects(fails_at("& b", xml::error_kind::invalid_reference, 1, 4)); t.expects(fails_at("&#x;", xml::error_kind::invalid_reference, 1, 4)); t.expects(fails_at("&", xml::error_kind::invalid_reference, 1, 4)); t.expects(fails_at("\x01", xml::error_kind::invalid_character, 1, 4)); t.expects(fails_at("\xEF\xBF\xBE", xml::error_kind::invalid_character, 1, 4)); t.expects(fails_at("\n\xC0\x80", xml::error_kind::invalid_utf8, 2, 1)); t.expects(fails_at("", xml::error_kind::unexpected_byte, 1, 4)); t.expects(fails_at("", xml::error_kind::unexpected_byte, 1, 5)); t.expects(fails_at("", xml::error_kind::unexpected_byte, 1, 13)); t.expects(fails_at("", xml::error_kind::unexpected_end, 1, 18)); t.expects(fails_at("<1a/>", xml::error_kind::unexpected_byte, 1, 2)); t.expects(xml::parse("") 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("") 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("", "x").view() == "1 2 3 4 5\t6\n7"); t.expects(root_attribute("", "y").view() == "say \"hi\""); t.expects(root_attribute("", "x").view() == "<>&'\"AB"); } @test def xml_entities_are_expanded(t: &mut tester): void { val doctype := """ bold &name;"> ]> """; var text := string::from(doctype, nothing {}); text.append("&greet; &markup; <2;"); val doc := xml::parse(text.view()) 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().view() == "hello, world! bold world <"); val bold := doc.root.child("b") else { t.expects(false); return; }; t.expects(bold.text().view() == "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("'>]>&e;").view() == ""); t.expects(root_text("'>]>&e;").view() == ""); # Text tokens go on across where an entity's text begins and ends. t.expects(tokens_of("]>before &e; after").view() == "text[before middle after]"); # A token within an entity is where the entity is referred to. var tokens := xml::reader::new("'>]> &e;"); _ = 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("]>&e;", xml::error_kind::recursive_entity, 1, 36)); t.expects(fails_at("]>", xml::error_kind::recursive_entity, 1, 56)); t.expects(fails_at("'>]>&e;", xml::error_kind::unbalanced_entity, 1, 36)); t.expects(fails_at("'>]>&e;", xml::error_kind::unbalanced_entity, 1, 40)); t.expects(fails_at("]>&e;", xml::error_kind::external_entity, 1, 45)); t.expects(fails_at("]>&e;", xml::error_kind::external_entity, 1, 77)); t.expects(fails_at("]>", xml::error_kind::unexpected_byte, 1, 37)); t.expects(fails_at("]>", xml::error_kind::invalid_character, 1, 27)); t.expects(fails_at("]>", xml::error_kind::invalid_reference, 1, 26)); t.expects(fails_at("]>&e;", xml::error_kind::invalid_reference, 1, 38)); t.expects(fails_at("]>&e", xml::error_kind::invalid_reference, 1, 34)); # Entities standing for more text than the limit stop, rather than exhaust memory. var laughs := string::from("", nothing {}); for level in 1..12 { laughs.append($""); } laughs.append("]>&l11;"); t.expects(fails_with(laughs.view(), xml::error_kind::too_much_expansion)); t.expects(xml::parse(laughs.view(), xml::options { expansion = 0x10000000 }) is value); } @test def xml_document_type_declares_attributes(t: &mut tester): void { val text := """ ]> """; 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("") is value); t.expects(xml::parse("]>") is value); t.expects(xml::parse("]>") is error); t.expects(xml::parse("]>") is value); t.expects(xml::parse("]>") is value); t.expects(fails_at("]>", xml::error_kind::unexpected_byte, 1, 32)); t.expects(fails_at("]>", xml::error_kind::unexpected_byte, 1, 39)); t.expects(fails_at("]>", xml::error_kind::unexpected_byte, 1, 28)); t.expects(fails_at("]>", xml::error_kind::unexpected_byte, 1, 26)); t.expects(fails_at("", xml::error_kind::unexpected_byte, 1, 22)); t.expects(fails_at("]>", xml::error_kind::unexpected_byte, 1, 14)); t.expects(fails_at("'>%decls;]>", "x").view() == "from"); # After one not read, the declarations that follow are left out, since it may # declare the same names first. t.expects(root_attribute("]>", "x").view() == "(none)"); t.expects(fails_with("", xml::error_kind::undeclared_entity)); } @test def xml_namespaces_are_resolved(t: &mut tester): void { val text := """ """; 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("", 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("", xml::error_kind::unbound_prefix, 1, 2)); t.expects(fails_at("", xml::error_kind::unbound_prefix, 1, 4)); t.expects(fails_at("", xml::error_kind::invalid_namespace, 1, 4)); t.expects(fails_at("", xml::error_kind::invalid_namespace, 1, 4)); t.expects(fails_at("", xml::error_kind::invalid_namespace, 1, 4)); t.expects(fails_at("", xml::error_kind::invalid_namespace, 1, 4)); t.expects(fails_at("", xml::error_kind::invalid_namespace, 1, 2)); t.expects(fails_at("", xml::error_kind::invalid_name, 1, 2)); t.expects(fails_at("", xml::error_kind::invalid_name, 1, 2)); t.expects(fails_at("", xml::error_kind::repeated_attribute, 1, 44)); t.expects(fails_at("]>", xml::error_kind::invalid_name, 1, 23)); t.expects(fails_at("", xml::error_kind::invalid_name, 1, 6)); t.expects(xml::parse("") is value); } @test def xml_many_attributes_are_checked(t: &mut tester): void { var many := string::from(""); t.expects(xml::parse(many.view()) is value); repeated.append(" n17='again'/>"); t.expects(fails_with(repeated.view(), xml::error_kind::repeated_attribute)); var bound := string::from(""); t.expects(fails_with(bound.view(), xml::error_kind::repeated_attribute)); } @test def xml_nesting_is_limited(t: &mut tester): void { t.expects(xml::parse("", xml::options { depth = 3 }) is value); match xml::parse("", 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(""); } for _ in 0..600 { deep.append(""); } t.expects(fails_with(deep.view(), xml::error_kind::too_deep)); t.expects(xml::parse(deep.view(), xml::options { depth = 1000 }) is value); } @test def xml_document_holds_every_node(t: &mut tester): void { val text := "\n\n\n one\n \n to\n \n\n\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.view() == "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.view() == "text element text comment text element text instruction text "); var items := string::new(nothing {}); for item in list.elements() { items.append(item.text().view()); items.append(","); t.expects(item.parent.unwrap().name == "list"); } t.expects(items.view() == "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("&z", nothing {}); var doc := xml::parse(text.view()) 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() == "\n \n") { value(&_) => t.expects(false); error(e) => t.expects(e.describe().view() == "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); }