From 51357c6d424fb1950cf6433fbc9decdc91004613 Mon Sep 17 00:00:00 2001 From: wrongwrong Date: Tue, 17 Mar 2020 07:02:48 +0900 Subject: [PATCH 1/6] =?UTF-8?q?=E3=83=87=E3=83=95=E3=82=A9=E3=83=AB?= =?UTF-8?q?=E3=83=88=E5=BC=95=E6=95=B0=E3=82=92=E7=84=A1=E6=9D=A1=E4=BB=B6?= =?UTF-8?q?=E3=81=A7=E4=BD=BF=E3=81=86=E6=8C=87=E5=AE=9A=E3=82=92=E8=BF=BD?= =?UTF-8?q?=E5=8A=A0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/kotlin/com/mapk/annotations/KUseDefaultArgument.kt | 5 +++++ 1 file changed, 5 insertions(+) create mode 100644 src/main/kotlin/com/mapk/annotations/KUseDefaultArgument.kt diff --git a/src/main/kotlin/com/mapk/annotations/KUseDefaultArgument.kt b/src/main/kotlin/com/mapk/annotations/KUseDefaultArgument.kt new file mode 100644 index 0000000..9df1c33 --- /dev/null +++ b/src/main/kotlin/com/mapk/annotations/KUseDefaultArgument.kt @@ -0,0 +1,5 @@ +package com.mapk.annotations + +@Target(AnnotationTarget.VALUE_PARAMETER) +@Retention(AnnotationRetention.RUNTIME) +annotation class KUseDefaultArgument From 940f7c136c597cb71bb4e62f6d392493294660c4 Mon Sep 17 00:00:00 2001 From: wrongwrong Date: Tue, 17 Mar 2020 07:10:52 +0900 Subject: [PATCH 2/6] =?UTF-8?q?=E3=83=87=E3=83=95=E3=82=A9=E3=83=AB?= =?UTF-8?q?=E3=83=88=E5=BC=95=E6=95=B0=E3=81=AE=E3=83=81=E3=82=A7=E3=83=83?= =?UTF-8?q?=E3=82=AF=E9=96=A2=E6=95=B0=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/main/kotlin/com/mapk/core/Functions.kt | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/src/main/kotlin/com/mapk/core/Functions.kt b/src/main/kotlin/com/mapk/core/Functions.kt index ef29771..9c763ae 100644 --- a/src/main/kotlin/com/mapk/core/Functions.kt +++ b/src/main/kotlin/com/mapk/core/Functions.kt @@ -1,6 +1,8 @@ package com.mapk.core import com.mapk.annotations.KParameterAlias +import com.mapk.annotations.KUseDefaultArgument +import java.lang.IllegalArgumentException import kotlin.reflect.KParameter import kotlin.reflect.full.findAnnotation @@ -8,3 +10,16 @@ import kotlin.reflect.full.findAnnotation * パラメータからエイリアスもしくはプロパティ名を取得する関数 */ fun KParameter.getAliasOrName(): String? = findAnnotation()?.value ?: name + +/** + * パラメータがignoreされているかをチェックする関数 + */ +fun KParameter.isUseDefaultArgument(): Boolean { + if (annotations.any { it is KUseDefaultArgument }) { + if (!isOptional) { + throw IllegalArgumentException("Find KUseDefaultArgument, but it's not has default argument.") + } + return true + } + return false +} From 8131ccfd58bedca7af0521c586b332cd8adac04c Mon Sep 17 00:00:00 2001 From: wrongwrong Date: Tue, 17 Mar 2020 07:30:03 +0900 Subject: [PATCH 3/6] =?UTF-8?q?=E3=82=A8=E3=82=A4=E3=83=AA=E3=82=A2?= =?UTF-8?q?=E3=82=B9=E3=82=92=E8=B2=BC=E3=81=A3=E3=81=9F=E6=99=82=E3=81=AE?= =?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 --- .../kotlin/com/mapk/core/FunctionsTest.kt | 42 +++++++++++++++++++ 1 file changed, 42 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..aeb2561 --- /dev/null +++ b/src/test/kotlin/com/mapk/core/FunctionsTest.kt @@ -0,0 +1,42 @@ +package com.mapk.core + +import com.mapk.annotations.KParameterAlias +import io.mockk.every +import io.mockk.mockk +import kotlin.reflect.KParameter +import org.junit.jupiter.api.Assertions.assertEquals +import org.junit.jupiter.api.DisplayName +import org.junit.jupiter.api.Nested +import org.junit.jupiter.api.Test + +@DisplayName("関数類のテスト") +class FunctionsTest { + @Nested + @DisplayName("エイリアス関連のテスト") + inner class GetAliasOrNameTest { + @Test + @DisplayName("エイリアス無し") + fun noAlias() { + val paramName = "mocked name" + val parameter = mockk() + every { parameter.annotations } returns emptyList() + every { parameter.name } returns paramName + + assertEquals(paramName, parameter.getAliasOrName()) + } + + @Test + @DisplayName("エイリアス有り") + fun withAlias() { + val aliasedName = "aliased name" + + val alias = mockk() + every { alias.value } returns aliasedName + + val parameter = mockk() + every { parameter.annotations } returns listOf(alias) + + assertEquals(aliasedName, parameter.getAliasOrName()) + } + } +} From a43b435a1c4fd53fa3311086743eda731215226d Mon Sep 17 00:00:00 2001 From: wrongwrong Date: Tue, 17 Mar 2020 07:34:26 +0900 Subject: [PATCH 4/6] =?UTF-8?q?beforeEach=E3=81=A7=E7=B0=A1=E7=95=A5?= =?UTF-8?q?=E5=8C=96?= 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, 8 insertions(+), 2 deletions(-) diff --git a/src/test/kotlin/com/mapk/core/FunctionsTest.kt b/src/test/kotlin/com/mapk/core/FunctionsTest.kt index aeb2561..ea8bc60 100644 --- a/src/test/kotlin/com/mapk/core/FunctionsTest.kt +++ b/src/test/kotlin/com/mapk/core/FunctionsTest.kt @@ -5,12 +5,20 @@ import io.mockk.every import io.mockk.mockk import kotlin.reflect.KParameter import org.junit.jupiter.api.Assertions.assertEquals +import org.junit.jupiter.api.BeforeEach import org.junit.jupiter.api.DisplayName import org.junit.jupiter.api.Nested import org.junit.jupiter.api.Test @DisplayName("関数類のテスト") class FunctionsTest { + lateinit var parameter: KParameter + + @BeforeEach + fun beforeEach() { + parameter = mockk() + } + @Nested @DisplayName("エイリアス関連のテスト") inner class GetAliasOrNameTest { @@ -18,7 +26,6 @@ class FunctionsTest { @DisplayName("エイリアス無し") fun noAlias() { val paramName = "mocked name" - val parameter = mockk() every { parameter.annotations } returns emptyList() every { parameter.name } returns paramName @@ -33,7 +40,6 @@ class FunctionsTest { val alias = mockk() every { alias.value } returns aliasedName - val parameter = mockk() every { parameter.annotations } returns listOf(alias) assertEquals(aliasedName, parameter.getAliasOrName()) From 2fc53f14741f95a7ffee0fec358382c79319c5e5 Mon Sep 17 00:00:00 2001 From: wrongwrong Date: Tue, 17 Mar 2020 07:40:16 +0900 Subject: [PATCH 5/6] =?UTF-8?q?=E3=83=87=E3=83=95=E3=82=A9=E3=83=AB?= =?UTF-8?q?=E3=83=88=E5=BC=95=E6=95=B0=E3=82=92=E7=94=A8=E3=81=84=E3=82=8B?= =?UTF-8?q?=E3=81=8B=E3=81=AE=E5=88=A4=E5=AE=9A=E9=96=A2=E6=95=B0=E3=81=AE?= =?UTF-8?q?=E3=83=86=E3=82=B9=E3=83=88?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../kotlin/com/mapk/core/FunctionsTest.kt | 32 +++++++++++++++++++ 1 file changed, 32 insertions(+) diff --git a/src/test/kotlin/com/mapk/core/FunctionsTest.kt b/src/test/kotlin/com/mapk/core/FunctionsTest.kt index ea8bc60..28a1287 100644 --- a/src/test/kotlin/com/mapk/core/FunctionsTest.kt +++ b/src/test/kotlin/com/mapk/core/FunctionsTest.kt @@ -1,14 +1,19 @@ package com.mapk.core import com.mapk.annotations.KParameterAlias +import com.mapk.annotations.KUseDefaultArgument import io.mockk.every import io.mockk.mockk +import java.lang.IllegalArgumentException import kotlin.reflect.KParameter import org.junit.jupiter.api.Assertions.assertEquals +import org.junit.jupiter.api.Assertions.assertFalse +import org.junit.jupiter.api.Assertions.assertTrue import org.junit.jupiter.api.BeforeEach import org.junit.jupiter.api.DisplayName import org.junit.jupiter.api.Nested import org.junit.jupiter.api.Test +import org.junit.jupiter.api.assertThrows @DisplayName("関数類のテスト") class FunctionsTest { @@ -45,4 +50,31 @@ class FunctionsTest { assertEquals(aliasedName, parameter.getAliasOrName()) } } + + @Nested + @DisplayName("デフォルト引数を用いるかの判定関数のテスト") + inner class IsUseDefaultArgumentTest { + @Test + @DisplayName("デフォルト引数を用いない場合") + fun noDefaultArgument() { + every { parameter.annotations } returns emptyList() + assertFalse(parameter.isUseDefaultArgument()) + } + + @Test + @DisplayName("デフォルト引数を用いる指定が有るが、実際はデフォルト引数が設定されていない場合") + fun isIncorrect() { + every { parameter.annotations } returns listOf(mockk()) + every { parameter.isOptional } returns false + assertThrows { parameter.isUseDefaultArgument() } + } + + @Test + @DisplayName("正常入力") + fun isCorrect() { + every { parameter.annotations } returns listOf(mockk()) + every { parameter.isOptional } returns true + assertTrue(parameter.isUseDefaultArgument()) + } + } } From 4e04e71e71be6c441cd73dad909133f4f015134e Mon Sep 17 00:00:00 2001 From: wrongwrong Date: Tue, 17 Mar 2020 07:40:47 +0900 Subject: [PATCH 6/6] =?UTF-8?q?=E3=82=B3=E3=83=A1=E3=83=B3=E3=83=88?= =?UTF-8?q?=E4=BF=AE=E6=AD=A3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/kotlin/com/mapk/core/Functions.kt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/main/kotlin/com/mapk/core/Functions.kt b/src/main/kotlin/com/mapk/core/Functions.kt index 9c763ae..2ec6302 100644 --- a/src/main/kotlin/com/mapk/core/Functions.kt +++ b/src/main/kotlin/com/mapk/core/Functions.kt @@ -12,7 +12,7 @@ import kotlin.reflect.full.findAnnotation fun KParameter.getAliasOrName(): String? = findAnnotation()?.value ?: name /** - * パラメータがignoreされているかをチェックする関数 + * デフォルト引数を用いるかチェックする関数 */ fun KParameter.isUseDefaultArgument(): Boolean { if (annotations.any { it is KUseDefaultArgument }) {