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