PageRenderTime 76ms CodeModel.GetById 49ms app.highlight 20ms RepoModel.GetById 2ms app.codeStats 0ms

/scalate-core/src/test/scala/org/fusesource/scalate/scaml/ScamlTestSupport.scala

http://github.com/scalate/scalate
Scala | 127 lines | 94 code | 13 blank | 20 comment | 1 complexity | 678e67caeb6695a7c64bd8307cb0d3a8 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 scaml
 20
 21import java.util.concurrent.atomic.AtomicInteger
 22import java.io.{ StringWriter, PrintWriter, File }
 23
 24import org.scalatest.exceptions.TestFailedException
 25
 26/**
 27 * @author <a href="http://hiramchirino.com">Hiram Chirino</a>
 28 */
 29class ScamlTestSupport extends TemplateTestSupport {
 30  val testCounter = new AtomicInteger(1)
 31
 32  val NOOP = () => {}
 33
 34  def testRender(description: String, template: String, result: String, before: () => Unit = NOOP, after: () => Unit = NOOP) = {
 35    test(description) {
 36      assertResult(result.trim) {
 37        before()
 38        try {
 39          val output = render(description, template.trim)
 40          if (showOutput) {
 41            log.info(output)
 42          }
 43          output.trim
 44        } finally {
 45          after()
 46        }
 47      }
 48    }
 49  }
 50
 51  def ignoreRender(description: String, template: String, result: String, before: () => Unit = NOOP, after: () => Unit = NOOP) = {
 52    ignore(description) {
 53    }
 54  }
 55
 56  def testInvalidSyntaxException(description: String, template: String, error: String) = {
 57    test(description) {
 58      try {
 59        val data = render(description, template.trim).trim
 60        debug(data)
 61        fail("Expected InvalidSyntaxException was not thrown")
 62      } catch {
 63        case e: TestFailedException => throw e
 64        case e: InvalidSyntaxException => {
 65          assertResult(error) {
 66            e.getMessage
 67          }
 68        }
 69        case x: Throwable =>
 70          x.printStackTrace
 71          fail("Expected InvalidSyntaxException was not thrown.  Instead got a: " + x)
 72      }
 73    }
 74  }
 75
 76  def ignoreInvalidSyntaxException(description: String, template: String, error: String) = {
 77    ignore(description) {
 78    }
 79  }
 80
 81  def testCompilerException(description: String, template: String, error: String) = {
 82    test(description) {
 83      try {
 84        val data = render(description, template.trim).trim
 85        debug(data)
 86        fail("Expected CompilerException was not thrown")
 87      } catch {
 88        case e: TestFailedException => throw e
 89        case e: CompilerException => {
 90          assertResult(error) {
 91            e.errors.head.message
 92          }
 93        }
 94        case x: Throwable =>
 95          x.printStackTrace
 96          fail("Expected CompilerException was not thrown.  Instead got a: " + x)
 97      }
 98    }
 99  }
100
101  def render(name: String, content: String): String = {
102    val buffer = new StringWriter()
103    val out = new PrintWriter(buffer)
104    val uri = "/org/fusesource/scalate/scaml/test" + name
105    val context = new DefaultRenderContext(uri, engine, out) {
106      val name = "Hiram"
107      val title = "MyPage"
108      val href = "http://scalate.fusesource.org"
109      val quality = "scrumptious"
110    }
111
112    engine.bindings = List(Binding("context", context.getClass.getName, true))
113
114    val testIdx = testCounter.incrementAndGet
115    val dir = new File("target/ScamlTest")
116    dir.mkdirs
117    engine.workingDirectory = dir
118    context.attributes("context") = context
119    context.attributes("bean") = Bean("red", 10)
120    context.attributes("label") = "Scalate"
121
122    val template = compileScaml(uri, content)
123    template.render(context)
124    out.close
125    buffer.toString
126  }
127}