1
1
Fork 0

Basic Class Initialization/Exercise 3 tests

This commit is contained in:
Pavel Nikolaev 2020-07-29 22:41:56 +02:00
parent 6e742bba43
commit 12b3de7e99
4 changed files with 89 additions and 16 deletions

View File

@ -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
"""
}

View File

@ -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

View File

@ -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"
)
)
}
}

View File

@ -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<*> {