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

Lift Framework example source code file (WizardSpec.scala)

This example Lift Framework source code file (WizardSpec.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

a, elwood, empty, liftsession, name, name, s, s, screen, screen, short, too, too, wizard

The Lift Framework WizardSpec.scala source code

/*
 * Copyright 2009-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 wizard

import org.specs.Specification

import common._
import util._
import http._

/**
 * System under specification for Wizard.
 */
object WizardSpec extends Specification("Wizard Specification") {

  val session: LiftSession = new LiftSession("", Helpers.randomString(20), Empty)

  val MyWizard = new Wizard {

    object completeInfo extends WizardVar(false)

    def finish() {
      S.notice("Thank you for registering your pet")
      completeInfo.set(true)
    }

    val nameAndAge = new Screen {
      val name = field(S ? "First Name", "",
                       valMinLen(2, S ?? "Name Too Short"))

      val age = field(S ? "Age", 0,
                      minVal(5, S ?? "Too young"),
                      maxVal(120, S ?? "You should be dead"))

      override def nextScreen = if (age.is < 18) parentName else favoritePet
    }

    val parentName = new Screen {
      val parentName = field(S ? "Mom or Dad's name", "",
                             valMinLen(2, S ?? "Name Too Short"),
                             valMaxLen(40, S ?? "Name Too Long"))
    }

    val favoritePet = new Screen {
      val petName = field(S ? "Pet's name", "",
                          valMinLen(2, S ?? "Name Too Short"),
                          valMaxLen(40, S ?? "Name Too Long"))
    }
  }

  "A Wizard can be defined" in {
    MyWizard.nameAndAge.screenName must_== "Screen 1"
    MyWizard.favoritePet.screenName must_== "Screen 3"
  }

  "A field must have a correct Manifest" in {
    MyWizard.nameAndAge.age.manifest.erasure.getName must_== classOf[Int].getName
  }

  "A wizard must transition from first screen to second screen" in {
    S.initIfUninitted(session) {
      MyWizard.currentScreen.open_! must_== MyWizard.nameAndAge

      MyWizard.nextScreen

      MyWizard.currentScreen.open_! must_== MyWizard.nameAndAge

      MyWizard.nameAndAge.name.set("David")
      MyWizard.nameAndAge.age.set(14)

      MyWizard.nextScreen

      MyWizard.currentScreen.open_! must_== MyWizard.parentName

      MyWizard.prevScreen

      MyWizard.currentScreen.open_! must_== MyWizard.nameAndAge

      MyWizard.nameAndAge.age.set(45)

      MyWizard.nextScreen

      MyWizard.currentScreen.open_! must_== MyWizard.favoritePet

      S.clearCurrentNotices

      MyWizard.favoritePet.petName.set("Elwood")

      MyWizard.nextScreen

      MyWizard.currentScreen must_== Empty

      MyWizard.completeInfo.is must_== true
    }
  }

  "A wizard must be able to snapshot itself" in {
    val ss = S.initIfUninitted(session) {
      MyWizard.currentScreen.open_! must_== MyWizard.nameAndAge

      MyWizard.nextScreen

      MyWizard.currentScreen.open_! must_== MyWizard.nameAndAge

      MyWizard.nameAndAge.name.set("David")
      MyWizard.nameAndAge.age.set(14)

      MyWizard.nextScreen

      MyWizard.currentScreen.open_! must_== MyWizard.parentName

      MyWizard.createSnapshot
    }

    S.initIfUninitted(session) {
      MyWizard.currentScreen.open_! must_== MyWizard.nameAndAge
    }

    S.initIfUninitted(session) {
      ss.restore()

      MyWizard.prevScreen

      MyWizard.currentScreen.open_! must_== MyWizard.nameAndAge

      MyWizard.nameAndAge.age.set(45)

      MyWizard.nextScreen

      MyWizard.currentScreen.open_! must_== MyWizard.favoritePet

      S.clearCurrentNotices

      MyWizard.favoritePet.petName.set("Elwood")

      MyWizard.nextScreen

      MyWizard.currentScreen must_== Empty

      MyWizard.completeInfo.is must_== true
    }
  }
}

Other Lift Framework examples (source code examples)

Here is a short list of links related to this Lift Framework WizardSpec.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.