'Testing custom Lint rule with custom scopes
On the project I have a bunch of custom Lint rules. At some point of time I had a need to report single issue with different severity, depending on the scope
. Since issue is tightly coupled with a scope I created two issues:
val ISSUE_1 = Issue.create(
...
severity = WARNING,
implementation = Implementation(MyDetector::class.java, Scope.ALL_RESOURCES_SCOPE)
)
val ISSUE_2 = Issue.create(
...
severity = INFORMATIONAL,
implementation = Implementation(MyDetector::class.java, Scope.RESOURCE_FILE_SCOPE),
)
Then in some place within the detector I've implemented the following logic:
if (context.scope.contains(Scope.ALL_RESOURCE_FILES) {
context.report(ISSUE_1, ...)
} else {
context.report(ISSUE_2, ...)
}
Everything works like a charm in both Gradle and IDE. The issue occurs on attempt to unit test the logic.
I wrote the following two tests:
@Test
fun test1() {
val layout = xml(...)
lint()
.customScope(Scope.ALL_RESOURCES_SCOPE)
.files(layout)
.issues(ISSUE_2)
.run()
.expectClean()
}
This one passes as ISSUE_2
requires another scope - it is simply not run.
@Test
fun test2() {
val layout = xml(...)
lint()
.customScope(Scope.ALL_RESOURCES_SCOPE)
.files(layout)
.issues(ISSUE_1)
.run()
.expect("some_output")
}
And this one fails! But it fails not because of the wrong expectation. No, it fails, because the following assertion in the LintDriver
class:
fun analyze() {
//noinspection ExpensiveAssertion
assert(!scope.contains(Scope.ALL_RESOURCE_FILES) || scope.contains(Scope.RESOURCE_FILE))
...
What is the correct way to verify issue/detector against the Scope.ALL_RESOURCE_FILES
then?
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|