PageRenderTime 9ms CodeModel.GetById 1ms app.highlight 5ms RepoModel.GetById 1ms app.codeStats 0ms

/scalate-core/src/test/scala/org/fusesource/scalate/InjectAttributeTest.scala

http://github.com/scalate/scalate
Scala | 60 lines | 30 code | 10 blank | 20 comment | 3 complexity | 2032da23dea29b6a4d92fab0fa67d9f5 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
19
20import java.io.File
21
22import org.scalatest.ConfigMap
23
24import scala.collection.immutable.Map
25
26class InjectAttributeTest extends TemplateTestSupport {
27
28  test("Using render context directly") {
29    val helper = context.inject[SomeHelper]
30    assert(helper != null)
31    log.info("got helper! " + helper)
32  }
33
34  // in the following test, the compiler does not pass in the
35  // attributes type from the left hand side, which is quite surprising at first
36  // I guess type inferencing only goes from right to left; not left to right
37  val compilerInfersTypeParamsFromTypeOfLHS = false
38  if (compilerInfersTypeParamsFromTypeOfLHS) {
39    test("Using render context directly without explicit type param") {
40      val helper: SomeHelper = context.inject
41      assert(helper != null)
42      log.info("got helper! " + helper)
43    }
44  }
45
46  test("template using injection") {
47    assertUriOutputContains("/org/fusesource/scalate/ioc.ssp", Map("name" -> "James"), "Hello James!")
48  }
49
50  def context = new DefaultRenderContext("dummy.ssp", engine)
51
52  override protected def beforeAll(configMap: ConfigMap) = {
53    super.beforeAll(configMap)
54    engine.sourceDirectories = List(new File(baseDir, "src/test/resources"))
55  }
56}
57
58class SomeHelper(context: RenderContext) {
59  def greeting = "Hello " + context.attributeOrElse("name", "Unknown") + "!"
60}