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