|
| 1 | +package com.mapk.fastkfunction.fastkfunction |
| 2 | + |
| 3 | +import com.mapk.fastkfunction.FastKFunction |
| 4 | +import org.junit.jupiter.api.Assertions.assertEquals |
| 5 | +import org.junit.jupiter.api.TestInstance |
| 6 | +import org.junit.jupiter.api.assertDoesNotThrow |
| 7 | +import org.junit.jupiter.params.ParameterizedTest |
| 8 | +import org.junit.jupiter.params.provider.Arguments |
| 9 | +import org.junit.jupiter.params.provider.MethodSource |
| 10 | +import java.util.stream.Stream |
| 11 | +import kotlin.reflect.KFunction |
| 12 | +import kotlin.reflect.KParameter |
| 13 | +import kotlin.reflect.full.companionObject |
| 14 | +import kotlin.reflect.full.companionObjectInstance |
| 15 | +import kotlin.reflect.full.functions |
| 16 | + |
| 17 | +enum class DefaultValues { |
| 18 | + Constructor, |
| 19 | + InstanceFunc, |
| 20 | + InstanceFuncWithInstance, |
| 21 | + CompanionObjectFunc, |
| 22 | + CompanionObjectFuncWithInstance, |
| 23 | + CompanionObjectFuncFromReflection, |
| 24 | + CompanionObjectFuncFromReflectionWithInstance, |
| 25 | + TopLevelFunc, |
| 26 | + TopLevelExtensionFunc, |
| 27 | + TopLevelExtensionFuncFromInstance, |
| 28 | + TopLevelExtensionFuncFromInstanceWithInstance |
| 29 | +} |
| 30 | + |
| 31 | +private fun topLevelFunc( |
| 32 | + arg1: Int, arg2: String, arg3: String = DefaultValues.TopLevelFunc.name |
| 33 | +): UseDefaultValueCallTest.Dst = UseDefaultValueCallTest.Dst(arg1, arg2, arg3) |
| 34 | + |
| 35 | +private fun UseDefaultValueCallTest.Class.topLevelExtensionFunc( |
| 36 | + arg1: Int, arg2: String, arg3: String = DefaultValues.TopLevelExtensionFunc.name |
| 37 | +): UseDefaultValueCallTest.Dst = UseDefaultValueCallTest.Dst(arg1, arg2, arg3) |
| 38 | + |
| 39 | +private fun UseDefaultValueCallTest.Class.topLevelExtensionFuncFromInstance( |
| 40 | + arg1: Int, arg2: String, arg3: String = DefaultValues.TopLevelExtensionFuncFromInstance.name |
| 41 | +): UseDefaultValueCallTest.Dst = UseDefaultValueCallTest.Dst(arg1, arg2, arg3) |
| 42 | + |
| 43 | +private fun UseDefaultValueCallTest.Class.topLevelExtensionFuncFromInstanceWithInstance( |
| 44 | + arg1: Int, arg2: String, arg3: String = DefaultValues.TopLevelExtensionFuncFromInstanceWithInstance.name |
| 45 | +): UseDefaultValueCallTest.Dst = UseDefaultValueCallTest.Dst(arg1, arg2, arg3) |
| 46 | + |
| 47 | +/** |
| 48 | + * 網羅しているパターン |
| 49 | + * - コンストラクタ |
| 50 | + * - インスタンスメソッド |
| 51 | + * - インスタンスメソッド + インスタンス |
| 52 | + * - コンパニオンオブジェクトに定義したメソッド |
| 53 | + * - コンパニオンオブジェクトに定義したメソッド + コンパニオンオブジェクトインスタンス |
| 54 | + * - リフレクションで取得したコンパニオンオブジェクトに定義したメソッド |
| 55 | + * - リフレクションで取得したコンパニオンオブジェクトに定義したメソッド + コンパニオンオブジェクトインスタンス |
| 56 | + * - トップレベル関数 |
| 57 | + * - クラスから取得したトップレベル拡張関数 + レシーバインスタンス |
| 58 | + * - インスタンスから取得したトップレベル拡張関数 |
| 59 | + * - インスタンスから取得したトップレベル拡張関数 + インスタンス |
| 60 | + */ |
| 61 | +@TestInstance(TestInstance.Lifecycle.PER_CLASS) |
| 62 | +private class UseDefaultValueCallTest { |
| 63 | + class Class |
| 64 | + |
| 65 | + data class Dst(val arg1: Int, val arg2: String, val arg3: String = DefaultValues.Constructor.name) { |
| 66 | + companion object { |
| 67 | + fun of(arg1: Int, arg2: String, arg3: String = DefaultValues.CompanionObjectFunc.name) = |
| 68 | + Dst(arg1, arg2, arg3) |
| 69 | + |
| 70 | + fun ofWithInstance( |
| 71 | + arg1: Int, arg2: String, arg3: String = DefaultValues.CompanionObjectFuncWithInstance.name |
| 72 | + ) = Dst(arg1, arg2, arg3) |
| 73 | + |
| 74 | + fun ofFromReflection( |
| 75 | + arg1: Int, arg2: String, arg3: String = DefaultValues.CompanionObjectFuncFromReflection.name |
| 76 | + ) = Dst(arg1, arg2, arg3) |
| 77 | + |
| 78 | + fun ofFromReflectionWithInstance( |
| 79 | + arg1: Int, arg2: String, arg3: String = DefaultValues.CompanionObjectFuncFromReflectionWithInstance.name |
| 80 | + ) = Dst(arg1, arg2, arg3) |
| 81 | + } |
| 82 | + } |
| 83 | + |
| 84 | + private fun instanceFunction(arg1: Int, arg2: String, arg3: String = DefaultValues.InstanceFunc.name) = |
| 85 | + Dst(arg1, arg2, arg3) |
| 86 | + |
| 87 | + private fun instanceFunctionWithInstance( |
| 88 | + arg1: Int, arg2: String, arg3: String = DefaultValues.InstanceFuncWithInstance.name |
| 89 | + ) = Dst(arg1, arg2, arg3) |
| 90 | + |
| 91 | + @ParameterizedTest |
| 92 | + @MethodSource("argumentsProvider") |
| 93 | + fun test(target: KFunction<Dst>, instance: Any?, default: DefaultValues) { |
| 94 | + val sut = FastKFunction(target, instance) |
| 95 | + val bucket = sut.generateBucket().apply { |
| 96 | + val params = target.parameters.filter { it.kind == KParameter.Kind.VALUE && !it.isOptional } |
| 97 | + |
| 98 | + put(params[0], 100) |
| 99 | + put(params[1], "txt") |
| 100 | + } |
| 101 | + |
| 102 | + assertDoesNotThrow("Fail ${default.name}") { |
| 103 | + assertEquals(Dst(100, "txt", default.name), sut.callBy(bucket), default.name) |
| 104 | + } |
| 105 | + } |
| 106 | + |
| 107 | + fun argumentsProvider(): Stream<Arguments> { |
| 108 | + val ofFromReflection = Dst::class.companionObject!!.functions.first { it.name == "ofFromReflection" } |
| 109 | + val ofFromReflectionWithInstance = |
| 110 | + Dst::class.companionObject!!.functions.first { it.name == "ofFromReflectionWithInstance" } |
| 111 | + |
| 112 | + return listOf( |
| 113 | + Arguments.of(UseDefaultValueCallTest::Dst, null, DefaultValues.Constructor), |
| 114 | + Arguments.of(::instanceFunction, null, DefaultValues.InstanceFunc), |
| 115 | + Arguments.of(::instanceFunctionWithInstance, this, DefaultValues.InstanceFuncWithInstance), |
| 116 | + Arguments.of((Dst)::of, null, DefaultValues.CompanionObjectFunc), |
| 117 | + Arguments.of( |
| 118 | + (Dst)::ofWithInstance, Dst::class.companionObjectInstance, DefaultValues.CompanionObjectFuncWithInstance |
| 119 | + ), |
| 120 | + Arguments.of(ofFromReflection, null, DefaultValues.CompanionObjectFuncFromReflection), |
| 121 | + Arguments.of( |
| 122 | + ofFromReflectionWithInstance, |
| 123 | + Dst::class.companionObjectInstance, |
| 124 | + DefaultValues.CompanionObjectFuncFromReflectionWithInstance |
| 125 | + ), |
| 126 | + Arguments.of(::topLevelFunc, null, DefaultValues.TopLevelFunc), |
| 127 | + Arguments.of(Class::topLevelExtensionFunc, Class(), DefaultValues.TopLevelExtensionFunc), |
| 128 | + Class().let { |
| 129 | + Arguments.of( |
| 130 | + it::topLevelExtensionFuncFromInstance, null, DefaultValues.TopLevelExtensionFuncFromInstance |
| 131 | + ) |
| 132 | + }, |
| 133 | + Class().let { |
| 134 | + Arguments.of( |
| 135 | + it::topLevelExtensionFuncFromInstanceWithInstance, |
| 136 | + it, |
| 137 | + DefaultValues.TopLevelExtensionFuncFromInstanceWithInstance |
| 138 | + ) |
| 139 | + } |
| 140 | + ).stream() |
| 141 | + } |
| 142 | +} |
0 commit comments