PageRenderTime 13ms CodeModel.GetById 9ms app.highlight 2ms RepoModel.GetById 1ms app.codeStats 0ms

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