1
1
Fork 0

Composition/Exercise 3 tests

This commit is contained in:
Pavel Nikolaev 2020-07-29 21:58:25 +02:00
parent 2f00fee628
commit 856e83389b
3 changed files with 86 additions and 9 deletions

View File

@ -18,7 +18,7 @@ class TestComplexConstructorsExercise1 {
"name" to String::class
)
loadAssertedMemberProperty(this, "myName", String::class)
assertNoMemberProperty(this, "name")
assertNoMemberProperty("name")
}
}

View File

@ -1,11 +1,81 @@
package compositionExercise3
import org.junit.FixMethodOrder
import org.junit.Test
import util.unimplementedTest
import org.junit.runners.MethodSorters
import util.*
import kotlin.reflect.KTypeProjection
import kotlin.reflect.full.createInstance
import kotlin.reflect.full.createType
import kotlin.test.assertEquals
@FixMethodOrder(MethodSorters.NAME_ASCENDING)
class TestCompositionExercise3 {
@Test fun testSolution() {
//TODO: implement your test here
unimplementedTest()
private val packageName = "compositionExercise3"
@Test
fun `#01 stack structure`() {
loadClass(packageName, "Stack").apply {
assertConstructorNumber(1)
assertParametersOfFirstConstructor()
assertMemberFunction("push")
assertMemberFunction("pop")
assertNoMemberFunction("get")
}
}
@Test
fun `#02 stack functioning`() {
val stackClass = loadClass(packageName, "Stack")
val stack = stackClass.createInstance()
val push = loadMemberFunction(stackClass, "push")
val pop = loadMemberFunction(stackClass, "pop")
push.call(stack, 1)
push.call(stack, 2)
push.call(stack, 3)
push.call(stack, 4)
assertEquals(
message = "First call of pop() in {1, 2, 3, 4} stack should return 4",
expected = 4,
actual = pop.call(stack)
)
assertEquals(
message = "Second call of pop() in {1, 2, 3, 4} stack should return 3",
expected = 3,
actual = pop.call(stack)
)
}
@Test
fun `#03 queue structure`() {
loadClass(packageName, "Queue").apply {
assertConstructorNumber(1)
assertParametersOfFirstConstructor()
assertMemberFunction("add")
assertMemberFunction("poll")
assertNoMemberFunction("get")
}
}
@Test
fun `#04 queue functioning`() {
val queueClass = loadClass(packageName, "Queue")
val queue = queueClass.createInstance()
val add = loadMemberFunction(queueClass, "add")
val poll = loadMemberFunction(queueClass, "poll")
add.call(queue, "a")
add.call(queue, "b")
add.call(queue, "c")
add.call(queue, "d")
assertEquals(
message = "First call of poll() in {'a','b','c','d'} queue should return 'a'",
expected = "a",
actual = poll.call(queue))
assertEquals(
message = "Second call of poll() in {'a','b','c','d'} queue should return 'b'",
expected = "b",
actual = poll.call(queue)
)
}
}

View File

@ -13,7 +13,6 @@ import kotlin.reflect.full.memberFunctions
import kotlin.reflect.full.memberProperties
import kotlin.reflect.jvm.isAccessible
import kotlin.test.assertEquals
import kotlin.test.assertTrue
const val TIMEOUT = 3000L
@ -179,6 +178,10 @@ fun loadMemberFunction(kClass: KClass<*>, methodName: String): KFunction<*> {
.single()
}
fun KClass<*>.assertMemberFunction(methodName: String) {
loadMemberFunction(this, methodName)
}
fun KClass<*>.assertNoMemberFunction(methodName: String) {
memberFunctions
.filter { it.name == methodName }
@ -192,10 +195,14 @@ fun loadMemberProperty(kClass: KClass<*>, propertyName: String): KProperty<*> {
.single()
}
fun assertNoMemberProperty(kClass: KClass<*>, propertyName: String) {
kClass.memberProperties
fun KClass<*>.assertNoMemberProperty(propertyName: String) {
memberProperties
.filter { it.name == propertyName }
.checkNotFoundEntities("the '$propertyName' member property", "'${kClass.simpleName}' class")
.checkNotFoundEntities("the '$propertyName' member property", "'$simpleName' class")
}
fun KClass<*>.assertMemberProperty(propertyName: String, expectedType: KType) {
loadMemberPropertyWithType(this, propertyName, expectedType)
}
fun loadMemberPropertyWithType(kClass: KClass<*>, propertyName: String, expectedType: KType): KProperty<*> {