Skip to content

Commit

Permalink
Merge pull request #13 from diffplug/feat/gc-within-method
Browse files Browse the repository at this point in the history
"Sub" snapshots.
  • Loading branch information
jknack authored Sep 4, 2023
2 parents 141f9b8 + cb76e48 commit 837c2b4
Show file tree
Hide file tree
Showing 3 changed files with 126 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ internal class MethodSnapshotGC {
continue
} else if (key.elementAt(gc.key.length) == '/') {
// startWith + not same length = can safely query the `/`
val suffix = key.substring(gc.key.length + 1)
val suffix = key.substring(gc.key.length)
if (!gc.value.keeps(suffix)) {
staleIndices.add(keyIdx)
}
Expand Down Expand Up @@ -162,6 +162,7 @@ internal class MethodSnapshotGC {
private val EMPTY_SET = ArraySet<String>(arrayOf())
}
}

/** An immutable, sorted, array-backed set. */
internal class ArraySet<K : Comparable<K>>(private val data: Array<Any>) : ListBackedSet<K>() {
override val size: Int
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
/*
* Copyright (C) 2023 DiffPlug
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.diffplug.selfie.junit5

import kotlin.test.Test
import org.junit.jupiter.api.MethodOrderer
import org.junit.jupiter.api.Order
import org.junit.jupiter.api.TestMethodOrder
import org.junitpioneer.jupiter.DisableIfTestFails

/** Write-only test which asserts adding and removing snapshots results in same-class GC. */
@TestMethodOrder(MethodOrderer.OrderAnnotation::class)
@DisableIfTestFails
class WithinMethodGCTest : Harness("undertest-junit5") {
@Test @Order(1)
fun noSelfiesNoFile() {
ut_snapshot().deleteIfExists()
ut_snapshot().assertDoesNotExist()
ut_mirror().linesFrom("UT_WithinMethodGC").toLast("}").shrinkByOne().commentOut()
}

@Test @Order(2)
fun rootOnly() {
ut_mirror().linesFrom("fun selfie()").toFirst("}").uncomment()
ut_mirror().lineWith("maple").commentOut()
gradleWriteSS()
ut_snapshot()
.assertContent(
"""
╔═ selfie ═╗
root
╔═ [end of file] ═╗
"""
.trimIndent())
}

@Test @Order(3)
fun rootAndLeaf() {
ut_mirror().lineWith("maple").uncomment()
gradleWriteSS()
ut_snapshot()
.assertContent(
"""
╔═ selfie ═╗
root
╔═ selfie/leaf ═╗
maple
╔═ [end of file] ═╗
"""
.trimIndent())
}

@Test @Order(4)
fun leafOnly() {
ut_mirror().lineWith("root").commentOut()
gradleWriteSS()
ut_snapshot()
.assertContent(
"""
╔═ selfie/leaf ═╗
maple
╔═ [end of file] ═╗
"""
.trimIndent())
}

@Test @Order(5)
fun renameTheWholeMethod() {
ut_mirror().lineWith("selfie2()").uncomment()
ut_mirror().lineWith("selfie()").commentOut()
gradleWriteSS()
ut_snapshot()
.assertContent(
"""
╔═ selfie2/leaf ═╗
maple
╔═ [end of file] ═╗
"""
.trimIndent())
}

@Test @Order(6)
fun resetUTMirror() {
ut_mirror().lineWith("root").uncomment()
ut_mirror().lineWith("selfie2()").commentOut()
ut_mirror().lineWith("selfie()").uncomment()
}

@Test @Order(7)
fun deleteSelfie() {
ut_snapshot().deleteIfExists()
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package com.diffplug.selfie.junit5
// spotless:off
import com.diffplug.selfie.expectSelfie
import kotlin.test.Test

// spotless:on

class UT_WithinMethodGCTest {
// @Test fun selfie2() {
@Test fun selfie() {
expectSelfie("root").toMatchDisk()
expectSelfie("maple").toMatchDisk("leaf")
}
}

0 comments on commit 837c2b4

Please sign in to comment.