Fix comparison being collapsed when extra characters in input

This commit is contained in:
Damien Elmes 2023-02-02 18:47:30 +10:00
parent 321402a6d0
commit 32ec6e06f6

View File

@ -93,7 +93,7 @@ impl DiffContext {
"<code id=typeans>{}</code>",
if self.provided.is_empty() {
self.expected.iter().collect()
} else if no_mistakes(&output.expected) {
} else if self.provided == self.expected {
provided
} else {
format!("{provided}<br><span id=typearrow>&darr;</span><br>{expected}")
@ -102,10 +102,6 @@ impl DiffContext {
}
}
fn no_mistakes(tokens: &[DiffToken]) -> bool {
tokens.iter().all(|v| v.kind == DiffTokenKind::Good)
}
fn prepare_expected(expected: &str) -> String {
let without_av = strip_av_tags(expected);
let without_newlines = LINEBREAKS.replace_all(&without_av, " ");
@ -279,4 +275,28 @@ mod test {
fn whitespace_is_trimmed() {
assert_eq!(prepare_expected("<div>foo</div>"), "foo");
}
#[test]
fn empty_input_shows_as_code() {
let ctx = DiffContext::new("123", "");
assert_eq!(ctx.to_html(), "<code id=typeans>123</code>");
}
#[test]
fn correct_input_is_collapsed() {
let ctx = DiffContext::new("123", "123");
assert_eq!(
ctx.to_html(),
"<code id=typeans><span class=typeGood>123</span></code>"
);
}
#[test]
fn incorrect_input_is_not_collapsed() {
let ctx = DiffContext::new("123", "1123");
assert_eq!(
ctx.to_html(),
"<code id=typeans><span class=typeBad>1</span><span class=typeGood>123</span><br><span id=typearrow>&darr;</span><br><span class=typeGood>123</span></code>"
);
}
}