Skip to content

Commit

Permalink
Fix complexity calculation.
Browse files Browse the repository at this point in the history
  • Loading branch information
gchallen committed Jun 26, 2021
1 parent 36d2c13 commit 84737bd
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 7 deletions.
Original file line number Diff line number Diff line change
@@ -1 +1 @@
version=2021.6.6
version=2021.6.7
5 changes: 3 additions & 2 deletions core/src/main/kotlin/KotlinComplexity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ class KotlinComplexityListener(val source: Source, entry: Map.Entry<String, Stri
} else {
ClassComplexity(
name,
SourceRange(name, source.mapLocation(name, start), source.mapLocation(name, end))
SourceRange(fileName, source.mapLocation(fileName, start), source.mapLocation(fileName, end))
)
}
if (complexityStack.isNotEmpty()) {
Expand Down Expand Up @@ -193,7 +193,8 @@ class KotlinComplexityListener(val source: Source, entry: Map.Entry<String, Stri
// lambdas and throws
override fun enterFunctionLiteral(ctx: KotlinParser.FunctionLiteralContext) {
require(complexityStack.isNotEmpty())
currentComplexity.complexity++
// TODO: For Ben to rethink...
// currentComplexity.complexity++
}

// if & else if
Expand Down
Original file line number Diff line number Diff line change
@@ -1 +1 @@
version=2021.6.6
version=2021.6.7
23 changes: 20 additions & 3 deletions core/src/test/kotlin/TestKotlinComplexity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,23 @@ fun main(first: Int, second: String, third: Blah?): Int {
}
}

"should compute complexity for Kotlin snippet" {
Source.fromSnippet(
"""
class Dog(val name: String)

listOf(Dog("Shadow"), Dog("Chuchu"), Dog("Lulu"))
.map { it.name }
.sorted()
.forEach { println(it) }
""".trim(), SnippetArguments(fileType = Source.FileType.KOTLIN)
).also {
println(it.rewrittenSource)
}.complexity().also {
it.lookup("", "").complexity shouldBe 1
}
}

"should compute complexity from sources map" {
Source(
mapOf(
Expand Down Expand Up @@ -349,7 +366,7 @@ class PingPonger constructor(private var state: String) {
""".trim()
)
).complexity().also {
it.lookup("PingPonger", "Test.kt").complexity shouldBe 7
it.lookup("PingPonger", "Test.kt").complexity shouldBe 5
}
}

Expand Down Expand Up @@ -524,7 +541,7 @@ fun main() {
}
""".trim()
).complexity().also {
it.lookup("main()", "Main.kt").complexity shouldBe 5
it.lookup("main()", "Main.kt").complexity shouldBe 2
}
}

Expand All @@ -539,7 +556,7 @@ fun main() {
}
""".trim()
).complexity().also {
it.lookup("main()", "Main.kt").complexity shouldBe 2
it.lookup("main()", "Main.kt").complexity shouldBe 1
}
}

Expand Down

0 comments on commit 84737bd

Please sign in to comment.