PageRenderTime 27ms CodeModel.GetById 13ms app.highlight 11ms RepoModel.GetById 1ms app.codeStats 0ms

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

http://github.com/scalate/scalate
Scala | 166 lines | 111 code | 29 blank | 26 comment | 3 complexity | 84a9661e87d86d074b92df7135330998 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 collection.immutable.Map
 21
 22case class Item(name: String, current: Boolean, url: String) {
 23  def link = !current
 24}
 25
 26case class Complex(header: String, item: List[Item]) {
 27  def list = !empty
 28
 29  def empty = item.isEmpty
 30}
 31
 32case class HigherOrder(name: String, helper: String) {
 33  def bolder(text: String) = <b>
 34                               { text }
 35                             </b> :: Text(" " + helper) :: Nil
 36}
 37
 38/**
 39 * Runs the system tests from the mustache.js distro
 40 */
 41class MustacheJsSystemTest extends MustacheTestSupport {
 42  // TODO FixMe
 43  val runFailingTests = false
 44
 45  mustacheTest("array_of_strings", Map("array_of_strings" -> List("hello", "world")))
 46  mustacheTest("array_of_strings_options", Map("array_of_strings_options" -> List("hello", "world")))
 47
 48  // Note we had to zap the comments from the sample results - seems bug in mustache.js
 49  mustacheTest("comments", Map("title" -> (() => "A Comedy of Errors")))
 50  mustacheTest("comments_multi_line", Map("title" -> (() => "A Comedy of Errors")))
 51
 52  mustacheTest("complex", "map", Map(
 53    "header" -> (() => "Colors"),
 54    "item" -> List(
 55      Map("name" -> "red", "current" -> true, "url" -> "#Red"),
 56      Map("name" -> "green", "current" -> false, "url" -> "#Green"),
 57      Map("name" -> "blue", "current" -> false, "url" -> "#Blue")),
 58    "link" -> ((s: Scope) => !(s("current").get.asInstanceOf[Boolean])),
 59    "list" -> ((s: Scope) => s("item").get.asInstanceOf[List[_]].size > 0),
 60    "empty" -> ((s: Scope) => s("item").get.asInstanceOf[List[_]].size == 0)))
 61
 62  mustacheTest("complex", "case class", Map(
 63    "header" -> (() => "Colors"),
 64    "item" -> List(
 65      Item("red", true, "#Red"),
 66      Item("green", false, "#Green"),
 67      Item("blue", false, "#Blue")),
 68    "list" -> ((s: Scope) => s("item").get.asInstanceOf[List[_]].size > 0),
 69    "empty" -> ((s: Scope) => s("item").get.asInstanceOf[List[_]].size == 0)))
 70
 71  mustacheTest("complex", "nested case class", Map(
 72    "it" -> Complex("Colors", List(
 73      Item("red", true, "#Red"),
 74      Item("green", false, "#Green"),
 75      Item("blue", false, "#Blue")))))
 76
 77  mustacheTest("crazy_recursive", Map(
 78    "top_nodes" -> Map(
 79      "contents" -> "1",
 80      "children" -> List(Map(
 81        "contents" -> "2",
 82        "children" -> List(Map(
 83          "contents" -> "3",
 84          "children" -> List()))), Map(
 85        "contents" -> "4",
 86        "children" -> List(Map(
 87          "contents" -> "5",
 88          "children" -> List(Map(
 89            "contents" -> "6",
 90            "children" -> List())))))))))
 91
 92  mustacheTest("delimiters", Map(
 93    "first" -> "It worked the first time.",
 94    "second" -> "And it worked the second time.",
 95    "third" -> "Then, surprisingly, it worked the third time.",
 96    "fourth" -> "Fourth time also fine!."))
 97
 98  mustacheTest("double_section", Map("t" -> true, "two" -> "second"))
 99
100  mustacheTest("empty_template", Map())
101
102  // Note that mustache.ruby quotes the &quot; which we follow unlike the mustache.js test case
103  mustacheTest("escaped", Map(
104    "title" -> (() => "Bear > Shark"),
105    "entities" -> "&quot;"))
106
107  mustacheTest("error_not_found", Map())
108
109  if (runFailingTests) {
110    mustacheTest("higher_order_sections", "map", Map(
111      "name" -> "Tater",
112      "helper" -> "To tinker?",
113      "bolder" -> ((text: String) => <b>
114                                       { text }
115                                     </b> :: Text(" To tinker?") :: Nil)))
116
117    mustacheTest("higher_order_sections", "case class with string functor", Map(
118      "it" -> HigherOrder("Tater", "To tinker?")))
119  }
120
121  mustacheTest("inverted_section", Map("repo" -> List()))
122
123  mustacheTest("null_string", Map(
124    "name" -> "Elise",
125    "glytch" -> true,
126    "binary" -> false,
127    "value" -> null,
128    "numeric" -> (() => Double.NaN)))
129
130  mustacheTest("recursive", Map("show" -> false))
131
132  mustacheTest("recursion_with_same_names", Map(
133    "name" -> "name",
134    "description" -> "desc",
135    "terms" -> List(
136      Map("name" -> "t1", "index" -> 0),
137      Map("name" -> "t2", "index" -> 1))))
138
139  mustacheTest("reuse_of_enumerables", Map("terms" -> List(
140    Map("name" -> "t1", "index" -> 0),
141    Map("name" -> "t2", "index" -> 1))))
142
143  mustacheTest("section_as_context", Map("a_object" -> Map(
144    "title" -> "this is an object",
145    "description" -> "one of its attributes is a list",
146    "a_list" -> List(
147      Map("label" -> "listitem1"),
148      Map("label" -> "listitem2")))))
149
150  // Note we use internationalisation by default, so commas introduced in the 1000s in these numbers
151  mustacheTest("simple", Map(
152    "name" -> "Chris",
153    "value" -> 10000,
154    "taxed_value" -> ((s: Scope) => s("value").get.asInstanceOf[Int] * 0.6),
155    "in_ca" -> true))
156
157  // Note used the result from mustache.ruby
158  //mustacheTest("template_partial", Map("title" -> (() => "Welcome"), "partial" -> Map("again" -> "Goodbye")))
159  mustacheTest("template_partial", Map("title" -> (() => "Welcome")))
160
161  mustacheTest("two_in_a_row", Map("name" -> "Joe", "greeting" -> "Welcome"))
162
163  mustacheTest("unescaped", Map("title" -> (() => "Bear > Shark")))
164  mustacheTest("utf8", Map("test" -> "中文"))
165  mustacheTest("unknown_pragma", Map())
166}