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.

MarkdownTest.java 6.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. /*
  2. * SonarQube
  3. * Copyright (C) 2009-2023 SonarSource SA
  4. * mailto:info AT sonarsource DOT com
  5. *
  6. * This program is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU Lesser General Public
  8. * License as published by the Free Software Foundation; either
  9. * version 3 of the License, or (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  14. * Lesser General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU Lesser General Public License
  17. * along with this program; if not, write to the Free Software Foundation,
  18. * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  19. */
  20. package org.sonar.markdown;
  21. import org.junit.Test;
  22. import static org.assertj.core.api.Assertions.assertThat;
  23. public class MarkdownTest {
  24. @Test
  25. public void shouldDecorateAbsoluteUrl() {
  26. assertThat(Markdown.convertToHtml("http://google.com"))
  27. .isEqualTo("<a href=\"http://google.com\" target=\"_blank\" rel=\"noopener noreferrer\">http://google.com</a>");
  28. }
  29. @Test
  30. public void shouldDecorateRelativeUrl() {
  31. assertThat(Markdown.convertToHtml("[Google](/google/com)"))
  32. .isEqualTo("<a href=\"/google/com\">Google</a>");
  33. }
  34. @Test
  35. public void shouldDecorateDocumentedLink() {
  36. assertThat(Markdown.convertToHtml("For more details, please [check online documentation](http://docs.sonarsource.com/sonarqube/display/SONAR)."))
  37. .isEqualTo("For more details, please <a href=\"http://docs.sonarsource.com/sonarqube/display/SONAR\" target=\"_blank\" rel=\"noopener noreferrer\">check online documentation</a>.");
  38. }
  39. @Test
  40. public void shouldDecorateEndOfLine() {
  41. assertThat(Markdown.convertToHtml("1\r2\r\n3\n")).isEqualTo("1<br/>2<br/>3<br/>");
  42. }
  43. @Test
  44. public void shouldDecorateUnorderedList() {
  45. assertThat(Markdown.convertToHtml(" * one\r* two\r\n* three\n * \n *five"))
  46. .isEqualTo("<ul><li>one</li>\r<li>two</li>\r\n<li>three</li>\n<li> </li>\n</ul> *five");
  47. assertThat(Markdown.convertToHtml(" * one\r* two")).isEqualTo("<ul><li>one</li>\r<li>two</li></ul>");
  48. assertThat(Markdown.convertToHtml("* \r*")).isEqualTo("<ul><li> </li>\r</ul>*");
  49. }
  50. @Test
  51. public void shouldDecorateOrderedList() {
  52. assertThat(Markdown.convertToHtml(" 1. one\r1. two\r\n1. three\n 1. \n 1.five"))
  53. .isEqualTo("<ol><li>one</li>\r<li>two</li>\r\n<li>three</li>\n<li> </li>\n</ol> 1.five");
  54. assertThat(Markdown.convertToHtml(" 1. one\r1. two")).isEqualTo("<ol><li>one</li>\r<li>two</li></ol>");
  55. assertThat(Markdown.convertToHtml("1. \r1.")).isEqualTo("<ol><li> </li>\r</ol>1.");
  56. }
  57. @Test
  58. public void shouldDecorateHeadings() {
  59. assertThat(Markdown.convertToHtml(" = Top\r== Sub\r\n=== Sub sub\n ==== \n ===== five\n============ max"))
  60. .isEqualTo("<h1>Top</h1><h2>Sub</h2><h3>Sub sub</h3><h4></h4><h5>five</h5><h6>max</h6>");
  61. }
  62. @Test
  63. public void shouldDecorateBlockquote() {
  64. assertThat(Markdown.convertToHtml("> Yesterday <br/> it worked\n> Today it is not working\r\n> Software is like that\r"))
  65. .isEqualTo("<blockquote>Yesterday &lt;br/&gt; it worked<br/>\nToday it is not working<br/>\r\nSoftware is like that<br/>\r</blockquote>");
  66. assertThat(Markdown.convertToHtml("HTML elements should <em>not</em> be quoted!"))
  67. .isEqualTo("HTML elements should &lt;em&gt;not&lt;/em&gt; be quoted!");
  68. }
  69. @Test
  70. public void shouldDecorateMixedOrderedAndUnorderedList() {
  71. assertThat(Markdown.convertToHtml(" 1. one\r* two\r\n1. three\n * \n 1.five"))
  72. .isEqualTo("<ol><li>one</li>\r</ol><ul><li>two</li>\r\n</ul><ol><li>three</li>\n</ol><ul><li> </li>\n</ul> 1.five");
  73. }
  74. @Test
  75. public void shouldDecorateCode() {
  76. assertThat(Markdown.convertToHtml("This is a ``line of code``")).isEqualTo("This is a <code>line of code</code>");
  77. assertThat(Markdown.convertToHtml("This is not a ``line of code")).isEqualTo("This is not a ``line of code");
  78. }
  79. @Test
  80. public void shouldDecorateMultipleLineCode() {
  81. assertThat(Markdown.convertToHtml("This is a ``\nline of code\nOn multiple lines\n``")).isEqualTo("This is a <pre><code>line of code\nOn multiple lines</code></pre>");
  82. assertThat(Markdown.convertToHtml("This is not a ``line of code\nOn multiple lines``")).isEqualTo("This is not a ``line of code<br/>On multiple lines``");
  83. assertThat(Markdown.convertToHtml("This is not a ``line of code\nOn multiple lines")).isEqualTo("This is not a ``line of code<br/>On multiple lines");
  84. }
  85. @Test
  86. public void shouldDecorateMultipleLineCodeWithLanguageSpecified() {
  87. assertThat(Markdown.convertToHtml("This is a ``java\nline of code\nOn multiple lines\n``")).isEqualTo("This is a <pre lang=\"java\"><code>line of code\nOn multiple lines</code></pre>");
  88. assertThat(Markdown.convertToHtml("This is not a ``java line of code\nOn multiple lines``")).isEqualTo("This is not a ``java line of code<br/>On multiple lines``");
  89. assertThat(Markdown.convertToHtml("This is not a ``java \nline of code\nOn multiple lines``")).isEqualTo("This is not a ``java <br/>line of code<br/>On multiple lines``");
  90. }
  91. @Test
  92. public void shouldEmphasisText() {
  93. assertThat(Markdown.convertToHtml("This is *Sparta !!!*")).isEqualTo("This is <strong>Sparta !!!</strong>");
  94. assertThat(Markdown.convertToHtml("This is *A*")).isEqualTo("This is <strong>A</strong>");
  95. assertThat(Markdown.convertToHtml("This should not be * \n emphasized")).isEqualTo("This should not be * <br/> emphasized");
  96. assertThat(Markdown.convertToHtml("This is *very* very *important*")).isEqualTo("This is <strong>very</strong> very <strong>important</strong>");
  97. assertThat(Markdown.convertToHtml("Not * emphasized * because of whitespaces")).isEqualTo("Not * emphasized * because of whitespaces");
  98. assertThat(Markdown.convertToHtml("Not *emphasized * because of whitespace")).isEqualTo("Not *emphasized * because of whitespace");
  99. assertThat(Markdown.convertToHtml("Not * emphasized* because of whitespace")).isEqualTo("Not * emphasized* because of whitespace");
  100. assertThat(Markdown.convertToHtml("emphasized*inside*word")).isEqualTo("emphasized<strong>inside</strong>word");
  101. assertThat(Markdown.convertToHtml("*Emphasize many words*")).isEqualTo("<strong>Emphasize many words</strong>");
  102. }
  103. @Test
  104. public void shouldNotChangeAnythingInTheText() {
  105. assertThat(Markdown.convertToHtml("My text is $123 ''")).isEqualTo("My text is $123 ''");
  106. }
  107. }