/scalate-core/src/test/scala/org/fusesource/scalate/scuery/LoopTest.scala
Scala | 131 lines | 87 code | 10 blank | 34 comment | 0 complexity | f245dbf3f7909e8b6b8fdf0fdd654a4f 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
22class LoopTest extends FunSuiteSupport {
23 val people = List(Person("James", "Beckington"), Person("Hiram", "Tampa"))
24
25 val xml = <html>
26 <head>
27 <title>My Title</title>
28 </head>
29 <body>
30 <div id="header">Header</div>
31 <div id="content">
32 <table class="people">
33 <tr>
34 <th>Name</th>
35 <th>Location</th>
36 </tr>
37 <tr class="person">
38 <td class="name"></td>
39 <td class="location"></td>
40 </tr>
41 </table>
42 </div>
43 <div id="messages"></div>
44 <div id="footer">Footer</div>
45 </body>
46 </html>
47
48 /*
49 test("loop using new transformer on each person") {
50 object transformer1 extends Transformer {
51 $(".person") { node =>
52
53 people.flatMap { p =>
54 new Transformer {
55 $(".name").contents = p.name
56 $(".location").contents = p.location
57 }.apply(node)
58 }
59 }
60 }
61 assertTransformed(transformer1(xml))
62 }
63 */
64
65 test("loop using new Transform statement on each person") {
66 object transformer2 extends Transformer {
67 $(".person") { node =>
68 people.flatMap { p =>
69 new Transform(node) {
70 $(".name").contents = p.name
71 $(".location").contents = p.location
72 }
73 }
74 }
75 }
76 assertTransformed(transformer2(xml))
77 }
78
79 test("loop using transform method on each person") {
80 object transformer3 extends Transformer {
81 $(".person") { node =>
82 people.flatMap { p =>
83 transform(node) { $ =>
84 $(".name").contents = p.name
85 $(".location").contents = p.location
86 }
87 }
88 }
89 }
90 assertTransformed(transformer3(xml))
91 }
92
93 test("loop using transform method with new transformer") {
94 object transformer4 extends Transformer {
95 $(".person") { node =>
96 people.flatMap { p =>
97 // TODO how to know what the current ancestor is?
98 transform(node, new Transformer {
99 $(".name").contents = p.name
100 $(".location").contents = p.location
101 })
102 }
103 }
104 }
105 assertTransformed(transformer4(xml))
106 }
107
108 test("loop using NestedTransformer") {
109 object transformer5 extends NestedTransformer {
110 $(".person") { node =>
111 people.flatMap { p =>
112 transform(node) { t =>
113 $(".name").contents = p.name
114 $(".location").contents = p.location
115 }
116 }
117 }
118 }
119 assertTransformed(transformer5(xml))
120 }
121
122 def assertTransformed(result: NodeSeq): Unit = {
123 debug("got result: %s", result)
124
125 assertResult("James") { (result \\ "td")(0).text }
126 assertResult("Beckington") { (result \\ "td")(1).text }
127
128 assertResult("Hiram") { (result \\ "td")(2).text }
129 assertResult("Tampa") { (result \\ "td")(3).text }
130 }
131}