PageRenderTime 35ms CodeModel.GetById 26ms app.highlight 6ms RepoModel.GetById 1ms app.codeStats 0ms

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