diff --git a/Object-Oriented Programming/Abstract Classes/Exercise 3/test/Tests.kt b/Object-Oriented Programming/Abstract Classes/Exercise 3/test/Tests.kt index 174e9f33..2e7d4eb1 100644 --- a/Object-Oriented Programming/Abstract Classes/Exercise 3/test/Tests.kt +++ b/Object-Oriented Programming/Abstract Classes/Exercise 3/test/Tests.kt @@ -39,9 +39,7 @@ class TestAbstractClassesExercise3 { assertNoDeclaredMemberProperty() } } - loadToplevelFunction(loadFileFacade(packageName), "tune").apply { - - } + loadToplevelFunction(loadFileFacade(packageName), "tune") } @Test diff --git a/Object-Oriented Programming/Upcasting/Exercise 1/src/Task.kt b/Object-Oriented Programming/Upcasting/Exercise 1/src/Task.kt index c79d7378..6005e99d 100644 --- a/Object-Oriented Programming/Upcasting/Exercise 1/src/Task.kt +++ b/Object-Oriented Programming/Upcasting/Exercise 1/src/Task.kt @@ -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 + """ } \ No newline at end of file diff --git a/Object-Oriented Programming/Upcasting/Exercise 1/task-info.yaml b/Object-Oriented Programming/Upcasting/Exercise 1/task-info.yaml index fe5e03f1..2d1f3da3 100644 --- a/Object-Oriented Programming/Upcasting/Exercise 1/task-info.yaml +++ b/Object-Oriented Programming/Upcasting/Exercise 1/task-info.yaml @@ -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 diff --git a/Object-Oriented Programming/Upcasting/Exercise 1/task.md b/Object-Oriented Programming/Upcasting/Exercise 1/task.md index 92059856..850ec2ff 100644 --- a/Object-Oriented Programming/Upcasting/Exercise 1/task.md +++ b/Object-Oriented Programming/Upcasting/Exercise 1/task.md @@ -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 diff --git a/Object-Oriented Programming/Upcasting/Exercise 1/test/Tests.kt b/Object-Oriented Programming/Upcasting/Exercise 1/test/Tests.kt index 5e9b5055..53126c7d 100644 --- a/Object-Oriented Programming/Upcasting/Exercise 1/test/Tests.kt +++ b/Object-Oriented Programming/Upcasting/Exercise 1/test/Tests.kt @@ -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" + ) + ) + } } \ No newline at end of file