#![allow(non_snake_case)]
use goish::prelude::*;
test!{ fn TestEscape(t) {
let cases: Vec<(&str, &str)> = vec![
("", ""),
("abc", "abc"),
("<", "<"),
(">", ">"),
("&", "&"),
("\"", """),
("'", "'"),
("<script>alert('x')</script>", "<script>alert('x')</script>"),
];
for (inp, want) in cases {
let got = html::EscapeString(inp);
if got != want {
t.Errorf(Sprintf!("EscapeString(%q) = %q, want %q", inp, got, want));
}
}
}}
test!{ fn TestUnescapeNamed(t) {
let cases: Vec<(&str, &str)> = vec![
("", ""),
("abc", "abc"),
("<", "<"),
(">", ">"),
("&", "&"),
(""", "\""),
("'", "'"),
("a<b&c>d", "a<b&c>d"),
];
for (inp, want) in cases {
let got = html::UnescapeString(inp);
if got != want {
t.Errorf(Sprintf!("UnescapeString(%q) = %q, want %q", inp, got, want));
}
}
}}
test!{ fn TestUnescapeNumeric(t) {
if html::UnescapeString("ABC") != "ABC" {
t.Errorf(Sprintf!("UnescapeString numeric failed"));
}
if html::UnescapeString("😀") != "😀" {
t.Errorf(Sprintf!("UnescapeString emoji failed"));
}
}}
test!{ fn TestUnknownPassthrough(t) {
if html::UnescapeString("&foo;") != "&foo;" {
t.Errorf(Sprintf!("&foo; should pass through"));
}
if html::UnescapeString("a & b") != "a & b" {
t.Errorf(Sprintf!("bare & should pass through"));
}
}}
test!{ fn TestRoundTrip(t) {
let cases = [
"plain text",
"<tag>",
"a & b < c > d",
"\"quoted\" and 'apos'",
];
for s in cases {
let e = html::EscapeString(s);
let back = html::UnescapeString(&e);
if back != s {
t.Errorf(Sprintf!("round-trip(%q) = %q", s, back));
}
}
}}