-
Notifications
You must be signed in to change notification settings - Fork 146
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #23 from lloydmeta/feature/scala-js-attempt-2
ScalaJS
- Loading branch information
Showing
11 changed files
with
308 additions
and
70 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
34 changes: 34 additions & 0 deletions
34
enumeratum-core-jvm-tests/src/test/scala/enumeratum/EnumJVMSpec.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 |
---|---|---|
@@ -0,0 +1,34 @@ | ||
package enumeratum | ||
|
||
import org.scalatest.{ FunSpec, Matchers } | ||
|
||
class EnumJVMSpec extends FunSpec with Matchers { | ||
|
||
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 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 = | ||
s""" | ||
import enumeratum._ | ||
sealed trait Enum$i extends EnumEntry | ||
|
||
case object Enum$i extends Enum[Enum$i] { | ||
$membersDefs | ||
val values = findValues | ||
} | ||
|
||
Enum$i | ||
""" | ||
val obj = Eval.apply[Enum[_ <: EnumEntry]](objDefinition) | ||
obj.values.map(_.entryName) shouldBe members | ||
} | ||
} | ||
|
||
} | ||
|
||
} |
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
15 changes: 15 additions & 0 deletions
15
enumeratum-upickle/src/main/scala/enumeratum/UPickleEnum.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 |
---|---|---|
@@ -0,0 +1,15 @@ | ||
package enumeratum | ||
|
||
import upickle.default.Aliases.RW | ||
import upickle.default.ReadWriter | ||
|
||
/** | ||
* Enum mix-in with default Reader and Writers defined (case sensitive) | ||
*/ | ||
trait UPickleEnum[A <: EnumEntry] { self: Enum[A] => | ||
|
||
import UPickler._ | ||
|
||
implicit val uPickleReadWriter: RW[A] = ReadWriter(writer(this).write, reader(this, false).read) | ||
|
||
} |
46 changes: 46 additions & 0 deletions
46
enumeratum-upickle/src/main/scala/enumeratum/UPickler.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 |
---|---|---|
@@ -0,0 +1,46 @@ | ||
package enumeratum | ||
|
||
import upickle.Js | ||
import upickle.default.{ Writer, Reader } | ||
|
||
object UPickler { | ||
|
||
/** | ||
* Returns a UPickle [[Reader]] for a given [[Enum]] | ||
* | ||
* @param enum the enum you wish to make a Reader for | ||
* @param insensitive whether or not to match case-insensitively | ||
*/ | ||
def reader[A <: EnumEntry](enum: Enum[A], insensitive: Boolean = false): Reader[A] = { | ||
Reader[A] { | ||
val memberFinder: String => Option[A] = if (insensitive) enum.withNameInsensitiveOption else enum.withNameOption | ||
val pfIfJsStr: PartialFunction[Js.Value, String] = { | ||
case Js.Str(s) => s | ||
} | ||
val pfMaybeMember = pfIfJsStr.andThen(memberFinder) | ||
val pfMaybeMemberToMember: PartialFunction[Option[A], A] = { | ||
case Some(a) => a | ||
} | ||
andThenPartial(pfMaybeMember, pfMaybeMemberToMember) | ||
} | ||
} | ||
|
||
/** | ||
* Returns a [[Writer]] for a given [[Enum]] | ||
* | ||
* @param enum [[Enum]] to make a [[Writer]] for | ||
*/ | ||
def writer[A <: EnumEntry](enum: Enum[A]): Writer[A] = Writer[A] { | ||
case member => Js.Str(member.toString) | ||
} | ||
|
||
/** | ||
* Private helper for composing PartialFunctions | ||
* | ||
* Stolen from http://stackoverflow.com/questions/23024626/compose-partial-functions | ||
*/ | ||
private def andThenPartial[A, B, C](pf1: PartialFunction[A, B], pf2: PartialFunction[B, C]): PartialFunction[A, C] = { | ||
Function.unlift(pf1.lift(_) flatMap pf2.lift) | ||
} | ||
|
||
} |
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 |
---|---|---|
@@ -0,0 +1,14 @@ | ||
package enumeratum | ||
|
||
/** | ||
* Created by Lloyd on 2/4/15. | ||
*/ | ||
sealed trait Dummy extends EnumEntry | ||
object Dummy extends Enum[Dummy] with UPickleEnum[Dummy] { | ||
case object A extends Dummy | ||
case object B extends Dummy | ||
case object C extends Dummy | ||
val values = findValues | ||
} | ||
|
||
object D extends Dummy |
57 changes: 57 additions & 0 deletions
57
enumeratum-upickle/src/test/scala/enumeratum/UPickleSpec.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 |
---|---|---|
@@ -0,0 +1,57 @@ | ||
package enumeratum | ||
|
||
import org.scalatest._ | ||
import upickle.Js | ||
|
||
/** | ||
* Created by Lloyd on 12/12/15. | ||
*/ | ||
class UPickleSpec extends FunSpec with Matchers { | ||
|
||
import Dummy._ | ||
|
||
describe("Reader") { | ||
|
||
val reader = UPickler.reader(Dummy) | ||
|
||
it("should work with valid values") { | ||
reader.read(Js.Str("A")) shouldBe A | ||
} | ||
|
||
it("should fail with invalid values") { | ||
intercept[Exception] { | ||
reader.read(Js.Str("D")) | ||
} | ||
intercept[Exception] { | ||
reader.read(Js.Num(2)) | ||
} | ||
} | ||
|
||
} | ||
|
||
describe("insensitive reader") { | ||
val reader = UPickler.reader(Dummy, true) | ||
|
||
it("should work with strings, disgregarding case") { | ||
reader.read(Js.Str("A")) shouldBe A | ||
reader.read(Js.Str("a")) shouldBe A | ||
} | ||
|
||
it("should work with invalid values") { | ||
intercept[Exception](reader.read(Js.Str("D"))) | ||
intercept[Exception](reader.read(Js.Num(5))) | ||
} | ||
|
||
} | ||
|
||
describe("Writer") { | ||
|
||
val writer = UPickler.writer(Dummy) | ||
|
||
it("should write enum values to JsString") { | ||
writer.write(A) shouldBe Js.Str("A") | ||
} | ||
|
||
} | ||
|
||
} |
Oops, something went wrong.