PageRenderTime 19ms CodeModel.GetById 7ms app.highlight 9ms RepoModel.GetById 1ms app.codeStats 0ms

/scalate-core/src/test/scala/org/fusesource/scalate/ssp/VelocityDirectiveTest.scala

http://github.com/scalate/scalate
Scala | 144 lines | 100 code | 18 blank | 26 comment | 0 complexity | 304a3aebd245f96d6108a594276c6ca4 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.ssp
 19
 20import org.fusesource.scalate.{ CompilerException, TemplateTestSupport }
 21
 22/**
 23 * @version $Revision : 1.1 $
 24 */
 25class VelocityDirectiveTest extends TemplateTestSupport {
 26  test("escape of #") {
 27    assertSspOutput("""hey # and \# foo""", """hey \# and \\# foo""")
 28    assertSspOutput("""#if (foo) blah #end""", """\#if (foo) blah \#end""")
 29  }
 30
 31  test("for loop") {
 32    assertSspOutput("1 2 3 ", "#for(i <- 1 to 3)${i} #end")
 33  }
 34
 35  test("if elseif else") {
 36    val template = compileSsp("if elseif else", """<%@ val n: String %>
 37#if (n == "James")
 38Hey James
 39#elseif (n == "Hiram")
 40Yo Hiram
 41#else
 42Dunno
 43#end
 44""")
 45
 46    assertTrimOutput("Hey James", template, Map("n" -> "James"))
 47    assertTrimOutput("Yo Hiram", template, Map("n" -> "Hiram"))
 48    assertTrimOutput("Dunno", template, Map("n" -> "Foo"))
 49  }
 50
 51  test("match case otherwise") {
 52    val template = compileSsp("match case otherwise", """<%@ val n: String %>
 53#match(n)
 54#case("James")
 55Hey James
 56#case("Hiram")
 57Yo Hiram
 58#otherwise
 59Dunno
 60#end
 61""")
 62
 63    assertTrimOutput("Hey James", template, Map("n" -> "James"))
 64    assertTrimOutput("Yo Hiram", template, Map("n" -> "Hiram"))
 65    assertTrimOutput("Dunno", template, Map("n" -> "Foo"))
 66  }
 67
 68  test("import test") {
 69    val template = compileSsp("import test", """
 70#import(java.util.Date)
 71time is: ${new Date()}
 72""")
 73    val output = engine.layout("foo1.ssp", template).trim
 74    assert(output.startsWith("time is:"))
 75  }
 76
 77  test("import using block") {
 78    val template = compileSsp("import using block", """
 79#{
 80  import java.util.Date
 81}#
 82time is: ${new Date()}
 83""")
 84    val output = engine.layout("foo2.ssp", template).trim
 85    assert(output.startsWith("time is:"))
 86  }
 87
 88  test("do with no expression works ok") {
 89    val template = compileSsp("do with no expression", """
 90start
 91#do()
 92foo
 93#end
 94end
 95""")
 96    val output = engine.layout("foo3.ssp", template).trim
 97    assertResult(List("start", "foo", "end")) { output.split("\\s+").toList }
 98  }
 99
100  // #match and #case issues
101  testSspSyntaxEception("non whitespace between #match #case", "#match(n) bad #case(5) a #otherwise b #end")
102  testSspSyntaxEception("cannot have other directive between #match #case", "#match(n) #if(5) #case(5) a #otherwise b #end")
103
104  // correct use of #end
105  testSspSyntaxEception("missing #end", "#for(i <- 1 to 3) blah")
106  testSspSyntaxEception("too many #end", "#for(i <- 1 to 3) blah #end #end")
107
108  // check incorrect nesting...
109  testSspSyntaxEception("#elseif without #if", "#for(i <- 1 to 3) #elseif(x > 5) #end")
110  testSspSyntaxEception("#else without #if", "#for(i <- 1 to 3) #else #end")
111
112  testSspSyntaxEception("#case without #match", "#for(i <- 1 to 3) #case(x) #end")
113  testSspSyntaxEception("#otherwie without #match", "#for(i <- 1 to 3) #otherwise #end")
114
115  // check that we don't use #elseif or #case after #otherwise or #else
116  testSspSyntaxEception("#else after #elseif", "#if(x > 5) a #else b #elseif(x < 1) c #end")
117  testSspSyntaxEception("#else after #elseelseif", "#if(x > 5) a #else b #else c #end")
118  testSspSyntaxEception("#otherwise after #case", "#match(x) #otherwise b #case(5) c #end")
119  testSspSyntaxEception("#otherwise after #otherwise", "#match(x) #otherwise b #otherwise c #end")
120
121  // check that we don't use multiple #else or #otherwise
122  testSspSyntaxEception("too many #else", "#if(x > 5) a #elseif(x < 1) z #else b #else c #end")
123  testSspSyntaxEception("too many #else without #eliseif", "#if(x > 5) a #else b #else c #end")
124  testSspSyntaxEception("too many #otherwise", "#match(x) #case(5) a #otherwise b #otherwise c #end")
125  testSspSyntaxEception("too many #otherwise without #case", "#match(x) #otherwise b #otherwise c #end")
126
127  // check that we can open/close clauses within a parent close
128  test("nested if statements") {
129    assertTrimSspOutput("worked", """<%@ val x: Int %>
130#if (x < 1)
131foo
132#else
133  #if (x < 1)
134  foo2
135  #elseif (x > 4)
136  worked
137  #else
138  foo3
139  #end
140#end
141""", Map("x" -> 5))
142  }
143
144}