Skip to content
This repository was archived by the owner on Jan 20, 2023. It is now read-only.

Commit f4ed366

Browse files
authored
Merge pull request #21 from k163377/add_tests
Minor revisions and test additions.
2 parents ccab06d + 10feb3b commit f4ed366

File tree

10 files changed

+209
-28
lines changed

10 files changed

+209
-28
lines changed

build.gradle.kts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ plugins {
66
}
77

88
group = "com.mapk"
9-
version = "0.11"
9+
version = "0.12"
1010

1111
java {
1212
sourceCompatibility = JavaVersion.VERSION_1_8

src/main/kotlin/com/mapk/core/ArgumentAdaptor.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ class ArgumentAdaptor(private val requiredParameters: Map<String, ValueParameter
1313
}
1414

1515
fun putIfAbsent(key: String, value: Any?) {
16-
if (!isInitialized(key) && !(requiredParameters.getValue(key).isNullable && value == null)) {
16+
if (!isInitialized(key) && !(!requiredParameters.getValue(key).isNullable && value == null)) {
1717
argumentMap[key] = value
1818
}
1919
}

src/main/kotlin/com/mapk/core/internal/ArgumentBinder.kt

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,7 @@ internal sealed class ArgumentBinder(val annotations: List<Annotation>) {
2222
override val isOptional: Boolean,
2323
override val name: String,
2424
override val requiredClazz: KClass<T>
25-
) : ArgumentBinder(annotations),
26-
ValueParameter<T> {
25+
) : ArgumentBinder(annotations), ValueParameter<T> {
2726
override fun bindArgument(adaptor: ArgumentAdaptor, valueArray: Array<Any?>): Boolean {
2827
return if (adaptor.isInitialized(name)) {
2928
valueArray[index] = adaptor.readout(name)

src/main/kotlin/com/mapk/core/internal/ArgumentBucket.kt

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,7 @@ internal class ArgumentBucket(
1717
override var value: Any?
1818
) : Map.Entry<KParameter, Any?>
1919

20-
private val initializationStatusManager =
21-
InitializationStatusManager(initializationStatus)
20+
private val initializationStatusManager = InitializationStatusManager(initializationStatus)
2221
val isInitialized: Boolean
2322
override val size: Int
2423

@@ -32,9 +31,7 @@ internal class ArgumentBucket(
3231
size = initializationStatusManager.count
3332
}
3433

35-
override fun containsKey(key: KParameter): Boolean {
36-
return initializationStatusManager.isInitialized(key.index)
37-
}
34+
override fun containsKey(key: KParameter): Boolean = initializationStatusManager.isInitialized(key.index)
3835

3936
override fun containsValue(value: Any?): Boolean = valueArray.any { Objects.equals(value, it) }
4037

src/main/kotlin/com/mapk/core/internal/BucketGenerator.kt

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -36,20 +36,20 @@ internal class BucketGenerator(
3636
}
3737

3838
fun generate(adaptor: ArgumentAdaptor): ArgumentBucket =
39-
ArgumentBucket(
40-
parameters, originalValueArray.clone(), originalInitializationStatus.clone(), binders, adaptor
41-
)
39+
ArgumentBucket(parameters, originalValueArray.clone(), originalInitializationStatus.clone(), binders, adaptor)
4240
}
4341

4442
private fun KParameter.toArgumentBinder(parameterNameConverter: ParameterNameConverter): ArgumentBinder {
4543
val name = getAliasOrName()!!
4644

4745
return findAnnotation<KParameterFlatten>()?.let { annotation ->
48-
// 名前の変換処理、結合が必要な場合はインスタンスを持ってきて対応する
46+
// 名前の変換処理
4947
val converter: ParameterNameConverter = if (annotation.fieldNameToPrefix) {
48+
// 結合が必要な場合は結合機能のインスタンスを持ってきて対応する
5049
parameterNameConverter.nest(name, annotation.nameJoiner.objectInstance!!)
5150
} else {
52-
parameterNameConverter
51+
// プレフィックスを要求しない場合は全てsimpleでマップするように修正
52+
parameterNameConverter.toSimple()
5353
}
5454

5555
ArgumentBinder.Function((type.classifier as KClass<*>).toKConstructor(converter), index, annotations)

src/main/kotlin/com/mapk/core/internal/Functions.kt

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import com.mapk.annotations.KUseDefaultArgument
55
import java.lang.IllegalArgumentException
66
import kotlin.reflect.KParameter
77
import kotlin.reflect.full.findAnnotation
8+
import kotlin.reflect.jvm.jvmName
89

910
/**
1011
* パラメータからエイリアスもしくはプロパティ名を取得する関数
@@ -16,9 +17,9 @@ internal fun KParameter.getAliasOrName(): String? = findAnnotation<KParameterAli
1617
*/
1718
internal fun KParameter.isUseDefaultArgument(): Boolean {
1819
if (annotations.any { it is KUseDefaultArgument }) {
19-
if (!isOptional) {
20-
throw IllegalArgumentException("Find KUseDefaultArgument, but it's not has default argument.")
21-
}
20+
if (!isOptional) throw IllegalArgumentException(
21+
"Find ${KUseDefaultArgument::class.jvmName}, but it's not has default argument."
22+
)
2223
return true
2324
}
2425
return false

src/main/kotlin/com/mapk/core/internal/ParameterNameConverter.kt

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,12 @@ internal sealed class ParameterNameConverter {
66
protected abstract val converter: (String) -> String
77
abstract fun convert(name: String): String
88
abstract fun nest(infix: String, nameJoiner: NameJoiner): WithPrefix
9+
abstract fun toSimple(): Simple
910

1011
class Simple(override val converter: (String) -> String) : ParameterNameConverter() {
1112
override fun convert(name: String) = converter(name)
12-
override fun nest(infix: String, nameJoiner: NameJoiner) =
13-
WithPrefix(infix, nameJoiner, converter)
13+
override fun nest(infix: String, nameJoiner: NameJoiner) = WithPrefix(infix, nameJoiner, converter)
14+
override fun toSimple(): Simple = this
1415
}
1516

1617
class WithPrefix(
@@ -22,11 +23,7 @@ internal sealed class ParameterNameConverter {
2223

2324
// 結合を伴う変換では、「双方変換 -> 結合」の順で処理を行う
2425
override fun convert(name: String) = converter(name).let { nameJoiner.join(prefix, it) }
25-
override fun nest(infix: String, nameJoiner: NameJoiner) =
26-
WithPrefix(
27-
convert(infix),
28-
nameJoiner,
29-
converter
30-
)
26+
override fun nest(infix: String, nameJoiner: NameJoiner) = WithPrefix(convert(infix), nameJoiner, converter)
27+
override fun toSimple(): Simple = Simple(converter)
3128
}
3229
}
Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
package com.mapk.core
2+
3+
import io.mockk.every
4+
import io.mockk.mockk
5+
import org.junit.jupiter.api.Assertions.assertEquals
6+
import org.junit.jupiter.api.Assertions.assertFalse
7+
import org.junit.jupiter.api.Assertions.assertTrue
8+
import org.junit.jupiter.api.DisplayName
9+
import org.junit.jupiter.api.Nested
10+
import org.junit.jupiter.api.Test
11+
import org.junit.jupiter.api.assertThrows
12+
13+
@DisplayName("ArgumentAdaptorのテスト")
14+
class ArgumentAdaptorTest {
15+
private val keys = listOf("foo", "bar", "baz")
16+
private val adaptor: ArgumentAdaptor = keys.mapIndexed { i, argName ->
17+
mockk<ValueParameter<*>> {
18+
every { name } returns argName
19+
every { isNullable } returns (i % 2 == 0)
20+
}
21+
}.associateBy {
22+
it.name
23+
}.let { ArgumentAdaptor(it) }
24+
25+
@Nested
26+
@DisplayName("値をバインドするテスト")
27+
inner class PutIfAbsentTest {
28+
@Nested
29+
@DisplayName("Null入力")
30+
inner class NullabilityTest {
31+
@Test
32+
@DisplayName("nullableかつnull入力")
33+
fun isNullableAndNull() {
34+
adaptor.putIfAbsent(keys[0], null)
35+
assertTrue(adaptor.isInitialized(keys[0]))
36+
}
37+
38+
@Test
39+
@DisplayName("non-nullかつnull入力")
40+
fun isNonNullAndNull() {
41+
adaptor.putIfAbsent(keys[1], null)
42+
assertFalse(adaptor.isInitialized(keys[0]))
43+
}
44+
45+
@Test
46+
@DisplayName("2重バインドテスト")
47+
fun isDuplicateKey() {
48+
adaptor.putIfAbsent(keys[0], keys[0])
49+
adaptor.putIfAbsent(keys[0], keys[1])
50+
assertEquals(keys[0], adaptor.readout(keys[0]))
51+
}
52+
}
53+
}
54+
55+
@Nested
56+
@DisplayName("完全初期化チェックのテスト")
57+
inner class IsFullInitializedTest {
58+
@Test
59+
@DisplayName("完全初期化していない場合")
60+
fun isNotFullInitialized() {
61+
adaptor.putIfAbsent(keys[0], keys[0])
62+
adaptor.putIfAbsent(keys[1], keys[1])
63+
assertFalse(adaptor.isFullInitialized())
64+
}
65+
66+
@Test
67+
@DisplayName("完全初期化した場合")
68+
fun isFullInitialized() {
69+
keys.forEach { adaptor.putIfAbsent(it, it) }
70+
assertTrue(adaptor.isFullInitialized())
71+
}
72+
}
73+
74+
@Nested
75+
@DisplayName("存在しないkeyの読み出しテスト")
76+
inner class NotContainsKeyTest {
77+
@Test
78+
@DisplayName("isInitializedのテスト")
79+
fun isInitialized() {
80+
assertThrows<IllegalArgumentException> { adaptor.isInitialized("hoge") }
81+
}
82+
83+
@Test
84+
@DisplayName("putIfAbsentのテスト")
85+
fun putIfAbsent() {
86+
assertThrows<IllegalArgumentException> { adaptor.putIfAbsent("hoge", "hoge") }
87+
}
88+
}
89+
90+
@Test
91+
@DisplayName("読み出しテスト")
92+
fun readoutTest() {
93+
keys.forEach { adaptor.putIfAbsent(it, it) }
94+
keys.forEach { assertEquals(it, adaptor.readout(it)) }
95+
}
96+
}

src/test/kotlin/com/mapk/core/KParameterFlattenTest.kt

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,8 @@ import org.junit.jupiter.api.Test
1111

1212
@DisplayName("パラメータのフラット化テスト")
1313
class KParameterFlattenTest {
14-
data class InnerDst1(val quxQux: Int)
14+
data class InnerDst1(val quxQux: Int, @KParameterFlatten(fieldNameToPrefix = false) val fredFred: InnerInnerDst)
15+
data class InnerInnerDst(val waldoWaldo: Int)
1516
data class InnerDst2(val quuxQuux: Int)
1617
data class InnerDst3(val graultGrault: String) {
1718
@KConstructor
@@ -31,8 +32,14 @@ class KParameterFlattenTest {
3132

3233
companion object {
3334
val expectedParams: Set<String> =
34-
linkedSetOf("fooFoo", "barBar", "bazBazQuxQux", "quuxQuux", "garplyGarply-graultGrault")
35-
val expected: Dst = Dst(0, 1, InnerDst1(2), InnerDst2(3), InnerDst3("4"))
35+
linkedSetOf("fooFoo", "barBar", "bazBazQuxQux", "waldoWaldo", "quuxQuux", "garplyGarply-graultGrault")
36+
val expected: Dst = Dst(
37+
0,
38+
1,
39+
InnerDst1(2, InnerInnerDst(3)),
40+
InnerDst2(4),
41+
InnerDst3("5")
42+
)
3643
}
3744

3845
@Test
Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
package com.mapk.core.internal
2+
3+
import com.mapk.core.NameJoiner
4+
import org.junit.jupiter.api.Assertions.assertEquals
5+
import org.junit.jupiter.api.DisplayName
6+
import org.junit.jupiter.api.Nested
7+
import org.junit.jupiter.api.Test
8+
9+
@DisplayName("パラメータ名変換関連のテスト")
10+
class ParameterNameConverterTest {
11+
@Nested
12+
@DisplayName("シンプルな変換機能のテスト")
13+
inner class SimpleTest {
14+
private val simple = ParameterNameConverter.Simple { it.toLowerCase() }
15+
16+
@Test
17+
@DisplayName("単純な変換テスト")
18+
fun convertTest() {
19+
val expected = "abcdef"
20+
val actual = simple.convert("AbCdEf")
21+
assertEquals(expected, actual)
22+
}
23+
24+
@Test
25+
@DisplayName("ネストしたインスタンスを作るテスト")
26+
fun nestTest() {
27+
val nested1 = simple.nest("AbCdEf", NameJoiner.Kebab)
28+
run {
29+
val expected = "abcdef-ghijkl"
30+
val actual = nested1.convert("gHiJkL")
31+
assertEquals(expected, actual)
32+
}
33+
34+
val nested2 = nested1.nest("gHiJkL", NameJoiner.Snake)
35+
run {
36+
val expected = "abcdef-ghijkl_mnopqr"
37+
val actual = nested2.convert("MNopQR")
38+
assertEquals(expected, actual)
39+
}
40+
}
41+
42+
@Test
43+
@DisplayName("Simpleにした場合のテスト")
44+
fun simple() {
45+
val simple2 = simple.toSimple()
46+
assertEquals(simple, simple2)
47+
}
48+
}
49+
50+
@Nested
51+
@DisplayName("プレフィックス付き変換機能のテスト")
52+
inner class WithPrefixTest {
53+
private val withPrefix =
54+
ParameterNameConverter.WithPrefix("abcdef", NameJoiner.Snake) { it.toUpperCase() }
55+
56+
@Test
57+
@DisplayName("単純な変換テスト")
58+
fun convertTest() {
59+
val expected = "ABCDEF_GHIJKL"
60+
val actual = withPrefix.convert("GhIJkL")
61+
assertEquals(expected, actual)
62+
}
63+
64+
@Test
65+
@DisplayName("ネストしたインスタンスを作るテスト")
66+
fun nestTest() {
67+
val nested = withPrefix.nest("GhIJkL", NameJoiner.Kebab)
68+
run {
69+
val expected = "ABCDEF_GHIJKL-MNOPQR"
70+
val actual = nested.convert("mnOpQr")
71+
assertEquals(expected, actual)
72+
}
73+
}
74+
75+
@Test
76+
@DisplayName("Simpleにした場合のテスト")
77+
fun simple() {
78+
val simple = withPrefix.toSimple()
79+
val expected = "ABCDEF"
80+
val actual = simple.convert(expected)
81+
assertEquals(expected, actual)
82+
}
83+
}
84+
}

0 commit comments

Comments
 (0)