PageRenderTime 44ms CodeModel.GetById 8ms app.highlight 28ms RepoModel.GetById 1ms app.codeStats 1ms

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