From 32ec6e06f6dce8ab70e5b63b4656186c2b94a4db Mon Sep 17 00:00:00 2001 From: Damien Elmes Date: Thu, 2 Feb 2023 18:47:30 +1000 Subject: [PATCH] Fix comparison being collapsed when extra characters in input --- rslib/src/typeanswer.rs | 30 +++++++++++++++++++++++++----- 1 file changed, 25 insertions(+), 5 deletions(-) diff --git a/rslib/src/typeanswer.rs b/rslib/src/typeanswer.rs index a5f384a8a..a0cb08ded 100644 --- a/rslib/src/typeanswer.rs +++ b/rslib/src/typeanswer.rs @@ -93,7 +93,7 @@ impl DiffContext { "{}", 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}

{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("
foo
"), "foo"); } + + #[test] + fn empty_input_shows_as_code() { + let ctx = DiffContext::new("123", ""); + assert_eq!(ctx.to_html(), "123"); + } + + #[test] + fn correct_input_is_collapsed() { + let ctx = DiffContext::new("123", "123"); + assert_eq!( + ctx.to_html(), + "123" + ); + } + + #[test] + fn incorrect_input_is_not_collapsed() { + let ctx = DiffContext::new("123", "1123"); + assert_eq!( + ctx.to_html(), + "1123

123
" + ); + } }