diff --git a/enumset/src/commonMain/kotlin/EnumEntriesSet.kt b/enumset/src/commonMain/kotlin/EnumEntriesSet.kt new file mode 100644 index 0000000..f4f4226 --- /dev/null +++ b/enumset/src/commonMain/kotlin/EnumEntriesSet.kt @@ -0,0 +1,31 @@ +/* + * Copyright (c) 2025, OpenSavvy and contributors. + * + * 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 + * + * http://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 opensavvy.enumset + +import kotlin.enums.EnumEntries + +/** + * Mini-wrapper to consider an [EnumEntries] (from the stdlib) as a [Set]. + * + * [EnumEntries] is only a [List] in the stdlib, not a [Set]. + */ +internal class EnumEntriesSet>( + private val entries: EnumEntries, +) : Set, Collection by entries { + + override fun toString() = entries.toString() +} diff --git a/enumset/src/commonMain/kotlin/EnumSet.kt b/enumset/src/commonMain/kotlin/EnumSet.kt index aed5b4a..fbcbb33 100644 --- a/enumset/src/commonMain/kotlin/EnumSet.kt +++ b/enumset/src/commonMain/kotlin/EnumSet.kt @@ -27,6 +27,10 @@ internal fun > enumSetOf( ): Set { return when (entries.size) { 0 -> emptySet() + + // If the passed elements are already an EnumEntries, we know the user wants a set with *all* elements. + else if elements is EnumEntries<*> -> EnumEntriesSet(entries) + else -> elements.toSet() } } diff --git a/enumset/src/commonTest/kotlin/datatypes/EnumSetSuite.kt b/enumset/src/commonTest/kotlin/datatypes/EnumSetSuite.kt index 4397ebf..8d3c945 100644 --- a/enumset/src/commonTest/kotlin/datatypes/EnumSetSuite.kt +++ b/enumset/src/commonTest/kotlin/datatypes/EnumSetSuite.kt @@ -17,10 +17,10 @@ package opensavvy.enumset.datatypes import io.kotest.assertions.throwables.shouldThrow +import opensavvy.enumset.EnumEntriesSet import opensavvy.prepared.suite.Prepared import opensavvy.prepared.suite.SuiteDsl import opensavvy.prepared.suite.TestDsl -import opensavvy.prepared.suite.config.Ignored import opensavvy.prepared.suite.prepared import opensavvy.prepared.suite.random.random import opensavvy.prepared.suite.random.randomInt @@ -105,8 +105,12 @@ fun > SuiteDsl.testEnumSetValidity( check(set().size == targetSize()) } - test("toString should mention exactly all elements, in order", Ignored) { // TODO: this test is only valid for our custom implementations, not for the stdlib - check(set().toString() == values().sorted().joinToString(", ", prefix = "[", postfix = "]")) + test("toString should mention exactly all elements, in order") { + if (set() is EnumEntriesSet) { + check(set().toString() == values().sorted().joinToString(", ", prefix = "[", postfix = "]")) + } else { + println("This test does not make sense for set ${set()} (${set()::class})") + } } test("The iterator's size should be correct") {