From d9c6951f603adc4ec8f3d43838d9e6d035da691a Mon Sep 17 00:00:00 2001 From: Haruhiko Takada Date: Mon, 12 Aug 2024 22:18:53 +0900 Subject: [PATCH 1/4] =?UTF-8?q?=E3=82=A4=E3=83=99=E3=83=B3=E3=83=88?= =?UTF-8?q?=E5=90=8D=E9=95=B7=E3=81=95=E3=80=81=E3=82=A4=E3=83=99=E3=83=B3?= =?UTF-8?q?=E3=83=88=E7=A8=AE=E9=A1=9E=E6=95=B0=E3=83=81=E3=82=A7=E3=83=83?= =?UTF-8?q?=E3=82=AF=E3=82=92=E4=BD=9C=E6=88=90?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../kotlin/com/tfandkusu/ga913yaml/Main.kt | 3 +- .../com/tfandkusu/ga913yaml/Validator.kt | 34 ++++++ .../com/tfandkusu/ga913yaml/ValidatorTest.kt | 115 ++++++++++++++++++ 3 files changed, 151 insertions(+), 1 deletion(-) create mode 100644 src/main/kotlin/com/tfandkusu/ga913yaml/Validator.kt create mode 100644 src/test/kotlin/com/tfandkusu/ga913yaml/ValidatorTest.kt diff --git a/src/main/kotlin/com/tfandkusu/ga913yaml/Main.kt b/src/main/kotlin/com/tfandkusu/ga913yaml/Main.kt index 5dcc950..ab8d8ff 100644 --- a/src/main/kotlin/com/tfandkusu/ga913yaml/Main.kt +++ b/src/main/kotlin/com/tfandkusu/ga913yaml/Main.kt @@ -9,7 +9,8 @@ fun main(args: Array) { val command = args[0] when (command) { "validate" -> { - YamlParser.parse() + val screens = YamlParser.parse() + Validator.validate(screens) } "make" -> { val screens = YamlParser.parse() diff --git a/src/main/kotlin/com/tfandkusu/ga913yaml/Validator.kt b/src/main/kotlin/com/tfandkusu/ga913yaml/Validator.kt new file mode 100644 index 0000000..75fbabc --- /dev/null +++ b/src/main/kotlin/com/tfandkusu/ga913yaml/Validator.kt @@ -0,0 +1,34 @@ +package com.tfandkusu.ga913yaml + +import com.tfandkusu.ga913yaml.model.Screen + +object Validator { + fun validate(screens: List) { + checkEventNameLength(screens) + } + + private fun checkEventNameLength(screens: List) { + var eventCount = 0 + for (screen in screens) { + if (screen.eventName.length > 40) { + throw IllegalArgumentException("画面遷移イベント " + screen.className + " は40文字を超えています。") + } + if (screen.isConversionEvent) { + eventCount++ + } + for (action in screen.actions) { + if (screen.eventName.length + action.eventName.length > 40) { + throw IllegalArgumentException( + "画面内操作イベント " + screen.className + "." + action.className + " は40文字を超えています。", + ) + } + if (action.isConversionEvent) { + eventCount++ + } + } + } + if (eventCount > 500) { + throw IllegalArgumentException("イベント種類数が500を超えています。") + } + } +} diff --git a/src/test/kotlin/com/tfandkusu/ga913yaml/ValidatorTest.kt b/src/test/kotlin/com/tfandkusu/ga913yaml/ValidatorTest.kt new file mode 100644 index 0000000..fbc0292 --- /dev/null +++ b/src/test/kotlin/com/tfandkusu/ga913yaml/ValidatorTest.kt @@ -0,0 +1,115 @@ +package com.tfandkusu.ga913yaml + +import com.tfandkusu.ga913yaml.model.Action +import com.tfandkusu.ga913yaml.model.Screen +import org.junit.Assert.assertEquals +import org.junit.Assert.fail +import org.junit.Test + +class ValidatorTest { + @Test + fun checkScreenEventNameLength() { + val screens = + listOf( + Screen(description = "40文字を超えない画面遷移イベント", className = "Screen1", eventName = "a".repeat(40)), + Screen(description = "40文字を超える画面遷移イベント", className = "Screen2", eventName = "b".repeat(41)), + ) + try { + Validator.validate(screens) + fail() + } catch (e: IllegalArgumentException) { + assertEquals("画面遷移イベント Screen2 は40文字を超えています。", e.message) + } + } + + @Test + fun checkActionEventNameLength() { + val screens = + listOf( + Screen( + description = "画面1", + className = "Screen1", + eventName = "a".repeat(20), + actions = + listOf( + Action( + description = "40文字を超えない画面内操作イベント", + className = "Action1", + eventName = "b".repeat(20), + ), + Action( + description = "40文字を超える画面内操作イベント", + className = "Action2", + eventName = "c".repeat(21), + ), + ), + ), + ) + try { + Validator.validate(screens) + fail() + } catch (e: IllegalArgumentException) { + assertEquals( + "画面内操作イベント Screen1.Action2 は40文字を超えています。", + e.message, + ) + } + } + + @Test + fun checkEventCountWithoutException() { + val screens = + (0 until 250).map { + Screen( + description = "画面$it", + className = "Screen$it", + eventName = "Scene$it", + actions = + listOf( + Action( + description = "画面内操作1", + className = "Action1", + eventName = "Action1", + isConversionEvent = true, + ), + ), + isConversionEvent = true, + ) + } + Validator.validate(screens) + } + + @Test + fun checkEventCountWithException() { + val screens = + (0 until 250).map { + Screen( + description = "画面$it", + className = "Screen$it", + eventName = "Scene$it", + actions = + listOf( + Action( + description = "画面内操作1", + className = "Action1", + eventName = "Action1", + isConversionEvent = true, + ), + ), + isConversionEvent = true, + ) + } + + Screen( + description = "画面250", + className = "Screen250", + eventName = "Scene250", + isConversionEvent = true, + ) + try { + Validator.validate(screens) + fail() + } catch (e: IllegalArgumentException) { + assertEquals("イベント種類数が500を超えています。", e.message) + } + } +} From 67a60b754d1674ea440d47df825e225104f6248e Mon Sep 17 00:00:00 2001 From: Haruhiko Takada Date: Mon, 12 Aug 2024 23:41:51 +0900 Subject: [PATCH 2/4] =?UTF-8?q?=E3=82=A4=E3=83=99=E3=83=B3=E3=83=88?= =?UTF-8?q?=E7=A8=AE=E9=A1=9E=E6=95=B0=E3=82=92=E5=88=A5=E9=96=A2=E6=95=B0?= =?UTF-8?q?=E3=81=AB=E3=81=99=E3=82=8B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../com/tfandkusu/ga913yaml/Validator.kt | 20 ++++++++++++------- 1 file changed, 13 insertions(+), 7 deletions(-) diff --git a/src/main/kotlin/com/tfandkusu/ga913yaml/Validator.kt b/src/main/kotlin/com/tfandkusu/ga913yaml/Validator.kt index 75fbabc..5253d84 100644 --- a/src/main/kotlin/com/tfandkusu/ga913yaml/Validator.kt +++ b/src/main/kotlin/com/tfandkusu/ga913yaml/Validator.kt @@ -5,28 +5,34 @@ import com.tfandkusu.ga913yaml.model.Screen object Validator { fun validate(screens: List) { checkEventNameLength(screens) + checkEventCount(screens) } + /** + * イベント名の長さが40文字を超えていないかチェックする。 + */ private fun checkEventNameLength(screens: List) { - var eventCount = 0 for (screen in screens) { if (screen.eventName.length > 40) { throw IllegalArgumentException("画面遷移イベント " + screen.className + " は40文字を超えています。") } - if (screen.isConversionEvent) { - eventCount++ - } for (action in screen.actions) { if (screen.eventName.length + action.eventName.length > 40) { throw IllegalArgumentException( "画面内操作イベント " + screen.className + "." + action.className + " は40文字を超えています。", ) } - if (action.isConversionEvent) { - eventCount++ - } } } + } + + /** + * イベント種類数が500を超えていないかチェックする。 + */ + private fun checkEventCount(screens: List) { + val eventCount = + screens.count { it.isConversionEvent } + + screens.flatMap { it.actions }.count { it.isConversionEvent } if (eventCount > 500) { throw IllegalArgumentException("イベント種類数が500を超えています。") } From 219d0d691becbad8e7d36f2bf54dda5f9a5faae4 Mon Sep 17 00:00:00 2001 From: Haruhiko Takada Date: Mon, 12 Aug 2024 23:57:12 +0900 Subject: [PATCH 3/4] =?UTF-8?q?=E3=82=A4=E3=83=99=E3=83=B3=E3=83=88?= =?UTF-8?q?=E3=83=91=E3=83=A9=E3=83=A1=E3=83=BC=E3=82=BF=E9=95=B7=E3=81=95?= =?UTF-8?q?=E3=83=81=E3=82=A7=E3=83=83=E3=82=AF?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../com/tfandkusu/ga913yaml/Validator.kt | 22 ++++++- .../com/tfandkusu/ga913yaml/ValidatorTest.kt | 58 ++++++++++++++++++- 2 files changed, 76 insertions(+), 4 deletions(-) diff --git a/src/main/kotlin/com/tfandkusu/ga913yaml/Validator.kt b/src/main/kotlin/com/tfandkusu/ga913yaml/Validator.kt index 5253d84..0cc19a9 100644 --- a/src/main/kotlin/com/tfandkusu/ga913yaml/Validator.kt +++ b/src/main/kotlin/com/tfandkusu/ga913yaml/Validator.kt @@ -5,6 +5,7 @@ import com.tfandkusu.ga913yaml.model.Screen object Validator { fun validate(screens: List) { checkEventNameLength(screens) + checkEventParameterKeyLength(screens) checkEventCount(screens) } @@ -26,13 +27,30 @@ object Validator { } } + /** + * イベントパラメータキーの長さが40文字を超えていないかチェックする。 + */ + private fun checkEventParameterKeyLength(screens: List) { + for (screen in screens) { + for (action in screen.actions) { + for (parameter in action.parameters) { + if (parameter.eventParameterKey.length > 40) { + throw IllegalArgumentException( + "画面内操作イベント " + screen.className + "." + action.className + " のパラメータキー " + + parameter.propertyName + " は40文字を超えています。", + ) + } + } + } + } + } + /** * イベント種類数が500を超えていないかチェックする。 */ private fun checkEventCount(screens: List) { val eventCount = - screens.count { it.isConversionEvent } + - screens.flatMap { it.actions }.count { it.isConversionEvent } + screens.count { it.isConversionEvent } + screens.flatMap { it.actions }.count { it.isConversionEvent } if (eventCount > 500) { throw IllegalArgumentException("イベント種類数が500を超えています。") } diff --git a/src/test/kotlin/com/tfandkusu/ga913yaml/ValidatorTest.kt b/src/test/kotlin/com/tfandkusu/ga913yaml/ValidatorTest.kt index fbc0292..02c59aa 100644 --- a/src/test/kotlin/com/tfandkusu/ga913yaml/ValidatorTest.kt +++ b/src/test/kotlin/com/tfandkusu/ga913yaml/ValidatorTest.kt @@ -1,6 +1,8 @@ package com.tfandkusu.ga913yaml import com.tfandkusu.ga913yaml.model.Action +import com.tfandkusu.ga913yaml.model.Parameter +import com.tfandkusu.ga913yaml.model.ParameterType import com.tfandkusu.ga913yaml.model.Screen import org.junit.Assert.assertEquals import org.junit.Assert.fail @@ -11,8 +13,16 @@ class ValidatorTest { fun checkScreenEventNameLength() { val screens = listOf( - Screen(description = "40文字を超えない画面遷移イベント", className = "Screen1", eventName = "a".repeat(40)), - Screen(description = "40文字を超える画面遷移イベント", className = "Screen2", eventName = "b".repeat(41)), + Screen( + description = "40文字を超えない画面遷移イベント", + className = "Screen1", + eventName = "a".repeat(40), + ), + Screen( + description = "40文字を超える画面遷移イベント", + className = "Screen2", + eventName = "b".repeat(41), + ), ) try { Validator.validate(screens) @@ -56,6 +66,50 @@ class ValidatorTest { } } + @Test + fun checkEventParameterKeyLength() { + val screens = + listOf( + Screen( + description = "画面1", + className = "Screen1", + eventName = "Scene1", + actions = + listOf( + Action( + description = "画面内操作1", + className = "Action1", + eventName = "Action1", + parameters = + listOf( + Parameter( + description = "40文字を超えないパラメータキー", + propertyName = "param1", + eventParameterKey = "a".repeat(40), + type = ParameterType.STRING, + ), + Parameter( + description = "40文字を超えるパラメータキー", + propertyName = "param2", + eventParameterKey = "b".repeat(41), + type = ParameterType.STRING, + ), + ), + ), + ), + ), + ) + try { + Validator.validate(screens) + fail() + } catch (e: IllegalArgumentException) { + assertEquals( + "画面内操作イベント Screen1.Action1 のパラメータキー param2 は40文字を超えています。", + e.message, + ) + } + } + @Test fun checkEventCountWithoutException() { val screens = From a80e2d58dd6ddd160b3c872b71941b8bd4fd842c Mon Sep 17 00:00:00 2001 From: Haruhiko Takada Date: Tue, 13 Aug 2024 00:06:00 +0900 Subject: [PATCH 4/4] =?UTF-8?q?=E3=83=90=E3=83=AA=E3=83=87=E3=83=BC?= =?UTF-8?q?=E3=82=B7=E3=83=A7=E3=83=B3=E3=82=92=E4=BD=9C=E6=88=90?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../com/tfandkusu/ga913yaml/Validator.kt | 19 ++++- .../com/tfandkusu/ga913yaml/ValidatorTest.kt | 85 +++++++++++++++++++ 2 files changed, 103 insertions(+), 1 deletion(-) diff --git a/src/main/kotlin/com/tfandkusu/ga913yaml/Validator.kt b/src/main/kotlin/com/tfandkusu/ga913yaml/Validator.kt index 0cc19a9..a49aefe 100644 --- a/src/main/kotlin/com/tfandkusu/ga913yaml/Validator.kt +++ b/src/main/kotlin/com/tfandkusu/ga913yaml/Validator.kt @@ -7,6 +7,7 @@ object Validator { checkEventNameLength(screens) checkEventParameterKeyLength(screens) checkEventCount(screens) + checkDuplicateEventName(screens) } /** @@ -37,7 +38,7 @@ object Validator { if (parameter.eventParameterKey.length > 40) { throw IllegalArgumentException( "画面内操作イベント " + screen.className + "." + action.className + " のパラメータキー " + - parameter.propertyName + " は40文字を超えています。", + parameter.propertyName + " は40文字を超えています。", ) } } @@ -55,4 +56,20 @@ object Validator { throw IllegalArgumentException("イベント種類数が500を超えています。") } } + + private fun checkDuplicateEventName(screens: List) { + val eventNames = mutableSetOf() + for (screen in screens) { + if (!eventNames.add(screen.eventName)) { + throw IllegalArgumentException("画面遷移イベント " + screen.className + " は他のイベントと重複しています。") + } + for (action in screen.actions) { + if (!eventNames.add(screen.eventName + action.eventName)) { + throw IllegalArgumentException( + "画面内操作イベント " + screen.className + "." + action.className + " は他のイベントと重複しています。", + ) + } + } + } + } } diff --git a/src/test/kotlin/com/tfandkusu/ga913yaml/ValidatorTest.kt b/src/test/kotlin/com/tfandkusu/ga913yaml/ValidatorTest.kt index 02c59aa..956d8f5 100644 --- a/src/test/kotlin/com/tfandkusu/ga913yaml/ValidatorTest.kt +++ b/src/test/kotlin/com/tfandkusu/ga913yaml/ValidatorTest.kt @@ -166,4 +166,89 @@ class ValidatorTest { assertEquals("イベント種類数が500を超えています。", e.message) } } + + @Test + fun checkScreenEventNameDuplicate() { + val screens = + listOf( + Screen( + description = "画面1", + className = "Screen1", + eventName = "Screen1", + ), + Screen( + description = "画面2", + className = "Screen2", + eventName = "Screen1", + ), + ) + try { + Validator.validate(screens) + fail() + } catch (e: IllegalArgumentException) { + assertEquals("画面遷移イベント Screen2 は他のイベントと重複しています。", e.message) + } + } + + @Test + fun checkActionEventNameDuplicate() { + val screens = + listOf( + Screen( + description = "画面1", + className = "Screen1", + eventName = "Screen1", + actions = + listOf( + Action( + description = "画面内操作1", + className = "Action1", + eventName = "Action1", + ), + Action( + description = "画面内操作2", + className = "Action2", + eventName = "Action1", + ), + ), + ), + ) + try { + Validator.validate(screens) + fail() + } catch (e: IllegalArgumentException) { + assertEquals("画面内操作イベント Screen1.Action2 は他のイベントと重複しています。", e.message) + } + } + + @Test + fun checkJoinedEventNameDuplicate() { + val screens = + listOf( + Screen( + description = "画面1", + className = "Screen1", + eventName = "Screen1", + actions = + listOf( + Action( + description = "画面内操作1", + className = "Action1", + eventName = "Action1", + ), + ), + ), + Screen( + description = "画面2", + className = "Screen2", + eventName = "Screen1Action1", + ), + ) + try { + Validator.validate(screens) + fail() + } catch (e: IllegalArgumentException) { + assertEquals("画面遷移イベント Screen2 は他のイベントと重複しています。", e.message) + } + } }