use super::render::text_renderer::{RichAnnotation, TaggedLine, TrivialDecorator}; use super::{from_read, from_read_with_decorator, parse, TextDecorator}; /// Like assert_eq!(), but prints out the results normally as well macro_rules! assert_eq_str { ($a:expr, $b:expr) => { if $a != $b { println!("<<<\n{}===\n{}>>>", $a, $b); assert_eq!($a, $b); } }; } fn test_html(input: &[u8], expected: &str, width: usize) { assert_eq_str!(from_read(input, width), expected); } fn test_html_decorator(input: &[u8], expected: &str, width: usize, decorator: D) where D: TextDecorator, { let output = from_read_with_decorator(input, width, decorator); assert_eq_str!(output, expected); } #[test] fn test_table() { test_html( br##"
1 2 3
"##, r#"───┬───┬──── 1 │2 │3 ───┴───┴──── "#, 12, ); } #[test] fn test_thead() { test_html( br##"
Col1 Col2 Col3
1 2 3
"##, r#"────┬────┬───── Col1│Col2│Col3 ────┼────┼───── 1 │2 │3 ────┴────┴───── "#, 15, ); } #[test] fn test_colspan() { test_html( br##"
1 2 3
12 3
1 23
"##, r#"───┬───┬──── 1 │2 │3 ───┴───┼──── 12 │3 ───┬───┴──── 1 │23 ───┴──────── "#, 12, ); } #[test] fn test_para() { assert_eq_str!(from_read(&b"

Hello

"[..], 10), "Hello\n"); } #[test] fn test_para2() { assert_eq_str!( from_read(&b"

Hello, world!

"[..], 20), "Hello, world!\n" ); } #[test] fn test_blockquote() { assert_eq_str!( from_read( &br#"

Hello

One, two, three

foo

"#[..], 12 ), r#"Hello > One, two, > three foo "# ); } #[test] fn test_ul() { test_html( br#" "#, r#"* Item one * Item two * Item three "#, 10, ); } #[test] fn test_ol1() { test_html( br#"
  1. Item one
  2. Item two
  3. Item three
"#, r#"1. Item one 2. Item two 3. Item three "#, 11, ); } #[test] fn test_ol2() { test_html( br#"
  1. Item one
  2. Item two
  3. Item three
  4. Item four
  5. Item five
  6. Item six
  7. Item seven
  8. Item eight
  9. Item nine
  10. Item ten
"#, r#"1. Item one 2. Item two 3. Item three 4. Item four 5. Item five 6. Item six 7. Item seven 8. Item eight 9. Item nine 10. Item ten "#, 20, ); } #[test] fn test_ol_start() { test_html( br#"
  1. Item three
  2. Item four
"#, r#"3. Item three 4. Item four "#, 20, ); } #[test] fn test_ol_start_9() { test_html( br#"
  1. Item nine
  2. Item ten
"#, r#"9. Item nine 10. Item ten "#, 20, ); } #[test] fn test_ol_start_neg() { test_html( br#"
  1. Item minus one
  2. Item zero
  3. Item one
"#, r#"-1. Item minus one 0. Item zero 1. Item one "#, 20, ); } #[test] fn test_strip_nl() { test_html( br#"

One Two Three

"#, "One Two Three\n", 40, ); } #[test] fn test_strip_nl2() { test_html( br#"

One Two Three

"#, "One Two Three\n", 40, ); } #[test] fn test_strip_nl_tbl() { test_html( br#"
One Two Three
"#, r"──────────────────── One Two Three ──────────────────── ", 20, ); } #[test] fn test_unknown_element() { test_html( br#"
One Two Three
"#, r"──────────────────── One Two Three ──────────────────── ", 20, ); } #[test] fn test_strip_nl_tbl_p() { test_html( br#"

One Two Three

"#, r"──────────────────── One Two Three ──────────────────── ", 20, ); } #[test] fn test_pre() { test_html( br#"
foo
bar
wib   asdf;

Hello

"#, r"foo bar wib asdf; Hello ", 20, ); } #[test] fn test_link() { test_html( br#"

Hello, world

"#, r"Hello, [world][1] [1] http://www.example.com/ ", 80, ); } #[test] fn test_link2() { test_html( br#"

Hello, world!

"#, r"Hello, [world][1]! [1] http://www.example.com/ ", 80, ); } #[test] fn test_link3() { test_html( br#"

Hello, world

"#, r"Hello, [w][1]orld [1] http://www.example.com/ ", 80, ); } #[test] fn test_link_wrap() { test_html( br#" Hello"#, r"[Hello][1] [1] http:/ /www.examp le.com/ ", 10, ); } #[test] fn test_wrap() { test_html( br"

Hello, world. Superlongwordreally

", r#"Hello, world. Superlon gwordrea lly "#, 8, ); } #[test] fn test_wrap2() { test_html( br"

Hello, world. This is a long sentence with a few words, which we want to be wrapped correctly.

", r#"Hello, world. This is a long sentence with a few words, which we want to be wrapped correctly. "#, 20, ); } #[test] fn test_wrap3() { test_html( br#"

http://example.org/blah/ one two three"#, r#"[http://example.org/blah/ ][1] one two three [1] dest "#, 25, ); } #[test] fn test_div() { test_html( br"

Hello

Div
", r#"Hello Div "#, 20, ); test_html( br"

Hello

Div
Div2
", r#"Hello Div Div2 "#, 20, ); } #[test] fn test_img_alt() { test_html( br"

Hello world

", "Hello [world]\n", 80, ); } #[test] fn test_br() { test_html(br"

Hello
World

", "Hello\nWorld\n", 20); } #[test] fn test_br2() { test_html(br"

Hello

World

", "Hello\n\nWorld\n", 20); } #[test] fn test_br3() { test_html(br"

Hello

World

", "Hello\n\nWorld\n", 20); } #[test] fn test_subblock() { test_html( br#"
Here's a link.
  • Bullet
  • Bullet
  • Bullet
"#, r"Here's a [link][1]. * Bullet * Bullet * Bullet [1] https://example.com/ ", 80, ); } #[test] fn test_controlchar() { test_html("Foo\u{0080}Bar".as_bytes(), "FooBar\n", 80); test_html("Foo\u{0080}Bar".as_bytes(), "FooB\nar\n", 4); test_html("FooBa\u{0080}r".as_bytes(), "FooB\nar\n", 4); } #[test] fn test_nested_table_1() { test_html( br##"
123
456
789
123
456
789
123
456
789
"##, r#"─┬─┬──┬─┬─┬──┬─┬─┬─── 1│2│3 │4│5│6 │7│8│9 ─┼─┼──┼─┼─┼──┼─┼─┼─── 1│2│3 │4│5│6 │7│8│9 ─┼─┼──┼─┼─┼──┼─┼─┼─── 1│2│3 │4│5│6 │7│8│9 ─┴─┴──┴─┴─┴──┴─┴─┴─── "#, 21, ); } #[test] fn test_nested_table_2() { test_html( br##"
1a
2b
one
two
three
four
five
"##, r#"─┬───┬───── 1│a │one ─┼───│two 2│b │three │ │four │ │five ─┴───┴───── "#, 11, ); } #[test] fn test_h1() { test_html( br##"

Hi

foo

"##, r#"# Hi foo "#, 21, ); } #[test] fn test_h3() { test_html( br##"

Hi

foo

"##, r#"### Hi foo "#, 21, ); } // General test that spacing is preserved #[test] fn test_pre2() { test_html( br##"
Hello  sp
world
"##, r#"Hello sp world "#, 21, ); } // Check that spans work correctly inside
#[test]
fn test_pre_span() {
    test_html(
        br##"
Hello $sp
Hi $foo
Hi foo, bar
"##, r#"Hello $sp Hi $foo Hi foo, bar "#, 21, ); } // Check tab behaviour #[test] fn test_pre_tab() { test_html(b"
\tworld
", " world\n", 40); test_html(b"
H\tworld
", "H world\n", 40); test_html(b"
He\tworld
", "He world\n", 40); test_html(b"
Hel\tworld
", "Hel world\n", 40); test_html(b"
Hell\tworld
", "Hell world\n", 40); test_html(b"
Hello\tworld
", "Hello world\n", 40); test_html(b"
Helloo\tworld
", "Helloo world\n", 40); test_html(b"
Hellooo\tworld
", "Hellooo world\n", 40); test_html(b"
Helloooo\tworld
", "Helloooo world\n", 40); } #[test] fn test_em_strong() { test_html( br##"

Hi em strong

"##, r#"Hi *em* **strong** "#, 21, ); } #[test] #[ignore] // Not yet fixed! fn test_nbsp_indent() { test_html( br##"
Top
 Indented
  Indented again
"##, r#"Top Indented Indented again "#, 21, ); } #[test] fn test_deeply_nested() { use ::std::iter::repeat; let html = repeat("").take(1000).collect::>().concat(); test_html(html.as_bytes(), "", 10); } #[test] fn test_deeply_nested_table() { use ::std::iter::repeat; let html = repeat("
hi") .take(1000) .collect::>() .concat() + &repeat("
") .take(1000) .collect::>() .concat(); test_html( html.as_bytes(), r#"────┬─┬─── hi │h│ │i│ ────┴─┴─── "#, 10, ); } #[test] fn test_table_no_id() { let html = r#"
hi, world
"#; test_html( html.as_bytes(), r#"────────── hi, world ────────── "#, 10, ); } #[test] fn test_table_cell_id() { let html = r#"
hi, world
"#; test_html( html.as_bytes(), r#"────────── hi, world ────────── "#, 10, ); } #[test] fn test_table_row_id() { let html = r#"
hi, world
"#; test_html( html.as_bytes(), r#"────────── hi, world ────────── "#, 10, ); } #[test] fn test_table_table_id() { let html = r#"
hi, world
"#; test_html( html.as_bytes(), r#"────────── hi, world ────────── "#, 10, ); } #[test] fn test_table_tbody_id() { let html = r#"
hi, world
"#; test_html( html.as_bytes(), r#"────────── hi, world ────────── "#, 10, ); } #[test] fn test_header_width() { //0 size test_html( br##"

Anything

"##, r#"## ### A ## ### n ## ### y ## ### t ## ### h ## ### i ## ### n ## ### g ## ## ──── "#, 7, ); //Underflow test_html( br##"

Anything

"##, r#"## ### A ## ### n ## ### y ## ### t ## ### h ## ### i ## ### n ## ### g ## ## ── "#, 5, ); } #[test] fn test_trivial_decorator() { test_html_decorator( br#"
Here's a link.
  • Bullet
  • Bullet
  • Bullet
"#, r"Here's a link. * Bullet * Bullet * Bullet ", 80, TrivialDecorator::new(), ); } #[test] fn test_issue_16() { test_html(b"
", "", 10); } #[test] fn test_pre_br() { test_html( b"
Foo
Bar
", r#"Foo Bar "#, 10, ); } #[test] fn test_pre_emptyline() { test_html(br#"
X 
"#, "X \n", 10); } #[test] fn test_link_id_longline() { test_html( br#"quitelongline"#, r#"[quitelong line][1] [1] foo "#, 10, ); } #[test] fn test_dl() { test_html( br#"
Foo
Definition of foo
"#, r#"*Foo* Definition of foo "#, 40, ); } #[test] fn test_s() { test_html( br#"Hi youthee!"#, "Hi y\u{336}o\u{336}u\u{336}thee!\n", 40, ); } #[test] fn test_multi_parse() { let html: &[u8] = b"one two three four five six seven eight nine ten eleven twelve thirteen \ fourteen fifteen sixteen seventeen"; let tree = parse(html); assert_eq!( "one two three four five six seven eight nine ten eleven twelve thirteen fourteen\n\ fifteen sixteen seventeen\n", tree.clone().render_plain(80).into_string() ); assert_eq!( "one two three four five six seven eight nine ten eleven twelve\n\ thirteen fourteen fifteen sixteen seventeen\n", tree.clone().render_plain(70).into_string() ); assert_eq!( "one two three four five six seven eight nine ten\n\ eleven twelve thirteen fourteen fifteen sixteen\n\ seventeen\n", tree.clone().render_plain(50).into_string() ); } #[test] fn test_read_rich() { let html: &[u8] = b"bold"; let lines = parse(html).render_rich(80).into_lines(); let tag = vec![RichAnnotation::Strong]; let line = TaggedLine::from_string("*bold*".to_owned(), &tag); assert_eq!(vec![line], lines); } #[test] fn test_read_custom() { let html: &[u8] = b"bold"; let lines = parse(html).render(80, TrivialDecorator::new()).into_lines(); let tag = vec![()]; let line = TaggedLine::from_string("bold".to_owned(), &tag); assert_eq!(vec![line], lines); } #[test] fn test_pre_rich() { use RichAnnotation::*; assert_eq!( crate::parse("
test
".as_bytes()) .render_rich(100) .into_lines(), [TaggedLine::from_string( "test".into(), &vec![Preformat(false)] )] ); assert_eq!( crate::parse("
testlong
".as_bytes()) .render_rich(4) .into_lines(), [ TaggedLine::from_string("test".into(), &vec![Preformat(false)]), TaggedLine::from_string("long".into(), &vec![Preformat(true)]) ] ); }