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

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