1
1
Fork 0

Inheritance & Extensions/Exercise 1 tests

This commit is contained in:
Pavel Nikolaev 2020-07-30 19:44:44 +02:00
parent aff74255bb
commit b520e3dd43
4 changed files with 97 additions and 19 deletions

View File

@ -1,4 +1,5 @@
package inheritanceAndExtensionsExercise1
import atomictest.trace
// Duck Library
@ -7,7 +8,7 @@ interface Duck {
fun swim()
}
class RealDuck: Duck {
class RealDuck : Duck {
override fun quack() = trace("quack")
override fun swim() = trace("swim")
}
@ -22,7 +23,7 @@ interface Crocodile {
fun bite()
}
class RealCrocodile: Crocodile {
class RealCrocodile : Crocodile {
override fun bite() = trace("Mnom-mnom")
}
@ -32,23 +33,23 @@ fun interactWithCrocodile(crocodile: Crocodile) {
}
class IAmHonestlyDuck(
val crocodile: Crocodile
val crocodile: Crocodile
) : Duck {
override fun quack() = crocodile.bite()
override fun swim() = crocodile.bite()
}
fun mimicDuck(crocodile: Crocodile): IAmHonestlyDuck =
IAmHonestlyDuck(crocodile)
IAmHonestlyDuck(crocodile)
fun main() {
val honestlyDuck = mimicDuck(RealCrocodile())
interactWithDuck(honestlyDuck)
interactWithCrocodile(honestlyDuck.crocodile)
trace eq """
Mnom-mnom
Mnom-mnom
Panic!!!
Mnom-mnom
Mnom-mnom
Mnom-mnom
Panic!!!
Mnom-mnom
"""
}

View File

@ -3,16 +3,16 @@ files:
- name: src/Task.kt
visible: true
placeholders:
- offset: 526
length: 145
- offset: 529
length: 147
placeholder_text: |-
class IAmHonestlyDuck(
val crocodile: Crocodile
)
- offset: 730
length: 26
- offset: 735
length: 28
placeholder_text: TODO()
- offset: 819
- offset: 826
length: 80
placeholder_text: |-
// interactWithDuck(honestlyDuck)

View File

@ -1,11 +1,82 @@
package inheritanceAndExtensionsExercise1
import logging.trace
import org.junit.FixMethodOrder
import org.junit.Test
import util.unimplementedTest
import org.junit.runners.MethodSorters
import util.*
import kotlin.reflect.KClass
import kotlin.reflect.full.createInstance
import kotlin.test.assertEquals
@FixMethodOrder(MethodSorters.NAME_ASCENDING)
class TestInheritanceAndExtensionsExercise1 {
@Test fun testSolution() {
//TODO: implement your test here
unimplementedTest()
private val packageName = "inheritanceAndExtensionsExercise1"
private fun loadClass(className: String): KClass<*> = loadClass(packageName, className)
@Test
fun `#01 classes structure`() {
loadClass("Duck").apply {
assertInterface()
assertNoInheritance()
assertDeclaredMemberFunction("quack")
assertDeclaredMemberFunction("swim")
}
loadClass("RealDuck").apply {
assertInheritance("Duck")
assertDeclaredMemberFunction("quack")
assertDeclaredMemberFunction("swim")
}
val crocodileClass = loadClass("Crocodile").apply {
assertInterface()
assertNoInheritance()
assertDeclaredMemberFunction("bite")
}
loadClass("RealCrocodile").apply {
assertInheritance("Crocodile")
assertDeclaredMemberFunction("bite")
}
loadClass("IAmHonestlyDuck").apply {
assertInheritance("Duck")
assertDeclaredMemberProperty("crocodile", crocodileClass)
assertDeclaredMemberFunction("quack")
assertDeclaredMemberFunction("swim")
}
val fileFacade = loadFileFacade(packageName)
loadToplevelFunction(fileFacade, "interactWithDuck")
loadToplevelFunction(fileFacade, "interactWithCrocodile")
loadToplevelFunction(fileFacade, "mimicDuck")
}
@Test
fun `#02 hero actions`() {
val fileFacade = loadFileFacade(packageName)
val interactWithDuck = loadToplevelFunction(fileFacade, "interactWithDuck")
val interactWithCrocodile = loadToplevelFunction(fileFacade, "interactWithCrocodile")
val mimicDuck = loadToplevelFunction(fileFacade, "mimicDuck")
val honestlyDuck = mimicDuck(null, loadClass("RealCrocodile")
.createInstance())
interactWithDuck(null, honestlyDuck)
val crocodile = loadClass("IAmHonestlyDuck")
.assertMemberProperty("crocodile")
.call(honestlyDuck)
interactWithCrocodile(null, crocodile)
assertEquals(
message = "Incorrect sequence of actions applied to the apples",
actual = loadTraceContent(),
expected = listOf(
"Mnom-mnom",
"Mnom-mnom",
"Panic!!!",
"Mnom-mnom"
)
)
}
}

View File

@ -312,13 +312,19 @@ fun KClass<*>.assertMemberProperty(propertyName: String, expectedType: KClass<*>
}
}
fun KClass<*>.assertDeclaredMemberProperty(propertyName: String): KProperty<*> {
fun KClass<*>.assertDeclaredMemberProperty(propertyName: String, expectedType: KClass<*>? = null): KProperty<*> {
return declaredMemberProperties
.assertSingle(
memberName = propertyName,
what = { "the '${simpleName}.${propertyName}' property" },
where = { "'${simpleName}' class"}
)
).also { property ->
expectedType?.let {
assertKType(actualKType = property.returnType, expectedKType = expectedType.createType()) {
"the '${simpleName}.${propertyName}' property"
}
}
}
}
class KFileFacade(val packageName: String, val fileName: String, val jClass: Class<*>)