Skip to content

Commit

Permalink
Add empty main option to improve complexity estimates.
Browse files Browse the repository at this point in the history
  • Loading branch information
gchallen committed Oct 25, 2021
1 parent 292c888 commit 594056f
Show file tree
Hide file tree
Showing 9 changed files with 67 additions and 10 deletions.
2 changes: 1 addition & 1 deletion build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ allprojects {
}
subprojects {
group = "com.github.cs125-illinois.jeed"
version = "2021.10.2"
version = "2021.10.3"
tasks.withType<Test> {
useJUnitPlatform()
enableAssertions = true
Expand Down
2 changes: 1 addition & 1 deletion containerrunner/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ dependencies {
implementation("ch.qos.logback:logback-classic:1.2.6")
implementation("io.github.microutils:kotlin-logging:2.0.11")
implementation("com.github.ajalt:clikt:2.8.0")
implementation("io.github.classgraph:classgraph:4.8.126")
implementation("io.github.classgraph:classgraph:4.8.128")
}
application {
@Suppress("DEPRECATION")
Expand Down
Original file line number Diff line number Diff line change
@@ -1 +1 @@
version=2021.10.2
version=2021.10.3
4 changes: 2 additions & 2 deletions core/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,10 @@ dependencies {
implementation("org.slf4j:slf4j-api:1.7.32")
implementation("ch.qos.logback:logback-classic:1.2.6")
implementation("io.github.microutils:kotlin-logging:2.0.11")
implementation("io.github.classgraph:classgraph:4.8.126")
implementation("io.github.classgraph:classgraph:4.8.128")
implementation("net.java.dev.jna:jna:5.9.0")
implementation("io.github.java-diff-utils:java-diff-utils:4.11")
implementation("com.google.googlejavaformat:google-java-format:1.11.0")
implementation("com.google.googlejavaformat:google-java-format:1.12.0")
implementation("net.sf.extjwnl:extjwnl:2.0.3")
implementation("net.sf.extjwnl:extjwnl-data-wn31:1.2")

Expand Down
15 changes: 12 additions & 3 deletions core/src/main/kotlin/Snippet.kt
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import org.antlr.v4.runtime.CharStreams
import org.antlr.v4.runtime.CommonTokenStream
import org.antlr.v4.runtime.RecognitionException
import org.antlr.v4.runtime.Recognizer
import org.jetbrains.kotlin.backend.common.pop

const val SNIPPET_SOURCE = ""

Expand Down Expand Up @@ -120,7 +121,8 @@ class SnippetErrorListener(
@JsonClass(generateAdapter = true)
data class SnippetArguments(
val indent: Int = 4,
var fileType: Source.FileType = Source.FileType.JAVA
var fileType: Source.FileType = Source.FileType.JAVA,
val noEmptyMain: Boolean = false
)

@Suppress("LongMethod", "ComplexMethod")
Expand Down Expand Up @@ -213,13 +215,15 @@ ${" ".repeat(snippetArguments.indent * 2)}@JvmStatic fun main() {""".lines().let
klassLines.add(it.start.line..it.stop.line)
}

var sawMainLines = false
val topLevelStart = parseTree.topLevelObject()?.firstOrNull()?.start?.line ?: 0
val topLevelEnd = parseTree.topLevelObject()?.lastOrNull()?.stop?.line?.inc() ?: 0
@Suppress("MagicNumber")
for (lineNumber in topLevelStart until topLevelEnd) {
if (methodLines.any { it.contains(lineNumber) } || klassLines.any { it.contains(lineNumber) }) {
continue
}
sawMainLines = true
val indentAmount = if (lineNumber in multilineLines) {
0
} else {
Expand All @@ -231,8 +235,13 @@ ${" ".repeat(snippetArguments.indent * 2)}@JvmStatic fun main() {""".lines().let
currentOutputLineNumber++
}

rewrittenSourceLines.add("""${" ".repeat(snippetArguments.indent * 2)}}""")
currentOutputLineNumber++
if (sawMainLines || !snippetArguments.noEmptyMain) {
rewrittenSourceLines.add("""${" ".repeat(snippetArguments.indent * 2)}}""")
currentOutputLineNumber++
} else {
rewrittenSourceLines.pop()
currentOutputLineNumber--
}

@Suppress("MagicNumber")
for (methodRange in methodLines) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1 +1 @@
version=2021.10.2
version=2021.10.3
16 changes: 16 additions & 0 deletions core/src/test/kotlin/TestKotlinComplexity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -695,4 +695,20 @@ fun first(first: Int, second: Int): Int {
it.lookup("first(Int,Int):Int", "Main.kt").complexity shouldBe 8
}
}
"should measure modulus correctly" {
Source.fromKotlin(
"""
class Main {
companion object {
fun sumIsOdd(first: Int, second: Int): Boolean {
return (first + second) % 2 != 0
}
}
}
""".trim()
).complexity().also {
it.lookup("Main", "Main.kt").complexity shouldBe 1
it.lookupFile("Main.kt") shouldBe 1
}
}
})
32 changes: 32 additions & 0 deletions core/src/test/kotlin/TestSnippet.kt
Original file line number Diff line number Diff line change
Expand Up @@ -695,6 +695,38 @@ class Example {
executionResult should haveOutput("")
}
}
"should parse kotlin snippets without empty main when requested" {
Source.fromSnippet(
"""
fun test() {
i = 0
}
""".trim(),
SnippetArguments(fileType = Source.FileType.KOTLIN)
).also {
it.rewrittenSource.lines() shouldHaveSize 9
val compilerError = shouldThrow<CompilationFailed> {
it.kompile()
}
compilerError.errors shouldHaveSize 1
compilerError.errors[0].location?.line shouldBe 2
}
Source.fromSnippet(
"""
fun test() {
i = 0
}
""".trim(),
SnippetArguments(fileType = Source.FileType.KOTLIN, noEmptyMain = true)
).also {
it.rewrittenSource.lines() shouldHaveSize 7
val compilerError = shouldThrow<CompilationFailed> {
it.kompile()
}
compilerError.errors shouldHaveSize 1
compilerError.errors[0].location?.line shouldBe 2
}
}
})

fun haveParseErrorOnLine(line: Int) = object : Matcher<SnippetTransformationFailed> {
Expand Down
Original file line number Diff line number Diff line change
@@ -1 +1 @@
version=2021.10.2
version=2021.10.3

0 comments on commit 594056f

Please sign in to comment.