-
Notifications
You must be signed in to change notification settings - Fork 149
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
263 additions
and
183 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
25 changes: 0 additions & 25 deletions
25
enumeratum-core-jvm-tests/src/test/scala/enumeratum/Eval.scala
This file was deleted.
Oops, something went wrong.
93 changes: 8 additions & 85 deletions
93
enumeratum-core-jvm-tests/src/test/scala/enumeratum/values/ValueEnumJVMSpec.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,91 +1,14 @@ | ||
package enumeratum.values | ||
|
||
import enumeratum.Eval | ||
import org.scalatest.funspec.AnyFunSpec | ||
import org.scalatest.matchers.should.Matchers | ||
|
||
import scala.reflect.ClassTag | ||
import scala.util.Random | ||
|
||
/** Created by Lloyd on 8/30/16. | ||
* | ||
* Copyright 2016 | ||
*/ | ||
class ValueEnumJVMSpec extends AnyFunSpec with Matchers { | ||
|
||
private def stringGenerator = | ||
Random.alphanumeric.grouped(10).toStream.map(_.mkString.replaceAll("[0-9]", "")).distinct | ||
|
||
/* | ||
Non-deterministically generates a bunch of different types of ValueEnums and tests the ability to resolve | ||
proper members by value | ||
*/ | ||
testValuesOf(Stream.continually(Random.nextInt())) | ||
testValuesOf(Stream.continually(Random.nextLong()), valueSuffix = "L") | ||
testValuesOf( | ||
Stream | ||
.continually(Random.nextInt(Short.MaxValue - Short.MinValue) + Short.MinValue) | ||
.map(_.toShort) | ||
) | ||
testValuesOf( | ||
Stream.continually(Random.nextInt(Byte.MaxValue - Byte.MinValue) + Byte.MinValue).map(_.toByte) | ||
) | ||
testValuesOf(stringGenerator, "\"", "\"") | ||
testValuesOf( | ||
Stream.continually(Random.nextPrintableChar()).filter(c => Character.isAlphabetic(c.toInt)), | ||
"'", | ||
"'" | ||
) | ||
|
||
private def testValuesOf[A: ClassTag]( | ||
valuesGenerator: => Stream[A], | ||
valuePrefix: String = "", | ||
valueSuffix: String = "" | ||
): Unit = { | ||
|
||
val typeName = implicitly[ClassTag[A]].runtimeClass.getSimpleName.capitalize | ||
|
||
describe(s"${typeName}Enum withValue") { | ||
|
||
it("should return proper members for valid values but throw otherwise") { | ||
(1 to 20).foreach { i => | ||
val enumName = s"Generated${typeName}Enum$i" | ||
val names = stringGenerator.take(5) | ||
val values = valuesGenerator.distinct.take(5) | ||
val namesToValues = names.zip(values) | ||
val memberDefs = namesToValues | ||
.map { case (n, v) => | ||
s"""case object $n extends $enumName($valuePrefix$v$valueSuffix)""" | ||
} | ||
.mkString("\n\n") | ||
val objDef = | ||
s""" | ||
|import enumeratum.values._ | ||
| | ||
|sealed abstract class $enumName(val value: $typeName) extends ${typeName}EnumEntry | ||
| | ||
|object $enumName extends ${typeName}Enum[$enumName] { | ||
| val values = findValues | ||
| | ||
| $memberDefs | ||
|} | ||
|$enumName | ||
""".stripMargin | ||
val obj = Eval.apply[ValueEnum[A, _ <: ValueEnumEntry[A]]](objDef) | ||
namesToValues.foreach { case (n, v) => | ||
obj.withValue(v).toString shouldBe n | ||
} | ||
// filterNot is not lazy until 2.12 | ||
valuesGenerator.filter(a => !values.contains(a)).take(5).foreach { invalidValue => | ||
intercept[NoSuchElementException] { | ||
obj.withValue(invalidValue) | ||
} | ||
} | ||
} | ||
} | ||
|
||
} | ||
|
||
} | ||
|
||
} | ||
// See `projects/project/CoreJVMTest.scala`#generateValueEnumTests | ||
final class ValueEnumJVMSpec | ||
extends generated.IntValueEnumBaseSpec | ||
with generated.LongValueEnumBaseSpec | ||
with generated.ShortValueEnumBaseSpec | ||
with generated.ByteValueEnumBaseSpec | ||
with generated.StringValueEnumBaseSpec | ||
with generated.CharValueEnumBaseSpec |
Oops, something went wrong.