liangzai-cool / hamcrest

Automatically exported from code.google.com/p/hamcrest
0 stars 0 forks source link

Definition of assertThat is limited #33

Closed GoogleCodeExporter closed 9 years ago

GoogleCodeExporter commented 9 years ago
Currently the method declaration is as follows:
public static <T> void assertThat(T actual, Matcher<T> matcher);

This is quite limiting. It forces client code to do casting although it is
not necessary.
As an example:

1: class ExtendedObject extends Object{}
2: ExtendedObject extendedObject = new ExtendedObject();
3: Object object = extendedObject;
4: assertThat(object, is(extendedObject)); // won't compile
5: assertThat((ExtendedObject)object, is(extendedObject)); // will compile

The code on line 4 should compile, we shouldn't have to do it as on line 5.

I think changing the method declaration as follows fixes this problem:
public static <T> void assertThat(T actual, Matcher<? extends T> matcher);

Original issue reported on code.google.com by celalzif...@gmail.com on 31 Mar 2008 at 7:00

GoogleCodeExporter commented 9 years ago
This type signature actually allows anything for actual and matcher as <T> can 
always
be bound to Object.

It allows you to write things like:

assertThat(new Object(), hasItem("Foo"));

Which is a pointless assertion that can be pushed down to the compiler to check.

Can you give a use case where the current signature didn't work?

Original comment by neild...@gmail.com on 1 Apr 2008 at 8:39

GoogleCodeExporter commented 9 years ago
Is this a duplicate of issue 31?

Original comment by nat.pr...@gmail.com on 1 Apr 2008 at 10:56

GoogleCodeExporter commented 9 years ago
err... I mean...

Is this a duplicate of issue 32?

Original comment by nat.pr...@gmail.com on 1 Apr 2008 at 10:57

GoogleCodeExporter commented 9 years ago
Hi nat.pryce,
I didn't see issue 31/32, sorry. I think that is referring to the same problem.
I would appreciate any comments/ETA.

Original comment by celalzif...@gmail.com on 1 Apr 2008 at 3:36

GoogleCodeExporter commented 9 years ago

Original comment by nat.pr...@gmail.com on 16 Oct 2008 at 12:09