alvinalexander.com | career | drupal | java | mac | mysql | perl | scala | uml | unix  

Lift Framework example source code file (SnippetSpec.scala)

This example Lift Framework source code file (SnippetSpec.scala) is included in the DevDaily.com "Java Source Code Warehouse" project. The intent of this project is to help you "Learn Java by Example" TM.

Java - Lift Framework tags/keywords

correctly, empty, empty, fatpoodle, full, full, liftsession, nodeseq, nodeseq, null, prefixedattribute, snippet, snippet, unprefixedattribute

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 ==/ (<div id="frog" class="lift:surround"/>) } "Correctly process l:content_id" in { val ret = LiftSession.checkForContentId(<html l:content_id="dog"> <head/> <body> <lift:surround id="dog">
</body> </html>) ret must ==/ (<lift:surround id="dog">
) } "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>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">ab) } myInfo.is must_== "ab" } } } "Snippet attributes" should { "properly reflect the full snippet stack with S.attrs" in { S.initIfUninitted(new LiftSession("", "", Empty)) { S.withAttrs(new UnprefixedAttribute("a", "a", Null)) { S.withAttrs(new UnprefixedAttribute("b", "b", new UnprefixedAttribute("c", "c", Null))) { S.withAttrs(new UnprefixedAttribute("d", "d", Null)) { S.attr("a") must_== Full("a") S.attr("b") must_== Full("b") S.attr("c") must_== Full("c") S.attr("d") must_== Full("d") // Also check currentAttrs (should be set only for the inner-most call) S.currentAttr("a") must_== Empty S.currentAttr("b") must_== Empty S.currentAttr("c") must_== Empty S.currentAttr("d") must_== Full("d") } // Make sure the stack is unwound S.attr("d") must_== Empty } S.attr("b") must_== Empty S.attr("c") must_== Empty } S.attr("a") must_== Empty S.attrs must_== Nil S.currentAttrs must_== Null } } "reflect only the last pushed values with S.currentAttrs" in { S.initIfUninitted(new LiftSession("", "", Empty)) { S.withAttrs(new UnprefixedAttribute("a", "a", Null)) { S.withAttrs(new UnprefixedAttribute("b", "b", new UnprefixedAttribute("c", "c", Null))) { S.withAttrs(new UnprefixedAttribute("d", "d", Null)) { S.currentAttr("a") must_== Empty S.currentAttr("b") must_== Empty S.currentAttr("c") must_== Empty S.currentAttr("d") must_== Full("d") } } } } } "handle prefixes" in { S.initIfUninitted(new LiftSession("", "", Empty)) { S.withAttrs(new PrefixedAttribute("foo", "a", "a", Null)) { S.withAttrs(new PrefixedAttribute("foo", "b", "b", Null)) { S.withAttrs(new PrefixedAttribute("bar", "a", "aBar", Null)) { S.attr("a") must_== Empty S.attr("foo", "a") must_== Full("a") S.attr("bar", "a") must_== Full("aBar") val fooMap = S.prefixedAttrsToMap("foo", Map()) fooMap("a") must_== "a" fooMap("b") must_== "b" val barMap = S.prefixedAttrsToMap("bar") barMap("a") must_== "aBar" barMap.get("b") must_== None } } } } } } }

Other 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

 

new blog posts

 

Copyright 1998-2021 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.