rootdir = File.dirname(File.dirname(__FILE__)) $LOAD_PATH.unshift "#{rootdir}/lib" require 'test/unit' require 'markdown' require 'nokogiri' MARKDOWN_TEST_DIR = "#{rootdir}/test/MarkdownTest_1.0.3" class MarkdownTest < Test::Unit::TestCase def html_equal(html_a, html_b) assert_equal Nokogiri::HTML::DocumentFragment.parse(html_a).to_xhtml, Nokogiri::HTML::DocumentFragment.parse(html_b).to_xhtml #assert_equal html_a, html_b end def test_that_extension_methods_are_present_on_markdown_class assert Markdown.instance_methods.map{|m| m.to_s }.include?('to_html'), "Markdown class should respond to #to_html" end def test_that_simple_one_liner_goes_to_html markdown = Markdown.new('Hello World.') assert_respond_to markdown, :to_html html_equal "
Hello World.
", markdown.to_html.strip end def test_that_inline_markdown_goes_to_html markdown = Markdown.new('_Hello World_!') html_equal "Hello World!
", markdown.to_html.strip end def test_that_inline_markdown_starts_and_ends_correctly markdown = Markdown.new('_start _ foo_bar bar_baz _ end_ *italic* **bold** _blah_', :no_intraemphasis) assert_respond_to markdown, :to_html html_equal "start _ foo_bar bar_baz _ end italic bold blah
", markdown.to_html.strip markdown = Markdown.new("Run 'rake radiant:extensions:rbac_base:migrate'") html_equal "Run 'rake radiant:extensions:rbac_base:migrate'
", markdown.to_html.strip end def test_that_filter_html_works markdown = Markdown.new('Through NO ', :filter_html) html_equal "Through NO DOUBLE NO
", markdown.to_html.strip end def test_that_bluecloth_restrictions_are_supported markdown = Markdown.new('Hello World.') [:filter_html, :filter_styles].each do |restriction| assert_respond_to markdown, restriction assert_respond_to markdown, "#{restriction}=" end assert_not_equal true, markdown.filter_html assert_not_equal true, markdown.filter_styles markdown = Markdown.new('Hello World.', :filter_html, :filter_styles) assert_equal true, markdown.filter_html assert_equal true, markdown.filter_styles end def test_that_redcloth_attributes_are_supported markdown = RedcarpetCompat.new('Hello World.') assert_respond_to markdown, :fold_lines assert_respond_to markdown, :fold_lines= assert_not_equal true, markdown.fold_lines markdown = RedcarpetCompat.new('Hello World.', :fold_lines) assert_equal true, markdown.fold_lines end def test_that_redcloth_to_html_with_single_arg_is_supported markdown = Markdown.new('Hello World.') assert_nothing_raised(ArgumentError) { markdown.to_html(true) } end def test_that_smart_converts_single_quotes_in_words_that_end_in_re markdown = Markdown.new("They're not for sale.", :smart) html_equal "They’re not for sale.
\n", markdown.to_html end def test_that_smart_converts_single_quotes_in_words_that_end_in_ll markdown = Markdown.new("Well that'll be the day", :smart) html_equal "Well that’ll be the day
\n", markdown.to_html end def test_that_urls_are_not_doubly_escaped markdown = Markdown.new('[Page 2](/search?query=Markdown+Test&page=2)') html_equal "\n", markdown.to_html end # FIXME: # The markdown standard requires a blank newline after a HTML tag, # [ \t]*\n[ \t*]\n # # This test shouldn't pass unless there's a blank newline before the `after` # word in the original Markdown. # # You can change this behavior by #undef'ing UPSKIRT_NEWLINE_AFTER_TAGS # before compiling the library def test_simple_inline_html #markdown = Markdown.new("before\n\nbefore
\n\nafter
\n", markdown.to_html end def test_that_html_blocks_do_not_require_their_own_end_tag_line markdown = Markdown.new("Para 1\n\nHTML block\n
Para 1
\n\nHTML block\n
Para 2 Link
\n", markdown.to_html end def test_filter_html_doesnt_break_two_space_hard_break markdown = Markdown.new("Lorem, \nipsum\n", :filter_html) html_equal "Lorem,
\nipsum
A wise man once said:
\n" + "\n\n", markdown.to_html end def test_para_before_block_html_should_not_wrap_in_p_tag markdown = Redcarpet.new( "Things to watch out for\n" + "Isn't it wonderful just to be alive.
\n
Things to watch out for
\n\n" + "foo
bar
baz
foo
bar
baz
By Candice Yellowflower
\n", markdown.to_html end # Build tests for each file in the MarkdownTest test suite Dir["#{MARKDOWN_TEST_DIR}/Tests/*.text"].each do |text_file| basename = File.basename(text_file).sub(/\.text$/, '') html_file = text_file.sub(/text$/, 'html') method_name = basename.gsub(/[-,()]/, '').gsub(/\s+/, '_').downcase define_method "test_#{method_name}" do markdown = Markdown.new(File.read(text_file)) actual_html = markdown.to_html assert_not_nil actual_html end define_method "test_#{method_name}_smart" do markdown = Markdown.new(File.read(text_file), :smart) actual_html = markdown.to_html assert_not_nil actual_html end end end