Skip to content

Commit

Permalink
Add Enum.indexOf w/ tests
Browse files Browse the repository at this point in the history
  • Loading branch information
lloydmeta committed Apr 22, 2015
1 parent 51bd60b commit 066b9d2
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 9 deletions.
14 changes: 10 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,16 @@ values, which not only means you get exhaustive pattern match warnings, but also
can take your enum values as arguments without having to worry about erasure (for more info, see [this blog post on Scala's
`Enumeration`](http://underscore.io/blog/posts/2014/09/03/enumerations.html))


Enumeratum has the following niceties:

- Simplicity; most of the complexity in this lib is in the macro, and the macro is fairly simple conceptually
- No usage of `synchronized` at runtime , which may help with performance and deadlocks prevention
- No usage of reflection at run time. This may also help with performance but it means Enumeratum is compatible with ScalaJS and other
environments where reflection is a best effort.
- All magic happens at compile-time so you know right away when things go awry


Compatible with Scala 2.10.x and 2.11.x

[Scaladocs](https://beachape.com/enumeratum/latest/api)
Expand Down Expand Up @@ -45,10 +55,6 @@ libraryDependencies ++= Seq(
Using Enumeratum is simple. Simply declare your own sealed trait or class `A`, and implement it as case objects inside
an object that extends from `Enum[A]` as follows.

*Note* `Enum` is BYOO (Bring Your Own Ordinality) - take care of ordinality in your own way when you implement
the `values` method. If you don't care about ordinality, just pass `findValues` directly into your
`val values` implementation.

```scala

import enumeratum._
Expand Down
13 changes: 9 additions & 4 deletions enumeratum-core/src/main/scala/enumeratum/Enum.scala
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,6 @@ import scala.language.postfixOps
*
* This is yet another one.
*
* Oh yeah, [[Enum]] is BYOO (bring your own ordinality). Take care of that when
* you implement the values method.
*
* How to use:
*
* {{{
Expand Down Expand Up @@ -41,7 +38,7 @@ trait Enum[A <: EnumEntry] {
* to implement this in your extending class as a `val` so that `withName`
* and friends are as efficient as possible.
*
* Feel free to implement this however you'd like (including ordering, etc) if that
* Feel free to implement this however you'd like (including messing around with ordering, etc) if that
* fits your needs better.
*/
def values: Seq[A]
Expand Down Expand Up @@ -84,4 +81,12 @@ trait Enum[A <: EnumEntry] {
def withName(name: String): A =
withNameOption(name) getOrElse (throw new NoSuchElementException(s"$name is not a member of Enum $this"))

/**
* Returns the index number of the member passed in the values picked up by this enum
*
* @param member
* @return the index of the first element of values that is equal (as determined by ==) to member, or -1, if none exists.
*/
def indexOf(member: A): Int = values.indexOf(member)

}
22 changes: 21 additions & 1 deletion enumeratum-core/src/test/scala/enumeratum/EnumSpec.scala
Original file line number Diff line number Diff line change
Expand Up @@ -128,12 +128,32 @@ class EnumSpec extends FunSpec with Matchers {

}

describe("indexOf") {

it("should return the proper index") {
import DummyEnum._
DummyEnum.indexOf(Hello) shouldBe 0
DummyEnum.indexOf(GoodBye) shouldBe 1
DummyEnum.indexOf(Hi) shouldBe 2
import InTheWoods.Mushroom
import InTheWoods.Mushroom._
Mushroom.indexOf(FlyAgaric) shouldBe 0
Mushroom.indexOf(LSD) shouldBe 1
Mushroom.indexOf(Shimeji) shouldBe 2
import Wrapper.SmartEnum
SmartEnum.indexOf(SmartEnum.Hello) shouldBe 0
SmartEnum.indexOf(SmartEnum.GoodBye) shouldBe 1
SmartEnum.indexOf(SmartEnum.Hi) shouldBe 2
}

}

describe("findValues Vector") {

// This is a fairly intense test.
it("should be in the same order that the objects were declared in") {
import scala.util._
(1 to 50).foreach { i =>
(1 to 100).foreach { i =>
val members = Random.shuffle((1 to Random.nextInt(20)).map { m => s"member$m" })
val membersDefs = members.map { m => s"case object $m extends Enum$i" }.mkString("\n\n")
val objDefinition =
Expand Down

0 comments on commit 066b9d2

Please sign in to comment.