# coding: UTF-8 rootdir = File.dirname(File.dirname(__FILE__)) $LOAD_PATH.unshift "#{rootdir}/lib" if defined? Encoding Encoding.default_internal = 'UTF-8' end require 'test/unit' require 'redcarpet' require 'redcarpet/render_man' require 'nokogiri' def html_equal(html_a, html_b) assert_equal Nokogiri::HTML::DocumentFragment.parse(html_a).to_html, Nokogiri::HTML::DocumentFragment.parse(html_b).to_html end class SmartyPantsTest < Test::Unit::TestCase def setup @pants = Redcarpet::Render::SmartyPants end def test_that_smart_converts_single_quotes_in_words_that_end_in_re markdown = @pants.render("
They're not for sale.
") assert_equal "They’re not for sale.
", markdown end def test_that_smart_converts_single_quotes_in_words_that_end_in_ll markdown = @pants.render("Well that'll be the day
") assert_equal "Well that’ll be the day
", markdown end def test_that_smart_converts_double_quotes_to_curly_quotes rd = @pants.render(%("Quoted text"
)) assert_equal %(“Quoted text”
), rd end def test_that_smart_gives_ve_suffix_a_rsquo rd = @pants.render("I've been meaning to tell you ..
") assert_equal "I’ve been meaning to tell you ..
", rd end def test_that_smart_gives_m_suffix_a_rsquo rd = @pants.render("I'm not kidding
") assert_equal "I’m not kidding
", rd end def test_that_smart_gives_d_suffix_a_rsquo rd = @pants.render("what'd you say?
") assert_equal "what’d you say?
", rd end end class HTMLRenderTest < Test::Unit::TestCase def setup @markdown = Redcarpet::Markdown.new(Redcarpet::Render::HTML) @rndr = { :no_html => Redcarpet::Render::HTML.new(:filter_html => true), :no_images => Redcarpet::Render::HTML.new(:no_images => true), :no_links => Redcarpet::Render::HTML.new(:no_links => true), :safe_links => Redcarpet::Render::HTML.new(:safe_links_only => true), :escape_html => Redcarpet::Render::HTML.new(:escape_html => true), :hard_wrap => Redcarpet::Render::HTML.new(:hard_wrap => true), } end def render_with(rndr, text) Redcarpet::Markdown.new(rndr).render(text) end # Hint: overrides filter_html, no_images and no_links def test_that_escape_html_works source = <<script>BAD</script>
<img src="/favicon.ico" /> EOE markdown = render_with(@rndr[:escape_html], source) html_equal expected, markdown end def test_that_filter_html_works markdown = render_with(@rndr[:no_html], 'Through NO ') html_equal "
Through NO DOUBLE NO
", markdown end def test_filter_html_doesnt_break_two_space_hard_break markdown = render_with(@rndr[:no_html], "Lorem, \nipsum\n") html_equal "Lorem,
\nipsum
Hello World.
", @markdown.render("Hello World.") end def test_that_inline_markdown_goes_to_html markdown = @markdown.render('_Hello World_!') html_equal "Hello World!
", 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_') html_equal "start _ foo_bar bar_baz _ end italic bold blah
", markdown markdown = @markdown.render("Run 'rake radiant:extensions:rbac_base:migrate'") html_equal "Run 'rake radiant:extensions:rbac_base:migrate'
", markdown end def test_that_urls_are_not_doubly_escaped markdown = @markdown.render('[Page 2](/search?query=Markdown+Test&page=2)') html_equal "\n", markdown end def test_simple_inline_html #markdown = Markdown.new("before\n\nbefore
\n\nafter
\n", markdown end def test_that_html_blocks_do_not_require_their_own_end_tag_line markdown = @markdown.render("Para 1\n\nHTML block\n
Para 1
\n\nHTML block\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" ) html_equal "A wise man once said:
\n\n" + "\n", markdown end def test_para_before_block_html_should_not_wrap_in_p_tag markdown = render_with({:lax_html_blocks => true}, "Things to watch out for\n" + "Isn't it wonderful just to be alive.
\n
Things to watch out for
\n\n" + "By Candice Yellowflower
\n", @markdown.render(text) end def test_that_intra_emphasis_works rd = render_with({}, "foo_bar_baz") html_equal "foobarbaz
\n", rd rd = render_with({:no_intra_emphasis => true},"foo_bar_baz") html_equal "foo_bar_baz
\n", rd end def test_that_autolink_flag_works rd = render_with({:autolink => true}, "http://github.com/rtomayko/rdiscount") html_equal "http://github.com/rtomayko/rdiscount
\n", rd end if "".respond_to?(:encoding) def test_should_return_string_in_same_encoding_as_input input = "Yogācāra" output = @markdown.render(input) assert_equal input.encoding.name, output.encoding.name end def test_should_return_string_in_same_encoding_not_in_utf8 input = "testing".encode('US-ASCII') output = @markdown.render(input) assert_equal input.encoding.name, output.encoding.name end def test_should_accept_non_utf8_or_ascii input = "testing \xAB\xCD".force_encoding('ASCII-8BIT') output = @markdown.render(input) assert_equal 'ASCII-8BIT', output.encoding.name end end def test_that_tags_can_have_dashes_and_underscores rd = @markdown.render("foofoo
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)
} html_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 html_equal @markdown.render(<<-header), "