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

http://github.com/scalate/scalate · Scala · 60 lines · 29 code · 7 blank · 24 comment · 2 complexity · c960734ed81d9ed76db0629114e30faa 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 java.io.File
  20. import util.parsing.json.JSON
  21. import org.fusesource.scalate.util.IOUtil
  22. /**
  23. * Runs the system tests from the mustache.js distro
  24. */
  25. class MustacheJSONTest extends MustacheTestSupport {
  26. // Note that these JS files must be valid JSON so using " quoted names
  27. mustacheJsonTest("array_of_strings")
  28. mustacheJsonTest("array_of_strings_options")
  29. mustacheJsonTest("inverted_section")
  30. mustacheJsonTest("template_partial")
  31. // Implementation methods
  32. //-------------------------------------------------------------------------
  33. def mustacheJsonTest(name: String): Unit = {
  34. test(name + " JSON") {
  35. val jText = IOUtil.loadTextFile(new File(rootDir, name + ".js")).trim.stripSuffix(";")
  36. // lets trim the 'var x = ' part to make valid json
  37. val idx = jText.indexOf("=")
  38. val jsonText = if (idx >= 0) jText.substring(idx + 1) else jText
  39. JSON.parseFull(jsonText) match {
  40. case Some(json) =>
  41. debug("Parsed json: %s", json)
  42. json match {
  43. case attributes: Map[_, _] =>
  44. assertMustacheTest(name, attributes.asInstanceOf[Map[String, Any]])
  45. case v =>
  46. fail("Cannot process JSON type: " + v)
  47. }
  48. case _ =>
  49. fail("Could not parse JSON text - strings maybe need quoting?: " + jsonText)
  50. }
  51. }
  52. }
  53. }