Skip to content

Commit

Permalink
Add tests for missing rules in a ruleset
Browse files Browse the repository at this point in the history
  • Loading branch information
romtsn committed Jun 10, 2019
1 parent a361146 commit 662ef4d
Show file tree
Hide file tree
Showing 5 changed files with 50 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package com.pinterest.ktlint.ruleset.experimental

import com.pinterest.ktlint.test.RuleSetProviderTest

class ExperimentalRuleSetProviderTest : RuleSetProviderTest(
rulesetClass = ExperimentalRuleSetProvider::class.java,
packageName = "com.pinterest.ktlint.ruleset.experimental"
)
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package com.pinterest.ktlint.ruleset.standard

import com.pinterest.ktlint.test.RuleSetProviderTest

class StandardRuleSetProviderTest : RuleSetProviderTest(
rulesetClass = StandardRuleSetProvider::class.java,
packageName = "com.pinterest.ktlint.ruleset.standard"
)
1 change: 1 addition & 0 deletions ktlint-test/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,5 @@ dependencies {
compile project(':ktlint-core')
compile deps.kotlin.stdlib
compile deps.assertj
compile deps.junit
}
5 changes: 5 additions & 0 deletions ktlint-test/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,11 @@
<artifactId>assertj-core</artifactId>
<version>${assertj.version}</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>${junit.version}</version>
</dependency>
</dependencies>

<build>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package com.pinterest.ktlint.test

import com.pinterest.ktlint.core.RuleSetProvider
import org.assertj.core.api.Assertions.assertThat
import org.junit.Test
import java.io.File

open class RuleSetProviderTest(
private val rulesetClass: Class<out RuleSetProvider>,
private val packageName: String
) {

@Test
fun checkAllRulesProvided() {
val srcLocation = rulesetClass.protectionDomain.codeSource.location.path
val rulesDir = File(srcLocation + packageName.replace(".", "/"))
val packageRules = rulesDir.listFiles()
.map { it.name.removeSuffix(".class") }
.filter { it.endsWith("Rule") }

val provider = rulesetClass
val providerRules = provider.newInstance().get().rules.map { it::class.java.simpleName }
val diff = packageRules - providerRules
assertThat(diff)
.withFailMessage("%s is missing to provide the following rules: %s", provider.simpleName, diff.joinToString())
.hasSize(0)
}
}

0 comments on commit 662ef4d

Please sign in to comment.