refactored inheritance assertion
This commit is contained in:
parent
b147d3898d
commit
600b44a3f1
|
@ -23,7 +23,7 @@ class TestCompositionExercise1 {
|
|||
"radius" to Int::class,
|
||||
"color" to String::class
|
||||
)
|
||||
assertInheritance(this, listOf("Shape"))
|
||||
assertInheritance("Shape")
|
||||
}
|
||||
loadClass(packageName, "Rectangle").apply {
|
||||
assertConstructorNumber(1)
|
||||
|
@ -32,7 +32,7 @@ class TestCompositionExercise1 {
|
|||
"width" to Int::class,
|
||||
"color" to String::class
|
||||
)
|
||||
assertInheritance(this, listOf("Shape"))
|
||||
assertInheritance("Shape")
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -13,48 +13,48 @@ import kotlin.test.assertEquals
|
|||
@FixMethodOrder(MethodSorters.NAME_ASCENDING)
|
||||
class TestPolymorphismExercise1 {
|
||||
|
||||
private val packageName = "polymorphismExercise1"
|
||||
private val packageName = "polymorphismExercise1"
|
||||
|
||||
@Test
|
||||
fun `#01 classes structure`() {
|
||||
loadClass(packageName, "Warrior").apply {
|
||||
assertConstructorNumber(1)
|
||||
assertInheritance(this, listOf("Character", "Fighter"))
|
||||
}
|
||||
loadClass(packageName, "Elf").apply {
|
||||
assertConstructorNumber(1)
|
||||
assertInheritance(this, listOf("Character", "Magician"))
|
||||
}
|
||||
loadClass(packageName, "FightingElf").apply {
|
||||
assertConstructorNumber(1)
|
||||
assertInheritance(this, listOf("Elf", "Fighter"))
|
||||
}
|
||||
loadClass(packageName, "Wizard").apply {
|
||||
assertConstructorNumber(1)
|
||||
assertInheritance(this, listOf("Character", "Magician", "Flyer"))
|
||||
}
|
||||
loadClass(packageName, "Dragon").apply {
|
||||
assertConstructorNumber(1)
|
||||
assertInheritance(this, listOf("Character", "Flyer"))
|
||||
}
|
||||
@Test
|
||||
fun `#01 classes structure`() {
|
||||
loadClass(packageName, "Warrior").apply {
|
||||
assertConstructorNumber(1)
|
||||
assertInheritance("Character", "Fighter")
|
||||
}
|
||||
loadClass(packageName, "Elf").apply {
|
||||
assertConstructorNumber(1)
|
||||
assertInheritance("Character", "Magician")
|
||||
}
|
||||
loadClass(packageName, "FightingElf").apply {
|
||||
assertConstructorNumber(1)
|
||||
assertInheritance("Elf", "Fighter")
|
||||
}
|
||||
loadClass(packageName, "Wizard").apply {
|
||||
assertConstructorNumber(1)
|
||||
assertInheritance("Character", "Magician", "Flyer")
|
||||
}
|
||||
loadClass(packageName, "Dragon").apply {
|
||||
assertConstructorNumber(1)
|
||||
assertInheritance("Character", "Flyer")
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `#02 what do the characters`() {
|
||||
listOf("Warrior", "Elf", "FightingElf", "Dragon", "Wizard")
|
||||
.map { loadClass(packageName, it).createInstance() as Character }
|
||||
.forEach { c -> c.playTurn() }
|
||||
@Test
|
||||
fun `#02 what do the characters`() {
|
||||
listOf("Warrior", "Elf", "FightingElf", "Dragon", "Wizard")
|
||||
.map { loadClass(packageName, it).createInstance() as Character }
|
||||
.forEach { c -> c.playTurn() }
|
||||
|
||||
assertEquals(
|
||||
message = "Incorrect result of playTurn() for all characters",
|
||||
actual = loadTraceContent(),
|
||||
expected = listOf(
|
||||
"Warrior: Fight!",
|
||||
"Elf: Magic!",
|
||||
"FightingElf: Magic!Fight!",
|
||||
"Dragon: Fly!",
|
||||
"Magician: Magic!Fly!"
|
||||
)
|
||||
assertEquals(
|
||||
message = "Incorrect result of playTurn() for all characters",
|
||||
actual = loadTraceContent(),
|
||||
expected = listOf(
|
||||
"Warrior: Fight!",
|
||||
"Elf: Magic!",
|
||||
"FightingElf: Magic!Fight!",
|
||||
"Dragon: Fly!",
|
||||
"Magician: Magic!Fly!"
|
||||
)
|
||||
}
|
||||
)
|
||||
}
|
||||
}
|
||||
|
|
|
@ -22,7 +22,7 @@ class TestPolymorphismExercise2 {
|
|||
}
|
||||
loadClass(packageName, "Frog").apply {
|
||||
assertConstructorNumber(1)
|
||||
assertInheritance(this, listOf("Animal"))
|
||||
assertInheritance("Animal")
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -30,7 +30,7 @@ class TestSealedClassesExercise1 {
|
|||
mainParameterName to String::class,
|
||||
"capacity" to Int::class
|
||||
)
|
||||
assertInheritance(this, listOf("Transport"))
|
||||
assertInheritance("Transport")
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -141,15 +141,15 @@ fun assertInheritance(derivedClass: KClass<*>, baseClass: KClass<*>) {
|
|||
)
|
||||
}
|
||||
|
||||
fun assertInheritance(derivedClass: KClass<*>, baseClassNames: List<String>) {
|
||||
val packageName = derivedClass.qualifiedName!!
|
||||
.removeSuffix(derivedClass.simpleName!!)
|
||||
fun KClass<*>.assertInheritance(vararg baseClassNames: String) {
|
||||
val packageName = qualifiedName!!
|
||||
.removeSuffix(simpleName!!)
|
||||
.removeSuffix(".")
|
||||
baseClassNames.forEach { className ->
|
||||
val baseClass = loadClass(packageName, className)
|
||||
assertTrue(
|
||||
actual = derivedClass.supertypes.contains(baseClass.createType()),
|
||||
message = "${derivedClass.simpleName} should inherit ${baseClass.simpleName}"
|
||||
actual = supertypes.contains(baseClass.createType()),
|
||||
message = "$simpleName should inherit ${baseClass.simpleName}"
|
||||
)
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue