Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix/3336/add default value for tab width #3404

Merged
merged 12 commits into from
Nov 17, 2023
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/)
### Fixed 🐞

- Fix an issue with web demo on Safari showing a white screen and not loading [#3396](https://github.com/MaibornWolff/codecharta/pull/3396)
- Allow user to skip the value for tabwidth when configuring the rawtextparser and estimate its value [#3404](https://github.com/MaibornWolff/codecharta/pull/3404)
fritschldwg marked this conversation as resolved.
Show resolved Hide resolved

### Chore 👨‍💻 👩‍💻

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,9 @@ class ParserDialog {
hint = "metric1, metric2, metric3 (leave empty for all metrics)"
)

val tabWidth: BigDecimal =
KInquirer.promptInputNumber(message = "What is the tab width used (estimated if not provided)?")
val tabWidth: String =
KInquirer.promptInput(message = "What is the tab width used (estimated if unknown)?", default = "unknown", hint = "unknown")
fritschldwg marked this conversation as resolved.
Show resolved Hide resolved
val tabWidthValue = tabWidth.toIntOrNull() ?: 0

val maxIndentationLevel: BigDecimal = KInquirer.promptInputNumber(message = "What is the maximum Indentation Level?", default = "10", hint = "10")

Expand All @@ -54,7 +55,7 @@ class ParserDialog {
if (isCompressed) null else "--not-compressed",
"--verbose=$verbose",
"--metrics=$metrics",
"--tab-width=$tabWidth",
"--tab-width=$tabWidthValue",
"--max-indentation-level=$maxIndentationLevel",
"--exclude=$exclude",
"--file-extensions=$fileExtensions",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ class IndentationCounter(
}

override fun getValue(): FileMetrics {
if (tabWidth == 0) {
if (tabWidth <= 0) {
guessTabWidth()
}
correctMismatchingIndents(tabWidth)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,9 @@ import org.assertj.core.api.Assertions
import org.junit.jupiter.api.AfterAll
import org.junit.jupiter.api.Test
import org.junit.jupiter.api.TestInstance
import org.junit.jupiter.params.ParameterizedTest
import org.junit.jupiter.params.provider.Arguments
import org.junit.jupiter.params.provider.MethodSource
import picocli.CommandLine
import java.io.File
import java.math.BigDecimal
Expand All @@ -25,40 +28,97 @@ class ParserDialogTest {
unmockkAll()
}

companion object {
@JvmStatic
fun provideInvalidTabWidth(): List<Arguments> {
return listOf(
Arguments.of("unknown"),
Arguments.of("string-value"),
Arguments.of("12."),
Arguments.of("12.0"))
fritschldwg marked this conversation as resolved.
Show resolved Hide resolved
}
}

private fun setupMockedInquirer(
fileName: String,
outputFileName: String,
metrics: String,
tabWidth: String,
exclude: String,
fileExtensions: String,
maxIndentLvl: BigDecimal,
isCompressed: Boolean,
verbose: Boolean,
withoutDefaultExcludes: Boolean
) {
mockkStatic("com.github.kinquirer.components.InputKt")
every {
KInquirer.promptInput(any(), any(), any())
} returns fileName andThen outputFileName andThen metrics andThen tabWidth andThen exclude andThen fileExtensions
every {
KInquirer.promptInputNumber(any(), any(), any())
} returns maxIndentLvl
mockkStatic("com.github.kinquirer.components.ConfirmKt")
every {
KInquirer.promptConfirm(any(), any())
} returns isCompressed andThen verbose andThen withoutDefaultExcludes
}

@Test
fun `should output correct arguments that can be parsed`() {
val fileName = "test.txt"
val outputFileName = "test.cc.json"
val isCompressed = false
val verbose = false
val metrics = "metric1"
val tabWidth = BigDecimal(0)
val tabWidth = "5"
val tabWidthValue = 5
val maxIndentLvl = BigDecimal(10)
val exclude = "file1"
val fileExtensions = ""
val withoutDefaultExcludes = false

mockkStatic("com.github.kinquirer.components.InputKt")
every {
KInquirer.promptInput(any(), any(), any())
} returns fileName andThen outputFileName andThen metrics andThen exclude andThen fileExtensions
every {
KInquirer.promptInputNumber(any(), any(), any())
} returns tabWidth andThen maxIndentLvl
mockkStatic("com.github.kinquirer.components.ConfirmKt")
every {
KInquirer.promptConfirm(any(), any())
} returns isCompressed andThen verbose andThen withoutDefaultExcludes
setupMockedInquirer(fileName, outputFileName, metrics, tabWidth, exclude, fileExtensions, maxIndentLvl, isCompressed, verbose, withoutDefaultExcludes)

val parserArguments = ParserDialog.collectParserArgs()
val commandLine = CommandLine(RawTextParser())
val parseResult = commandLine.parseArgs(*parserArguments.toTypedArray())
Assertions.assertThat(parseResult.matchedOption("output-file").getValue<String>().equals(outputFileName))
Assertions.assertThat(parseResult.matchedOption("not-compressed").getValue<Boolean>()).isEqualTo(isCompressed)
Assertions.assertThat(parseResult.matchedOption("metrics").getValue<List<String>>()).isEqualTo(listOf(metrics))
Assertions.assertThat(parseResult.matchedOption("max-indentation-level").getValue<Int>()).isEqualTo(maxIndentLvl.toInt())
Assertions.assertThat(parseResult.matchedOption("tab-width").getValue<Int>()).isEqualTo(tabWidthValue)
Assertions.assertThat(parseResult.matchedOption("file-extensions").getValue<Array<String>>()).isEqualTo(arrayOf(fileExtensions))
Assertions.assertThat(parseResult.matchedOption("without-default-excludes").getValue<Boolean>()).isEqualTo(withoutDefaultExcludes)
Assertions.assertThat(parseResult.matchedOption("verbose").getValue<Boolean>()).isEqualTo(withoutDefaultExcludes)
Assertions.assertThat(parseResult.matchedOption("exclude").getValue<Array<String>>()).isEqualTo(arrayOf(exclude))
Assertions.assertThat(parseResult.matchedPositional(0).getValue<File>().name).isEqualTo(fileName)
}

@ParameterizedTest
@MethodSource("provideInvalidTabWidth")
fun `should set non-integer tab-width to 0`(invalidTabWidth: String) {
val fileName = "test.txt"
val outputFileName = "test.cc.json"
val isCompressed = false
val verbose = false
val metrics = "metric1"
val tabWidthValue = 0
val maxIndentLvl = BigDecimal(10)
val exclude = "file1"
val fileExtensions = ""
val withoutDefaultExcludes = false

setupMockedInquirer(fileName, outputFileName, metrics, invalidTabWidth, exclude, fileExtensions, maxIndentLvl, isCompressed, verbose, withoutDefaultExcludes)

val parserArguments = ParserDialog.collectParserArgs()
val commandLine = CommandLine(RawTextParser())
val parseResult = commandLine.parseArgs(*parserArguments.toTypedArray())
Assertions.assertThat(parseResult.matchedOption("output-file").getValue<String>().equals(outputFileName))
Assertions.assertThat(parseResult.matchedOption("not-compressed").getValue<Boolean>()).isEqualTo(isCompressed)
Assertions.assertThat(parseResult.matchedOption("metrics").getValue<List<String>>()).isEqualTo(listOf(metrics))
Assertions.assertThat(parseResult.matchedOption("max-indentation-level").getValue<Int>()).isEqualTo(maxIndentLvl.toInt())
Assertions.assertThat(parseResult.matchedOption("tab-width").getValue<Int>()).isEqualTo(tabWidth.toInt())
Assertions.assertThat(parseResult.matchedOption("tab-width").getValue<Int>()).isEqualTo(tabWidthValue)
Assertions.assertThat(parseResult.matchedOption("file-extensions").getValue<Array<String>>()).isEqualTo(arrayOf(fileExtensions))
Assertions.assertThat(parseResult.matchedOption("without-default-excludes").getValue<Boolean>()).isEqualTo(withoutDefaultExcludes)
Assertions.assertThat(parseResult.matchedOption("verbose").getValue<Boolean>()).isEqualTo(withoutDefaultExcludes)
Expand Down
Loading