Skip to content

Commit f690d20

Browse files
committed
Merge pull request #2 from mauhiz/feature/sl-abs-cls
Also support abstract sealed classes
2 parents 99ec85e + be8e1e2 commit f690d20

File tree

4 files changed

+49
-5
lines changed

4 files changed

+49
-5
lines changed

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
# 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)
22

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

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

1919
## How-to + example
2020

21-
Using Enumeratum is simple. Simply declare your own sealed trait `A`, and implement it as case objects inside
21+
Using Enumeratum is simple. Simply declare your own sealed trait or class `A`, and implement it as case objects inside
2222
an object that extends from `Enum[A]` as follows.
2323

2424
*Note* `Enum` is BYOO (Bring Your Own Ordinality) - take care of ordinality in your own way when you implement

macros/src/main/scala/enumeratum/EnumMacros.scala

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,10 @@ object EnumMacros {
1414
}
1515

1616
private def validateType(c: Context)(typeSymbol: c.universe.Symbol): Unit = {
17-
if (!typeSymbol.asClass.isTrait || !typeSymbol.asClass.isSealed)
17+
if (!typeSymbol.asClass.isSealed)
1818
c.abort(
1919
c.enclosingPosition,
20-
"You can only use findValues on sealed traits"
20+
"You can only use findValues on sealed traits or classes"
2121
)
2222
}
2323

src/test/scala/enumeratum/EnumSpec.scala

Lines changed: 31 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ class EnumSpec extends FunSpec with Matchers {
3434

3535
}
3636

37-
describe("when wrapped in another object") {
37+
describe("when a sealed trait is wrapped in another object") {
3838

3939
import Wrapper._
4040
import Wrapper.SmartEnum._
@@ -65,6 +65,36 @@ class EnumSpec extends FunSpec with Matchers {
6565

6666
}
6767

68+
describe("when a sealed abstract class is wrapped in another object") {
69+
70+
import InTheWoods.Mushroom._
71+
72+
describe("#values") {
73+
74+
it("should contain objects") {
75+
values shouldBe Set(FlyAgaric, LSD, Shimeji)
76+
}
77+
78+
}
79+
80+
describe("#withName") {
81+
82+
it("should return the proper object when passed the proper string") {
83+
withName("FlyAgaric") should be(FlyAgaric)
84+
withName("LSD") should be(LSD)
85+
withName("Shimeji") should be(Shimeji)
86+
}
87+
88+
it("should throw an error otherwise") {
89+
intercept[IllegalArgumentException] {
90+
withName("hello")
91+
}
92+
}
93+
94+
}
95+
96+
}
97+
6898
describe("trying to use with improper types") {
6999

70100
it("should fail to compile for unsealed traits") {

src/test/scala/enumeratum/Models.scala

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,4 +26,18 @@ object Wrapper {
2626

2727
}
2828

29+
}
30+
31+
object InTheWoods {
32+
sealed abstract class Mushroom(val toxic: Boolean)
33+
34+
object Mushroom extends Enum[Mushroom] {
35+
36+
val values = findValues
37+
38+
case object FlyAgaric extends Mushroom(true)
39+
case object LSD extends Mushroom(false)
40+
case object Shimeji extends Mushroom(false)
41+
42+
}
2943
}

0 commit comments

Comments
 (0)