From 16124e890e34d64616d398a2d13398c9407019b6 Mon Sep 17 00:00:00 2001 From: Bat Date: Wed, 18 Jul 2018 18:35:50 +0200 Subject: [PATCH 1/3] Add some test for mentions --- plume-common/src/utils.rs | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) diff --git a/plume-common/src/utils.rs b/plume-common/src/utils.rs index 8b14959c..364a3912 100644 --- a/plume-common/src/utils.rs +++ b/plume-common/src/utils.rs @@ -68,3 +68,26 @@ pub fn md_to_html(md: &str) -> (String, Vec) { html::push_html(&mut buf, parser); (buf, mentions.collect()) } + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn test_mentions() { + let tests = vec![ + ("nothing", vec![]), + ("@mention", vec!["mention"]), + ("@mention@instance.tld", vec!["mention@instance.tld"]), + ("@many @mentions", vec!["many", "mentions"]), + ("@start with a mentions", vec!["start"]), + ("mention at @end", vec!["end"]), + ("between parenthesis (@test)", vec!["test"]), + ("with some punctuation @test!", vec!["test"]), + ]; + + for (md, mentions) in tests { + assert_eq!(md_to_html(md).1, mentions.into_iter().map(|s| s.to_string()).collect::>()); + } + } +} From 6b58dcfda54be951971dacefcc062a25b3553f8c Mon Sep 17 00:00:00 2001 From: Bat Date: Wed, 18 Jul 2018 19:00:49 +0200 Subject: [PATCH 2/3] Fix a bug in mentions Fixes #98 --- plume-common/src/utils.rs | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/plume-common/src/utils.rs b/plume-common/src/utils.rs index 364a3912..8e1acbaa 100644 --- a/plume-common/src/utils.rs +++ b/plume-common/src/utils.rs @@ -28,10 +28,15 @@ pub fn md_to_html(md: &str) -> (String, Vec) { Event::Text(txt) => { let (evts, _, _, _, new_mentions) = txt.chars().fold((vec![], false, String::new(), 0, vec![]), |(mut events, in_mention, text_acc, n, mut mentions), c| { if in_mention { - if (c.is_alphanumeric() || c == '@' || c == '.' || c == '-' || c == '_') && (n < (txt.chars().count() - 1)) { + let char_matches = c.is_alphanumeric() || c == '@' || c == '.' || c == '-' || c == '_'; + if char_matches && (n < (txt.chars().count() - 1)) { (events, in_mention, text_acc + c.to_string().as_ref(), n + 1, mentions) } else { - let mention = text_acc + c.to_string().as_ref(); + let mention = if char_matches { + text_acc + c.to_string().as_ref() + } else { + text_acc + }; let short_mention = mention.clone(); let short_mention = short_mention.splitn(1, '@').nth(0).unwrap_or(""); let link = Tag::Link(format!("/@/{}/", mention).into(), short_mention.to_string().into()); @@ -84,6 +89,7 @@ mod tests { ("mention at @end", vec!["end"]), ("between parenthesis (@test)", vec!["test"]), ("with some punctuation @test!", vec!["test"]), + (" @spaces ", vec!["spaces"]), ]; for (md, mentions) in tests { From 493fe731d0f3a82ec40614d206f849120f0eeaaf Mon Sep 17 00:00:00 2001 From: Bat Date: Wed, 18 Jul 2018 19:02:31 +0200 Subject: [PATCH 3/3] Remove a debug message while we are at it --- plume-models/src/comments.rs | 1 - 1 file changed, 1 deletion(-) diff --git a/plume-models/src/comments.rs b/plume-models/src/comments.rs index a76b1018..260118eb 100644 --- a/plume-models/src/comments.rs +++ b/plume-models/src/comments.rs @@ -75,7 +75,6 @@ impl Comment { let mentions = Mention::list_for_comment(conn, self.id).into_iter() .map(|m| m.get_mentioned(conn).map(|u| u.get_fqn(conn)).unwrap_or(String::new())) .collect::>(); - println!("{:?}", mentions); json["mentions"] = serde_json::to_value(mentions).unwrap(); json }