/
BindingsSpec.scala
111 lines (87 loc) · 2.93 KB
/
BindingsSpec.scala
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
/*
* Copyright 2010-2011 WorldWide Conferencing, LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package net.liftweb
package http
import xml.{NodeSeq, Text}
import org.specs.Specification
import common._
import util.Helpers._
import Bindings._
/**
* System under specification for Bindings.
*/
object BindingsSpec extends Specification("Bindings Bindings") {
case class MyClass(str: String, i: Int, other: MyOtherClass)
case class MyOtherClass(foo: String)
trait MyClassBinding extends DataBinding[MyClass] {
implicit val otherBinding: DataBinding[MyOtherClass]
override def apply(entity: MyClass) = (xhtml: NodeSeq) => {
val otherTemplate = chooseTemplate("myclass", "other", xhtml)
bind(
"myclass", xhtml,
"str" -> Text("#" + entity.str + "#"),
"i" -> Text(entity.i.toString),
"other" -> entity.other.bind(otherTemplate)
)
}
}
object myOtherClassBinding extends DataBinding[MyOtherClass] {
override def apply(other: MyOtherClass) = (xhtml: NodeSeq) => {
bind("other", xhtml, "foo" -> Text("%" + other.foo + "%"))
}
}
implicit object MyClassConcreteBinding extends MyClassBinding {
override val otherBinding = myOtherClassBinding
}
val template = <div>
<span><myclass:str/></span>
<span><myclass:i/></span>
<myclass:other>
<span><other:foo/></span>
</myclass:other>
</div>
val expected = <div>
<span>#hi#</span>
<span>1</span>
<span>%bar%</span>
</div>
"Bindings.binder with an available implicit databinding" should {
"allow the application of that databinding to an appropriate object" in {
MyClass("hi", 1, MyOtherClass("bar")).bind(template) must equalIgnoreSpace(expected)
}
}
"SHtml" should {
"deal with # in link" in {
val session = new LiftSession("hello", "", Empty)
val href = S.initIfUninitted(session) {
val a = SHtml.link("/foo#bar", () => true, Text("Test"))
(a \ "@href").text
}
href.endsWith("#bar") must_== true
}
}
"CSS Selector Transforms" should {
"retain attributes for input" in {
val session = new LiftSession("hello", "", Empty)
S.initIfUninitted(session) {
val org = <span><input id="frog" class="dog cat"/></span>
val res = ("#frog" #> SHtml.text("", s => ()) ).apply(org)
(res \ "input" \ "@id").text must_== "frog"
(res \ "input" \ "@class").text must_== "dog cat"
}
}
}
}