You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

markdown_formatter_test.rb 5.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  1. # frozen_string_literal: true
  2. # Redmine - project management software
  3. # Copyright (C) 2006-2019 Jean-Philippe Lang
  4. #
  5. # This program is free software; you can redistribute it and/or
  6. # modify it under the terms of the GNU General Public License
  7. # as published by the Free Software Foundation; either version 2
  8. # of the License, or (at your option) any later version.
  9. #
  10. # This program is distributed in the hope that it will be useful,
  11. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. # GNU General Public License for more details.
  14. #
  15. # You should have received a copy of the GNU General Public License
  16. # along with this program; if not, write to the Free Software
  17. # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  18. require File.expand_path('../../../../../test_helper', __FILE__)
  19. class Redmine::WikiFormatting::MarkdownFormatterTest < ActionView::TestCase
  20. if Object.const_defined?(:Redcarpet)
  21. def setup
  22. @formatter = Redmine::WikiFormatting::Markdown::Formatter
  23. end
  24. def test_syntax_error_in_image_reference_should_not_raise_exception
  25. assert @formatter.new("!>[](foo.png)").to_html
  26. end
  27. def test_empty_image_should_not_raise_exception
  28. assert @formatter.new("![]()").to_html
  29. end
  30. # re-using the formatter after getting above error crashes the
  31. # ruby interpreter. This seems to be related to
  32. # https://github.com/vmg/redcarpet/issues/318
  33. def test_should_not_crash_redcarpet_after_syntax_error
  34. @formatter.new("!>[](foo.png)").to_html rescue nil
  35. assert @formatter.new("![](foo.png)").to_html.present?
  36. end
  37. def test_inline_style
  38. assert_equal "<p><strong>foo</strong></p>", @formatter.new("**foo**").to_html.strip
  39. end
  40. def test_not_set_intra_emphasis
  41. assert_equal "<p>foo_bar_baz</p>", @formatter.new("foo_bar_baz").to_html.strip
  42. end
  43. def test_wiki_links_should_be_preserved
  44. text = 'This is a wiki link: [[Foo]]'
  45. assert_include '[[Foo]]', @formatter.new(text).to_html
  46. end
  47. def test_redmine_links_with_double_quotes_should_be_preserved
  48. text = 'This is a redmine link: version:"1.0"'
  49. assert_include 'version:"1.0"', @formatter.new(text).to_html
  50. end
  51. def test_should_support_syntax_highlight
  52. text = <<-STR
  53. ~~~ruby
  54. def foo
  55. end
  56. ~~~
  57. STR
  58. assert_select_in @formatter.new(text).to_html, 'pre code.ruby.syntaxhl' do
  59. assert_select 'span.k', :text => 'def'
  60. end
  61. end
  62. def test_should_not_allow_invalid_language_for_code_blocks
  63. text = <<-STR
  64. ~~~foo
  65. test
  66. ~~~
  67. STR
  68. assert_equal "<pre>test\n</pre>", @formatter.new(text).to_html
  69. end
  70. def test_external_links_should_have_external_css_class
  71. text = 'This is a [link](http://example.net/)'
  72. assert_equal '<p>This is a <a href="http://example.net/" class="external">link</a></p>', @formatter.new(text).to_html.strip
  73. end
  74. def test_locals_links_should_not_have_external_css_class
  75. text = 'This is a [link](/issues)'
  76. assert_equal '<p>This is a <a href="/issues">link</a></p>', @formatter.new(text).to_html.strip
  77. end
  78. def test_markdown_should_not_require_surrounded_empty_line
  79. text = <<-STR
  80. This is a list:
  81. * One
  82. * Two
  83. STR
  84. assert_equal "<p>This is a list:</p>\n\n<ul>\n<li>One</li>\n<li>Two</li>\n</ul>", @formatter.new(text).to_html.strip
  85. end
  86. def test_footnotes
  87. text = <<-STR
  88. This is some text[^1].
  89. [^1]: This is the foot note
  90. STR
  91. expected = <<-EXPECTED
  92. <p>This is some text<sup id="fnref1"><a href="#fn1">1</a></sup>.</p>
  93. <div class="footnotes">
  94. <hr>
  95. <ol>
  96. <li id="fn1">
  97. <p>This is the foot note&nbsp;<a href="#fnref1">&#8617;</a></p>
  98. </li>
  99. </ol>
  100. </div>
  101. EXPECTED
  102. assert_equal expected.gsub(%r{[\r\n\t]}, ''), @formatter.new(text).to_html.gsub(%r{[\r\n\t]}, '')
  103. end
  104. STR_WITH_PRE = [
  105. # 0
  106. "# Title
  107. Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Maecenas sed libero.",
  108. # 1
  109. "## Heading 2
  110. ~~~ruby
  111. def foo
  112. end
  113. ~~~
  114. Morbi facilisis accumsan orci non pharetra.
  115. ```
  116. Pre Content:
  117. ## Inside pre
  118. <tag> inside pre block
  119. Morbi facilisis accumsan orci non pharetra.
  120. ```",
  121. # 2
  122. "### Heading 3
  123. Nulla nunc nisi, egestas in ornare vel, posuere ac libero."]
  124. def test_get_section_should_ignore_pre_content
  125. text = STR_WITH_PRE.join("\n\n")
  126. assert_section_with_hash STR_WITH_PRE[1..2].join("\n\n"), text, 2
  127. assert_section_with_hash STR_WITH_PRE[2], text, 3
  128. end
  129. def test_update_section_should_not_escape_pre_content_outside_section
  130. text = STR_WITH_PRE.join("\n\n")
  131. replacement = "New text"
  132. assert_equal [STR_WITH_PRE[0..1], "New text"].flatten.join("\n\n"),
  133. @formatter.new(text).update_section(3, replacement)
  134. end
  135. def test_should_support_underlined_text
  136. text = 'This _text_ should be underlined'
  137. assert_equal '<p>This <u>text</u> should be underlined</p>', @formatter.new(text).to_html.strip
  138. end
  139. private
  140. def assert_section_with_hash(expected, text, index)
  141. result = @formatter.new(text).get_section(index)
  142. assert_kind_of Array, result
  143. assert_equal 2, result.size
  144. assert_equal expected, result.first, "section content did not match"
  145. assert_equal Digest::MD5.hexdigest(expected), result.last, "section hash did not match"
  146. end
  147. end
  148. end