|
Lift Framework example source code file (SnippetSpec.scala)
The Lift Framework SnippetSpec.scala source code/*
* 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._
import org.specs.Specification
import common._
import util.Helpers._
/**
* System under specification for SnippetSpec.
*/
object SnippetSpec extends Specification("SnippetSpec Specification") {
def makeReq = new Req(Req.NilPath, "", GetRequest, Empty, null,
System.nanoTime, System.nanoTime, false,
() => ParamCalcInfo(Nil, Map.empty, Nil, Empty), Map())
"LiftSession" should {
"Correctly process lift:content_id" in {
val ret = LiftSession.checkForContentId(<html lift:content_id="content">
<head/>
<body>
<div id="content" class="lift:surround"/>
</body>
</html>)
ret must ==/ (<div id="content" class="lift:surround"/>)
}
"Correctly process body class" in {
val ret = LiftSession.checkForContentId(<html>
<head/>
<body class="lift:content_id=frog">
<div> mooose dog
<div id="frog" class="lift:surround"/>
</span>
</body>
</html>)
ret must ==/ (<lift:surround id="dog">
val session = new LiftSession("", "hello", Empty)
val ret =
S.init(makeReq, session) {
S.mapSnippetsWith("foo" -> ChangeVar.foo _) {
for {
s <- S.session
} yield s.processSurroundAndInclude("test",
<lift:foo>{res})
}
}
ret.open_! must ==/(res)
}
"Snippet invocation fails in stateless mode (function table)" in {
val res = <div>dog
val ret =
S.statelessInit(Req.nil) {
S.mapSnippetsWith("foo" -> Funky.foo _) {
for {
s <- S.session
} yield s.processSurroundAndInclude("test",
<lift:foo>{res})
}
}
(ret.open_! \ "@class").text must_== "snippeterror"
}
"Snippet invocation succeeds in normal mode (function table)" in {
val res = <div>dog
val session = new LiftSession("", "hello", Empty)
val ret =
S.init(makeReq, session) {
S.mapSnippetsWith("foo" -> Funky.foo _) {
for {
s <- S.session
} yield s.processSurroundAndInclude("test",
<lift:foo>{res})
}
}
ret.open_! must ==/(res)
}
"run string input" in {
val session = new LiftSession("", "hello", Empty)
S.init(makeReq, session) {
val ret = SHtml.onSubmit(s => ())(<input/>)
ret.size must_== 1
(ret \ "@name").text.length must be > 0
}
}
"run string checkbox must have hidden element" in {
val session = new LiftSession("", "hello", Empty)
S.init(makeReq, session) {
val ret = SHtml.onSubmitBoolean(s => ())(<input type="checkbox"/>)
ret.size must_== 2
(ret \\ "input" ).flatMap(_ \ "@name").map(_.text).mkString.length must be > 0
}
}
"Check snippets as Function1[NodeSeq, NodeSeq]" in {
/* FIXME SBT: Very very inconsistent in the build environment, pendingUntilFixed misreports
val session = new LiftSession("", "hello", Empty)
val ret = S.init(makeReq, session) {
for {
s <- S.session
} yield s.processSurroundAndInclude("test",
<lift:Meower>Moo)
}
ret.open_! must ==/ (<yak/>)
*/
}
"Check snippets via run" in {
/* FIXME SBT: Very very inconsistent in the build environment, pendingUntilFixed misreports
val session = new LiftSession("", "hello", Empty)
val ret = S.init(makeReq, session) {
for {
s <- S.session
} yield s.processSurroundAndInclude("test",
<input class="lift:Splunker"/>)
}
(ret.open_! \ "@name").text.length must be > 0
*/
}
"Eager Eval works" in {
val res = <div>dog
val session = new LiftSession("", "hello", Empty)
S.init(makeReq, session) {
S.mapSnippetsWith("foo" -> ChangeVar.foo _) {
for {
s <- S.session
} yield s.processSurroundAndInclude("test",
<div class="l:foo?eager_eval=true">a)
}
"Correctly process not lift:designer_friendly" in {
val xml = <html>
<head/>
<body>
<div class="lift:surround"/>
</body>
</html>
val ret = LiftSession.checkForContentId(xml)
ret must_== xml
}
"Snippet invocation works <lift:xxx/>" in {
val res = <div/>
val ret =
S.statelessInit(Req.nil) {
S.mapSnippetsWith("foo" -> ((a: NodeSeq) => a)) {
for {
s <- S.session
} yield s.processSurroundAndInclude("test", <lift:foo>{res})
}
}
ret.open_! must ==/( res)
}
"Snippet invocation works <l:xxx/>" in {
val res = <div/>
val ret =
S.statelessInit(Req.nil) {
S.mapSnippetsWith("foo" -> ((a: NodeSeq) => a)) {
for {
s <- S.session
} yield s.processSurroundAndInclude("test", <l:foo>{res})
}
}
ret.open_! must ==/( res)
}
"Snippet invocation works class='l:foo'" in {
val res = <div/>
val ret =
S.statelessInit(Req.nil) {
S.mapSnippetsWith("foo" -> ((a: NodeSeq) => a)) {
for {
s <- S.session
} yield s.processSurroundAndInclude("test", <div class="l:foo" />)
}
}
ret.open_! must ==/( res)
}
"Snippet invocation works class='l:foo' and ? for attr sep" in {
val res = <div/>
def testAttrs(in: NodeSeq): NodeSeq = {
S.attr("bing") must_== Full("bong")
S.attr("fuzz") must_== Full("faz snark")
S.attr("noodle") must_== Full("FatPoodle")
in
}
val ret =
S.statelessInit(Req.nil) {
S.mapSnippetsWith("foo" -> testAttrs _) {
for {
s <- S.session
} yield s.processSurroundAndInclude("test", <div class="l:foo?bing=bong?fuzz=faz+snark?noodle=FatPoodle" />)
}
}
ret.open_! must ==/( res)
}
"Snippet invocation works class='l:foo' and ; for attr sep" in {
val res = <div/>
def testAttrs(in: NodeSeq): NodeSeq = {
S.attr("bing") must_== Full("bong")
S.attr("fuzz") must_== Full("faz snark")
S.attr("noodle") must_== Full("FatPoodle")
in
}
val ret =
S.statelessInit(Req.nil) {
S.mapSnippetsWith("foo" -> testAttrs _) {
for {
s <- S.session
} yield s.processSurroundAndInclude("test", <div class="l:foo?bing=bong;fuzz=faz+snark;noodle=FatPoodle" />)
}
}
ret.open_! must ==/( res)
}
"Snippet invocation works class='l:foo' and & for attr sep" in {
val res = <div/>
def testAttrs(in: NodeSeq): NodeSeq = {
S.attr("bing") must_== Full("bong")
S.attr("fuzz") must_== Full("faz snark")
S.attr("noodle") must_== Full("FatPoodle")
in
}
val ret =
S.statelessInit(Req.nil) {
S.mapSnippetsWith("foo" -> testAttrs _) {
for {
s <- S.session
} yield s.processSurroundAndInclude("test", <div class="l:foo?bing=bong&fuzz=faz+snark&noodle=FatPoodle" />)
}
}
ret.open_! must ==/( res)
}
"Snippet invocation works class='l:foo' and mixed attr sep" in {
val res = <div/>
def testAttrs(in: NodeSeq): NodeSeq = {
S.attr("bing") must_== Full("bong")
S.attr("fuzz") must_== Full("faz snark")
S.attr("noodle") must_== Full("FatPoodle")
in
}
val ret =
S.statelessInit(Req.nil) {
S.mapSnippetsWith("foo" -> testAttrs _) {
for {
s <- S.session
} yield s.processSurroundAndInclude("test", <div class="l:foo?bing=bong?fuzz=faz+snark;noodle=FatPoodle" />)
}
}
ret.open_! must ==/( res)
}
"Snippet invocation works class='lift:foo'" in {
val res = <div/>
val ret =
S.statelessInit(Req.nil) {
S.mapSnippetsWith("foo" -> ((a: NodeSeq) => a)) {
for {
s <- S.session
} yield s.processSurroundAndInclude("test", <div class='lift:foo' />)
}
}
ret.open_! must ==/( res)
}
"Snippet invocation fails class='l:bar'" in {
val res = <div/>
val ret =
S.statelessInit(Req.nil) {
S.mapSnippetsWith("foo" -> ((a: NodeSeq) => a)) {
for {
s <- S.session
} yield s.processSurroundAndInclude("test", <div class="lift:bar" />)
}
}
(ret.open_! \ "@class").text must_== "snippeterror"
}
object myInfo extends SessionVar("")
object ChangeVar {
def foo(in: NodeSeq): NodeSeq = {
myInfo.set(in.text)
in
}
}
object Funky {
def foo(in: NodeSeq): NodeSeq = {
SHtml.text("", s => myInfo.set(s))
in
}
}
"Snippet invocation fails in stateless mode" in {
val res = <div>dog
val ret =
S.statelessInit(Req.nil) {
S.mapSnippetsWith("foo" -> ChangeVar.foo _) {
for {
s <- S.session
} yield s.processSurroundAndInclude("test",
<lift:foo>{res})
}
}
(ret.open_! \ "@class").text must_== "snippeterror"
}
"Snippet invocation succeeds in normal mode" in {
val res = <div>dogOther Lift Framework examples (source code examples)Here is a short list of links related to this Lift Framework SnippetSpec.scala source code file: |
... this post is sponsored by my books ... | |
![]() #1 New Release! |
![]() FP Best Seller |
Copyright 1998-2024 Alvin Alexander, alvinalexander.com
All Rights Reserved.
A percentage of advertising revenue from
pages under the /java/jwarehouse
URI on this website is
paid back to open source projects.