Re: [scalatest-users] Check the type of an object

236 views
Skip to first unread message

Bill Venners

unread,
Aug 26, 2012, 12:17:53 PM8/26/12
to scalate...@googlegroups.com
Hi Ayose,

You can use a manifest to get rid of the classOf, and you can use
BeMatcher instead of a Matcher to be able to put it after "be".

import org.scalatest._
import matchers.ShouldMatchers._
import matchers.Matcher
import matchers.BeMatcher
import matchers.MatchResult

def beOfType[T: Manifest] = Matcher { obj: Any =>
val cls = manifest[T].erasure
MatchResult(
obj.getClass == cls,
obj.toString + " was not an instance of " + cls.toString,
obj.toString + " was an instance of " + cls.toString
)
}

def ofType[T: Manifest] = BeMatcher { obj: Any =>
val cls = manifest[T].erasure
MatchResult(
obj.getClass == cls,
obj.toString + " was not an instance of " + cls.toString,
obj.toString + " was an instance of " + cls.toString
)
}

"hi" should beOfType[String]
"hi" should not (beOfType[String])

"hi" should be (ofType[String])
"hi" should not be ofType[String]

A matcher for this use case, by the way, will be included in our
matchers enhancements post-2.0.

Bill

On Sun, Aug 26, 2012 at 8:56 AM, Ayose Cazorla <ayo...@gmail.com> wrote:
>
> Hi people,
>
> I wanted to check the type of an object in a set. After searching for a
> matcher in the current Scalatest documentation, I decided to write my own
> matcher. This is
>
> def beOfType(cls: Class[_]) = Matcher { obj: Any =>
> MatchResult(
> obj.getClass == cls,
> obj.toString + " was not an instance of " + cls.toString,
> obj.toString + " was an instance of " + cls.toString
> )
> }
>
> (Full source at https://gist.github.com/3481251)
>
> Previously I was using
>
> result.rejections.head.isInstanceOf[AuthenticationRequiredRejection]
> must be (true)
>
> But, with this, when it fails I don't see what is the actual object, just
> the message «false was not true», which is not very useful.
>
> Now, I have
>
> result.rejections.head must
> beOfType(classOf[AuthenticationRequiredRejection])
>
> My question is: is this a good way to solve the problem? Is there any better
> way?
>
>
> --
> 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



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

Bill Venners

unread,
Feb 6, 2013, 10:20:56 AM2/6/13
to scalate...@googlegroups.com
Hi Martin,

On Wed, Feb 6, 2013 at 5:45 AM, Martin Krischik
<martin....@gmail.com> wrote:
> Hello,
>
> it seems that both solutions won't support implemented interfaces. Which is
> a shame. And to bad that this test is not supplied out-of-the-box.
>
If you want to do an instanceof check, you could use this:

obj.getClass.isAssignableFrom(cls)

instead of this:

obj.getClass == cls

That will pick up interfaces. And we will be releasing matcher
enhancements in the next few 2.0 milestone releases, and that will
include some syntax for this use case.

Bill

> Martin
>
> --
> --
> 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.
Reply all
Reply to author
Forward
0 new messages