Skip to content

Commit

Permalink
master: Adjusted IdNaming to correct unnecessary Switch case. Added t…
Browse files Browse the repository at this point in the history
…ests for the CheckBox case
  • Loading branch information
alistairsykes committed Nov 5, 2019
1 parent cfd66f6 commit 3dfd613
Show file tree
Hide file tree
Showing 5 changed files with 110 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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_")
}
Expand Down
13 changes: 13 additions & 0 deletions src/test/kotlin/uk/co/brightec/xmlcheck/EndToEndTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<Attr> {
every { value } returns parentTagName
}
val owner = mockk<Element> {
every { this@mockk.tagName } returns tagName
every {
attributes.getNamedItem("$ATTR_NAMESPACE_TOOLS:parentTag")
} returns parentTagAttr
}
val node = mockk<Attr> {
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<Attr> {
every { value } returns parentTagName
}
val owner = mockk<Element> {
every { this@mockk.tagName } returns tagName
every {
attributes.getNamedItem("$ATTR_NAMESPACE_TOOLS:parentTag")
} returns parentTagAttr
}
val node = mockk<Attr> {
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<Attr> {
every { value } returns parentTagName
}
val owner = mockk<Element> {
every { this@mockk.tagName } returns tagName
every {
attributes.getNamedItem("$ATTR_NAMESPACE_TOOLS:parentTag")
} returns parentTagAttr
}
val node = mockk<Attr> {
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`(
Expand Down Expand Up @@ -639,6 +724,9 @@ private class IdNamingTest {
@JvmStatic
fun createArgumentsGuideline() = createArguments("Guideline")

@JvmStatic
fun createArgumentsCheckBox() = createArguments("CheckBox")

@JvmStatic
fun createArgumentsSomeButton() = createArguments("SomeButton")

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
<?xml version="1.0" encoding="utf-8"?>
<CheckBox xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/something" />
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
<?xml version="1.0" encoding="utf-8"?>
<CheckBox xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/check_box_something" />

0 comments on commit 3dfd613

Please sign in to comment.