Skip to content

Commit

Permalink
Fix mutations.
Browse files Browse the repository at this point in the history
  • Loading branch information
gchallen committed Jul 31, 2021
1 parent b4ec472 commit a2366ae
Show file tree
Hide file tree
Showing 11 changed files with 43 additions and 13 deletions.
5 changes: 5 additions & 0 deletions .idea/jarRepositories.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ plugins {
}
allprojects {
repositories {
mavenLocal()
mavenCentral()
maven("https://jitpack.io")
maven("https://maven.google.com/")
Expand Down
Original file line number Diff line number Diff line change
@@ -1 +1 @@
version=2021.7.8
version=2021.7.9
2 changes: 1 addition & 1 deletion core/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ dependencies {
implementation(kotlin("reflect"))

implementation("org.jetbrains.kotlinx:kotlinx-coroutines-core:1.5.1")
implementation("com.puppycrawl.tools:checkstyle:8.44")
implementation("com.puppycrawl.tools:checkstyle:8.45")
implementation("com.pinterest.ktlint:ktlint-core:0.42.0")
implementation("com.pinterest.ktlint:ktlint-ruleset-standard:0.42.0")
implementation("com.github.jknack:handlebars:4.2.0")
Expand Down
13 changes: 9 additions & 4 deletions core/src/main/kotlin/Mutater.kt
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,10 @@ fun MutableList<Mutation.Location.SourcePath>.method(): String =
findLast { it.type == Mutation.Location.SourcePath.Type.METHOD }?.name ?: error("No current method in path")

@JsonClass(generateAdapter = true)
data class SourceMutation(val name: String, val mutation: Mutation)
data class SourceMutation(
val name: String,
val mutation: Mutation
)

@Suppress("unused", "MemberVisibilityCanBePrivate")
class MutatedSource(
Expand Down Expand Up @@ -340,15 +343,17 @@ data class MutationsResults(val source: Map<String, String>, val mutatedSources:
@Throws(MutationsFailed::class)
fun Source.mutations(mutationsArguments: MutationsArguments = MutationsArguments()): MutationsResults {
try {
val mutatedSources =
mutationStream(mutationsArguments.suppressWithComments).take(mutationsArguments.limit).map {
val mutatedSources = mutationStream(mutationsArguments.suppressWithComments)
.map {
require(it.mutations.size == 1) { "Stream applied multiple mutations" }
MutationsResults.MutatedSource(
it.mutations.first().name,
it.sources.sources,
AppliedMutation(it.mutations.first().mutation)
)
}.toList()
}
.take(mutationsArguments.limit)
.toList()
return MutationsResults(this.sources, mutatedSources)
} catch (e: JeedParsingException) {
throw MutationsFailed(e.errors)
Expand Down
6 changes: 5 additions & 1 deletion core/src/main/kotlin/Mutation.kt
Original file line number Diff line number Diff line change
Expand Up @@ -291,6 +291,10 @@ class NumberLiteral(
override val estimatedCount = numberPositions.size * 2

override fun applyMutation(random: Random): String {
// Special case since 0 -> 9 is a bit too obvious
if (original == "0") {
return "1"
}
val position = numberPositions.shuffled(random).first()
return original.toCharArray().also { characters ->
val direction = random.nextBoolean()
Expand All @@ -301,7 +305,7 @@ class NumberLiteral(
}.let {
@Suppress("MagicNumber")
// Avoid adding leading zeros
if (position == 0 && it == 0) {
if (position == 0 && original.length > 1 && it == 0) {
if (direction) {
1
} else {
Expand Down
Original file line number Diff line number Diff line change
@@ -1 +1 @@
version=2021.7.8
version=2021.7.9
19 changes: 17 additions & 2 deletions core/src/test/kotlin/TestJavaMutater.kt
Original file line number Diff line number Diff line change
Expand Up @@ -474,6 +474,21 @@ public class Example {
mutations[1].check(contents, "1", "0")
}
}
"it should remove plus and minus 1 with number literal" {
Source.fromJava(
"""
public class Example {
public static int test(int first) {
int i = i + 1;
int j = j - 1;
}
}"""
).checkMutations<NumberLiteral> { mutations, contents ->
mutations shouldHaveSize 2
mutations[0].check(contents, "1", "0")
mutations[1].check(contents, "1", "0")
}
}
"it should remove loops correctly" {
Source.fromJava(
"""
Expand Down Expand Up @@ -805,7 +820,7 @@ public class Example {
}
}"""
).also { source ->
source.mutationStream().take(1024).toList().size shouldBe 6
source.mutationStream().take(1024).toList().size shouldBe 5
}
}
"it should not mutate annotations" {
Expand Down Expand Up @@ -926,7 +941,7 @@ fun Mutation.check(contents: String, original: String, modified: String? = null)
applied shouldBe false
this.original shouldBe original
this.modified shouldBe null
val toReturn = apply(contents)
val toReturn = apply(contents, Random(seed = 124))
applied shouldBe true
this.original shouldBe original
this.modified shouldNotBe original
Expand Down
2 changes: 1 addition & 1 deletion core/src/test/kotlin/TestKotlinMutater.kt
Original file line number Diff line number Diff line change
Expand Up @@ -802,7 +802,7 @@ fun testStream(): Int {
}
""".trim()
).also { source ->
source.mutationStream().take(1024).toList().size shouldBe 7
source.mutationStream().take(1024).toList().size shouldBe 6
}
}

Expand Down
2 changes: 1 addition & 1 deletion server/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ dependencies {
implementation("com.uchuhimo:konf-core:1.1.2")
implementation("com.uchuhimo:konf-yaml:1.1.2")
implementation("io.github.microutils:kotlin-logging:2.0.10")
implementation("com.github.cs125-illinois:libcs1:2021.5.7")
implementation("com.github.cs125-illinois:libcs1:2021.7.0")

testImplementation("io.kotest:kotest-runner-junit5:4.6.1")
testImplementation("io.kotest:kotest-assertions-ktor:4.4.3")
Expand Down
Original file line number Diff line number Diff line change
@@ -1 +1 @@
version=2021.7.8
version=2021.7.9

0 comments on commit a2366ae

Please sign in to comment.