From 292c888d012988698510afb8f5bc02facbcc1dc9 Mon Sep 17 00:00:00 2001 From: Geoffrey Challen Date: Wed, 13 Oct 2021 21:28:18 -0500 Subject: [PATCH] More complexity fixes. --- build.gradle.kts | 2 +- ...nois.cs.cs125.jeed.containerrunner.version | 2 +- core/src/main/kotlin/JavaComplexity.kt | 2 +- core/src/main/kotlin/KotlinComplexity.kt | 57 ++++++++++++++----- .../edu.illinois.cs.cs125.jeed.core.version | 2 +- core/src/test/kotlin/TestKotlinComplexity.kt | 47 ++++++++++++++- .../edu.illinois.cs.cs125.jeed.server.version | 2 +- 7 files changed, 95 insertions(+), 19 deletions(-) diff --git a/build.gradle.kts b/build.gradle.kts index f9c95495..c5b22933 100644 --- a/build.gradle.kts +++ b/build.gradle.kts @@ -18,7 +18,7 @@ allprojects { } subprojects { group = "com.github.cs125-illinois.jeed" - version = "2021.10.1" + version = "2021.10.2" tasks.withType { useJUnitPlatform() enableAssertions = true diff --git a/containerrunner/src/main/resources/edu.illinois.cs.cs125.jeed.containerrunner.version b/containerrunner/src/main/resources/edu.illinois.cs.cs125.jeed.containerrunner.version index 6389f992..f07555f5 100644 --- a/containerrunner/src/main/resources/edu.illinois.cs.cs125.jeed.containerrunner.version +++ b/containerrunner/src/main/resources/edu.illinois.cs.cs125.jeed.containerrunner.version @@ -1 +1 @@ -version=2021.10.1 \ No newline at end of file +version=2021.10.2 \ No newline at end of file diff --git a/core/src/main/kotlin/JavaComplexity.kt b/core/src/main/kotlin/JavaComplexity.kt index 4cbbbfbc..c5c89b5d 100644 --- a/core/src/main/kotlin/JavaComplexity.kt +++ b/core/src/main/kotlin/JavaComplexity.kt @@ -171,7 +171,7 @@ class JavaComplexityListener(val source: Source, entry: Map.Entry, except the default one override fun enterWhenCondition(ctx: KotlinParser.WhenConditionContext) { require(complexityStack.isNotEmpty()) currentComplexity.complexity++ + inIfExpression = true + } + + override fun exitWhenCondition(ctx: KotlinParser.WhenConditionContext) { + inIfExpression = false } // catch block @@ -245,6 +269,13 @@ class KotlinComplexityListener(val source: Source, entry: Map.Entry second && second > first) { + 1 + } else if (second < first && first < second) { + -1 + } else { + 0 + } +} +fun second(first: Int, second: Int): Int { + return when { + first > second && second > first -> 1 + second < first && first < second -> -1 + else -> 0 + } +} +""".trim() + ).complexity().also { + it.lookup("first(Int,Int):Int", "Main.kt").complexity shouldBe 5 + it.lookup("second(Int,Int):Int", "Main.kt").complexity shouldBe 5 + } + } + "should measure if correctly" { + Source.fromKotlin( + """ +fun first(first: Int, second: Int): Int { + var it = first > second || second == first + return if (first > second && second > first || second == first && second == second) { + 1 + } else if (second < first && first < second) { + -1 + } else { + 0 + } +} +""".trim() + ).complexity().also { + it.lookup("first(Int,Int):Int", "Main.kt").complexity shouldBe 8 + } + } }) diff --git a/server/src/main/resources/edu.illinois.cs.cs125.jeed.server.version b/server/src/main/resources/edu.illinois.cs.cs125.jeed.server.version index 6389f992..f07555f5 100644 --- a/server/src/main/resources/edu.illinois.cs.cs125.jeed.server.version +++ b/server/src/main/resources/edu.illinois.cs.cs125.jeed.server.version @@ -1 +1 @@ -version=2021.10.1 \ No newline at end of file +version=2021.10.2 \ No newline at end of file