|
| 1 | +package com.mapk.core |
| 2 | + |
| 3 | +import com.mapk.annotations.KConstructor |
| 4 | +import kotlin.reflect.full.companionObjectInstance |
| 5 | +import org.junit.jupiter.api.Assertions.assertEquals |
| 6 | +import org.junit.jupiter.api.Assertions.assertNull |
| 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 | + |
| 12 | +@DisplayName("共通利用関数関連のテスト") |
| 13 | +class FunctionsTest { |
| 14 | + class NoCompanionObject |
| 15 | + |
| 16 | + @Target(AnnotationTarget.FUNCTION) |
| 17 | + @Retention(AnnotationRetention.RUNTIME) |
| 18 | + @MustBeDocumented |
| 19 | + annotation class TestAnnotation1 |
| 20 | + |
| 21 | + @Target(AnnotationTarget.FUNCTION) |
| 22 | + @Retention(AnnotationRetention.RUNTIME) |
| 23 | + @MustBeDocumented |
| 24 | + annotation class TestAnnotation2 |
| 25 | + |
| 26 | + class WithCompanionObject { |
| 27 | + companion object { |
| 28 | + @KConstructor |
| 29 | + fun kConstructor() {} |
| 30 | + |
| 31 | + @TestAnnotation2 |
| 32 | + fun testAnnotation1() {} |
| 33 | + |
| 34 | + @TestAnnotation2 |
| 35 | + fun testAnnotation2() {} |
| 36 | + } |
| 37 | + } |
| 38 | + |
| 39 | + @Nested |
| 40 | + @DisplayName("コンパニオンオブジェクトから指定したアノテーションを取得するテスト") |
| 41 | + inner class GetAnnotatedFunctionsFromCompanionObject { |
| 42 | + @Test |
| 43 | + @DisplayName("コンパニオンオブジェクトが無い場合") |
| 44 | + fun noCompanionObject() { |
| 45 | + assertNull(NoCompanionObject::class.getAnnotatedFunctionsFromCompanionObject<KConstructor>()) |
| 46 | + } |
| 47 | + |
| 48 | + @Test |
| 49 | + @DisplayName("コンパニオンオブジェクトが有るが関数が取れない場合") |
| 50 | + fun withCompanionObjectButNotFound() { |
| 51 | + assertNull(WithCompanionObject::class.getAnnotatedFunctionsFromCompanionObject<TestAnnotation1>()) |
| 52 | + } |
| 53 | + |
| 54 | + @Test |
| 55 | + @DisplayName("単体取得") |
| 56 | + fun kConstructor() { |
| 57 | + val result = |
| 58 | + WithCompanionObject::class.getAnnotatedFunctionsFromCompanionObject<KConstructor>()!! |
| 59 | + |
| 60 | + assertEquals(WithCompanionObject::class.companionObjectInstance!!, result.first) |
| 61 | + assertEquals(1, result.second.size) |
| 62 | + assertEquals("kConstructor", result.second.single().name) |
| 63 | + } |
| 64 | + |
| 65 | + @Test |
| 66 | + @DisplayName("複数取得") |
| 67 | + fun testAnnotation() { |
| 68 | + val result = |
| 69 | + WithCompanionObject::class.getAnnotatedFunctionsFromCompanionObject<TestAnnotation2>()!! |
| 70 | + |
| 71 | + assertEquals(WithCompanionObject::class.companionObjectInstance!!, result.first) |
| 72 | + assertEquals(2, result.second.size) |
| 73 | + val names = listOf("testAnnotation1", "testAnnotation2") |
| 74 | + assertTrue(names.contains(result.second[0].name)) |
| 75 | + assertTrue(names.contains(result.second[1].name)) |
| 76 | + } |
| 77 | + } |
| 78 | + |
| 79 | + data class InnerClass(val arg: Int) |
| 80 | + data class DataClass(val innerClass: InnerClass) |
| 81 | + |
| 82 | + @Test |
| 83 | + @DisplayName("パラメータからの型取得テスト") |
| 84 | + fun getKClassTest() { |
| 85 | + val param = ::DataClass.parameters.single() |
| 86 | + assertEquals(InnerClass::class, param.getKClass()) |
| 87 | + } |
| 88 | +} |
0 commit comments