diff --git a/tests/test_advanced.py b/tests/test_advanced.py index 846d8aa..14bf3cd 100644 --- a/tests/test_advanced.py +++ b/tests/test_advanced.py @@ -1,6 +1,17 @@ from markdownify import markdownify as md +def test_chomp(): + assert md(' ') == ' ' + assert md(' ') == ' ' + assert md(' ') == ' ' + assert md(' ') == ' ' + assert md(' s ') == ' **s** ' + assert md(' s ') == ' **s** ' + assert md(' s ') == ' **s** ' + assert md(' s ') == ' **s** ' + + def test_nested(): text = md('

This is an example link.

') assert text == 'This is an [example link](http://example.com/).\n\n' diff --git a/tests/test_conversions.py b/tests/test_conversions.py index 5c544ed..b90067a 100644 --- a/tests/test_conversions.py +++ b/tests/test_conversions.py @@ -1,174 +1,13 @@ from markdownify import markdownify as md, ATX, ATX_CLOSED, BACKSLASH, UNDERSCORE -nested_uls = """ - """ - -nested_ols = """ -
    -
  1. 1 -
      -
    1. a -
        -
      1. I
      2. -
      3. II
      4. -
      5. III
      6. -
      -
    2. -
    3. b
    4. -
    5. c
    6. -
    -
  2. -
  3. 2
  4. -
  5. 3
  6. - """ - - -table = """ - - - - - - - - - - - - - - - -
    FirstnameLastnameAge
    JillSmith50
    EveJackson94
    """ - - -table_with_html_content = """ - - - - - - - - - - - - - - - -
    FirstnameLastnameAge
    JillSmith50
    EveJackson94
    """ - - -table_with_header_column = """ - - - - - - - - - - - - - - - -
    FirstnameLastnameAge
    JillSmith50
    EveJackson94
    """ - - -table_head_body = """ - - - - - - - - - - - - - - - - - - - -
    FirstnameLastnameAge
    JillSmith50
    EveJackson94
    """ - -table_missing_text = """ - - - - - - - - - - - - - - - - - - - -
    LastnameAge
    Jill50
    EveJackson94
    """ - -table_missing_head = """ - - - - - - - - - - - - - - - -
    FirstnameLastnameAge
    JillSmith50
    EveJackson94
    """ - - -def test_chomp(): - assert md(' ') == ' ' - assert md(' ') == ' ' - assert md(' ') == ' ' - assert md(' ') == ' ' - assert md(' s ') == ' **s** ' - assert md(' s ') == ' **s** ' - assert md(' s ') == ' **s** ' - assert md(' s ') == ' **s** ' +def inline_tests(tag, markup): + # test template for different inline tags + assert md(f'<{tag}>Hello') == f'{markup}Hello{markup}' + assert md(f'foo <{tag}>Hello bar') == f'foo {markup}Hello{markup} bar' + assert md(f'foo<{tag}> Hello bar') == f'foo {markup}Hello{markup} bar' + assert md(f'foo <{tag}>Hello bar') == f'foo {markup}Hello{markup} bar' + assert md(f'foo <{tag}> bar') in ['foo bar', 'foo bar'] # Either is OK def test_a(): @@ -219,58 +58,30 @@ def test_blockquote_with_paragraph(): assert md('
    Hello

    handsome

    ') == '\n> Hello\n\nhandsome\n\n' -def test_nested_blockquote(): +def test_blockquote_nested(): text = md('
    And she was like
    Hello
    ') assert text == '\n> And she was like \n> > Hello\n> \n> \n\n' def test_br(): assert md('a
    b
    c') == 'a \nb \nc' - - -def test_em(): - assert md('Hello') == '*Hello*' - - -def test_em_spaces(): - assert md('foo Hello bar') == 'foo *Hello* bar' - assert md('foo Hello bar') == 'foo *Hello* bar' - assert md('foo Hello bar') == 'foo *Hello* bar' - assert md('foo bar') == 'foo bar' - - -def inline_tests(tag, markup): - # Basically re-use test_em() and test_em_spaces(), - assert md(f'<{tag}>Hello') == f'{markup}Hello{markup}' - assert md(f'foo <{tag}>Hello bar') == f'foo {markup}Hello{markup} bar' - assert md(f'foo<{tag}> Hello bar') == f'foo {markup}Hello{markup} bar' - assert md(f'foo <{tag}>Hello bar') == f'foo {markup}Hello{markup} bar' - assert md(f'foo <{tag}> bar') in ['foo bar', 'foo bar'] # Either is OK + assert md('a
    b
    c', newline_style=BACKSLASH) == 'a\\\nb\\\nc' def test_code(): inline_tests('code', '`') -def test_samp(): - inline_tests('samp', '`') - - -def test_kbd(): - inline_tests('kbd', '`') - - -def test_pre(): - assert md('
    test\n    foo\nbar
    ') == '\n```\ntest\n foo\nbar\n```\n' - assert md('
    test\n    foo\nbar
    ') == '\n```\ntest\n foo\nbar\n```\n' - - def test_del(): inline_tests('del', '~~') -def test_s(): - inline_tests('s', '~~') +def test_div(): + assert md('Hello World') == 'Hello World' + + +def test_em(): + inline_tests('em', '*') def test_h1(): @@ -283,6 +94,8 @@ def test_h2(): def test_hn(): assert md('

    Hello

    ') == '### Hello\n\n' + assert md('

    Hello

    ') == '#### Hello\n\n' + assert md('
    Hello
    ') == '##### Hello\n\n' assert md('
    Hello
    ') == '###### Hello\n\n' @@ -329,87 +142,58 @@ def test_hn_nested_img(): assert md('

    A B

    ') == '### A ' + markdown + ' B\n\n' -def test_hr(): - assert md('Hello
    World') == 'Hello\n\n---\n\nWorld' - assert md('Hello
    World') == 'Hello\n\n---\n\nWorld' - assert md('

    Hello

    \n
    \n

    World

    ') == 'Hello\n\n\n\n\n---\n\n\nWorld\n\n' +def test_hn_atx_headings(): + assert md('

    Hello

    ', heading_style=ATX) == '# Hello\n\n' + assert md('

    Hello

    ', heading_style=ATX) == '## Hello\n\n' + + +def test_hn_atx_closed_headings(): + assert md('

    Hello

    ', heading_style=ATX_CLOSED) == '# Hello #\n\n' + assert md('

    Hello

    ', heading_style=ATX_CLOSED) == '## Hello ##\n\n' def test_head(): assert md('head') == 'head' -def test_atx_headings(): - assert md('

    Hello

    ', heading_style=ATX) == '# Hello\n\n' - assert md('

    Hello

    ', heading_style=ATX) == '## Hello\n\n' - - -def test_atx_closed_headings(): - assert md('

    Hello

    ', heading_style=ATX_CLOSED) == '# Hello #\n\n' - assert md('

    Hello

    ', heading_style=ATX_CLOSED) == '## Hello ##\n\n' +def test_hr(): + assert md('Hello
    World') == 'Hello\n\n---\n\nWorld' + assert md('Hello
    World') == 'Hello\n\n---\n\nWorld' + assert md('

    Hello

    \n
    \n

    World

    ') == 'Hello\n\n\n\n\n---\n\n\nWorld\n\n' def test_i(): assert md('Hello') == '*Hello*' -def test_ol(): - assert md('
    1. a
    2. b
    ') == '1. a\n2. b\n' - assert md('
    1. a
    2. b
    ') == '3. a\n4. b\n' - - -def test_p(): - assert md('

    hello

    ') == 'hello\n\n' - - -def test_strong(): - assert md('Hello') == '**Hello**' - - -def test_ul(): - assert md('') == '* a\n* b\n' - - -def test_nested_ols(): - assert md(nested_ols) == '\n1. 1\n\t1. a\n\t\t1. I\n\t\t2. II\n\t\t3. III\n\t2. b\n\t3. c\n2. 2\n3. 3\n' - - -def test_inline_ul(): - assert md('

    foo

    bar

    ') == 'foo\n\n* a\n* b\n\nbar\n\n' - - -def test_nested_uls(): - """ - Nested ULs should alternate bullet characters. - - """ - assert md(nested_uls) == '\n* 1\n\t+ a\n\t\t- I\n\t\t- II\n\t\t- III\n\t+ b\n\t+ c\n* 2\n* 3\n' - - -def test_bullets(): - assert md(nested_uls, bullets='-') == '\n- 1\n\t- a\n\t\t- I\n\t\t- II\n\t\t- III\n\t- b\n\t- c\n- 2\n- 3\n' - - -def test_li_text(): - assert md('') == '* foo [bar](#)\n* foo bar\n* foo **bar** *space*.\n' - - def test_img(): assert md('Alt text') == '![Alt text](/path/to/img.jpg "Optional title")' assert md('Alt text') == '![Alt text](/path/to/img.jpg)' -def test_div(): - assert md('Hello World') == 'Hello World' +def test_kbd(): + inline_tests('kbd', '`') -def test_table(): - assert md(table) == '\n\n| Firstname | Lastname | Age |\n| --- | --- | --- |\n| Jill | Smith | 50 |\n| Eve | Jackson | 94 |\n\n' - assert md(table_with_html_content) == '\n\n| Firstname | Lastname | Age |\n| --- | --- | --- |\n| **Jill** | *Smith* | [50](#) |\n| Eve | Jackson | 94 |\n\n' - assert md(table_with_header_column) == '\n\n| Firstname | Lastname | Age |\n| --- | --- | --- |\n| Jill | Smith | 50 |\n| Eve | Jackson | 94 |\n\n' - assert md(table_head_body) == '\n\n| Firstname | Lastname | Age |\n| --- | --- | --- |\n| Jill | Smith | 50 |\n| Eve | Jackson | 94 |\n\n' - assert md(table_missing_text) == '\n\n| | Lastname | Age |\n| --- | --- | --- |\n| Jill | | 50 |\n| Eve | Jackson | 94 |\n\n' - assert md(table_missing_head) == '\n\n| | | |\n| --- | --- | --- |\n| Firstname | Lastname | Age |\n| Jill | Smith | 50 |\n| Eve | Jackson | 94 |\n\n' +def test_p(): + assert md('

    hello

    ') == 'hello\n\n' + + +def test_pre(): + assert md('
    test\n    foo\nbar
    ') == '\n```\ntest\n foo\nbar\n```\n' + assert md('
    test\n    foo\nbar
    ') == '\n```\ntest\n foo\nbar\n```\n' + + +def test_s(): + inline_tests('s', '~~') + + +def test_samp(): + inline_tests('samp', '`') + + +def test_strong(): + assert md('Hello') == '**Hello**' def test_strong_em_symbol(): @@ -417,7 +201,3 @@ def test_strong_em_symbol(): assert md('Hello', strong_em_symbol=UNDERSCORE) == '__Hello__' assert md('Hello', strong_em_symbol=UNDERSCORE) == '_Hello_' assert md('Hello', strong_em_symbol=UNDERSCORE) == '_Hello_' - - -def test_newline_style(): - assert md('a
    b
    c', newline_style=BACKSLASH) == 'a\\\nb\\\nc' diff --git a/tests/test_lists.py b/tests/test_lists.py new file mode 100644 index 0000000..08c82c7 --- /dev/null +++ b/tests/test_lists.py @@ -0,0 +1,73 @@ +from markdownify import markdownify as md + + +nested_uls = """ + """ + +nested_ols = """ +
      +
    1. 1 +
        +
      1. a +
          +
        1. I
        2. +
        3. II
        4. +
        5. III
        6. +
        +
      2. +
      3. b
      4. +
      5. c
      6. +
      +
    2. +
    3. 2
    4. +
    5. 3
    6. + """ + + +def test_ol(): + assert md('
      1. a
      2. b
      ') == '1. a\n2. b\n' + assert md('
      1. a
      2. b
      ') == '3. a\n4. b\n' + + +def test_nested_ols(): + assert md(nested_ols) == '\n1. 1\n\t1. a\n\t\t1. I\n\t\t2. II\n\t\t3. III\n\t2. b\n\t3. c\n2. 2\n3. 3\n' + + +def test_ul(): + assert md('') == '* a\n* b\n' + + +def test_inline_ul(): + assert md('

      foo

      bar

      ') == 'foo\n\n* a\n* b\n\nbar\n\n' + + +def test_nested_uls(): + """ + Nested ULs should alternate bullet characters. + + """ + assert md(nested_uls) == '\n* 1\n\t+ a\n\t\t- I\n\t\t- II\n\t\t- III\n\t+ b\n\t+ c\n* 2\n* 3\n' + + +def test_bullets(): + assert md(nested_uls, bullets='-') == '\n- 1\n\t- a\n\t\t- I\n\t\t- II\n\t\t- III\n\t- b\n\t- c\n- 2\n- 3\n' + + +def test_li_text(): + assert md('') == '* foo [bar](#)\n* foo bar\n* foo **bar** *space*.\n' diff --git a/tests/test_tables.py b/tests/test_tables.py new file mode 100644 index 0000000..e481e92 --- /dev/null +++ b/tests/test_tables.py @@ -0,0 +1,130 @@ +from markdownify import markdownify as md + + +table = """ + + + + + + + + + + + + + + + +
      FirstnameLastnameAge
      JillSmith50
      EveJackson94
      """ + + +table_with_html_content = """ + + + + + + + + + + + + + + + +
      FirstnameLastnameAge
      JillSmith50
      EveJackson94
      """ + + +table_with_header_column = """ + + + + + + + + + + + + + + + +
      FirstnameLastnameAge
      JillSmith50
      EveJackson94
      """ + + +table_head_body = """ + + + + + + + + + + + + + + + + + + + +
      FirstnameLastnameAge
      JillSmith50
      EveJackson94
      """ + +table_missing_text = """ + + + + + + + + + + + + + + + + + + + +
      LastnameAge
      Jill50
      EveJackson94
      """ + +table_missing_head = """ + + + + + + + + + + + + + + + +
      FirstnameLastnameAge
      JillSmith50
      EveJackson94
      """ + + +def test_table(): + assert md(table) == '\n\n| Firstname | Lastname | Age |\n| --- | --- | --- |\n| Jill | Smith | 50 |\n| Eve | Jackson | 94 |\n\n' + assert md(table_with_html_content) == '\n\n| Firstname | Lastname | Age |\n| --- | --- | --- |\n| **Jill** | *Smith* | [50](#) |\n| Eve | Jackson | 94 |\n\n' + assert md(table_with_header_column) == '\n\n| Firstname | Lastname | Age |\n| --- | --- | --- |\n| Jill | Smith | 50 |\n| Eve | Jackson | 94 |\n\n' + assert md(table_head_body) == '\n\n| Firstname | Lastname | Age |\n| --- | --- | --- |\n| Jill | Smith | 50 |\n| Eve | Jackson | 94 |\n\n' + assert md(table_missing_text) == '\n\n| | Lastname | Age |\n| --- | --- | --- |\n| Jill | | 50 |\n| Eve | Jackson | 94 |\n\n' + assert md(table_missing_head) == '\n\n| | | |\n| --- | --- | --- |\n| Firstname | Lastname | Age |\n| Jill | Smith | 50 |\n| Eve | Jackson | 94 |\n\n'