diff --git a/jvm/CHANGELOG.md b/jvm/CHANGELOG.md index 8a102a7e..41b8075e 100644 --- a/jvm/CHANGELOG.md +++ b/jvm/CHANGELOG.md @@ -14,6 +14,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ### Fixed - Off-by-one in the error message for a VCR key mismatch. ([#526](https://github.com/diffplug/selfie/pull/526)) - Fix `StringIndexOutOfBoundsException` when an empty snapshot had a facet added. (fixes [#529](https://github.com/diffplug/selfie/issues/529)) +- Fix `ClassCastException` when multiple nested test cases need to update snapshot. (fixes [#531](https://github.com/diffplug/selfie/issues/531)) ## [2.5.1] - 2025-03-04 ### Fixed diff --git a/jvm/selfie-lib/src/commonMain/kotlin/com/diffplug/selfie/ArrayMap.kt b/jvm/selfie-lib/src/commonMain/kotlin/com/diffplug/selfie/ArrayMap.kt index e5699a32..cc1afcb0 100644 --- a/jvm/selfie-lib/src/commonMain/kotlin/com/diffplug/selfie/ArrayMap.kt +++ b/jvm/selfie-lib/src/commonMain/kotlin/com/diffplug/selfie/ArrayMap.kt @@ -287,10 +287,12 @@ class ArraySet>(private val data: Array) : ListBackedSet< else -> { // TODO: use idxInsert and arrayCopy to do this faster, see ArrayMap#insert val array = Array(size + 1) { if (it < size) data[it] else key } + if (key is String) { array.sortWith(STRING_SLASHFIRST as Comparator) } else { - (array as Array).sort() + array.sortWith( + Comparator { a, b -> @Suppress("UNCHECKED_CAST") (a as K).compareTo(b as K) }) } ArraySet(array) } diff --git a/jvm/selfie-lib/src/commonTest/kotlin/com/diffplug/selfie/ArrayMapTest.kt b/jvm/selfie-lib/src/commonTest/kotlin/com/diffplug/selfie/ArrayMapTest.kt index f72025b7..bb259cf9 100644 --- a/jvm/selfie-lib/src/commonTest/kotlin/com/diffplug/selfie/ArrayMapTest.kt +++ b/jvm/selfie-lib/src/commonTest/kotlin/com/diffplug/selfie/ArrayMapTest.kt @@ -1,5 +1,5 @@ /* - * Copyright (C) 2023-2024 DiffPlug + * Copyright (C) 2023-2025 DiffPlug * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -173,3 +173,40 @@ class ArrayMapTest { map.minusSortedIndices(listOf(0, 2, 3, 6, 7, 8)).toString() shouldBe "{1=1, 4=4, 5=5}" } } + +class ArraySetTest() { + @Test + fun empty() { + val empty = ArraySet.empty() + assertEmpty(empty) + } + + @Test + fun addition() { + var mySet = ArraySet.empty() + assertEmpty(mySet) + + mySet = mySet.plusOrThis("one") + assertSingle(mySet, "one") + mySet = mySet.plusOrThis("one") + assertSingle(mySet, "one") + mySet = mySet.plusOrThis("two") + assertDouble(mySet, "one", "two") + } + private fun assertEmpty(map: ArraySet) { + map.size shouldBe 0 + } + private fun assertSingle(map: ArraySet, key: String) { + map.size shouldBe 1 + map.contains(key) shouldBe true + } + private fun assertDouble( + map: ArraySet, + key1: String, + key2: String, + ) { + map.size shouldBe 2 + map.contains(key1) shouldBe true + map.contains(key2) shouldBe true + } +}