# coding: UTF-8 require 'test_helper' class MarkdownTest < Redcarpet::TestCase def setup @markdown = Redcarpet::Markdown.new(Redcarpet::Render::HTML) end def render_with(flags, text) Redcarpet::Markdown.new(Redcarpet::Render::HTML, flags).render(text) end def test_that_simple_one_liner_goes_to_html assert_respond_to @markdown, :render assert_equal "

Hello World.

\n", @markdown.render("Hello World.") end def test_that_inline_markdown_goes_to_html markdown = @markdown.render('_Hello World_!') assert_equal "

Hello World!

\n", markdown end def test_that_inline_markdown_starts_and_ends_correctly markdown = render_with({:no_intra_emphasis => true}, '_start _ foo_bar bar_baz _ end_ *italic* **bold** _blah_') assert_equal "

start _ foo_bar bar_baz _ end italic bold blah

\n", markdown markdown = @markdown.render("Run 'rake radiant:extensions:rbac_base:migrate'") assert_equal "

Run 'rake radiant:extensions:rbac_base:migrate'

\n", markdown end def test_that_urls_are_not_doubly_escaped markdown = @markdown.render('[Page 2](/search?query=Markdown+Test&page=2)') assert_equal "

Page 2

\n", markdown end def test_simple_inline_html #markdown = Markdown.new("before\n\n
\n foo\n
\nafter") markdown = @markdown.render("before\n\n
\n foo\n
\n\nafter") assert_equal "

before

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

after

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

Para 1

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

Para 2 Link

\n", markdown end # This isn't in the spec but is Markdown.pl behavior. def test_block_quotes_preceded_by_spaces markdown = @markdown.render( "A wise man once said:\n\n" + " > Isn't it wonderful just to be alive.\n" ) assert_equal "

A wise man once said:

\n\n" + "
\n

Isn't it wonderful just to be alive.

\n
\n", markdown end def test_para_before_block_html_should_not_wrap_in_p_tag markdown = render_with({:lax_spacing => true}, "Things to watch out for\n" + "\n") assert_equal "

Things to watch out for

\n\n" + "\n", markdown 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 = <
  • a
  • b
  • c
  • This paragraph is not part of the list.

    HTML assert_equal expected, @markdown.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

    \n", @markdown.render(text) end def test_that_intra_emphasis_works rd = render_with({}, "foo_bar_baz") assert_equal "

    foobarbaz

    \n", rd rd = render_with({:no_intra_emphasis => true},"foo_bar_baz") assert_equal "

    foo_bar_baz

    \n", rd end def test_that_autolink_flag_works rd = render_with({:autolink => true}, "http://github.com/rtomayko/rdiscount") assert_equal "

    http://github.com/rtomayko/rdiscount

    \n", rd end def test_that_tags_can_have_dashes_and_underscores rd = @markdown.render("foo bar and baz") assert_equal "

    foo bar and baz

    \n", rd end def test_link_syntax_is_not_processed_within_code_blocks markdown = @markdown.render(" This is a code block\n This is a link [[1]] inside\n") assert_equal "
    This is a code block\nThis is a link [[1]] inside\n
    \n", markdown end def test_whitespace_after_urls rd = render_with({:autolink => true}, "Japan: http://www.abc.net.au/news/events/japan-quake-2011/beforeafter.htm (yes, japan)") exp = %{

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

    \n} assert_equal exp, rd end def test_memory_leak_when_parsing_char_links @markdown.render(<<-leaks) 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

    \n", @markdown.render(<<-header) ###### #Body# ###### header end def test_a_hyphen_and_a_equal_should_not_be_converted_to_heading assert_equal "

    -

    \n", @markdown.render("-") assert_equal "

    =

    \n", @markdown.render("=") end def test_that_tables_flag_works text = < true}, text) =~ / true}, text) =~ /
    true}, text) =~ /underlined' output = render_with({:underline => true}, text) assert output.include? 'underlined' assert output.include? 'some' end def test_highlight_flag_works text = "this is ==highlighted==" refute render_with({}, text).include? 'highlighted' output = render_with({:highlight => true}, text) assert output.include? 'highlighted' end def test_quote_flag_works text = 'this is "quote"' refute render_with({}, text).include? 'quote' output = render_with({:quote => true}, text) assert output.include? 'quote' end def test_that_fenced_flag_works text = < true}, text) =~ / true, :lax_spacing => true).render(text) assert out.include?("
    ")
    
        out = Redcarpet::Markdown.new(Redcarpet::Render::HTML, :fenced_code_blocks => true).render(text)
        assert !out.include?("
    ")
      end
    
      def test_that_indented_code_preserves_references
        text = < true).render(text)
        assert out.include?("[1]: http://google.com")
      end
    
      def test_that_fenced_flag_preserves_references
        text = < true).render(text)
        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_with({:fenced_code_blocks => true}, text)
        assert_equal "
    x = 'foo'\n
    \n", html end def test_that_fenced_code_copies_language_verbatim text = "```rust,no_run\nx = 'foo'\n```" html = render_with({:fenced_code_blocks => true}, text) assert_equal "
    x = 'foo'\n
    \n", html end def test_that_indented_flag_works text = < true}, text) !~ /Hello GitHub\n", markdown end def test_autolinking_with_ent_chars markdown = render_with({:autolink => true}, <This a stupid link: https://github.com/rtomayko/tilt/issues?milestone=1&state=open

    \n", markdown end def test_spaced_headers rd = render_with({:space_after_headers => true}, "#123 a header yes\n") assert rd !~ /

    / end def test_proper_intra_emphasis assert render_with({:no_intra_emphasis => true}, "http://en.wikipedia.org/wiki/Dave_Allen_(comedian)") !~ // assert render_with({:no_intra_emphasis => true}, "this fails: hello_world_") !~ // assert render_with({:no_intra_emphasis => true}, "this also fails: hello_world_#bye") !~ // assert render_with({:no_intra_emphasis => true}, "this works: hello_my_world") !~ // assert render_with({:no_intra_emphasis => true}, "句中**粗體**測試") =~ // markdown = "This is (**bold**) and this_is_not_italic!" html = "

    This is (bold) and this_is_not_italic!

    \n" assert_equal html, render_with({:no_intra_emphasis => true}, markdown) markdown = "This is \"**bold**\"" html = "

    This is "bold"

    \n" assert_equal html, render_with({:no_intra_emphasis => true}, markdown) end def test_emphasis_escaping markdown = @markdown.render("**foo\\*** _dd\\_dd_") assert_equal "

    foo* dd_dd

    \n", markdown end def test_char_escaping_when_highlighting markdown = "==attribute\\===" output = render_with({highlight: true}, markdown) assert_equal "

    attribute=

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

      Link

      \n", markdown end def test_superscript markdown = render_with({:superscript => true}, "this is the 2^nd time") assert_equal "

      this is the 2nd time

      \n", markdown end def test_superscript_enclosed_in_parenthesis markdown = render_with({:superscript => true}, "this is the 2^(nd) time") assert_equal "

      this is the 2nd time

      \n", markdown end def test_no_rewind_into_previous_inline result = "

      !dl1@danlec.com

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

      abc123www.foo.com@foo.com

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