PageRenderTime 87ms CodeModel.GetById 22ms RepoModel.GetById 1ms app.codeStats 0ms

/scalate-core/src/test/scala/org/fusesource/scalate/mustache/MustacheParserTest.scala

http://github.com/scalate/scalate
Scala | 165 lines | 117 code | 26 blank | 22 comment | 1 complexity | 7ce6e3121b5025810187c4975adafe11 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.mustache
  19. import org.fusesource.scalate.{ InvalidSyntaxException, FunSuiteSupport }
  20. import org.fusesource.scalate.util.IOUtil
  21. import java.io.File
  22. /**
  23. * @version $Revision : 1.1 $
  24. */
  25. class MustacheParserTest extends FunSuiteSupport {
  26. implicit def stringToText(x: String) = Text(x)
  27. test("set directive") {
  28. assertParses(
  29. List(Text("* "), Variable("default_tags"), Text(" * "),
  30. SetDelimiter("<%", "%>"), Text("* "), Variable("erb_style_tags"), Text(" * "),
  31. SetDelimiter("{{", "}}"), Text("* "), Variable("default_tags_again")),
  32. "* {{default_tags}} * {{=<% %>=}} * <% erb_style_tags %> * <%={{ }}=%> * {{ default_tags_again }}")
  33. }
  34. test("plain text") {
  35. assertValid("some text more text")
  36. assertParses(List(Text("some text more text")), """some text more text""")
  37. }
  38. test("variable") {
  39. assertParses(
  40. List(Text("some text "), Variable("foo"), Text(" "), Variable("bar"), Text(" more text")),
  41. "some text {{foo}} {{bar}} more text")
  42. }
  43. test("unescape variable") {
  44. assertParses(
  45. List(Text("some text "), Variable("foo", true), Text(" "), Variable("bar", true), Text(" more text")),
  46. "some text {{&foo}} {{& bar}} more text")
  47. }
  48. test("unescape with treble moustache") {
  49. assertParses(
  50. List(Text("some text "), Variable("foo", true), Text(" more text")),
  51. "some text {{{foo}}} more text")
  52. }
  53. test("open close section") {
  54. assertParses(
  55. List(Text("* "), Section("foo", List(Text("bar "))), Text("*")),
  56. "* {{#foo}} bar {{/foo}} *")
  57. }
  58. test("invert variable and partial") {
  59. assertParses(
  60. List(Text("* "), InvertSection("foo", List(Partial("bar"))), Text("*")),
  61. "* {{^foo}} {{>bar}}{{/foo}} *")
  62. }
  63. // set delimiter
  64. test("just set directive") {
  65. assertParses(
  66. List(SetDelimiter("<%", "%>")),
  67. "{{=<% %>=}}")
  68. }
  69. test("text and set directive") {
  70. assertParses(
  71. List(Text("* "), SetDelimiter("<%", "%>"), Text("*")),
  72. "* {{=<% %>=}} *")
  73. }
  74. test("whitespace with sections") {
  75. assertParses(
  76. List(
  77. Section("terms", List(Variable("name"), Text("\n "),
  78. Variable("index"), Text("\n"))),
  79. Section("terms", List(Variable("name"), Text("\n "), Variable("index"), Text("\n")))),
  80. loadTestFile("reuse_of_enumerables.html"))
  81. }
  82. test("newline after expression") {
  83. assertParses(
  84. List(Variable("greeting"), Text(", "), Variable("name"), Text("!")),
  85. loadTestFile("two_in_a_row.html"))
  86. }
  87. ignore("complex whitespace") {
  88. assertParses(
  89. List(Variable("greeting"), Text(", "), Variable("name"), Text("!")),
  90. loadTestFile("complex.html"))
  91. }
  92. // test bad syntax
  93. assertFail("text {{-}}")
  94. assertFail("text {{")
  95. assertFail("text {{}")
  96. assertFail("text {{}}")
  97. test("missing end tag") {
  98. expectSyntaxException("Missing section end '{{/foo}}' for section beginning at 1.3") {
  99. "* {{#foo}} bar "
  100. }
  101. }
  102. def assertValid(text: String): List[Statement] = {
  103. debug("Parsing...")
  104. debug(text)
  105. debug("")
  106. val lines = (new MustacheParser).parse(text)
  107. for (line <- lines) {
  108. debug("=> " + line)
  109. }
  110. debug("")
  111. lines
  112. }
  113. def assertParses(expected: List[Statement], text: String): List[Statement] = {
  114. val lines = assertValid(text)
  115. assertResult(expected) { lines }
  116. lines
  117. }
  118. def syntaxException(block: => Unit) = {
  119. val e = intercept[InvalidSyntaxException] {
  120. block
  121. }
  122. debug(e, "caught: " + e)
  123. e
  124. }
  125. def assertFail(template: String): Unit = {
  126. test("bad syntax: " + template) {
  127. syntaxException {
  128. assertValid(template)
  129. }
  130. }
  131. }
  132. def expectSyntaxException(message: String)(block: => String): Unit = {
  133. val e = intercept[InvalidSyntaxException] {
  134. assertValid(block)
  135. }
  136. assert(e.getMessage.contains(message), "InvalidSyntaxException message did not contain the text: \n " + message + "\nInstead got: \n " + e.getMessage)
  137. }
  138. protected def loadTestFile(name: String) = IOUtil.loadTextFile(new File(baseDir, "src/test/resources/moustache/js/" + name))
  139. }