From 1f678a86216c75b7c0178ee33434f5be488f2642 Mon Sep 17 00:00:00 2001 From: Johann150 Date: Thu, 13 Jan 2022 02:05:55 +0100 Subject: [PATCH] refactor tests to have meaningful names --- src/lib.rs | 228 +++++++++++++++++++++++++++++------------------------ 1 file changed, 126 insertions(+), 102 deletions(-) diff --git a/src/lib.rs b/src/lib.rs index 28985c1..aee105b 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -206,116 +206,140 @@ pub fn html2text(html: &str) -> String { #[cfg(test)] mod tests { use super::*; - const cases: &[(&str, &str)] = &[ - ("blah", "blah"), + + macro_rules! test { + ($name:ident, $from:literal, $to:literal $(,)?) => { + #[test] + fn $name() { + assert_eq!(&html2text($from), $to); + } + }; + ($($name:ident: $from:literal to $to:literal,)* $(,)?) => { + $(test!{$name, $from, $to})* + }; + } + + test! { + plaintext: "blah" to "blah", + tag: "
" to "", + tag_contents: "
simple text
" to "simple text", // links - ("
", ""), - ("
simple text
", "simple text"), - ("click here", "click test"), - ("click here", "click test"), - ( - "click here", - "click ents/'x'", - ), - ("click here", "click "), - ( - "click here or here", - "click test", - ), - ( - "click news", - "click http://bit.ly/2n4wXRs", - ), - ("yet, not yet", "/wiki/yet#English, /wiki/not_yet#English"), - + link: + "click here" + to "click test", + links_ignore_attributes: + "click here" + to "click test", + link_entities_in_url: + "click here" + to "click ents/'x'", + link_javascript: + "click here" + to "click ", + link_ignore_content_tags: + "click here or here" + to "click test", + link_absolute_url: + "click news" + to "click http://bit.ly/2n4wXRs", + link_ignore_attributes_2: + "yet, not yet" + to "/wiki/yet#English, /wiki/not_yet#English", // inlines - ("strong text", "strong text"), - ("some
div
", "some div"), + ignore_inline: + "strong text" + to "strong text", + ignore_inline_attributes: + "some
div
" + to "some div", // lines breaks and spaces - ("should ignore more spaces", "should ignore more spaces"), - ("should \nignore \r\nnew lines", "should ignore new lines"), - ("a\nb\nc", "a b c"), - ("two
line
breaks", "two\r\nline\r\nbreaks"), - ("

two

paragraphs

", "two\r\n\r\nparagraphs"), + collapse_spaces: + "should ignore more spaces" to "should ignore more spaces", + collapse_linebreaks: + "a\nb\nc" to "a b c", + collapse_mixed: + "should \nignore \r\nnew lines" to "should ignore new lines", + br_tag: + "two
line
breaks" to "two\r\nline\r\nbreaks", + paragraph: + "

two

paragraphs

" to "two\r\n\r\nparagraphs", // Headers - ("

First

main text", "First\r\n\r\nmain text"), - ( - "First

Second

next section", - "First\r\n\r\nSecond\r\n\r\nnext section", - ), - ("

Second

next section", "Second\r\n\r\nnext section"), - ( - "Second

Third

next section", - "Second\r\n\r\nThird\r\n\r\nnext section", - ), - ("

Third

next section", "Third\r\n\r\nnext section"), - ( - "Third

Fourth

next section", - "Third\r\n\r\nFourth\r\n\r\nnext section", - ), - ("

Fourth

next section", "Fourth\r\n\r\nnext section"), - ( - "Fourth
Fifth
next section", - "Fourth\r\n\r\nFifth\r\n\r\nnext section", - ), - ("
Fifth
next section", "Fifth\r\n\r\nnext section"), - ( - "Fifth
Sixth
next section", - "Fifth\r\n\r\nSixth\r\n\r\nnext section", - ), - ("
Sixth
next section", "Sixth\r\n\r\nnext section"), - ("Not Headernext section", "Not Headernext section"), + h1: + "

First

main text" to "First\r\n\r\nmain text", + h2_inline: + "First

Second

next section" + to "First\r\n\r\nSecond\r\n\r\nnext section", + h2: + "

Second

next section" to "Second\r\n\r\nnext section", + h3_inline: + "Second

Third

next section" + to "Second\r\n\r\nThird\r\n\r\nnext section", + h3: + "

Third

next section" to "Third\r\n\r\nnext section", + h4_inline: + "Third

Fourth

next section" + to "Third\r\n\r\nFourth\r\n\r\nnext section", + h4: + "

Fourth

next section" to "Fourth\r\n\r\nnext section", + h5_inline: + "Fourth
Fifth
next section" + to "Fourth\r\n\r\nFifth\r\n\r\nnext section", + h5: + "
Fifth
next section" to "Fifth\r\n\r\nnext section", + h6_inline: + "Fifth
Sixth
next section" + to "Fifth\r\n\r\nSixth\r\n\r\nnext section", + h6: + "
Sixth
next section" to "Sixth\r\n\r\nnext section", + no_h7: + "Not Headernext section" to "Not Headernext section", // html entitites - ("two  spaces", "two  spaces"), - ("© 2017 K3A", "© 2017 K3A"), - ("<printtag>", ""), - ( - "would you pay in ¢, £, ¥ or €?", - "would you pay in ¢, £, ¥ or €?", - ), - ( - "Tom & Jerry is not an entity", - "Tom & Jerry is not an entity", - ), - ("this &neither; as you see", "this &neither; as you see"), - ( - "list of items", - "list of items\r\nOne\r\nTwo\r\nThree\r\n", - ), - ("fish & chips", "fish & chips"), - ( - ""I'm sorry, Dave. I'm afraid I can't do that." – HAL, 2001: A Space Odyssey", - "\"I'm sorry, Dave. I'm afraid I can't do that.\" – HAL, 2001: A Space Odyssey", - ), - ("Google ®", "Google ®"), - ( - "⁌ decimal and hex entities supported ⁍", - "⁌ decimal and hex entities supported ⁍", - ), + entity_nbsp: + "two  spaces" to "two  spaces", + entity_copy: + "© 2017 K3A" to "© 2017 K3A", + entity_tag: + "<printtag>" to "", + entity_currencies: + "would you pay in ¢, £, ¥ or €?" + to "would you pay in ¢, £, ¥ or €?", + ampersand_not_entity: + "Tom & Jerry is not an entity" to "Tom & Jerry is not an entity", + entity_unknown: + "this &neither; as you see" to "this &neither; as you see", + entity_amp: + "fish & chips" to "fish & chips", + unordered_list: + "list of items
  • One
  • Two
  • Three
" + to "list of items\r\nOne\r\nTwo\r\nThree\r\n", + entity_quot: + ""I'm sorry, Dave. I'm afraid I can't do that." – HAL, 2001: A Space Odyssey" + to "\"I'm sorry, Dave. I'm afraid I can't do that.\" – HAL, 2001: A Space Odyssey", + entity_reg: + "Google ®" to "Google ®", // Large entity - ("&abcdefghij;", "&abcdefghij;"), + entity_large_unknown: + "&abcdefghij;" to "&abcdefghij;", // Numeric HTML entities - ( - "'single quotes' and 츝", - "'single quotes' and 츝", - ), + entity_numeric: + "⁌ decimal and hex entities supported ⁍" + to "⁌ decimal and hex entities supported ⁍", + entity_numeric_2: + "'single quotes' and 츝" + to "'single quotes' and 츝", // full thml structure - ("", ""), - ("Goodx", "x"), - ( - "we are not interested in scripts", - "we are not interested in scripts", - ), + empty: "" to "", + full_html: + "Goodx" to "x", + ignore_script: + "we are not interested in scripts" + to "we are not interested in scripts", // custom html tags - ("hello", "hello"), - ("hello", "hello"), - ("hello", "hello"), - ]; - - #[test] - fn test_all() { - for case in cases { - assert_eq!(&html2text(case.0), case.1); - } + ignore_unknown_tag: + "hello" to "hello", + ignore_unknown_tag_whitespace: + "hello" to "hello", + ignore_unknown_tag_attributes: + "hello" to "hello", } }