/scalate-wikitext/src/test/scala/org/fusesource/scalate/wikitext/SnippetsTest.scala

http://github.com/scalate/scalate · Scala · 145 lines · 109 code · 14 blank · 22 comment · 4 complexity · 1a1f7900d8aab0955a0608b778990cf6 MD5 · raw file

  1. /**
  2. * Copyright (C) 2009-2011 the original author or authors.
  3. * See the notice.md file distributed with this work for additional
  4. * information regarding copyright ownership.
  5. *
  6. * Licensed under the Apache License, Version 2.0 (the "License");
  7. * you may not use this file except in compliance with the License.
  8. * You may obtain a copy of the License at
  9. *
  10. * http://www.apache.org/licenses/LICENSE-2.0
  11. *
  12. * Unless required by applicable law or agreed to in writing, software
  13. * distributed under the License is distributed on an "AS IS" BASIS,
  14. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  15. * See the License for the specific language governing permissions and
  16. * limitations under the License.
  17. */
  18. package org.fusesource.scalate.wikitext
  19. import java.io.File
  20. import org.junit.Assert.assertTrue
  21. /**
  22. * Test cases for {snippet} macro support
  23. */
  24. class SnippetsTest extends AbstractConfluenceTest {
  25. // setting up a snippet prefix 'test' -> 'src/test/resources'
  26. Snippets.addPrefix("test", new File(baseDir, "src/test/resources").toURI.toString)
  27. Snippets.usePygmentize = false
  28. test("snippets macro without snippet id") {
  29. assertFilter(
  30. """
  31. h1. Full snippet here
  32. {snippet:url=test/Test.java}
  33. """,
  34. """<h1 id="Fullsnippethere">Full snippet here</h1><div class="snippet"><pre class="java">
  35. /**
  36. * Copyright (C) 2009-2011 the original author or authors.
  37. * See the notice.md file distributed with this work for additional
  38. * information regarding copyright ownership.
  39. *
  40. * Licensed under the Apache License, Version 2.0 (the "License");
  41. * you may not use this file except in compliance with the License.
  42. * You may obtain a copy of the License at
  43. *
  44. * http://www.apache.org/licenses/LICENSE-2.0
  45. *
  46. * Unless required by applicable law or agreed to in writing, software
  47. * distributed under the License is distributed on an "AS IS" BASIS,
  48. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  49. * See the License for the specific language governing permissions and
  50. * limitations under the License.
  51. */
  52. /**
  53. * A simple Test class
  54. */
  55. public class Test {
  56. // START SNIPPET: doSomething
  57. public void doSomething() {
  58. // does something very interesting
  59. }
  60. // END SNIPPET: doSomething
  61. public void doSomethingElse() {
  62. // does something else, even more interesting
  63. }
  64. }
  65. </pre></div>""")
  66. }
  67. test("snippets macro with snippet id") {
  68. assertFilter(
  69. """
  70. h1. Snippet with id
  71. {snippet:url=test/Test.java|id=doSomething}
  72. """,
  73. """<h1 id="Snippetwithid">Snippet with id</h1><div class="snippet"><pre class="java">
  74. public void doSomething() {
  75. // does something very interesting
  76. }
  77. </pre></div>""")
  78. }
  79. test("snippets macro with snippet id and explicit language") {
  80. assertFilter(
  81. """
  82. h1. Snippet with id
  83. {snippet:url=test/Test.java|id=doSomething|lang=erlang}
  84. """,
  85. """<h1 id="Snippetwithid">Snippet with id</h1><div class="snippet"><pre class="erlang">
  86. public void doSomething() {
  87. // does something very interesting
  88. }
  89. </pre></div>""")
  90. }
  91. test("snippets macro with relative url and snippet id") {
  92. assertFilter(
  93. """
  94. h1. Snippet with id
  95. {snippet:url=src/test/resources/Test.java|id=doSomething}
  96. """,
  97. """<h1 id="Snippetwithid">Snippet with id</h1><div class="snippet"><pre class="java">
  98. public void doSomething() {
  99. // does something very interesting
  100. }
  101. </pre></div>""")
  102. }
  103. if (Pygmentize.isInstalled) {
  104. test("snippets macro with pygmentize enabled") {
  105. val source = """
  106. h1. Snippet with id
  107. {snippet:url=test/Test.java|id=doSomething|pygmentize=true}
  108. """
  109. // Since the output result changed from Pygmentize 2.x, there are tests for 1.x and 2.x
  110. if (Pygmentize.majorVersion >= 2) {
  111. assertFilter(
  112. source,
  113. """<h1 id="Snippetwithid">Snippet with id</h1><div class="syntax"><div class="highlight"><pre><span></span> <span class="kd">public</span> <span class="kt">void</span> <span class="nf">doSomething</span><span class="o">()</span> <span class="o">{</span>&#x000A; <span class="c1">// does something very interesting</span>&#x000A; <span class="o">}</span>&#x000A;</pre></div>&#x000A;</div>""")
  114. } else {
  115. assertFilter(
  116. source,
  117. """<h1 id="Snippetwithid">Snippet with id</h1><div class="syntax"><div class="highlight"><pre> <span class="kd">public</span> <span class="kt">void</span> <span class="nf">doSomething</span><span class="o">()</span> <span class="o">{</span>&#x000A; <span class="c1">// does something very interesting</span>&#x000A; <span class="o">}</span>&#x000A;</pre></div>&#x000A;</div>""")
  118. }
  119. }
  120. } else {
  121. warn("Pygmentize not installed so ignoring the tests")
  122. }
  123. test("URL prefix handling") {
  124. assertTrue(
  125. "Only leading occurence of prefix should only have been replaced",
  126. Snippets.handlePrefix("test/with/a/subfolder/named/test").endsWith("with/a/subfolder/named/test"))
  127. }
  128. }