PageRenderTime 23ms CodeModel.GetById 13ms app.highlight 7ms RepoModel.GetById 1ms app.codeStats 0ms

/scalate-test/src/main/scala/org/fusesource/scalate/test/TemplateTestSupport.scala

http://github.com/scalate/scalate
Scala | 97 lines | 60 code | 16 blank | 21 comment | 9 complexity | 9fb6503e025620ce9c59199c52a803c0 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
19package test
20
21import java.io.File
22import util.IOUtil
23import org.scalatest.ConfigMap
24
25/**
26 * A useful base class for testing templates
27 */
28class TemplateTestSupport extends FunSuiteSupport {
29
30  var engine: TemplateEngine = _
31  var showOutput = false
32
33  override protected def beforeAll(configMap: ConfigMap) = {
34    super.beforeAll(configMap)
35
36    engine = createTemplateEngine
37    val workingDir = new File(baseDir, "target/test-data/" + getClass.getSimpleName)
38    if (workingDir.exists) {
39      // lets delete it before we run the tests
40      IOUtil.recursiveDelete(workingDir)
41    }
42    engine.sourceDirectories = List(new File(baseDir, "src/test/resources"))
43    engine.workingDirectory = workingDir
44  }
45
46  protected def createTemplateEngine = new TemplateEngine
47
48  def assertUriOutput(expectedOutput: String, uri: String, attributes: Map[String, Any] = Map(), trim: Boolean = false): String =
49    assertOutput(expectedOutput, fromUri(uri), attributes, trim)
50
51  def assertOutput(expectedOutput: String, template: TemplateSource, attributes: Map[String, Any] = Map(), trim: Boolean = false): String = {
52    var output = engine.layout(template, attributes)
53    debug("output: '" + output + "'")
54
55    if (trim) {
56      output = output.trim
57    }
58    assertResult(expectedOutput) { output }
59    output
60  }
61
62  def assertOutputContains(source: TemplateSource, expected: String*): String =
63    assertOutputContains(source, Map[String, Any](), expected: _*)
64
65  def assertOutputContains(source: TemplateSource, attributes: Map[String, Any], expected: String*): String = {
66    val output = engine.layout(source, attributes)
67    if (showOutput) {
68      info("output: '" + output + "'")
69    } else {
70      debug("output: '" + output + "'")
71    }
72
73    assertTextContains(output, "template " + source, expected: _*)
74    output
75  }
76
77  def assertUriOutputContains(uri: String, expected: String*): String =
78    assertUriOutputContains(uri, Map[String, Any](), expected: _*)
79
80  def assertUriOutputContains(uri: String, attributes: Map[String, Any], expected: String*): String =
81    assertOutputContains(fromUri(uri), attributes, expected: _*)
82
83  protected def fromUri(uri: String) = TemplateSource.fromUri(uri, engine.resourceLoader)
84
85  def assertTextContains(source: String, description: => String, textLines: String*): Unit = {
86    assume(source != null, "text was null for " + description)
87    var index = 0
88    for (text <- textLines if index >= 0) {
89      index = source.indexOf(text, index)
90      if (index >= 0) {
91        index += text.length
92      } else {
93        assume(false, "Text does not contain '" + text + "' for " + description + " when text was:\n" + source)
94      }
95    }
96  }
97}