'be' matchers equality check for Any and null does not compile

824 views
Skip to first unread message

Vladimir Matveev

unread,
Jul 15, 2013, 8:49:39 AM7/15/13
to scalate...@googlegroups.com
Hi,

According to the official docs and API docs when using ShouldMatchers it is possible to use word be for equality comparison to produce more readable tests:

    result should be (null)

However, this does not compile when result is not AnyRef but Any. But it is perfectly correct to check Any value for null:

    ConvertToMap.convert(YNullable(YString), "123") should equal ("123")
    ConvertToMap.convert(YNullable(YString), null) should be (null)


convert() method here returns Any, and it is wholly possible for it to return null. But the compiler rejects this code with the following message:

overloaded method value should with alternatives:
  (notWord: ConvertToMapTest.this.NotWord)ConvertToMapTest.this.ResultOfNotWord[Any] <and>
  (rightMatcher: org.scalatest.matchers.Matcher[Any])Unit
 cannot be applied to (org.scalatest.matchers.Matcher[AnyRef])
    ConvertToMap.convert(YNullable(YString), null) should be (null)
                                                   ^


I understand I can use equal word but it doesn't feel right:

    ConvertToMap.convert(YNullable(YString), null) should equal (null)

 Is it possible to use be word somehow in my case?

Best regards,
Vladimir.

Bill Venners

unread,
Jul 15, 2013, 12:27:39 PM7/15/13
to scalate...@googlegroups.com
Hi Valdimir,

Well that's my fault. I put in a requirement that be (null) can only be used on AnyRef or below, because Null is a bottom type for AnyRef. In all this time using Scala, it never occurred to me that an Any reference could be null. But obviously it can, it just didn't occur to me. (I don't use null much in Scala.) I thought about this this morning, and I believe I can loosen the type constraint to just Any, so you'd still get a compiler error if you tried something like:

1 should be (null)

If I get that to work I'll deploy a snapshot and let you know. What you could do in the meantime is use an assertion instead.

Bill


--
You received this message because you are subscribed to the Google
Groups "scalatest-users" group.
To post to this group, send email to scalate...@googlegroups.com
To unsubscribe from this group, send email to
scalatest-use...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/scalatest-users?hl=en
ScalaTest itself, and documentation, is available here:
http://www.artima.com/scalatest
---
You received this message because you are subscribed to the Google Groups "scalatest-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to scalatest-use...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.
 
 



--
Bill Venners
Artima, Inc.
http://www.artima.com

Vladimir Matveev

unread,
Jul 16, 2013, 3:14:55 AM7/16/13
to scalate...@googlegroups.com
Hi Bill,

Thank you very much, I will be waiting.

понедельник, 15 июля 2013 г., 20:27:39 UTC+4 пользователь Bill Venners написал:

user

unread,
May 6, 2014, 6:26:14 PM5/6/14
to scalate...@googlegroups.com
Hi,
This has been posted a year ago but I'm now facing the same issue. How can I test null?

Bill Venners

unread,
May 6, 2014, 6:35:17 PM5/6/14
to scalate...@googlegroups.com
Hi,

I think this may have gotten forgotten. I'll resurrect it. It will likely need to be in 2.2.0 because it isn't going to be binary compatible. In the meantime, you can probably get your code to compile by giving null a type ascription, like:

null: String

If it is a string.

Bill


For more options, visit https://groups.google.com/d/optout.
Reply all
Reply to author
Forward
0 new messages