Upcasting/Exercise 1 tests
This commit is contained in:
parent
d97aba1fb4
commit
74d7af4754
|
@ -39,9 +39,7 @@ class TestAbstractClassesExercise3 {
|
|||
assertNoDeclaredMemberProperty()
|
||||
}
|
||||
}
|
||||
loadToplevelFunction(loadFileFacade(packageName), "tune").apply {
|
||||
|
||||
}
|
||||
loadToplevelFunction(loadFileFacade(packageName), "tune")
|
||||
}
|
||||
|
||||
@Test
|
||||
|
|
|
@ -1,6 +1,8 @@
|
|||
// Upcasting/UpcastExercise1.kt
|
||||
package upcastingExercise1
|
||||
|
||||
import atomictest.trace
|
||||
|
||||
interface Rodent {
|
||||
fun eat(): String
|
||||
fun speak(): String
|
||||
|
@ -18,15 +20,22 @@ class KangarooRat: Rodent {
|
|||
}
|
||||
|
||||
fun upcast(rodent: Rodent) {
|
||||
println(rodent.eat())
|
||||
println(rodent.speak())
|
||||
trace(rodent.eat())
|
||||
trace(rodent.speak())
|
||||
// rodent.jump() // Won't compile
|
||||
}
|
||||
|
||||
fun main() {
|
||||
val mouse = Mouse()
|
||||
val kangarooRat = KangarooRat()
|
||||
println(kangarooRat.jump())
|
||||
trace(kangarooRat.jump())
|
||||
upcast(mouse)
|
||||
upcast(kangarooRat)
|
||||
trace eq """
|
||||
KangarooRat.jump
|
||||
Mouse.eat
|
||||
Mouse.speak
|
||||
KangarooRat.eat
|
||||
KangarooRat.speak
|
||||
"""
|
||||
}
|
|
@ -3,8 +3,8 @@ files:
|
|||
- name: src/Task.kt
|
||||
visible: true
|
||||
placeholders:
|
||||
- offset: 405
|
||||
length: 83
|
||||
- offset: 430
|
||||
length: 79
|
||||
placeholder_text: TODO()
|
||||
- name: test/Tests.kt
|
||||
visible: false
|
||||
|
|
|
@ -6,7 +6,7 @@ The starter code contains a `Rodent` interface and its two implementations:
|
|||
`jump()` function.
|
||||
|
||||
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()`.
|
||||
|
||||
`main()` creates an instance of `Mouse` and `KangarooRat`, and shows that you
|
||||
|
|
|
@ -1,11 +1,68 @@
|
|||
package upcastingExercise1
|
||||
|
||||
import atomictest.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 TestUpcastingExercise1 {
|
||||
@Test fun testSolution() {
|
||||
//TODO: implement your test here
|
||||
unimplementedTest()
|
||||
|
||||
private val packageName = "upcastingExercise1"
|
||||
|
||||
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"
|
||||
)
|
||||
)
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue