#![allow(non_snake_case)]
use goish::prelude::*;
fn html_escaper() -> strings::Replacer {
strings::NewReplacer(&[
"&", "&",
"<", "<",
">", ">",
"\"", """,
"'", "'",
])
}
fn html_unescaper() -> strings::Replacer {
strings::NewReplacer(&[
"&", "&",
"<", "<",
">", ">",
""", "\"",
"'", "'",
])
}
fn capital_letters() -> strings::Replacer {
strings::NewReplacer(&["a", "A", "b", "B"])
}
struct Case { r: strings::Replacer, r#in: String, out: String }
test!{ fn TestReplacer(t) {
let cases: Vec<Case> = vec![
Case { r: capital_letters(), r#in: "brad".into(), out: "BrAd".into() },
Case { r: capital_letters(),
r#in: strings::Repeat("a", (32 << 10) + 123),
out: strings::Repeat("A", (32 << 10) + 123) },
Case { r: capital_letters(), r#in: "".into(), out: "".into() },
Case { r: html_escaper(),
r#in: "<script>alert(\"xss\")</script>".into(),
out: "<script>alert("xss")</script>".into() },
Case { r: html_escaper(),
r#in: "AT&T".into(),
out: "AT&T".into() },
Case { r: html_unescaper(),
r#in: "&lt;".into(),
out: "<".into() }, Case { r: html_unescaper(),
r#in: "<b>hi</b>".into(),
out: "<b>hi</b>".into() },
];
for c in cases {
let got = c.r.Replace(&c.r#in);
if got != c.out {
t.Errorf(Sprintf!("Replace(%q) got %q; want %q", c.r#in, got, c.out));
}
}
}}
test!{ fn TestReplacerNoOp(t) {
let r = strings::NewReplacer(&[] as &[&str]);
let got = r.Replace("anything");
if got != "anything" {
t.Errorf(Sprintf!("empty Replacer got %q; want %q", got, "anything"));
}
}}
test!{ fn TestReplacerOrderMatters(t) {
let r = strings::NewReplacer(&["ab", "X", "a", "Y"]);
let got = r.Replace("ab ac");
if got != "X Yc" {
t.Errorf(Sprintf!("got %q; want %q", got, "X Yc"));
}
}}