From 3dfd61324af4e4bd1711fa609533d9beb70567c2 Mon Sep 17 00:00:00 2001 From: Alistair Sykes Date: Tue, 5 Nov 2019 14:55:54 +0000 Subject: [PATCH] master: Adjusted IdNaming to correct unnecessary Switch case. Added tests for the CheckBox case --- .../xmlcheck/rules/attr/android/IdNaming.kt | 9 +- .../uk/co/brightec/xmlcheck/EndToEndTest.kt | 13 +++ .../rules/attr/android/IdNamingTest.kt | 88 +++++++++++++++++++ .../failures/IdNaming/IdNaming-CheckBox.xml | 3 + .../success/IdNaming/IdNaming-CheckBox.xml | 3 + 5 files changed, 110 insertions(+), 6 deletions(-) create mode 100644 src/test/resources/files/failures/IdNaming/IdNaming-CheckBox.xml create mode 100644 src/test/resources/files/success/IdNaming/IdNaming-CheckBox.xml diff --git a/src/main/kotlin/uk/co/brightec/xmlcheck/rules/attr/android/IdNaming.kt b/src/main/kotlin/uk/co/brightec/xmlcheck/rules/attr/android/IdNaming.kt index 3e8e12f..d84ca5b 100644 --- a/src/main/kotlin/uk/co/brightec/xmlcheck/rules/attr/android/IdNaming.kt +++ b/src/main/kotlin/uk/co/brightec/xmlcheck/rules/attr/android/IdNaming.kt @@ -50,17 +50,14 @@ class IdNaming : AttrRule( if (className == "RadioButton") { return attrId == "radio" || attrId.startsWith("radio_") } + if (className.contains("CheckBox")) { + return attrId == "check_box" || attrId.startsWith("check_box_") + } // General rules if (words.contains("Button")) { return attrId == "button" || attrId.startsWith("button_") } - if (words.contains("CheckBox")) { - return attrId == "check_box" || attrId.startsWith("check_box_") - } - if (words.contains("Switch")) { - return attrId == "switch" || attrId.startsWith("switch_") - } if (words.contains("Card")) { return attrId == "card" || attrId.startsWith("card_") } diff --git a/src/test/kotlin/uk/co/brightec/xmlcheck/EndToEndTest.kt b/src/test/kotlin/uk/co/brightec/xmlcheck/EndToEndTest.kt index c3afa23..d855b08 100644 --- a/src/test/kotlin/uk/co/brightec/xmlcheck/EndToEndTest.kt +++ b/src/test/kotlin/uk/co/brightec/xmlcheck/EndToEndTest.kt @@ -346,6 +346,10 @@ private class EndToEndTest { IdNaming-Words.xml PASSED """.trimIndent() assertContains(expectedLog5, outputOut) + val expectedLog6 = """ + IdNaming-CheckBox.xml PASSED + """.trimIndent() + assertContains(expectedLog6, outputOut) val outputErr = errContent.toString() assertEquals("", outputErr) } @@ -392,6 +396,11 @@ private class EndToEndTest { (IdNaming) Line:3: android:id="@+id/something" - Id for SomeOtherClassName doesn't conform to naming convention """.trimIndent() assertContains(expectedLog5, outputErr) + val expectedLog6 = """ + ./src/test/resources/files/failures/IdNaming/IdNaming-CheckBox.xml + (IdNaming) Line:3: android:id="@+id/something" - Id for CheckBox doesn't conform to naming convention + """.trimIndent() + assertContains(expectedLog6, outputErr) } @Test @@ -428,6 +437,10 @@ private class EndToEndTest { IdNaming-Words.xml PASSED """.trimIndent() assertContains(expectedLog5, outputOut) + val expectedLog6 = """ + IdNaming-CheckBox.xml PASSED + """.trimIndent() + assertContains(expectedLog6, outputOut) val outputErr = errContent.toString() assertEquals("", outputErr) } diff --git a/src/test/kotlin/uk/co/brightec/xmlcheck/rules/attr/android/IdNamingTest.kt b/src/test/kotlin/uk/co/brightec/xmlcheck/rules/attr/android/IdNamingTest.kt index c441898..2c1ce3a 100644 --- a/src/test/kotlin/uk/co/brightec/xmlcheck/rules/attr/android/IdNamingTest.kt +++ b/src/test/kotlin/uk/co/brightec/xmlcheck/rules/attr/android/IdNamingTest.kt @@ -422,6 +422,91 @@ private class IdNamingTest { assertNull(result) } + @ParameterizedTest + @MethodSource("createArgumentsCheckBox") + fun `Given tagName CheckBox and naming failure | When run check | Then failure`( + tagName: String, + parentTagName: String? + ) { + // GIVEN + val parentTagAttr = mockk { + every { value } returns parentTagName + } + val owner = mockk { + every { this@mockk.tagName } returns tagName + every { + attributes.getNamedItem("$ATTR_NAMESPACE_TOOLS:parentTag") + } returns parentTagAttr + } + val node = mockk { + every { value } returns "@+id/other" + every { ownerElement } returns owner + } + + // WHEN + val result = rule.run(node) + + // THEN + val expected = rule.failure(node, "Id for ${owner.tagName} doesn't conform to naming convention") + assertEquals(expected, result) + } + + @ParameterizedTest + @MethodSource("createArgumentsCheckBox") + fun `Given tagName CheckBox and naming exact correct | When run check | Then no failure`( + tagName: String, + parentTagName: String? + ) { + // GIVEN + val parentTagAttr = mockk { + every { value } returns parentTagName + } + val owner = mockk { + every { this@mockk.tagName } returns tagName + every { + attributes.getNamedItem("$ATTR_NAMESPACE_TOOLS:parentTag") + } returns parentTagAttr + } + val node = mockk { + every { value } returns "@+id/check_box" + every { ownerElement } returns owner + } + + // WHEN + val result = rule.run(node) + + // THEN + assertNull(result) + } + + @ParameterizedTest + @MethodSource("createArgumentsCheckBox") + fun `Given tagName CheckBox and naming correct | When run check | Then no failure`( + tagName: String, + parentTagName: String? + ) { + // GIVEN + val parentTagAttr = mockk { + every { value } returns parentTagName + } + val owner = mockk { + every { this@mockk.tagName } returns tagName + every { + attributes.getNamedItem("$ATTR_NAMESPACE_TOOLS:parentTag") + } returns parentTagAttr + } + val node = mockk { + every { value } returns "@+id/check_box_anything" + every { ownerElement } returns owner + } + + // WHEN + val result = rule.run(node) + + // THEN + assertNull(result) + } + @ParameterizedTest @MethodSource("createArgumentsSomeButton") fun `Given tagName SomeButton and naming failure | When run check | Then failure`( @@ -639,6 +724,9 @@ private class IdNamingTest { @JvmStatic fun createArgumentsGuideline() = createArguments("Guideline") + @JvmStatic + fun createArgumentsCheckBox() = createArguments("CheckBox") + @JvmStatic fun createArgumentsSomeButton() = createArguments("SomeButton") diff --git a/src/test/resources/files/failures/IdNaming/IdNaming-CheckBox.xml b/src/test/resources/files/failures/IdNaming/IdNaming-CheckBox.xml new file mode 100644 index 0000000..460802b --- /dev/null +++ b/src/test/resources/files/failures/IdNaming/IdNaming-CheckBox.xml @@ -0,0 +1,3 @@ + + diff --git a/src/test/resources/files/success/IdNaming/IdNaming-CheckBox.xml b/src/test/resources/files/success/IdNaming/IdNaming-CheckBox.xml new file mode 100644 index 0000000..b23f735 --- /dev/null +++ b/src/test/resources/files/success/IdNaming/IdNaming-CheckBox.xml @@ -0,0 +1,3 @@ + +