PageRenderTime 28ms CodeModel.GetById 11ms app.highlight 12ms RepoModel.GetById 1ms app.codeStats 0ms

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

http://github.com/scalate/scalate
Scala | 89 lines | 63 code | 9 blank | 17 comment | 5 complexity | a3b0da7db52a2496956b46cc5b2ab809 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.NodeSeq
22
23class TransformTableStripeTest extends FunSuiteSupport {
24  val xml = <table class="people">
25              <thead>
26                <tr>
27                  <th>Name</th>
28                  <th>Location</th>
29                </tr>
30              </thead>
31              <tbody>
32                <tr class="person odd">
33                  <td class="name">Odd name</td>
34                  <td class="location">Odd location</td>
35                </tr>
36                <tr class="person even">
37                  <td class="name">Odd name</td>
38                  <td class="location">Odd location</td>
39                </tr>
40                <tr class="person empty">
41                  <td colspan="2">There are no people yet!</td>
42                </tr>
43              </tbody>
44            </table>
45
46  class PersonTransformer(people: List[Person]) extends Transformer {
47    $("tbody").contents {
48      node =>
49        if (people.isEmpty) {
50          node.$("tr.empty")
51        } else {
52          people.zipWithIndex.flatMap {
53            case (p, i) =>
54              val row = if (i % 2 == 0) node.$("tr.odd") else node.$("tr.even")
55              transform(row) {
56                $ =>
57                  $(".name").contents = p.name
58                  $(".location").contents = p.location
59              }
60          }
61        }
62    }
63  }
64
65  test("stripe table") {
66    val transformer = new PersonTransformer(List(Person("James", "Beckington"), Person("Hiram", "Tampa")))
67    val result = transformer(xml)
68    debug("got result: " + result)
69
70    assertSize("tbody tr", result, 2)
71    assertSize("tbody tr.odd", result, 1)
72    assertSize("tbody tr.even", result, 1)
73
74    assertText("tbody tr.odd .name", result, "James")
75    assertText("tbody tr.even .name", result, "Hiram")
76    assertText("tbody tr.odd .location", result, "Beckington")
77    assertText("tbody tr.even .location", result, "Tampa")
78  }
79
80  test("stripe empty table") {
81    val striper = new PersonTransformer(List())
82    val result = striper(xml)
83    debug("got result: " + result)
84
85    assertSize("tbody tr", result, 1)
86    assertSize("tbody tr.empty", result, 1)
87  }
88
89}