# coding: UTF-8 require 'test_helper' class MarkdownTest < Redcarpet::TestCase def setup @renderer = Redcarpet::Render::HTML end def test_that_simple_one_liner_goes_to_html assert_equal "

Hello World.

", render("Hello World.") end def test_that_inline_markdown_goes_to_html assert_equal "

Hello World!

", render('_Hello World_!') end def test_that_inline_markdown_starts_and_ends_correctly output = render('_start _ foo_bar bar_baz _ end_ *italic* **bold** _blah_', with: [:no_intra_emphasis]) expected = "

start _ foo_bar bar_baz _ end italic bold blah

" assert_equal expected, output output = render("Run 'rake radiant:extensions:rbac_base:migrate'") expected = "

Run 'rake radiant:extensions:rbac_base:migrate'

" assert_equal expected, output end def test_that_urls_are_not_doubly_escaped output = render('[Page 2](/search?query=Markdown+Test&page=2)') assert_equal "

Page 2

", output end def test_simple_inline_html output = render("before\n\n
\n foo\n
\n\nafter") expected = "

before

\n\n
\n foo\n
\n\n

after

" assert_equal expected, output end def test_that_html_blocks_do_not_require_their_own_end_tag_line output = render("Para 1\n\n
HTML block\n
\n\nPara 2 [Link](#anchor)") expected = "

Para 1

\n\n
HTML block\n
\n\n

Para 2 Link

" assert_equal expected, output end # This isn't in the spec but is Markdown.pl behavior. def test_block_quotes_preceded_by_spaces output = render <<-Markdown.strip_heredoc A wise man once said: > Isn't it wonderful just to be alive. Markdown expected = <<-HTML.chomp.strip_heredoc

A wise man once said:

Isn't it wonderful just to be alive.

HTML assert_equal expected, output end def test_para_before_block_html_should_not_wrap_in_p_tag output = render("Things to watch out for\n", with: [:lax_spacing]) expected = "

Things to watch out for

\n\n" assert_equal expected, output end # https://github.com/vmg/redcarpet/issues/111 def test_p_with_less_than_4space_indent_should_not_be_part_of_last_list_item text = <<-Markdown * a * b * c This paragraph is not part of the list. Markdown expected = <<-HTML.chomp.strip_heredoc

This paragraph is not part of the list.

HTML assert_equal expected, render(text) end # http://github.com/rtomayko/rdiscount/issues/#issue/13 def test_headings_with_trailing_space text = "The Ant-Sugar Tales \n" + "=================== \n\n" + "By Candice Yellowflower \n" assert_equal "

The Ant-Sugar Tales

\n\n

By Candice Yellowflower

", render(text) end def test_that_intra_emphasis_works assert_equal "

foobarbaz

", render("foo_bar_baz") assert_equal "

foo_bar_baz

", render("foo_bar_baz", with: [:no_intra_emphasis]) end def test_that_autolink_flag_works output = render("http://github.com/rtomayko/rdiscount", with: [:autolink]) expected = "

http://github.com/rtomayko/rdiscount

" assert_equal expected, output end def test_that_tags_can_have_dashes_and_underscores output = render("foo bar and baz") expected = "

foo bar and baz

" assert_equal expected, output end def test_link_syntax_is_not_processed_within_code_blocks output = render(" This is a code block\n This is a link [[1]] inside\n") expected = "
This is a code block\nThis is a link [[1]] inside\n
" assert_equal expected, output end def test_whitespace_after_urls output = render("Japan: http://www.abc.net.au/news/events/japan-quake-2011/beforeafter.htm (yes, japan)", with: [:autolink]) expected = %(

Japan: http://www.abc.net.au/news/events/japan-quake-2011/beforeafter.htm (yes, japan)

) assert_equal expected, output end def test_memory_leak_when_parsing_char_links render(<<-leaks.strip_heredoc) 2. Identify the wild-type cluster and determine all clusters containing or contained by it: wildtype <- wildtype.cluster(h) wildtype.mask <- logical(nclust) wildtype.mask[c(contains(h, wildtype), wildtype, contained.by(h, wildtype))] <- TRUE This could be more elegant. leaks end def test_infinite_loop_in_header assert_equal "

Body

", render(<<-header.strip_heredoc) ###### #Body# ###### header end def test_a_hyphen_and_a_equal_should_not_be_converted_to_heading assert_equal "

-

", render("-") assert_equal "

=

", render("=") end def test_that_tables_flag_works text = <<-Markdown.strip_heredoc aaa | bbbb -----|------ hello|sailor Markdown assert render(text) !~ /underlined' assert output.include? 'underlined' assert output.include? 'some' end def test_highlight_flag_works text = "this is ==highlighted==" output = render(text, with: [:highlight]) refute render(text).include? 'highlighted' assert output.include? 'highlighted' end def test_quote_flag_works text = 'this is a "quote"' output = render(text, with: [:quote]) refute render(text).include? 'quote' assert_equal '

this is a quote

', output end def test_that_fenced_flag_works text = <<-fenced.strip_heredoc This is a simple test ~~~~~ This is some awesome code with tabs and shit ~~~ fenced assert render(text) !~ /") output = render(text, with: [:fenced_code_blocks]) assert !output.include?("
")
  end

  def test_that_indented_code_preserves_references
    text = <<-indented.strip_heredoc
      This is normal text

          Link to [Google][1]

          [1]: http://google.com
    indented

    output = render(text, with: [:fenced_code_blocks])
    assert output.include?("[1]: http://google.com")
  end

  def test_that_fenced_flag_preserves_references
    text = <<-fenced.strip_heredoc
      This is normal text

      ```
      Link to [Google][1]

      [1]: http://google.com
      ```
    fenced

    out = render(text, with: [:fenced_code_blocks])
    assert out.include?("[1]: http://google.com")
  end

  def test_that_fenced_code_copies_language_verbatim_with_braces
    text = "```{rust,no_run}\nx = 'foo'\n```"
    html = render(text, with: [:fenced_code_blocks])

    assert_equal "
x = 'foo'\n
", html end def test_that_fenced_code_copies_language_verbatim text = "```rust,no_run\nx = 'foo'\n```" html = render(text, with: [:fenced_code_blocks]) assert_equal "
x = 'foo'\n
", html end def test_that_indented_flag_works text = <<-indented.strip_heredoc This is a simple text This is some awesome code with shit And this is again a simple text indented assert render(text) =~ /GitHub" assert_equal expected, output end def test_autolinking_with_ent_chars markdown = <<-Markdown.strip_heredoc This a stupid link: https://github.com/rtomayko/tilt/issues?milestone=1&state=open Markdown output = render(markdown, with: [:autolink]) assert_equal "

This a stupid link: https://github.com/rtomayko/tilt/issues?milestone=1&state=open

", output end def test_spaced_headers output = render("#123 a header yes\n", with: [:space_after_headers]) assert output !~ /

/ end def test_proper_intra_emphasis assert render("http://en.wikipedia.org/wiki/Dave_Allen_(comedian)", with: [:no_intra_emphasis]) !~ // assert render("this fails: hello_world_", with: [:no_intra_emphasis]) !~ // assert render("this also fails: hello_world_#bye", with: [:no_intra_emphasis]) !~ // assert render("this works: hello_my_world", with: [:no_intra_emphasis]) !~ // assert render("句中**粗體**測試", with: [:no_intra_emphasis]) =~ // markdown = "This is (**bold**) and this_is_not_italic!" output = "

This is (bold) and this_is_not_italic!

" assert_equal output, render(markdown, with: [:no_intra_emphasis]) markdown = "This is \"**bold**\"" output = "

This is "bold"

" assert_equal output, render(markdown, with: [:no_intra_emphasis]) end def test_emphasis_escaping assert_equal "

foo* dd_dd

", render("**foo\\*** _dd\\_dd_") end def test_char_escaping_when_highlighting output = render("==attribute\\===", with: [:highlight]) assert_equal "

attribute=

", output end def test_ordered_lists_with_lax_spacing output = render("Foo:\n1. Foo\n2. Bar", with: [:lax_spacing]) assert_match /
    /, output assert_match /
  1. Foo<\/li>/, output end def test_references_with_tabs_after_colon output = render("[Link][id]\n[id]:\t\t\thttp://google.es") assert_equal "

    Link

    ", output end def test_superscript output = render("this is the 2^nd time", with: [:superscript]) assert_equal "

    this is the 2nd time

    ", output end def test_superscript_enclosed_in_parenthesis output = render("this is the 2^(nd) time", with: [:superscript]) assert_equal "

    this is the 2nd time

    ", output end def test_no_rewind_into_previous_inline result = "

    !dl1@danlec.com

    " output = render("_!dl_1@danlec.com", with: [:autolink]) assert_equal result, output result = "

    abc123www.foo.com@foo.com

    " output = render("abc123_www.foo.com_@foo.com", with: [:autolink]) assert_equal result, output end def test_autolink_with_period_next_to_url result = %(

    Checkout a cool site like https://github.com.

    ) output = render("Checkout a cool site like https://github.com.", with: [:autolink]) assert_equal result, output end def test_single_dashes_on_table_headers markdown = <<-Markdown.strip_heredoc | a | b | | - | - | | c | d | Markdown output = render(markdown, with: [:tables]) assert_match /

/, output end end