From f028886ebd2f6ff5bda2620ca4af131250fd8a28 Mon Sep 17 00:00:00 2001 From: Pavel Nikolaev Date: Wed, 29 Jul 2020 19:45:09 +0200 Subject: [PATCH] refactored inheritance assertion --- .../Inheritance/Exercise 1/test/Tests.kt | 2 +- .../Inheritance/Exercise 2/test/Tests.kt | 6 +-- .../Inheritance/Exercise 3/test/Tests.kt | 4 +- util/test/util.kt | 42 +++++++++++++------ 4 files changed, 36 insertions(+), 18 deletions(-) diff --git a/Object-Oriented Programming/Inheritance/Exercise 1/test/Tests.kt b/Object-Oriented Programming/Inheritance/Exercise 1/test/Tests.kt index 9aadc5fc..a4f79c09 100644 --- a/Object-Oriented Programming/Inheritance/Exercise 1/test/Tests.kt +++ b/Object-Oriented Programming/Inheritance/Exercise 1/test/Tests.kt @@ -28,7 +28,7 @@ class TestInheritanceExercise1 { loadMemberFunction(cleanserClass, "scrub") val detergentClass = Detergent::class - assertInheritance(detergentClass, cleanserClass) + detergentClass.assertInheritance(cleanserClass) } @Test diff --git a/Object-Oriented Programming/Inheritance/Exercise 2/test/Tests.kt b/Object-Oriented Programming/Inheritance/Exercise 2/test/Tests.kt index 8c13476e..71d7023e 100644 --- a/Object-Oriented Programming/Inheritance/Exercise 2/test/Tests.kt +++ b/Object-Oriented Programming/Inheritance/Exercise 2/test/Tests.kt @@ -26,13 +26,13 @@ class TestInheritanceExercise2 { loadMemberFunction(cleanserClass, "dilute") loadMemberFunction(cleanserClass, "apply") loadMemberFunction(cleanserClass, "scrub") - assertNoMemberFunction(cleanserClass, "sterilize") + cleanserClass.assertNoMemberFunction("sterilize") val detergentClass = Detergent::class - assertNoMemberFunction(detergentClass, "sterilize") + detergentClass.assertNoMemberFunction("sterilize") val disinfectantClass = Disinfectant::class - assertInheritance(disinfectantClass, detergentClass) + disinfectantClass.assertInheritance(detergentClass) loadMemberFunction(disinfectantClass, "sterilize") } diff --git a/Object-Oriented Programming/Inheritance/Exercise 3/test/Tests.kt b/Object-Oriented Programming/Inheritance/Exercise 3/test/Tests.kt index 5fde1522..3907fead 100644 --- a/Object-Oriented Programming/Inheritance/Exercise 3/test/Tests.kt +++ b/Object-Oriented Programming/Inheritance/Exercise 3/test/Tests.kt @@ -21,8 +21,8 @@ class TestInheritanceExercise3 { bClass.assertConstructorNumber(1) cClass.assertConstructorNumber(1) - assertInheritance(bClass, aClass) - assertInheritance(cClass, bClass) + bClass.assertInheritance(aClass) + cClass.assertInheritance(bClass) val optProperty = loadMemberProperty(aClass, "initOrder") assertEquals( diff --git a/util/test/util.kt b/util/test/util.kt index 0342e588..7b01952c 100644 --- a/util/test/util.kt +++ b/util/test/util.kt @@ -134,22 +134,40 @@ fun loadClass(packageName: String, className: String): KClass<*> { } } -fun assertInheritance(derivedClass: KClass<*>, baseClass: KClass<*>) { - assertTrue( - actual = derivedClass.supertypes.contains(baseClass.createType()), - message = "${derivedClass.simpleName} should inherit ${baseClass.simpleName}" +fun KClass<*>.assertInheritance(baseClass: KClass<*>) { + assertInherit(true, baseClass) +} + +fun KClass<*>.assertNoInheritance(baseClass: KClass<*>) { + assertInherit(false, baseClass) +} + +private fun KClass<*>.assertInherit(inherited: Boolean, baseClass: KClass<*>) { + assertEquals( + actual = supertypes.contains(baseClass.createType()), + expected = inherited, + message = "$simpleName should ${if(inherited) "" else "not "}inherit ${baseClass.simpleName}", ) } fun KClass<*>.assertInheritance(vararg baseClassNames: String) { + assertInherit(true, *baseClassNames) +} + +fun KClass<*>.assertNoInheritance(vararg baseClassNames: String) { + assertInherit(false, *baseClassNames) +} + +private fun KClass<*>.assertInherit(inherited: Boolean, vararg baseClassNames: String) { val packageName = qualifiedName!! - .removeSuffix(simpleName!!) - .removeSuffix(".") + .removeSuffix(simpleName!!) + .removeSuffix(".") baseClassNames.forEach { className -> val baseClass = loadClass(packageName, className) - assertTrue( - actual = supertypes.contains(baseClass.createType()), - message = "$simpleName should inherit ${baseClass.simpleName}" + assertEquals( + message = "$simpleName should ${if(inherited) "" else "not "}inherit ${baseClass.simpleName}", + actual = supertypes.contains(baseClass.createType()), + expected = inherited ) } } @@ -161,10 +179,10 @@ fun loadMemberFunction(kClass: KClass<*>, methodName: String): KFunction<*> { .single() } -fun assertNoMemberFunction(kClass: KClass<*>, methodName: String) { - kClass.memberFunctions +fun KClass<*>.assertNoMemberFunction(methodName: String) { + memberFunctions .filter { it.name == methodName } - .checkNotFoundEntities("the '$methodName' member function", "'${kClass.simpleName}' class") + .checkNotFoundEntities("the '$methodName' member function", "'$simpleName' class") } fun loadMemberProperty(kClass: KClass<*>, propertyName: String): KProperty<*> {