diff --git a/Cargo.toml b/Cargo.toml index bdf539d..1e41752 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -17,10 +17,8 @@ console_error_panic_hook = { version = "0.1.1" } serde = { version = "1.0.203", features = ["derive"] } serde_json = "1.0.118" scraper = "0.19.0" +indoc = "2.0.5" # We don't use getrandom, but we enable the "js" feature to enable # WebAssembly support. getrandom = { version = "0.2", features = ["js"] } - -[dev-dependencies] -indoc = "2.0.5" diff --git a/src/format.rs b/src/format.rs index 57d27b9..6577652 100644 --- a/src/format.rs +++ b/src/format.rs @@ -1,3 +1,5 @@ +use indoc::{formatdoc, indoc}; + use crate::items::ShopItem; pub fn diff_items(old: ShopItem, new: ShopItem) -> Option { @@ -42,15 +44,43 @@ pub fn diff_items(old: ShopItem, new: ShopItem) -> Option { } pub fn format_new_item(item: ShopItem) -> String { - format!( - "*New item added:* {full_name}\n*Description:* {description}\n*Price:* {price}\n*Stock:* {stock}", + formatdoc! { + "*New item added:* {full_name} + *Description:* {description} + *Price:* {price} + *Stock:* {stock}", full_name = item.full_name, description = item.description.unwrap_or("_not set_".into()), price = item.price, stock = item.stock .map(|stock| stock.to_string()) .unwrap_or("Unlimited".into()), - ) + } +} + +#[cfg(test)] +mod format_new_tests { + use super::*; + + #[test] + fn item_formatted() { + let item = ShopItem { + full_name: "Test".into(), + description: Some("Lorem ipsum".into()), + price: 1, + stock: Some(10), + ..Default::default() + }; + + assert_eq!( + format_new_item(item), + indoc! {" + *New item added:* Test + *Description:* Lorem ipsum + *Price:* 1 + *Stock:* 10"} + ); + } } #[cfg(test)]