From 3bbfeef2a6051c3447f2d42295f9027667ad763a Mon Sep 17 00:00:00 2001 From: Nathaniel Bauernfeind Date: Wed, 21 Jan 2015 18:16:36 -0500 Subject: [PATCH] Write a test for #42 and related to higher-kinded types. --- src/main/scala/net/codingwell/package.scala | 2 +- .../scalaguice/ClassesForTesting.scala | 3 +++ .../scalaguice/InjectorExtensionsSpec.scala | 5 +++++ .../scalaguice/ScalaModuleSpec.scala | 20 +++++++++++++++++++ .../scalaguice/ScalaPrivateModuleSpec.scala | 10 ++++++++++ .../scalaguice/TypeLiteralSpec.scala | 8 ++++++++ 6 files changed, 47 insertions(+), 1 deletion(-) diff --git a/src/main/scala/net/codingwell/package.scala b/src/main/scala/net/codingwell/package.scala index ae5cf2e..5daa1ee 100644 --- a/src/main/scala/net/codingwell/package.scala +++ b/src/main/scala/net/codingwell/package.scala @@ -61,7 +61,7 @@ package object scalaguice { } private def isArray[T: TypeTag]: Boolean = synchronized { - isArray(typeOf[T]) + isArray(typeTag[T]) } private def isArray(typ: Type): Boolean = synchronized { diff --git a/src/test/scala/net/codingwell/scalaguice/ClassesForTesting.scala b/src/test/scala/net/codingwell/scalaguice/ClassesForTesting.scala index e470269..02283ce 100644 --- a/src/test/scala/net/codingwell/scalaguice/ClassesForTesting.scala +++ b/src/test/scala/net/codingwell/scalaguice/ClassesForTesting.scala @@ -32,6 +32,9 @@ object Outer { } } +import scala.language.higherKinds +class HigherKindedType[T[_]] + trait A class B extends A diff --git a/src/test/scala/net/codingwell/scalaguice/InjectorExtensionsSpec.scala b/src/test/scala/net/codingwell/scalaguice/InjectorExtensionsSpec.scala index 9945121..7f81f51 100644 --- a/src/test/scala/net/codingwell/scalaguice/InjectorExtensionsSpec.scala +++ b/src/test/scala/net/codingwell/scalaguice/InjectorExtensionsSpec.scala @@ -29,6 +29,7 @@ class InjectorExtensionsSpec extends WordSpec with Matchers { bind[A].annotatedWith(named("d")).to[B] bind[B].annotatedWith(classOf[Named]).to[B] bind[Gen[String]].to[C] + bind[HigherKindedType[List]] } } @@ -53,5 +54,9 @@ class InjectorExtensionsSpec extends WordSpec with Matchers { "allow instance to be retreived using a type parameter and an annotation class" in { injector.instance[B, Named] } + + "allow instance of higher order type" in { + injector.instance[HigherKindedType[List]] + } } } \ No newline at end of file diff --git a/src/test/scala/net/codingwell/scalaguice/ScalaModuleSpec.scala b/src/test/scala/net/codingwell/scalaguice/ScalaModuleSpec.scala index acf5b26..7eef74e 100644 --- a/src/test/scala/net/codingwell/scalaguice/ScalaModuleSpec.scala +++ b/src/test/scala/net/codingwell/scalaguice/ScalaModuleSpec.scala @@ -133,6 +133,26 @@ class ScalaModuleSpec extends WordSpec with Matchers { val say = Guice.createInjector(module).getInstance(classOf[Say]) say.hi("Bob") should be ("Hi Bob") } + + "allow binding of higher-kinded types" in { + val module = new AbstractModule with ScalaModule { + def configure() = { + bind[HigherKindedType[List]] + } + } + Guice.createInjector(module).getInstance(new Key[HigherKindedType[List]] {}) + } + + "allow binding of differently parameterized higher-kinded types" in { + val module = new AbstractModule with ScalaModule { + def configure() = { + bind[HigherKindedType[List]] + bind[HigherKindedType[Set]] + } + } + Guice.createInjector(module).getInstance(new Key[HigherKindedType[List]] {}) + Guice.createInjector(module).getInstance(new Key[HigherKindedType[Set]] {}) + } } } diff --git a/src/test/scala/net/codingwell/scalaguice/ScalaPrivateModuleSpec.scala b/src/test/scala/net/codingwell/scalaguice/ScalaPrivateModuleSpec.scala index 1ec3665..8988a21 100644 --- a/src/test/scala/net/codingwell/scalaguice/ScalaPrivateModuleSpec.scala +++ b/src/test/scala/net/codingwell/scalaguice/ScalaPrivateModuleSpec.scala @@ -94,6 +94,16 @@ class ScalaPrivateModuleSpec extends WordSpec with Matchers { Guice.createInjector(module).getInstance(Key.get(classOf[A],classOf[Named])) } + "allow binding of higher-order types" in { + val module = new PrivateModule with ScalaPrivateModule { + def configure() = { + bind[HigherKindedType[List]] + expose[HigherKindedType[List]] + } + } + Guice.createInjector(module).getInstance(new Key[HigherKindedType[List]] {}) + } + "give a useful error when bound on itself" in { val module = new PrivateModule with ScalaPrivateModule { def configure() = { diff --git a/src/test/scala/net/codingwell/scalaguice/TypeLiteralSpec.scala b/src/test/scala/net/codingwell/scalaguice/TypeLiteralSpec.scala index f65addc..93af084 100644 --- a/src/test/scala/net/codingwell/scalaguice/TypeLiteralSpec.scala +++ b/src/test/scala/net/codingwell/scalaguice/TypeLiteralSpec.scala @@ -54,5 +54,13 @@ class TypeLiteralSpec extends FunSpec with Matchers { it("should handle type parameters that are nested arrays") { typeLiteral[Array[Array[Int]]] should equal (new TypeLiteral[Array[Array[Int]]] {}) } + + it("should handle type parameters of higher-kinded types") { + typeLiteral[HigherKindedType[Set]] should equal (new TypeLiteral[HigherKindedType[Set]] {}) + } + + it("should handle type parameters of higher-kinded types parameterized with an Array") { + typeLiteral[HigherKindedType[Array]] should equal (new TypeLiteral[HigherKindedType[Array]] {}) + } } }