1
1
Fork 0

Abstract Classes/Exercise 2 tests

This commit is contained in:
Pavel Nikolaev 2020-07-30 15:14:35 +02:00
parent c8a12a6053
commit fbd569e550
4 changed files with 64 additions and 14 deletions

View File

@ -1,4 +1,3 @@
// Abstract/AbsExercise1.kt
package abstractClassesExercise2
import atomictest.eq

View File

@ -3,16 +3,16 @@ files:
- name: src/Task.kt
visible: true
placeholders:
- offset: 84
- offset: 56
length: 17
placeholder_text: abstract class Movable
- offset: 129
- offset: 101
length: 19
placeholder_text: abstract class Sleepable
- offset: 180
- offset: 152
length: 30
placeholder_text: '// class Sofa: Movable(), Sleepable()'
- offset: 225
- offset: 197
length: 71
placeholder_text: |-
/*

View File

@ -1,11 +1,56 @@
package abstractClassesExercise2
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 TestAbstractClassesExercise2 {
@Test fun testSolution() {
//TODO: implement your test here
unimplementedTest()
private val packageName = "abstractClassesExercise2"
@Test
fun `#01 classes structure`() {
loadClass(packageName, "Movable").apply {
assertEquals(
message = "$simpleName should be an interface",
expected = true,
actual = isAbstract
)
assertDeclaredMemberFunction("move")
}
loadClass(packageName, "Sleepable").apply {
assertEquals(
message = "$simpleName should be an interface",
expected = true,
actual = isAbstract
)
assertDeclaredMemberFunction("sleepOn")
}
loadClass(packageName, "Sofa").apply {
assertNoDeclaredMemberFunction("move")
assertNoDeclaredMemberFunction("sleepOn")
assertInheritance("Movable", "Sleepable")
}
}
@Test
fun `#02 sofa operations`() {
loadClass(packageName, "Sofa").apply {
val sofa = createInstance()
assertEquals(
message = "Incorrect result of sofa.move() call",
actual = assertMemberFunction("move").call(sofa),
expected = "move"
)
assertEquals(
message = "Incorrect result of sofa.sleepOn() call",
actual = assertMemberFunction("sleepOn").call(sofa),
expected = "sleep"
)
}
}
}

View File

@ -183,17 +183,23 @@ fun KClass<*>.assertMemberFunction(methodName: String): KFunction<*> {
.single()
}
fun KClass<*>.assertNoMemberFunction(methodName: String) {
memberFunctions
.filter { it.name == methodName }
.checkNotFoundEntities("the '$methodName' member function", "'$simpleName' class")
}
fun KClass<*>.assertDeclaredMemberFunction(methodName: String): KFunction<*> {
return declaredMemberFunctions
.filter { it.name == methodName }
.checkFoundEntities(what = "the '$methodName()' member function", where = "'${simpleName}' class")
.checkFoundEntities(what = "the declared '$methodName()' member function", where = "'${simpleName}' class")
.single()
}
fun KClass<*>.assertNoMemberFunction(methodName: String) {
memberFunctions
.filter { it.name == methodName }
.checkNotFoundEntities("the '$methodName' member function", "'$simpleName' class")
fun KClass<*>.assertNoDeclaredMemberFunction(methodName: String) {
declaredMemberFunctions
.filter { it.name == methodName }
.checkNotFoundEntities("the declared '$methodName' member function", "'$simpleName' class")
}
fun loadMemberProperty(kClass: KClass<*>, propertyName: String): KProperty<*> {