Skip to content

Commit 72cf8e4

Browse files
committed
LimeDocRulesValidator: ease validation of placeholders
This change introduces a new optional configuration parameter for rules: 'resolvePlaceholder'. If the rule is intended to only check if placeholder is used then this parameter ease the regex -- we can just specify: '@Placeholder <NAME>'. Signed-off-by: Patryk Wrobel <183546751+pwrobeldev@users.noreply.github.com>
1 parent 2923bfd commit 72cf8e4

4 files changed

Lines changed: 99 additions & 3 deletions

File tree

gluecodium/src/main/java/com/here/gluecodium/validator/LimeDocRulesValidator.kt

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,14 @@ class LimeDocRulesValidator(
8787
continue
8888
}
8989

90-
val applyResult = regex.containsMatchIn(element.comment.getFor(platform))
90+
val validatedComment =
91+
if (rule.resolvePlaceholders) {
92+
element.comment.getFor(platform)
93+
} else {
94+
element.comment.getForWithoutResolvingPlaceholder(platform)
95+
}
96+
97+
val applyResult = regex.containsMatchIn(validatedComment)
9198
if (!applyResult) {
9299
logRuleApplicationFailed(rule, element, platform)
93100
result = false

gluecodium/src/main/java/com/here/gluecodium/validator/LimeDocValidationRule.kt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,5 +27,6 @@ data class LimeDocValidationRule(
2727
val limeElements: List<String>,
2828
val regex: String,
2929
val isWarningOnly: Boolean,
30+
val resolvePlaceholders: Boolean = true,
3031
val platforms: List<String> = listOf(),
3132
)

gluecodium/src/test/java/com/here/gluecodium/validator/LimeDocRulesValidatorTest.kt

Lines changed: 78 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -899,7 +899,7 @@ class LimeDocRulesValidatorTest {
899899
}
900900

901901
@Test
902-
fun validationOfCommentWithResolvedPlaceholder() {
902+
fun validationPassesForCommentWithResolvedPlaceholder() {
903903
// Given a class in LimeModel, which obeys the rules.
904904
allElements[outerTypePath.toString()] =
905905
LimeClass(
@@ -916,7 +916,7 @@ class LimeDocRulesValidatorTest {
916916
),
917917
)
918918

919-
// And given a correct validation rule for classes that uses the placeholder.
919+
// And given a correct validation rule for classes.
920920
val docValidationRules: List<LimeDocValidationRule> =
921921
listOf(
922922
LimeDocValidationRule(
@@ -937,6 +937,82 @@ class LimeDocRulesValidatorTest {
937937
assertTrue(validationResult)
938938
}
939939

940+
@Test
941+
fun validationPassesForCommentWithoutResolvedPlaceholder() {
942+
// Given a class in LimeModel, which obeys the rules.
943+
allElements[outerTypePath.toString()] =
944+
LimeClass(
945+
path = outerTypePath,
946+
comment =
947+
LimeComment(
948+
path = outerTypePath.child("comment"),
949+
taggedSections =
950+
listOf(
951+
"" to "This is some important outer type class. ",
952+
"Placeholder" to "AvailableOnline",
953+
),
954+
placeholders = mapOf("AvailableOnline" to LimeComment("This type is available only online")),
955+
),
956+
)
957+
958+
// And given a correct validation rule for classes that uses the placeholder but does not resolve it.
959+
val docValidationRules: List<LimeDocValidationRule> =
960+
listOf(
961+
LimeDocValidationRule(
962+
name = "SPECIAL_RULE",
963+
limeElements = listOf("class"),
964+
regex = "@Placeholder AvailableOnline",
965+
isWarningOnly = false,
966+
resolvePlaceholders = false,
967+
),
968+
)
969+
970+
// And given correctly constructed validator.
971+
val validator = LimeDocRulesValidator(mockk(relaxed = true), docValidationRules, allGenerators)
972+
973+
// When trying to verify if rules are obeyed.
974+
val validationResult = validator.validate(limeModel)
975+
976+
// Then validation passes.
977+
assertTrue(validationResult)
978+
}
979+
980+
@Test
981+
fun validationFailsForCommentWithoutResolvedPlaceholder() {
982+
// Given a class in LimeModel, which does not obey the rules.
983+
allElements[outerTypePath.toString()] =
984+
LimeClass(
985+
path = outerTypePath,
986+
comment =
987+
LimeComment(
988+
path = outerTypePath.child("comment"),
989+
taggedSections =
990+
listOf("" to "This is some important outer type class. "),
991+
),
992+
)
993+
994+
// And given a correct validation rule for classes that uses the placeholder but does not resolve it.
995+
val docValidationRules: List<LimeDocValidationRule> =
996+
listOf(
997+
LimeDocValidationRule(
998+
name = "SPECIAL_RULE",
999+
limeElements = listOf("class"),
1000+
regex = "@Placeholder AvailableOnline",
1001+
isWarningOnly = false,
1002+
resolvePlaceholders = false,
1003+
),
1004+
)
1005+
1006+
// And given correctly constructed validator.
1007+
val validator = LimeDocRulesValidator(mockk(relaxed = true), docValidationRules, allGenerators)
1008+
1009+
// When trying to verify if rules are obeyed.
1010+
val validationResult = validator.validate(limeModel)
1011+
1012+
// Then validation fails.
1013+
assertFalse(validationResult)
1014+
}
1015+
9401016
@Test
9411017
fun validationPassesForRuleForTwoPlatforms() {
9421018
// Given a class in LimeModel, which obeys the rules.

lime-runtime/src/main/java/com/here/gluecodium/model/lime/LimeComment.kt

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,18 @@ class LimeComment(
5151
.joinToString("") { if (!isPlaceholder(it.first)) it.second else resolvePlaceholder(it.second, platform) }
5252
.trim()
5353

54+
fun getForWithoutResolvingPlaceholder(platform: String) =
55+
taggedSections
56+
.filter { it.first == "" || it.first == platform || isPlaceholder(it.first) }
57+
.joinToString("") {
58+
if (!isPlaceholder(it.first)) {
59+
it.second
60+
} else {
61+
"{ @Placeholder ${it.second} }"
62+
}
63+
}
64+
.trim()
65+
5466
fun withPath(newPath: LimePath) = LimeComment(newPath, taggedSections, isExcluded, placeholders)
5567

5668
fun withExcluded(newExcluded: Boolean): LimeComment {

0 commit comments

Comments
 (0)