Permalink
Browse files

Added withTemplateFor method to WebSpec

Closes #904

Also added example usage to WebSpecSpec
  • Loading branch information...
1 parent 07d6b0b commit 63a50558864a3d70fa1de0b3cf6148122a3e07ca @dchenbecker dchenbecker committed Feb 27, 2011
@@ -48,23 +48,22 @@ import org.specs._
*
*/
object MockWeb {
-// TODO : Uncomment this code when LiftRules can be scoped
-// /**
-// * Setting this var to <code>true</code>
-// * will force all tests to use LiftRules. See
-// * useLiftRules for more granular control.
-// */
-// var useLiftRulesGlobally = false
-//
-// object useLiftRules extends ThreadGlobal[Boolean]
-//
-// private def liftRulesEnabled = useLiftRulesGlobally || useLiftRules.box == Full(true)
-//
-// private def withLiftRules [T] (f : => T) = {
-// if (liftRulesEnabled) {
-// f
-// }
-// }
+ /**
+ * Setting this var to <code>true</code>
+ * will force all tests to use LiftRules. See
+ * useLiftRules for more granular control.
+ */
+ var useLiftRulesGlobally = false
+
+ object useLiftRules extends ThreadGlobal[Boolean]
+
+ private def liftRulesEnabled = useLiftRulesGlobally || useLiftRules.box == Full(true)
+
+ private def withLiftRules [T] (f : => T) = {
+ if (liftRulesEnabled) {
+ f
+ }
+ }
/**
@@ -88,23 +87,22 @@ object MockWeb {
// TODO : Confirm that we can pass in a null provider without issue
val req = new HTTPRequestServlet(request, null)
-// TODO : Uncomment this code when LiftRules can be scoped
-// withLiftRules {
-// tryo {
-// LiftRules.early.toList.foreach(_(req))
-// }
-// }
-//
-// val r =
-// if(liftRulesEnabled) {
-// // Apply stateless rewrites
-// Req(req, LiftRules.statelessRewrite.toList,
-// LiftRules.statelessTest.toList, System.nanoTime)
-// } else {
-// Req(req, Nil, System.nanoTime)
-// }
+ withLiftRules {
+ tryo {
+ LiftRules.early.toList.foreach(_(req))
+ }
+ }
+
+ val r =
+ if(liftRulesEnabled) {
+ // Apply stateless rewrites
+ Req(req, LiftRules.statelessRewrite.toList,
+ LiftRules.statelessTest.toList, System.nanoTime)
+ } else {
+ Req(req, Nil, System.nanoTime)
+ }
- f(Req(req, Nil, System.nanoTime))
+ f(r)
}
/**
@@ -202,9 +200,7 @@ object MockWeb {
*/
def withSnippet[T](name : String, attrs : MetaData = Null)(f : => T) : T =
S.withAttrs(attrs) {
- S.setVars(attrs) {
- http.httpPackageProxy.doSnippet(name)(f)
- }
+ http.httpPackageProxy.doSnippet(name)(f)
}
}
}
@@ -26,33 +26,34 @@ import common.{Box,Empty,Full}
import http._
import json.JsonAST._
import mocks.MockHttpServletRequest
+import specification.Example
/**
* This trait provides Lift-specific extensions to the Specification
* base trait to simplify unit testing of your code. In addition to
* the Scaladoc, Please see the
- * source to WebSpecSpec.scala for an example of how to use this.
+ * source to WebSpecSpec.scala for an example of how to use this. This class
+ * allows you to optionally configure LiftRules for your spec. LiftRules rules
+ * are always used, so if you don't want that to happen you can just use MockWeb directly
+ * in your own custom spec and simply omit any LiftRules setup.
+ *
+ * @param boot defines a method that is called prior to
+ * testing to set up the Lift environment. This is where you'll
+ * initialize LiftRules, Mapper, etc. The simplest approach
+ * is to just point this at your Boostrap.boot method.
*/
-abstract class WebSpec extends Specification {
-
-// TODO : Uncomment this code when LiftRules can be scoped
-///**
-// * This trait provides Lift-specific extensions to the Specification
-// * base trait to simplify unit testing of your code. In addition to
-// * the Scaladoc, Please see the
-// * source to WebSpecSpec.scala for an example of how to use this.
-// *
-// * @param boot defines a method that is called prior to
-// * testing to set up the Lift environment. This is where you'll
-// * initialize LiftRules, Mapper, etc. The simplest approach
-// * is to just point this at your Boostrap.boot method.
-// *
-// * @param useLiftRules controls whether LiftRules are used for the expectations
-// */
-//abstract class WebSpec(boot : () => Any = () => {}, useLiftRules : Boolean = false) extends Specification {
-// boot()
-
+abstract class WebSpec(boot : () => Any = () => {}) extends Specification {
+ /**
+ * This is our private spec instance of Liftrules. Everything we run will
+ * use this instance instead of the global instance
+ */
+ private val liftRules = new LiftRules()
+
+ LiftRulesMocker.devTestLiftRulesInstance.doWith(liftRules) {
+ boot()
+ }
+
/**
* A class that bridges between the description string
* and classes that provide the Lift-specific wrapping
@@ -82,6 +83,21 @@ abstract class WebSpec extends Specification {
def withReqFor (req : HttpServletRequest) =
new ReqSpecification(description, req)
+
+ def withTemplateFor (url : String, session : Box[LiftSession] = Empty, contextPath : String = "") =
+ new TemplateSpecification(description, url, session, contextPath)
+
+ def withTemplateFor (url : String, session : LiftSession) =
+ new TemplateSpecification(description, url, Box.!!(session), "")
+
+ def withTemplateFor (req : HttpServletRequest) =
+ new TemplateSpecification(description, req, Empty)
+
+ def withTemplateFor (req : HttpServletRequest, session : Box[LiftSession]) =
+ new TemplateSpecification(description, req, session)
+
+ def withTemplateFor (req : HttpServletRequest, session : LiftSession) =
+ new TemplateSpecification(description, req, Box.!!(session))
}
/**
@@ -175,19 +191,18 @@ abstract class WebSpec extends Specification {
def this (description : String, url : String, session : Box[LiftSession], contextPath : String) =
this(description, new MockHttpServletRequest(url, contextPath), session)
-
def in [T](expectations : => T)(implicit m : scala.reflect.ClassManifest[T]) = {
val example = exampleContainer.createExample(description)
if (sequential) example.setSequential()
example.in {
-// TODO : Uncomment this code when LiftRules can be scoped
-// MockWeb.useLiftRules.doWith(useLiftRules) {
- MockWeb.testS(req, session) {
- expectations
+ LiftRulesMocker.devTestLiftRulesInstance.doWith(liftRules) {
+ MockWeb.useLiftRules.doWith(true) {
+ MockWeb.testS(req, session) {
+ expectations
+ }
}
-// TODO : Uncomment this code when LiftRules can be scoped
-// }
+ }
}(m)
}
}
@@ -206,11 +221,41 @@ abstract class WebSpec extends Specification {
if (sequential) example.setSequential()
example.in {
-// TODO : Uncomment this code when LiftRules can be scoped
-// MockWeb.useLiftRules.doWith(useLiftRules) {
- MockWeb.testReq(req)(expectations)
-// TODO : Uncomment this code when LiftRules can be scoped
-// }
+ LiftRulesMocker.devTestLiftRulesInstance.doWith(liftRules) {
+ MockWeb.useLiftRules.doWith(true) {
+ MockWeb.testReq(req)(expectations)
+ }
+ }
+ }(m)
+ }
+ }
+
+ /**
+ * This class provides a wrapper to test methods that require
+ * a processed template.
+ */
+ class TemplateSpecification (description : String,
+ val req : HttpServletRequest,
+ session : Box[LiftSession]) extends ModifiableRequest[TemplateSpecification] {
+ def this (description : String, url : String, session : Box[LiftSession], contextPath : String) =
+ this(description, new MockHttpServletRequest(url, contextPath), session)
+
+ def in [T](expectations : Box[NodeSeq] => T)(implicit m : scala.reflect.ClassManifest[T]) = {
+ val example = exampleContainer.createExample(description)
+ if (sequential) example.setSequential()
+
+ example.in {
+ LiftRulesMocker.devTestLiftRulesInstance.doWith(liftRules) {
+ MockWeb.useLiftRules.doWith(true) {
+ MockWeb.testS(req, session) {
+ S.request match {
+ case Full(sReq) => expectations(S.runTemplate(sReq.path.partPath))
+ case other => fail("Error: withTemplateFor call did not result in " +
+ "request initialization (S.request = " + other + ")")
+ }
+ }
+ }
+ }
}(m)
}
}
@@ -0,0 +1,10 @@
+<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
+ "http://www.w3.org/TR/html4/loose.dtd">
+<html>
+<head>
+ <title></title>
+</head>
+<body>
+<div class="lift:WebSpecSpecSnippet" />
+</body>
+</html>
Oops, something went wrong.

0 comments on commit 63a5055

Please sign in to comment.