Let's say I have an interface
public interface ICardSuit {
/**short name*/
public String getName();
/** the colour of this card*/
public ICardColour getColour();
}
that I decide to implement with an enum:
public enum CardSuit implements ICardSuit {
HEART{
@Override
public ICardColour getColour() {
return CardColour.RED;
}
},
SPADE{
@Override
public ICardColour getColour() {
return CardColour.BLACK;
}
},
DIAMOND{
@Override
public ICardColour getColour() {
return CardColour.RED;
}
},
CLUBS {
@Override
public ICardColour getColour() {
return CardColour.BLACK;
}
}
;
@Override
public String getName() {
return this.name();
}
}
I now want to test it (using kotlintest because I'm developing a fondness for it):
class CardSuitTest : FunSpec(){
init {
test("there are exactly four suits"){CardSuit.values().size shouldBe 4}
test("suits implement interface"){CardSuit.values().forEach { it shouldBe instanceOf(ICardSuit::class) }}
test("suits have correct names"){
val suits = CardSuit.values() as Array<out ICardSuit>
suits.forEach { when(it.name){
"HEART" -> it should beTheSameInstanceAs(CardSuit.HEART as ICardSuit)
"SPADE" -> it should beTheSameInstanceAs(CardSuit.SPADE as ICardSuit)
"DIAMOND" -> it should beTheSameInstanceAs(CardSuit.DIAMOND as ICardSuit)
"CLUBS" -> it should beTheSameInstanceAs(CardSuit.CLUBS as ICardSuit)
} }
}
test("suits have correct colours"){
CardSuit.values().forEach { when(it){
CardSuit.HEART,CardSuit.DIAMOND -> it.colour shouldBe CardColour.RED
CardSuit.CLUBS, CardSuit.SPADE -> it.colour shouldBe CardColour.BLACK
} }
}
}
}
where I need to cast to ICardSuit
because if I do not, the compiler complains that
None of the following functions can be called with the arguments supplied.
* T.should(Matcher<T>) where T cannot be inferred for infix fun <T> T.should(matcher: Matcher<T>): Unit defined in io.kotlintest.matchers
* ICardSuit.should((ICardSuit) → Unit) where T = ICardSuit for infix fun <T> T.should(matcher: (T) → Unit): Unit defined in io.kotlintest.matchers
I want to keep the as Array<out ICardSuit>
because it's the easiest way to make sure I only access interface properties,
but I'm really not fond of having to cast the instances I'm testing for.
Is there anything I can do about it?