Skip to content

Commit

Permalink
✨ (Core): Add the enum forgeries
Browse files Browse the repository at this point in the history
  • Loading branch information
xgouchet committed Dec 9, 2019
1 parent efd123c commit 22d19d4
Show file tree
Hide file tree
Showing 2 changed files with 69 additions and 5 deletions.
32 changes: 27 additions & 5 deletions core/src/main/kotlin/fr/xgouchet/elmyr/Forge.kt
Original file line number Diff line number Diff line change
Expand Up @@ -72,11 +72,10 @@ open class Forge {
@Suppress("UNCHECKED_CAST")
val strictMatch = factories[clazz] as? ForgeryFactory<T>

@Suppress("IfThenToElvis")
return if (strictMatch == null) {
getSubclassForgery(clazz)
} else {
strictMatch.getForgery(this)
return when {
clazz.isEnum -> anElementFrom(*clazz.enumConstants)
strictMatch == null -> getSubclassForgery(clazz)
else -> strictMatch.getForgery(this)
}
}

Expand Down Expand Up @@ -932,6 +931,29 @@ open class Forge {

// endregion

// region Enum

/**
* @param enumClass an Enum class
* @param exclude a list of enum constants to exclude from the values
* @return an element “randomly” picked in the enum values
*/
@Suppress("SpreadOperator")
@JvmOverloads
fun <E : Enum<E>> aValueFrom(
enumClass: Class<E>,
exclude: Collection<E> = emptyList()
): E {
val chooseFrom = if (exclude.isNotEmpty()) {
enumClass.enumConstants.subtract(exclude).toList()
} else {
enumClass.enumConstants.toList()
}
return anElementFrom(chooseFrom)
}

// endregion

// region Nullable

/**
Expand Down
42 changes: 42 additions & 0 deletions core/src/test/kotlin/fr/xgouchet/elmyr/ForgeCollectionSpek.kt
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package fr.xgouchet.elmyr

import java.time.Month
import org.assertj.core.api.Assertions.assertThat
import org.assertj.core.api.KotlinAssertions.assertThat as assertThatK
import org.spekframework.spek2.Spek
Expand Down Expand Up @@ -159,6 +160,47 @@ class ForgeCollectionSpek : Spek({

// endregion

// region Choosing from an enum

context("forging value from an enum") {
it("selects a value from an enum class") {
repeat(testRepeatCountSmall) {
val value = forge.aValueFrom(Month::class.java)

assertThat(value).isInstanceOf(Month::class.java)
}
}

it("selects a value from an enum class") {
repeat(testRepeatCountSmall) {
val value = forge.getForgery(Month::class.java)

assertThat(value).isInstanceOf(Month::class.java)
}
}

it("selects a value from an enum class") {
repeat(testRepeatCountSmall) {
val value = forge.getForgery<Month>()

assertThat(value).isInstanceOf(Month::class.java)
}
}

it("selects a value from an enum class with excluded values") {
repeat(testRepeatCountSmall) {
val excluded = forge.aList(3) { aValueFrom(Month::class.java) }
val value = forge.aValueFrom(Month::class.java, excluded)

assertThat(value)
.isInstanceOf(Month::class.java)
.isNotIn(excluded)
}
}
}

//endregion

// region Transforming collections

context("forging sublists") {
Expand Down

0 comments on commit 22d19d4

Please sign in to comment.