@@ -67,8 +67,8 @@ class BoxSpec extends Specification with ScalaCheck with BoxGenerator {
6767 Full (1 ) reduceLeft {(x : Int , y : Int ) => x + y} must_== 1
6868 }
6969 " be used as an Option" in {
70- Full (1 ) orElse Some (2 ) must_ == Some (1 )
71- Empty orElse Some (2 ) must_ == Some (2 )
70+ Full (1 ) orElse Some (2 ) must beSome (1 )
71+ Empty orElse Some (2 ) must beSome (2 )
7272 }
7373 " be implicitly defined from an Option. The openOrThrowException method can be used on an Option for example" in {
7474 Some (1 ).openOrThrowException(" This is a test" ) must_== 1
@@ -152,14 +152,38 @@ class BoxSpec extends Specification with ScalaCheck with BoxGenerator {
152152 Full (Empty ).flatten must_== Empty
153153 }
154154 }
155+ " define a 'collect' method that takes a PartialFunction to transform its contents" in {
156+ " If the partial-function is defined for the contents of this box, returns a full box containing the result of applying that partial function to this Box's contents" in {
157+ Full (" Albus" ) collect { case " Albus" => " Dumbledore" } must_== Full (" Dumbledore" )
158+ }
159+ " If the partial-function is not defined for the contents of this box, returns Empty" in {
160+ Full (" Hermione" ) collect { case " Albus" => " Dumbledore" } must beEmpty
161+ }
162+ }
163+ " define a 'transform' method that takes a PartialFunction to transform this box into another box" in {
164+ " If the partial-function is defined for this box, returns the result of applying the partial function to it" in {
165+ Full (404 ) transform {
166+ case Full (x : Int ) if x != 200 => Failure (" Server error" )
167+ } must_== Failure (" Server error" )
168+ }
169+ " If the partial-function is not defined for this box, returns itself unchanged" in {
170+ Full (" Intended Result" ) transform {
171+ case _ : EmptyBox => Full (" Alternative" )
172+ case Full (" Unexpected Result" ) => Full (" Alternative" )
173+ } must_== Full (" Intended Result" )
174+ }
175+ }
176+ " define a 'flip' method returning Empty" in {
177+ Full (1 ) flip { _ => " No data found" } mustEqual Empty
178+ }
155179 " define an 'elements' method returning an iterator containing its value" in {
156180 Full (1 ).elements.next must_== 1
157181 }
158182 " define a 'toList' method returning a List containing its value" in {
159183 Full (1 ).toList must_== List (1 )
160184 }
161185 " define a 'toOption' method returning a Some object containing its value" in {
162- Full (1 ).toOption must_ == Some (1 )
186+ Full (1 ).toOption must beSome (1 )
163187 }
164188 " return itself if asked for its status with the operator ?~" in {
165189 Full (1 ) ?~ " error" must_== Full (1 )
@@ -298,14 +322,34 @@ class BoxSpec extends Specification with ScalaCheck with BoxGenerator {
298322 " define a 'flatten' method returning Empty" in {
299323 Empty .flatten must beEmpty
300324 }
325+ " define a 'collect' method returning Empty" in {
326+ Empty collect { case _ => " Some Value" } must beEmpty
327+ }
328+ " define a 'transform' method that takes a PartialFunction to transform this Empty box into another box" in {
329+ " If the partial-function is defined for Empty, returns the result of applying the partial function to it" in {
330+ Empty transform {
331+ case Failure (" error" , Empty , Empty ) => Full (" failure-alternative" )
332+ case Empty => Full (" alternative" )
333+ } must_== Full (" alternative" )
334+ }
335+ " If the partial-function is not defined for Empty, returns Empty" in {
336+ Empty transform { case Failure (" The Phantom Menace" , Empty , Empty ) => Full (" Return Of The Jedi" ) } must_== Empty
337+ }
338+ }
339+ " define a 'flip' method returning a Full box" in {
340+ Empty flip {
341+ case Empty => " flipped-empty"
342+ case _ => " flipped-failure"
343+ } mustEqual Full (" flipped-empty" )
344+ }
301345 " define an 'elements' method returning an empty iterator" in {
302346 Empty .elements.hasNext must beFalse
303347 }
304348 " define a 'toList' method returning Nil" in {
305349 Empty .toList must_== Nil
306350 }
307351 " define a 'toOption' method returning None" in {
308- Empty .toOption must_ == None
352+ Empty .toOption must beNone
309353 }
310354 " return a failure with a message if asked for its status with the operator ?~" in {
311355 Empty ?~ " nothing" must_== Failure (" nothing" , Empty , Empty )
@@ -358,7 +402,31 @@ class BoxSpec extends Specification with ScalaCheck with BoxGenerator {
358402 Failure (" error" , Empty , Empty ) flatMap {x : String => Full (x.toString)} must_== Failure (" error" , Empty , Empty )
359403 Failure (" error" , Empty , Empty ).flatten must_== Failure (" error" , Empty , Empty )
360404 }
361- " return a itself when asked for its status with the operator ?~" in {
405+ " define a 'collect' method returning itself" in {
406+ Failure (" error" , Empty , Empty ) collect { case _ => " Some Value" } must_== Failure (" error" , Empty , Empty )
407+ }
408+ " define a 'transform' method that takes a PartialFunction to transform this Failure into another box" in {
409+ " If the partial-function is defined for this Failure, returns the result of applying the partial function to it" in {
410+ Failure (" The Phantom Menace" ) transform {
411+ case Failure (" The Phantom Menace" , Empty , Empty ) => Full (" Return Of The Jedi" )
412+ } must_== Full (" Return Of The Jedi" )
413+
414+ Failure (" The Phantom Menace" ) transform {
415+ case Failure (" The Phantom Menace" , Empty , Empty ) => Failure (" Clones" )
416+ case _ => Full (" Jedi" )
417+ } must_== Failure (" Clones" )
418+ }
419+ " If the partial-function is not defined for this Failure, returns itself unchanged" in {
420+ Failure (" Clones" ) transform { case Failure (" The Phantom Menace" , Empty , Empty ) => Full (" Jedi" ) } must_== Failure (" Clones" )
421+ }
422+ }
423+ " define a 'flip' method returning a Full box" in {
424+ Failure (" error" , Empty , Empty ) flip {
425+ case Empty => " flipped-empty"
426+ case _ : Failure => " flipped-failure"
427+ } must_== Full (" flipped-failure" )
428+ }
429+ " return itself when asked for its status with the operator ?~" in {
362430 Failure (" error" , Empty , Empty ) ?~ " nothing" must_== Failure (" error" , Empty , Empty )
363431 }
364432 " create a new failure with a chained message if asked for its status with the operator ?~!" in {
0 commit comments