Questions on the ObjectGraph example

2 views
Skip to first unread message

Thomas Sant'ana

unread,
Feb 2, 2011, 9:06:24 AM2/2/11
to specs...@googlegroups.com
I was looking at the Specs wiki and ran into a very impressive example:

http://code.google.com/p/specs/wiki/MatchersGuide#Matchers_composition_for_object_graphs

Unable to understand the example, I tried to make use of the code. What I tried was this:

class ObjectGraphTest extends SpecificationWithJUnit with ObjectGraphMatchers {
  val f1 = Foo("hello")
  f1.singlebar = Bar(10)

  //Test 1
  "f1 one macth" in {
    val f2 = Foo("hello")
    f2.singlebar = Bar(10)

    f1 must matchFoo(f2)
  }

  // Test 2
  "f1 not match partial" in {
    f1 mustNot matchFoo(Foo("hello"))
  }
}

Test 1 runs perfectly. And not matching also reportos ok:

- Issues 1: While Test 2 I get a NullPointerException. Like so:

  f1 not match partial
  java.lang.NullPointerException
      at other.ObjectGraphMatchers$matchBar$$anonfun$apply$13.apply(ObjectGraph.scala:51)
 
  Line 51:
      ((beEqualTo(_:Long)) ^^^ ((_: Bar).id))(bar)(other)
 

- Issue 2: This is a funny thing, IDEA reports a type mismatch:

       (matchBar(_)).toSet ^^^ ((_:Foo).bars))(foo)(other)

   Curly red line on the foo, message: Type mismatch, expected: Nothing, actual: ObjectGraphMatchers.this.type#Foo

   I compiles and works ok, and runs fine. Maybe this is an IDEA issue;

My question, did I miss something on the example? How do I avoid the NullPointerException?

Thomas




etorreborre

unread,
Feb 4, 2011, 6:31:10 AM2/4/11
to specs-users
Congratulations Thomas, you found a logical bug!

The matchOptionalBar matcher is wrong. The correct version is:

case class matchOptionalBar(bar: Bar) extends Matcher[Bar] {
def apply(other: =>Bar) = {
if (bar != null && other != null)
(matchBar(_:Bar))(bar)(other)
else
(beAsNullAs(_:Bar))(bar)(other)
}
}

With this version "matchBar" is only called when both "bar" and
"other" are not null. Otherwise they must be both null.

For the second issue, this is an IDEA specific one indeed.

Eric.

On Feb 3, 1:06 am, "Thomas Sant'ana" <maill...@gmail.com> wrote:
> I was looking at the Specs wiki and ran into a very impressive example:
>
> http://code.google.com/p/specs/wiki/MatchersGuide#Matchers_compositio...
>
> Unable to understand the example, I tried to make use of the code. What I
> tried was this:
>
> class ObjectGraphTest extends SpecificationWithJUnit with
> ObjectGraphMatchers {
>   val f1 = Foo("hello")
>   f1.singlebar = Bar(10)
>
>   //Test 1
>   "f1 one macth" in {
>     val f2 = Foo("hello")
>     f2.singlebar = Bar(10)
>
>     f1 must matchFoo(f2)
>   }
>
>   // Test 2
>   "f1 not match partial" in {
>     f1 mustNot matchFoo(Foo("hello"))
>   }
>
> }
>
> Test 1 runs perfectly. And not matching also reportos ok:
>
> - Issues 1: While Test 2 I get a NullPointerException. Like so:
>
>   f1 not match partial
>   java.lang.NullPointerException
>       at
> other.ObjectGraphMatchers$matchBar$$anonfun$apply$13.apply(ObjectGraph.scal a:51)
Reply all
Reply to author
Forward
0 new messages