diff --git a/Object-Oriented Programming/Base Class Initialization/Exercise 3/src/Task.kt b/Object-Oriented Programming/Base Class Initialization/Exercise 3/src/Task.kt index c4cf038a..02bc3573 100644 --- a/Object-Oriented Programming/Base Class Initialization/Exercise 3/src/Task.kt +++ b/Object-Oriented Programming/Base Class Initialization/Exercise 3/src/Task.kt @@ -1,6 +1,7 @@ -// BaseClassInit/BCIExercise1.kt package baseClassInitializationExercise3 +import atomictest.trace + open class Animal(val sound: String = "???") class Cat(sound: String = "meow") : Animal(sound) @@ -9,9 +10,14 @@ class Dog(sound: String = "woof") : Animal(sound) fun main() { val animal = Animal() - println(animal.sound) + trace(animal.sound) val cat = Cat() - println(cat.sound) + trace(cat.sound) val dog = Dog() - println(dog.sound) + trace(dog.sound) + trace eq """ + ??? + meow + woof + """ } \ No newline at end of file diff --git a/Object-Oriented Programming/Base Class Initialization/Exercise 3/task-info.yaml b/Object-Oriented Programming/Base Class Initialization/Exercise 3/task-info.yaml index ca2ad63a..6028c582 100644 --- a/Object-Oriented Programming/Base Class Initialization/Exercise 3/task-info.yaml +++ b/Object-Oriented Programming/Base Class Initialization/Exercise 3/task-info.yaml @@ -3,19 +3,19 @@ files: - name: src/Task.kt visible: true placeholders: - - offset: 75 + - offset: 67 length: 146 placeholder_text: '/* TODO: Implement Animal, Cat and Dog classes */' - - offset: 236 - length: 125 + - offset: 228 + length: 119 placeholder_text: |- /* val animal = Animal() - println(animal.sound) + trace(animal.sound) val cat = Cat() - println(cat.sound) + trace(cat.sound) val dog = Dog() - println(dog.sound) + trace(dog.sound) */ - name: test/Tests.kt visible: false diff --git a/Object-Oriented Programming/Base Class Initialization/Exercise 3/test/Tests.kt b/Object-Oriented Programming/Base Class Initialization/Exercise 3/test/Tests.kt index 6de08442..6f73dd85 100644 --- a/Object-Oriented Programming/Base Class Initialization/Exercise 3/test/Tests.kt +++ b/Object-Oriented Programming/Base Class Initialization/Exercise 3/test/Tests.kt @@ -1,11 +1,71 @@ package baseClassInitializationExercise3 +import atomictest.trace +import org.junit.FixMethodOrder import org.junit.Test -import util.unimplementedTest +import org.junit.runners.MethodSorters +import util.* +import kotlin.reflect.full.createInstance +import kotlin.test.assertEquals +@FixMethodOrder(MethodSorters.NAME_ASCENDING) class TestBaseClassInitializationExercise3 { - @Test fun testSolution() { - //TODO: implement your test here - unimplementedTest() + + private val packageName = "baseClassInitializationExercise3" + + @Test + fun `#01 classes structure`() { + loadClass(packageName, "Animal").apply { + assertConstructorNumber(1) + assertParametersOfFirstConstructor( + "sound" to String::class + ) + assertMemberProperty("sound", String::class) } + loadClass(packageName, "Cat").apply { + assertInheritance("Animal") + assertConstructorNumber(1) + assertParametersOfFirstConstructor( + "sound" to String::class + ) + assertNoDeclaredMemberProperty("sound") + } + loadClass(packageName, "Dog").apply { + assertInheritance("Animal") + assertConstructorNumber(1) + assertParametersOfFirstConstructor( + "sound" to String::class + ) + assertNoDeclaredMemberProperty("sound") + } + } + + @Test + fun `#02 animal sounds`() { + loadClass(packageName, "Animal").apply { + val animal = createInstance() + val sound = loadMemberProperty(this, "sound").call(animal) as String + trace(sound) + } + loadClass(packageName, "Cat").apply { + val cat = createInstance() + val sound = loadMemberProperty(this, "sound").call(cat) as String + trace(sound) + } + loadClass(packageName, "Dog").apply { + val dog = createInstance() + val sound = loadMemberProperty(this, "sound").call(dog) as String + trace(sound) + } + + assertEquals( + message = "Incorrect animal sounds", + actual = loadTraceContent(), + expected = listOf( + "???", + "meow", + "woof" + ) + ) + } } \ No newline at end of file diff --git a/util/test/util.kt b/util/test/util.kt index feec4417..f768fee7 100644 --- a/util/test/util.kt +++ b/util/test/util.kt @@ -9,6 +9,7 @@ import java.lang.reflect.Field import java.lang.reflect.Method import kotlin.reflect.* import kotlin.reflect.full.createType +import kotlin.reflect.full.declaredMemberProperties import kotlin.reflect.full.memberFunctions import kotlin.reflect.full.memberProperties import kotlin.reflect.jvm.isAccessible @@ -195,14 +196,20 @@ fun loadMemberProperty(kClass: KClass<*>, propertyName: String): KProperty<*> { .single() } +fun KClass<*>.assertNoDeclaredMemberProperty(propertyName: String) { + declaredMemberProperties + .filter { it.name == propertyName } + .checkNotFoundEntities("the '$propertyName' member property", "'$simpleName' class") +} + fun KClass<*>.assertNoMemberProperty(propertyName: String) { memberProperties .filter { it.name == propertyName } .checkNotFoundEntities("the '$propertyName' member property", "'$simpleName' class") } -fun KClass<*>.assertMemberProperty(propertyName: String, expectedType: KType) { - loadMemberPropertyWithType(this, propertyName, expectedType) +fun KClass<*>.assertMemberProperty(propertyName: String, expectedType: KClass<*>) { + loadMemberPropertyWithType(this, propertyName, expectedType.createType()) } fun loadMemberPropertyWithType(kClass: KClass<*>, propertyName: String, expectedType: KType): KProperty<*> {