假设我有一个界面
public interface ICardSuit {
/**short name*/
public String getName();
/** the colour of this card*/
public ICardColour getColour();
}
我决定使用枚举实现的:
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();
}
}
我现在要对其进行测试(使用kotlintest,因为我对此很喜欢):
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
} }
}
}
}
我需要强制转换为ICardSuit
的地方,因为如果我不这样做,编译器会抱怨
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
我想保留as Array<out ICardSuit>
,因为这是确保我仅访问界面属性的最简单方法,
但我真的不喜欢强制转换要测试的实例。
我能做些什么吗?
答案 0 :(得分:3)
您是否有特定原因需要使用匹配器beSameInstanceAs
?
您可以执行以下操作:
val suits = CardSuit.values() as Array<out ICardSuite>
suits.forEach {
when (it.name) {
"HEART" -> it shouldBe CardSuit.HEART
"SPADE" -> it shouldBe CardSuit.SPADE
}
}
但是,如果您确实想使用beSameInstanceAs
,则可以:
suits.forEach {
when(it.name) {
"HEART" -> it shouldBeSameInstanceAs CardSuit.HEART
"SPADE" -> it shouldBeSameInstanceAs CardSuit.SPADE
}
}
我在这里并没有收到编译器的任何抱怨
答案 1 :(得分:2)
beTheSameInstanceAs(CardSuit.HEART)
返回一个Matcher<CardSuit>
,因此它不能匹配任意的ICardSuit
。这是有道理的(尽管Matcher
可能是 contra -variant,但在这里您需要协方差)。但是您可以:
显式调用beTheSameInstanceAs<ICardSuit>(CardSuit.HEART)
。
创建一个辅助函数
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}")
}
并致电
it should beTheSameInstanceAs(CardSuit.HEART).widen()
(我认为类型推断应该在这里起作用)。
由于beTheSameInstanceAs(x)
确实可以匹配任何内容,因此声明一个等效函数,该函数返回一个Matcher<Any>
:
fun beTheSameInstanceAsAny(x: Any) = beTheSameInstanceAs(x)
// usage
"HEART" -> it should beTheSameInstanceAsAny(CardSuit.HEART)