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