From a6a7effd3d19e0efdbdd12712ec9e106653e50b2 Mon Sep 17 00:00:00 2001 From: Jakob Cornell Date: Sat, 30 Sep 2023 15:21:08 -0500 Subject: [PATCH] Port number parsing tests to Rust --- sharedmodel/src/update_parse.rs | 68 +++++++++++++++++++++++++++++++++ 1 file changed, 68 insertions(+) diff --git a/sharedmodel/src/update_parse.rs b/sharedmodel/src/update_parse.rs index 758a5af..04495cb 100644 --- a/sharedmodel/src/update_parse.rs +++ b/sharedmodel/src/update_parse.rs @@ -18,6 +18,7 @@ pub struct ParsedUpdate { deletable: bool, } +#[derive(Debug)] pub enum ParseError { InvalidElement(String), } @@ -322,6 +323,8 @@ fn is_special(count: Count) -> bool { #[cfg(test)] mod tests { + use crate::parse_html; + use super::*; #[test] @@ -338,4 +341,69 @@ mod tests { assert!(is_special(12341234)); assert!(!is_special(1937592)); } + + fn expect_update(result: Result) -> ParsedUpdate { + result.expect("parse should have succeeded") + } + + #[test] + fn test_valid_counts() { + let update = expect_update(parse_update(parse_html("
12,345,678 spaghetti
"), None, "")); + assert_eq!(update.number, Some(12_345_678)); + assert_eq!(update.deletable, false); + + let update = expect_update(parse_update(parse_html("

0


oz
"), None, "")); + assert_eq!(update.number, Some(0)); + assert_eq!(update.deletable, false); + + let update = expect_update(parse_update(parse_html("
121 345 621
"), Some(121), "")); + assert_eq!(update.number, Some(121)); + assert_eq!(update.deletable, false); + + let update = expect_update(parse_update(parse_html("28 336 816"), Some(28_336_816), "")); + assert_eq!(update.number, Some(28_336_816)); + assert_eq!(update.deletable, true); + } + + #[test] + fn test_non_counts() { + let update = expect_update(parse_update(parse_html("
zoo
"), None, "")); + assert_eq!(update.count_attempt, false); + assert_eq!(update.deletable, false); + } + + #[test] + fn test_typos() { + let update = expect_update(parse_update(parse_html("v9"), Some(888), "")); + assert_eq!(update.number, None); + assert_eq!(update.count_attempt, true); + + let update = expect_update(parse_update(parse_html("
v11.585 Empire
"), None, "")); + assert_eq!(update.number, None); + assert_eq!(update.count_attempt, true); + assert_eq!(update.deletable, false); + + let update = expect_update(parse_update(parse_html("
11, 585, 22
"), Some(11_585_202), "")); + assert_eq!(update.number, None); + assert_eq!(update.count_attempt, true); + assert_eq!(update.deletable, true); + + let update = expect_update(parse_update(parse_html("0490499"), Some(4_999), "")); + assert_eq!(update.number, None); + assert_eq!(update.count_attempt, true); + + // this update is marked non-deletable, but I'm not sure it should be + let update = expect_update(parse_update(parse_html("12,123123"), Some(12_123_123), "")); + assert_eq!(update.number, None); + assert_eq!(update.count_attempt, true); + } + + #[test] + fn test_html_handling() { + let update = expect_update(parse_update(parse_html("123
,456"), None, "")); + assert_eq!(update.number, Some(123)); + + let update = expect_update(parse_update(parse_html("
123\n456
"), None, "")); + assert_eq!(update.number, Some(123)); + } } -- 2.30.2