Skip to content

Commit

Permalink
Merge pull request #2 from mauhiz/feature/sl-abs-cls
Browse files Browse the repository at this point in the history
Also support abstract sealed classes
  • Loading branch information
lloydmeta committed Dec 9, 2014
2 parents 99ec85e + be8e1e2 commit f690d20
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 5 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# Enumeratum [![Build Status](https://travis-ci.org/lloydmeta/enumeratum.svg)](https://travis-ci.org/lloydmeta/enumeratum) [![Coverage Status](https://coveralls.io/repos/lloydmeta/enumeratum/badge.png)](https://coveralls.io/r/lloydmeta/enumeratum)

Yet another enumeration implementation for Scala for the sake of exhaustive pattern match warnings, Enumeratum is
an implementation based on a single Scala macro that searches for implementations of a sealed trait.
an implementation based on a single Scala macro that searches for implementations of a sealed trait or class.

Enumeratum aims to be similar enough to Scala's built in `Enumeration` to be easy-to-use and understand while offering
more flexibility and power.
Expand All @@ -18,7 +18,7 @@ libraryDependencies ++= Seq(

## How-to + example

Using Enumeratum is simple. Simply declare your own sealed trait `A`, and implement it as case objects inside
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
Expand Down
4 changes: 2 additions & 2 deletions macros/src/main/scala/enumeratum/EnumMacros.scala
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,10 @@ object EnumMacros {
}

private def validateType(c: Context)(typeSymbol: c.universe.Symbol): Unit = {
if (!typeSymbol.asClass.isTrait || !typeSymbol.asClass.isSealed)
if (!typeSymbol.asClass.isSealed)
c.abort(
c.enclosingPosition,
"You can only use findValues on sealed traits"
"You can only use findValues on sealed traits or classes"
)
}

Expand Down
32 changes: 31 additions & 1 deletion src/test/scala/enumeratum/EnumSpec.scala
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ class EnumSpec extends FunSpec with Matchers {

}

describe("when wrapped in another object") {
describe("when a sealed trait is wrapped in another object") {

import Wrapper._
import Wrapper.SmartEnum._
Expand Down Expand Up @@ -65,6 +65,36 @@ class EnumSpec extends FunSpec with Matchers {

}

describe("when a sealed abstract class is wrapped in another object") {

import InTheWoods.Mushroom._

describe("#values") {

it("should contain objects") {
values shouldBe Set(FlyAgaric, LSD, Shimeji)
}

}

describe("#withName") {

it("should return the proper object when passed the proper string") {
withName("FlyAgaric") should be(FlyAgaric)
withName("LSD") should be(LSD)
withName("Shimeji") should be(Shimeji)
}

it("should throw an error otherwise") {
intercept[IllegalArgumentException] {
withName("hello")
}
}

}

}

describe("trying to use with improper types") {

it("should fail to compile for unsealed traits") {
Expand Down
14 changes: 14 additions & 0 deletions src/test/scala/enumeratum/Models.scala
Original file line number Diff line number Diff line change
Expand Up @@ -26,4 +26,18 @@ object Wrapper {

}

}

object InTheWoods {
sealed abstract class Mushroom(val toxic: Boolean)

object Mushroom extends Enum[Mushroom] {

val values = findValues

case object FlyAgaric extends Mushroom(true)
case object LSD extends Mushroom(false)
case object Shimeji extends Mushroom(false)

}
}

0 comments on commit f690d20

Please sign in to comment.