Skip to content

Commit 794d3ea

Browse files
authored
Prevent ClassCastException during ArraySet sorting (#532 fixes #531)
2 parents cc68080 + 1067e29 commit 794d3ea

File tree

3 files changed

+42
-2
lines changed

3 files changed

+42
-2
lines changed

jvm/CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
1414
### Fixed
1515
- Off-by-one in the error message for a VCR key mismatch. ([#526](https://github.com/diffplug/selfie/pull/526))
1616
- Fix `StringIndexOutOfBoundsException` when an empty snapshot had a facet added. (fixes [#529](https://github.com/diffplug/selfie/issues/529))
17+
- Fix `ClassCastException` when multiple nested test cases need to update snapshot. (fixes [#531](https://github.com/diffplug/selfie/issues/531))
1718

1819
## [2.5.1] - 2025-03-04
1920
### Fixed

jvm/selfie-lib/src/commonMain/kotlin/com/diffplug/selfie/ArrayMap.kt

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -287,10 +287,12 @@ class ArraySet<K : Comparable<K>>(private val data: Array<Any>) : ListBackedSet<
287287
else -> {
288288
// TODO: use idxInsert and arrayCopy to do this faster, see ArrayMap#insert
289289
val array = Array(size + 1) { if (it < size) data[it] else key }
290+
290291
if (key is String) {
291292
array.sortWith(STRING_SLASHFIRST as Comparator<Any>)
292293
} else {
293-
(array as Array<K>).sort()
294+
array.sortWith(
295+
Comparator { a, b -> @Suppress("UNCHECKED_CAST") (a as K).compareTo(b as K) })
294296
}
295297
ArraySet(array)
296298
}

jvm/selfie-lib/src/commonTest/kotlin/com/diffplug/selfie/ArrayMapTest.kt

Lines changed: 38 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (C) 2023-2024 DiffPlug
2+
* Copyright (C) 2023-2025 DiffPlug
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -173,3 +173,40 @@ class ArrayMapTest {
173173
map.minusSortedIndices(listOf(0, 2, 3, 6, 7, 8)).toString() shouldBe "{1=1, 4=4, 5=5}"
174174
}
175175
}
176+
177+
class ArraySetTest() {
178+
@Test
179+
fun empty() {
180+
val empty = ArraySet.empty<String>()
181+
assertEmpty(empty)
182+
}
183+
184+
@Test
185+
fun addition() {
186+
var mySet = ArraySet.empty<String>()
187+
assertEmpty(mySet)
188+
189+
mySet = mySet.plusOrThis("one")
190+
assertSingle(mySet, "one")
191+
mySet = mySet.plusOrThis("one")
192+
assertSingle(mySet, "one")
193+
mySet = mySet.plusOrThis("two")
194+
assertDouble(mySet, "one", "two")
195+
}
196+
private fun assertEmpty(map: ArraySet<String>) {
197+
map.size shouldBe 0
198+
}
199+
private fun assertSingle(map: ArraySet<String>, key: String) {
200+
map.size shouldBe 1
201+
map.contains(key) shouldBe true
202+
}
203+
private fun assertDouble(
204+
map: ArraySet<String>,
205+
key1: String,
206+
key2: String,
207+
) {
208+
map.size shouldBe 2
209+
map.contains(key1) shouldBe true
210+
map.contains(key2) shouldBe true
211+
}
212+
}

0 commit comments

Comments
 (0)