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