Skip to content

Commit

Permalink
feat: do not count the last line
Browse files Browse the repository at this point in the history
  • Loading branch information
vlmaier committed Jan 5, 2024
1 parent b317b17 commit 6035124
Show file tree
Hide file tree
Showing 2 changed files with 3 additions and 3 deletions.
2 changes: 1 addition & 1 deletion src/main/kotlin/Main.kt
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ class WordCount : CliktCommand(name = "wk") {
"${countLines(content)} ${countWords(content)} ${countCharacters(content)}"

private fun countBytes(content: String) = content.toByteArray(Charsets.UTF_8).size
private fun countLines(content: String) = content.lineSequence().count()
private fun countLines(content: String) = content.lineSequence().count() - 1
private fun countWords(content: String) = content.trim().split("\\s+".toRegex()).size

private fun countCharacters(content: String): Int {
Expand Down
4 changes: 2 additions & 2 deletions src/test/kotlin/WordCountTests.kt
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ Options:
@Test
fun `test lines counter`() {
val result = wk.test("-l $testFile")
assertEquals(" 7146 $testFile\n", result.stdout)
assertEquals(" 7145 $testFile\n", result.stdout)
assertEquals("", result.stderr)
assertEquals(0, result.statusCode)
}
Expand Down Expand Up @@ -86,7 +86,7 @@ Options:
val result = wk.test(testFile)
val parts = result.stdout.trim().split(Regex("\\s+"))
assertContains(result.stdout, "$testFile\n")
assertEquals("7146", parts[0])
assertEquals("7145", parts[0])
assertEquals("58164", parts[1])
assertTrue(Regex("[0-9]").containsMatchIn(parts[2]))
assertEquals("", result.stderr)
Expand Down

0 comments on commit 6035124

Please sign in to comment.