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