PageRenderTime 18ms CodeModel.GetById 2ms app.highlight 11ms RepoModel.GetById 1ms app.codeStats 1ms

/scalate-core/src/test/scala/org/fusesource/scalate/scuery/SetAttributeTest.scala

http://github.com/scalate/scalate
Scala | 63 lines | 35 code | 10 blank | 18 comment | 0 complexity | 778c88c07b6afe6dd3b11dfae18b8ff7 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.scuery
19
20import _root_.org.fusesource.scalate.FunSuiteSupport
21import xml.Node
22
23class SetAttributeTest extends FunSuiteSupport {
24  val xml = <html>
25              <body>
26                <div id="content">
27                  <a href="#" class="foo" title="A foo link">foo link</a>
28                  <a href="#" class="bar" title="A bar link">bar link</a>
29                  <a href="#" class="jog" title="A jog link">jog link</a>
30                </div>
31              </body>
32            </html>
33
34  test(" transform") {
35    object transformer extends Transformer {
36
37      // 3 different approaches to changing attributes
38      $("a.foo").attribute("href", "http://scalate.fusesource.org/")
39
40      $("a.bar").attribute("href").value = "http://scalate.fusesource.org/documentation/"
41
42      $("a.jog").attribute("href") {
43        e =>
44          "http://scalate.fusesource.org/documentation/" + (e \ "@class") + ".html"
45      }
46    }
47
48    val result = transformer(xml)
49
50    debug("got result: " + result)
51
52    assertLink((result \\ "a")(0), "http://scalate.fusesource.org/", "foo", "A foo link")
53    assertLink((result \\ "a")(1), "http://scalate.fusesource.org/documentation/", "bar", "A bar link")
54    assertLink((result \\ "a")(2), "http://scalate.fusesource.org/documentation/jog.html", "jog", "A jog link")
55  }
56
57  def assertLink(a: Node, href: String, className: String, title: String): Unit = {
58    debug("testing link node: " + a)
59    assertResult(href) { (a \ "@href").toString }
60    assertResult(className) { (a \ "@class").toString }
61    assertResult(title) { (a \ "@title").toString }
62  }
63}