2

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?

User1291
  • 7,664
  • 8
  • 51
  • 108

2 Answers2

3

Is there a specific reason you need to use the matcher beSameInstanceAs?

You can do something like:

val suits = CardSuit.values() as Array<out ICardSuite>

suits.forEach {
    when (it.name) {
        "HEART" -> it shouldBe CardSuit.HEART
        "SPADE" -> it shouldBe CardSuit.SPADE
   }
}

However, if you really want to use beSameInstanceAs, you can:

suits.forEach {
    when(it.name) {
       "HEART" -> it shouldBeSameInstanceAs CardSuit.HEART
       "SPADE" -> it shouldBeSameInstanceAs CardSuit.SPADE
    }
}

I'm not really getting any complaints from the compiler here

LeoColman
  • 6,950
  • 7
  • 34
  • 63
2

beTheSameInstanceAs(CardSuit.HEART) returns a Matcher<CardSuit>, so it can't match an arbitrary ICardSuit. That makes sense (though Matcher could be contra-variant, you'd need covariance here). But you can:

  1. Call explicitly beTheSameInstanceAs<ICardSuit>(CardSuit.HEART).

  2. Create a helper function

    inline fun <T1, reified T2 : T1> Matcher<T2>.widen() = object : Matcher<T1>() {
        override fun test(value: T1) = 
            if (value is T2) 
                this.test(value) 
            else 
                Result(false, "$value is not a ${T2::class.name}", "$value is a ${T2::class.name}")
    }
    

    and call

    it should beTheSameInstanceAs(CardSuit.HEART).widen()
    

    (I think type inference should work here).

  3. Because beTheSameInstanceAs(x) really can match anything, declare an equivalent function which returns a Matcher<Any>:

    fun beTheSameInstanceAsAny(x: Any) = beTheSameInstanceAs(x)
    
    // usage
    "HEART" -> it should beTheSameInstanceAsAny(CardSuit.HEART)
    
Alexey Romanov
  • 167,066
  • 35
  • 309
  • 487