From 13cd30ea94e6d6319f26fc2b9bb5d8eaab86b191 Mon Sep 17 00:00:00 2001 From: wrongwrong Date: Sun, 17 May 2020 04:00:41 +0900 Subject: [PATCH 1/3] =?UTF-8?q?EnumMapper=E3=81=AE=E3=83=86=E3=82=B9?= =?UTF-8?q?=E3=83=88=E3=82=92=E8=BF=BD=E5=8A=A0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../kotlin/com/mapk/core/EnumMapperTest.kt | 36 +++++++++++++++++++ 1 file changed, 36 insertions(+) create mode 100644 src/test/kotlin/com/mapk/core/EnumMapperTest.kt diff --git a/src/test/kotlin/com/mapk/core/EnumMapperTest.kt b/src/test/kotlin/com/mapk/core/EnumMapperTest.kt new file mode 100644 index 0000000..ed3fb76 --- /dev/null +++ b/src/test/kotlin/com/mapk/core/EnumMapperTest.kt @@ -0,0 +1,36 @@ +package com.mapk.core + +import org.junit.jupiter.api.Assertions.assertEquals +import org.junit.jupiter.api.Assertions.assertNull +import org.junit.jupiter.api.DisplayName +import org.junit.jupiter.api.Test +import org.junit.jupiter.api.assertThrows +import org.junit.jupiter.params.ParameterizedTest +import org.junit.jupiter.params.provider.EnumSource + +@DisplayName("EnumMapperのテスト") +class EnumMapperTest { + enum class JvmLanguage { + Java, Scala, Groovy, Kotlin + } + + @ParameterizedTest + @EnumSource(value = JvmLanguage::class) + @DisplayName("正常系") + fun test(value: JvmLanguage) { + assertEquals(value, EnumMapper.getEnum(JvmLanguage::class.java, value.name)) + } + + @Test + @DisplayName("null/空文字列入力") + fun isNull() { + assertNull(EnumMapper.getEnum(JvmLanguage::class.java, null)) + assertNull(EnumMapper.getEnum(JvmLanguage::class.java, "")) + } + + @Test + @DisplayName("存在しないもの") + fun isNotExisting() { + assertThrows { EnumMapper.getEnum(JvmLanguage::class.java, "C") } + } +} From 7cfc95798735aec4012715e85483fc67f999cb84 Mon Sep 17 00:00:00 2001 From: wrongwrong Date: Sun, 17 May 2020 04:21:05 +0900 Subject: [PATCH 2/3] =?UTF-8?q?"=E3=82=B3=E3=83=B3=E3=83=91=E3=83=8B?= =?UTF-8?q?=E3=82=AA=E3=83=B3=E3=82=AA=E3=83=96=E3=82=B8=E3=82=A7=E3=82=AF?= =?UTF-8?q?=E3=83=88=E3=81=8B=E3=82=89=E6=8C=87=E5=AE=9A=E3=81=97=E3=81=9F?= =?UTF-8?q?=E3=82=A2=E3=83=8E=E3=83=86=E3=83=BC=E3=82=B7=E3=83=A7=E3=83=B3?= =?UTF-8?q?=E3=82=92=E5=8F=96=E5=BE=97=E3=81=99=E3=82=8B=E3=83=86=E3=82=B9?= =?UTF-8?q?=E3=83=88=E3=82=92=E8=BF=BD=E5=8A=A0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../kotlin/com/mapk/core/FunctionsTest.kt | 78 +++++++++++++++++++ 1 file changed, 78 insertions(+) create mode 100644 src/test/kotlin/com/mapk/core/FunctionsTest.kt diff --git a/src/test/kotlin/com/mapk/core/FunctionsTest.kt b/src/test/kotlin/com/mapk/core/FunctionsTest.kt new file mode 100644 index 0000000..c1fa115 --- /dev/null +++ b/src/test/kotlin/com/mapk/core/FunctionsTest.kt @@ -0,0 +1,78 @@ +package com.mapk.core + +import com.mapk.annotations.KConstructor +import kotlin.reflect.full.companionObjectInstance +import org.junit.jupiter.api.Assertions.assertEquals +import org.junit.jupiter.api.Assertions.assertNull +import org.junit.jupiter.api.Assertions.assertTrue +import org.junit.jupiter.api.DisplayName +import org.junit.jupiter.api.Nested +import org.junit.jupiter.api.Test + +@DisplayName("共通利用関数関連のテスト") +class FunctionsTest { + class NoCompanionObject + + @Target(AnnotationTarget.FUNCTION) + @Retention(AnnotationRetention.RUNTIME) + @MustBeDocumented + annotation class TestAnnotation1 + + @Target(AnnotationTarget.FUNCTION) + @Retention(AnnotationRetention.RUNTIME) + @MustBeDocumented + annotation class TestAnnotation2 + + class WithCompanionObject { + companion object { + @KConstructor + fun kConstructor() {} + + @TestAnnotation2 + fun testAnnotation1() {} + + @TestAnnotation2 + fun testAnnotation2() {} + } + } + + @Nested + @DisplayName("コンパニオンオブジェクトから指定したアノテーションを取得するテスト") + inner class GetAnnotatedFunctionsFromCompanionObject { + @Test + @DisplayName("コンパニオンオブジェクトが無い場合") + fun noCompanionObject() { + assertNull(NoCompanionObject::class.getAnnotatedFunctionsFromCompanionObject()) + } + + @Test + @DisplayName("コンパニオンオブジェクトが有るが関数が取れない場合") + fun withCompanionObjectButNotFound() { + assertNull(WithCompanionObject::class.getAnnotatedFunctionsFromCompanionObject()) + } + + @Test + @DisplayName("単体取得") + fun kConstructor() { + val result = + WithCompanionObject::class.getAnnotatedFunctionsFromCompanionObject()!! + + assertEquals(WithCompanionObject::class.companionObjectInstance!!, result.first) + assertEquals(1, result.second.size) + assertEquals("kConstructor", result.second.single().name) + } + + @Test + @DisplayName("複数取得") + fun testAnnotation() { + val result = + WithCompanionObject::class.getAnnotatedFunctionsFromCompanionObject()!! + + assertEquals(WithCompanionObject::class.companionObjectInstance!!, result.first) + assertEquals(2, result.second.size) + val names = listOf("testAnnotation1", "testAnnotation2") + assertTrue(names.contains(result.second[0].name)) + assertTrue(names.contains(result.second[1].name)) + } + } +} From ed961a88f2e0cdf6056f106ee543a73766e62a61 Mon Sep 17 00:00:00 2001 From: wrongwrong Date: Sun, 17 May 2020 04:28:50 +0900 Subject: [PATCH 3/3] =?UTF-8?q?=E3=83=91=E3=83=A9=E3=83=A1=E3=83=BC?= =?UTF-8?q?=E3=82=BF=E3=81=8B=E3=82=89=E3=81=AE=E5=9E=8B=E5=8F=96=E5=BE=97?= =?UTF-8?q?=E3=83=86=E3=82=B9=E3=83=88=E3=82=92=E8=BF=BD=E5=8A=A0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/test/kotlin/com/mapk/core/FunctionsTest.kt | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/src/test/kotlin/com/mapk/core/FunctionsTest.kt b/src/test/kotlin/com/mapk/core/FunctionsTest.kt index c1fa115..5164183 100644 --- a/src/test/kotlin/com/mapk/core/FunctionsTest.kt +++ b/src/test/kotlin/com/mapk/core/FunctionsTest.kt @@ -75,4 +75,14 @@ class FunctionsTest { assertTrue(names.contains(result.second[1].name)) } } + + data class InnerClass(val arg: Int) + data class DataClass(val innerClass: InnerClass) + + @Test + @DisplayName("パラメータからの型取得テスト") + fun getKClassTest() { + val param = ::DataClass.parameters.single() + assertEquals(InnerClass::class, param.getKClass()) + } }