|
1 | 1 | package com.mapk.core |
2 | 2 |
|
| 3 | +import com.mapk.annotations.KParameterRequireNonNull |
3 | 4 | import org.junit.jupiter.api.Assertions.assertEquals |
4 | 5 | import org.junit.jupiter.api.Assertions.assertFalse |
| 6 | +import org.junit.jupiter.api.Assertions.assertNull |
5 | 7 | import org.junit.jupiter.api.Assertions.assertTrue |
6 | 8 | import org.junit.jupiter.api.BeforeEach |
7 | 9 | import org.junit.jupiter.api.DisplayName |
8 | 10 | import org.junit.jupiter.api.Nested |
9 | 11 | import org.junit.jupiter.api.Test |
| 12 | +import org.junit.jupiter.api.assertDoesNotThrow |
| 13 | +import org.junit.jupiter.api.assertThrows |
10 | 14 |
|
11 | 15 | private fun sampleFunction(arg1: Any?, arg2: Any?, arg3: Any?) { |
12 | 16 | println(arg1) |
13 | 17 | println(arg2) |
14 | 18 | println(arg3) |
15 | 19 | } |
16 | 20 |
|
| 21 | +private fun sampleAnnotatedFunction(@KParameterRequireNonNull arg1: Any, arg2: Any?) { |
| 22 | + println(arg1) |
| 23 | + println(arg2) |
| 24 | +} |
| 25 | + |
17 | 26 | @DisplayName("ArgumentBucketTestのテスト") |
18 | 27 | class ArgumentBucketTest { |
19 | | - private lateinit var argumentBucket: ArgumentBucket |
| 28 | + @Nested |
| 29 | + @DisplayName("シンプルな呼び出しのテスト") |
| 30 | + inner class SimpleTest { |
| 31 | + private lateinit var argumentBucket: ArgumentBucket |
20 | 32 |
|
21 | | - @BeforeEach |
22 | | - fun beforeEach() { |
23 | | - argumentBucket = KFunctionForCall(::sampleFunction).getArgumentBucket() |
24 | | - } |
| 33 | + @BeforeEach |
| 34 | + fun beforeEach() { |
| 35 | + argumentBucket = KFunctionForCall(::sampleFunction).getArgumentBucket() |
| 36 | + } |
25 | 37 |
|
26 | | - @Nested |
27 | | - @DisplayName("初期化状態のチェックテスト") |
28 | | - inner class IsInitializedTest { |
29 | | - @Test |
30 | | - @DisplayName("初期化前") |
31 | | - fun isNotInitialized() { |
32 | | - assertFalse(argumentBucket.isInitialized) |
| 38 | + @Nested |
| 39 | + @DisplayName("初期化状態のチェックテスト") |
| 40 | + inner class IsInitializedTest { |
| 41 | + @Test |
| 42 | + @DisplayName("初期化前") |
| 43 | + fun isNotInitialized() { |
| 44 | + assertFalse(argumentBucket.isInitialized) |
| 45 | + } |
| 46 | + |
| 47 | + @Test |
| 48 | + @DisplayName("初期化後") |
| 49 | + fun isInitialized() { |
| 50 | + ::sampleFunction.parameters.forEach { |
| 51 | + argumentBucket.putIfAbsent(it, object {}) |
| 52 | + } |
| 53 | + |
| 54 | + assertTrue(argumentBucket.isInitialized) |
| 55 | + } |
33 | 56 | } |
34 | 57 |
|
35 | | - @Test |
36 | | - @DisplayName("初期化後") |
37 | | - fun isInitialized() { |
38 | | - ::sampleFunction.parameters.forEach { |
39 | | - argumentBucket.putIfAbsent(it, object {}) |
| 58 | + @Nested |
| 59 | + @DisplayName("引数セットのテスト") |
| 60 | + inner class SetArgumentTest { |
| 61 | + @Test |
| 62 | + @DisplayName("正常に追加した場合") |
| 63 | + fun setNewArgument() { |
| 64 | + val parameter = ::sampleFunction.parameters.first { it.index == 0 } |
| 65 | + argumentBucket.putIfAbsent(parameter, "argument") |
| 66 | + assertEquals("argument", argumentBucket.getByIndex(0)) |
40 | 67 | } |
41 | 68 |
|
42 | | - assertTrue(argumentBucket.isInitialized) |
| 69 | + @Test |
| 70 | + @DisplayName("同じインデックスに2回追加した場合") |
| 71 | + fun setArgumentTwice() { |
| 72 | + val parameter = ::sampleFunction.parameters.first { it.index == 0 } |
| 73 | + |
| 74 | + argumentBucket.putIfAbsent(parameter, "first") |
| 75 | + argumentBucket.putIfAbsent(parameter, "second") |
| 76 | + assertEquals("first", argumentBucket.getByIndex(0)) |
| 77 | + } |
43 | 78 | } |
44 | 79 | } |
45 | 80 |
|
46 | 81 | @Nested |
47 | | - @DisplayName("引数セットのテスト") |
48 | | - inner class SetArgumentTest { |
| 82 | + @DisplayName("アノテーションを付与した場合のテスト") |
| 83 | + inner class AnnotatedParametersTest { |
49 | 84 | @Test |
50 | | - @DisplayName("正常に追加した場合") |
51 | | - fun setNewArgument() { |
52 | | - val parameter = ::sampleFunction.parameters.first { it.index == 0 } |
53 | | - argumentBucket.putIfAbsent(parameter, "argument") |
54 | | - assertEquals("argument", argumentBucket.getByIndex(0)) |
55 | | - } |
| 85 | + @DisplayName("non-null要求のテスト") |
| 86 | + fun isRequireNonNull() { |
| 87 | + val forCall = KFunctionForCall(::sampleAnnotatedFunction) |
| 88 | + val argumentBucket = forCall.getArgumentBucket() |
| 89 | + val parameters = forCall.parameters |
56 | 90 |
|
57 | | - @Test |
58 | | - @DisplayName("同じインデックスに2回追加した場合") |
59 | | - fun setArgumentTwice() { |
60 | | - val parameter = ::sampleFunction.parameters.first { it.index == 0 } |
| 91 | + argumentBucket.putIfAbsent(parameters[0], null) |
| 92 | + assertThrows<IllegalStateException> { argumentBucket.getByIndex(0) } |
61 | 93 |
|
62 | | - argumentBucket.putIfAbsent(parameter, "first") |
63 | | - argumentBucket.putIfAbsent(parameter, "second") |
64 | | - assertEquals("first", argumentBucket.getByIndex(0)) |
| 94 | + argumentBucket.putIfAbsent(parameters[0], "input") |
| 95 | + assertDoesNotThrow { |
| 96 | + assertEquals("input", argumentBucket.getByIndex(0)) |
| 97 | + } |
| 98 | + |
| 99 | + argumentBucket.putIfAbsent(parameters[1], null) |
| 100 | + assertDoesNotThrow { |
| 101 | + assertNull(argumentBucket.getByIndex(1)) |
| 102 | + } |
65 | 103 | } |
66 | 104 | } |
67 | 105 | } |
0 commit comments