diff --git a/Object-Oriented Programming/Upcasting/Exercise 2/src/Task.kt b/Object-Oriented Programming/Upcasting/Exercise 2/src/Task.kt index 802a4f18..e04851d6 100644 --- a/Object-Oriented Programming/Upcasting/Exercise 2/src/Task.kt +++ b/Object-Oriented Programming/Upcasting/Exercise 2/src/Task.kt @@ -1,6 +1,7 @@ -// Upcasting/UpcastExercise2.kt package upcastingExercise2 +import atomictest.trace + interface Apple { fun consume(): String } @@ -29,6 +30,12 @@ fun main() { Braeburn() ) apples.forEach { - println(it.consume()) + trace(it.consume()) } + trace eq """ + chomp GrannySmith + bite Gala + press Fuji + peel Braeburn + """ } \ No newline at end of file diff --git a/Object-Oriented Programming/Upcasting/Exercise 2/task-info.yaml b/Object-Oriented Programming/Upcasting/Exercise 2/task-info.yaml index e5a29866..dd4adb30 100644 --- a/Object-Oriented Programming/Upcasting/Exercise 2/task-info.yaml +++ b/Object-Oriented Programming/Upcasting/Exercise 2/task-info.yaml @@ -3,20 +3,20 @@ files: - name: src/Task.kt visible: true placeholders: - - offset: 105 + - offset: 98 length: 76 placeholder_text: class GrannySmith - - offset: 183 + - offset: 176 length: 61 placeholder_text: class Gala - - offset: 246 + - offset: 239 length: 62 placeholder_text: class Fuji - - offset: 310 + - offset: 303 length: 69 placeholder_text: class Braeburn - - offset: 498 - length: 25 - placeholder_text: // println(it.consume()) + - offset: 491 + length: 23 + placeholder_text: // trace(it.consume()) - name: test/Tests.kt visible: false diff --git a/Object-Oriented Programming/Upcasting/Exercise 2/test/Tests.kt b/Object-Oriented Programming/Upcasting/Exercise 2/test/Tests.kt index d57c2faf..1eb539db 100644 --- a/Object-Oriented Programming/Upcasting/Exercise 2/test/Tests.kt +++ b/Object-Oriented Programming/Upcasting/Exercise 2/test/Tests.kt @@ -1,11 +1,57 @@ package upcastingExercise2 +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 TestUpcastingExercise2 { - @Test fun testSolution() { - //TODO: implement your test here - unimplementedTest() + + private val packageName = "upcastingExercise2" + + private fun loadClass(className: String): KClass<*> = loadClass(packageName, className) + + @Test + fun `#01 classes structure`() { + loadClass("Apple").apply { + assertInterface() + assertDeclaredMemberFunction("consume") } + + listOf("GrannySmith", "Gala", "Fuji", "Braeburn").forEach { + loadClass(it).apply { + assertAbstract(expected = false) + assertInheritance("Apple") + assertDeclaredMemberFunction("consume") + } + } + } + + @Test + fun `#02 consume apples`() { + + listOf("GrannySmith", "Gala", "Fuji", "Braeburn").forEach { + loadClass("Apple").apply { + val consume = assertDeclaredMemberFunction("consume") + val apple = loadClass(it).createInstance() + trace(consume.call(apple) as String) + } + } + + assertEquals( + message = "Incorrect sequence of actions applied to the apples", + actual = loadTraceContent(), + expected = listOf( + "chomp GrannySmith", + "bite Gala", + "press Fuji", + "peel Braeburn" + ) + ) + } } \ No newline at end of file