Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Split rule documentation printer to improve testability (#4578)
* split rule documentation printer to improve testability * add tests for missed code paths Co-authored-by: Markus Schwarz <post@markus-schwarz.net>
- Loading branch information
Showing
7 changed files
with
301 additions
and
97 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
43 changes: 43 additions & 0 deletions
43
...src/main/kotlin/io/gitlab/arturbosch/detekt/generator/printer/RuleConfigurationPrinter.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
package io.gitlab.arturbosch.detekt.generator.printer | ||
|
||
import io.gitlab.arturbosch.detekt.generator.collection.Configuration | ||
import io.gitlab.arturbosch.detekt.generator.out.bold | ||
import io.gitlab.arturbosch.detekt.generator.out.code | ||
import io.gitlab.arturbosch.detekt.generator.out.crossOut | ||
import io.gitlab.arturbosch.detekt.generator.out.description | ||
import io.gitlab.arturbosch.detekt.generator.out.h4 | ||
import io.gitlab.arturbosch.detekt.generator.out.item | ||
import io.gitlab.arturbosch.detekt.generator.out.list | ||
import io.gitlab.arturbosch.detekt.generator.out.markdown | ||
|
||
internal object RuleConfigurationPrinter : DocumentationPrinter<List<Configuration>> { | ||
|
||
override fun print(item: List<Configuration>): String { | ||
if (item.isEmpty()) return "" | ||
return markdown { | ||
h4 { "Configuration options:" } | ||
list { | ||
item.forEach { | ||
val defaultValues = it.defaultValue.getQuotedIfNecessary() | ||
val defaultAndroidValues = it.defaultAndroidValue?.getQuotedIfNecessary() | ||
val defaultString = if (defaultAndroidValues != null) { | ||
"(default: ${code { defaultValues }}) (android default: ${code { defaultAndroidValues }})" | ||
} else { | ||
"(default: ${code { defaultValues }})" | ||
} | ||
if (it.isDeprecated()) { | ||
item { | ||
crossOut { code { it.name } } + " " + defaultString | ||
} | ||
description { "${bold { "Deprecated" }}: ${it.deprecated}" } | ||
} else { | ||
item { | ||
code { it.name } + " " + defaultString | ||
} | ||
} | ||
description { it.description } | ||
} | ||
} | ||
} | ||
} | ||
} |
65 changes: 65 additions & 0 deletions
65
...kt-generator/src/main/kotlin/io/gitlab/arturbosch/detekt/generator/printer/RulePrinter.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
package io.gitlab.arturbosch.detekt.generator.printer | ||
|
||
import io.gitlab.arturbosch.detekt.generator.collection.Active | ||
import io.gitlab.arturbosch.detekt.generator.collection.Rule | ||
import io.gitlab.arturbosch.detekt.generator.out.MarkdownContent | ||
import io.gitlab.arturbosch.detekt.generator.out.bold | ||
import io.gitlab.arturbosch.detekt.generator.out.codeBlock | ||
import io.gitlab.arturbosch.detekt.generator.out.h3 | ||
import io.gitlab.arturbosch.detekt.generator.out.h4 | ||
import io.gitlab.arturbosch.detekt.generator.out.markdown | ||
import io.gitlab.arturbosch.detekt.generator.out.paragraph | ||
|
||
internal object RulePrinter : DocumentationPrinter<Rule> { | ||
|
||
override fun print(item: Rule): String { | ||
return markdown { | ||
h3 { item.name } | ||
|
||
if (item.description.isNotEmpty()) { | ||
paragraph { item.description } | ||
} else { | ||
paragraph { "TODO: Specify description" } | ||
} | ||
|
||
paragraph { | ||
"${bold { "Active by default" }}: ${if (item.defaultActivationStatus.active) "Yes" else "No"}" + | ||
((item.defaultActivationStatus as? Active)?.let { " - Since v${it.since}" }.orEmpty()) | ||
} | ||
|
||
if (item.requiresTypeResolution) { | ||
paragraph { | ||
bold { "Requires Type Resolution" } | ||
} | ||
} | ||
|
||
if (item.debt.isNotEmpty()) { | ||
paragraph { | ||
"${bold { "Debt" }}: ${item.debt}" | ||
} | ||
} | ||
|
||
if (!item.aliases.isNullOrEmpty()) { | ||
paragraph { | ||
"${bold { "Aliases" }}: ${item.aliases}" | ||
} | ||
} | ||
|
||
markdown { RuleConfigurationPrinter.print(item.configuration) } | ||
|
||
printRuleCodeExamples(item) | ||
} | ||
} | ||
|
||
private fun MarkdownContent.printRuleCodeExamples(rule: Rule) { | ||
if (rule.nonCompliantCodeExample.isNotEmpty()) { | ||
h4 { "Noncompliant Code:" } | ||
paragraph { codeBlock { rule.nonCompliantCodeExample } } | ||
} | ||
|
||
if (rule.compliantCodeExample.isNotEmpty()) { | ||
h4 { "Compliant Code:" } | ||
paragraph { codeBlock { rule.compliantCodeExample } } | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
81 changes: 81 additions & 0 deletions
81
...test/kotlin/io/gitlab/arturbosch/detekt/generator/printer/RuleConfigurationPrinterTest.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
package io.gitlab.arturbosch.detekt.generator.printer | ||
|
||
import io.gitlab.arturbosch.detekt.generator.collection.Configuration | ||
import io.gitlab.arturbosch.detekt.generator.collection.DefaultValue | ||
import org.assertj.core.api.Assertions.assertThat | ||
import org.junit.jupiter.api.Nested | ||
import org.junit.jupiter.api.Test | ||
|
||
internal class RuleConfigurationPrinterTest { | ||
private val configTemplate = Configuration( | ||
name = "configName", | ||
description = "config description", | ||
defaultValue = DefaultValue.of(true), | ||
defaultAndroidValue = null, | ||
deprecated = null | ||
) | ||
|
||
@Nested | ||
inner class DefaultValues { | ||
@Test | ||
fun `boolean default`() { | ||
val config = configTemplate.copy(defaultValue = DefaultValue.of(true)) | ||
val actual = RuleConfigurationPrinter.print(listOf(config)) | ||
assertThat(actual).contains("""* ``configName`` (default: ``true``)""") | ||
} | ||
|
||
@Test | ||
fun `int default`() { | ||
val config = configTemplate.copy(defaultValue = DefaultValue.of(99)) | ||
val actual = RuleConfigurationPrinter.print(listOf(config)) | ||
assertThat(actual).contains("""* ``configName`` (default: ``99``)""") | ||
} | ||
|
||
@Test | ||
fun `int default with groupings`() { | ||
val config = configTemplate.copy(defaultValue = DefaultValue.of(99_999)) | ||
val actual = RuleConfigurationPrinter.print(listOf(config)) | ||
assertThat(actual).contains("""* ``configName`` (default: ``99999``)""") | ||
} | ||
|
||
@Test | ||
fun `string default`() { | ||
val config = configTemplate.copy(defaultValue = DefaultValue.of("abc")) | ||
val actual = RuleConfigurationPrinter.print(listOf(config)) | ||
assertThat(actual).contains("""* ``configName`` (default: ``'abc'``)""") | ||
} | ||
|
||
@Test | ||
fun `string list default`() { | ||
val config = configTemplate.copy(defaultValue = DefaultValue.of(listOf("a", "b", "c"))) | ||
val actual = RuleConfigurationPrinter.print(listOf(config)) | ||
assertThat(actual).contains("""* ``configName`` (default: ``['a', 'b', 'c']``)""") | ||
} | ||
|
||
@Test | ||
fun `with android default`() { | ||
val config = configTemplate.copy( | ||
defaultValue = DefaultValue.of(99), | ||
defaultAndroidValue = DefaultValue.of(100) | ||
) | ||
val actual = RuleConfigurationPrinter.print(listOf(config)) | ||
assertThat(actual).contains("""* ``configName`` (default: ``99``) (android default: ``100``)""") | ||
} | ||
} | ||
|
||
@Nested | ||
inner class DeprecatedProperties { | ||
private val config = configTemplate.copy(deprecated = "Use something else instead") | ||
private val actual = RuleConfigurationPrinter.print(listOf(config)) | ||
|
||
@Test | ||
fun `contain deprecation information`() { | ||
assertThat(actual).contains("""**Deprecated**: Use something else instead""") | ||
} | ||
|
||
@Test | ||
fun `have strike through`() { | ||
assertThat(actual).contains("""~~``configName``~~""") | ||
} | ||
} | ||
} |
Oops, something went wrong.