1
1
Fork 0

Upcasting/Exercise 1 tests

This commit is contained in:
Pavel Nikolaev 2020-07-30 17:05:27 +02:00
parent d97aba1fb4
commit 74d7af4754
5 changed files with 77 additions and 13 deletions

View File

@ -39,9 +39,7 @@ class TestAbstractClassesExercise3 {
assertNoDeclaredMemberProperty() assertNoDeclaredMemberProperty()
} }
} }
loadToplevelFunction(loadFileFacade(packageName), "tune").apply { loadToplevelFunction(loadFileFacade(packageName), "tune")
}
} }
@Test @Test

View File

@ -1,6 +1,8 @@
// Upcasting/UpcastExercise1.kt // Upcasting/UpcastExercise1.kt
package upcastingExercise1 package upcastingExercise1
import atomictest.trace
interface Rodent { interface Rodent {
fun eat(): String fun eat(): String
fun speak(): String fun speak(): String
@ -18,15 +20,22 @@ class KangarooRat: Rodent {
} }
fun upcast(rodent: Rodent) { fun upcast(rodent: Rodent) {
println(rodent.eat()) trace(rodent.eat())
println(rodent.speak()) trace(rodent.speak())
// rodent.jump() // Won't compile // rodent.jump() // Won't compile
} }
fun main() { fun main() {
val mouse = Mouse() val mouse = Mouse()
val kangarooRat = KangarooRat() val kangarooRat = KangarooRat()
println(kangarooRat.jump()) trace(kangarooRat.jump())
upcast(mouse) upcast(mouse)
upcast(kangarooRat) upcast(kangarooRat)
trace eq """
KangarooRat.jump
Mouse.eat
Mouse.speak
KangarooRat.eat
KangarooRat.speak
"""
} }

View File

@ -3,8 +3,8 @@ files:
- name: src/Task.kt - name: src/Task.kt
visible: true visible: true
placeholders: placeholders:
- offset: 405 - offset: 430
length: 83 length: 79
placeholder_text: TODO() placeholder_text: TODO()
- name: test/Tests.kt - name: test/Tests.kt
visible: false visible: false

View File

@ -6,7 +6,7 @@ The starter code contains a `Rodent` interface and its two implementations:
`jump()` function. `jump()` function.
Create a function `upcast(rodent: Rodent)` which displays the result of `eat()` Create a function `upcast(rodent: Rodent)` which displays the result of `eat()`
and `speak()` using `println()`. Show that the compiler won't allow you to call and `speak()` using `trace()`. Show that the compiler won't allow you to call
`jump()`. `jump()`.
`main()` creates an instance of `Mouse` and `KangarooRat`, and shows that you `main()` creates an instance of `Mouse` and `KangarooRat`, and shows that you

View File

@ -1,11 +1,68 @@
package upcastingExercise1 package upcastingExercise1
import atomictest.trace
import org.junit.FixMethodOrder
import org.junit.Test 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 TestUpcastingExercise1 { class TestUpcastingExercise1 {
@Test fun testSolution() {
//TODO: implement your test here private val packageName = "upcastingExercise1"
unimplementedTest()
private fun loadClass(className: String): KClass<*> = loadClass(packageName, className)
@Test
fun `#01 classes structure`() {
loadClass("Rodent").apply {
assertInterface()
assertDeclaredMemberFunction("eat")
assertDeclaredMemberFunction("speak")
} }
loadClass("Mouse").apply {
assertAbstract(expected = false)
assertInheritance("Rodent")
assertDeclaredMemberFunction("eat")
assertDeclaredMemberFunction("speak")
}
loadClass("KangarooRat").apply {
assertAbstract(expected = false)
assertInheritance("Rodent")
assertDeclaredMemberFunction("eat")
assertDeclaredMemberFunction("speak")
assertDeclaredMemberFunction("jump")
}
loadToplevelFunction(loadFileFacade(packageName), "upcast")
}
@Test
fun `#02 sofa operations`() {
val mouse = loadClass("Mouse").createInstance()
val kangarooRat = loadClass("KangarooRat").createInstance()
val upcast = loadToplevelFunction(loadFileFacade(packageName), "upcast")
val jump = loadClass("KangarooRat").assertDeclaredMemberFunction("jump")
trace(jump.call(kangarooRat))
upcast(null, mouse)
upcast(null, kangarooRat)
assertEquals(
message = "Incorrect sequence of actions of rodents",
actual = loadTraceContent(),
expected = listOf(
"KangarooRat.jump",
"Mouse.eat",
"Mouse.speak",
"KangarooRat.eat",
"KangarooRat.speak"
)
)
}
} }