|
| 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 | +} |
0 commit comments