From 3fefe56fcfa93b07f8cf8e4a0e9b8033bf0d8a2c Mon Sep 17 00:00:00 2001 From: Alois Cochard Date: Sat, 13 Aug 2011 16:34:13 +0200 Subject: [PATCH] Added basic functional specification --- src/test/scala/sindi/FunctionalSpec.scala | 75 +++++++++++++++++++++++ src/test/scala/sindi/Sindi.scala | 46 +++----------- 2 files changed, 85 insertions(+), 36 deletions(-) create mode 100644 src/test/scala/sindi/FunctionalSpec.scala diff --git a/src/test/scala/sindi/FunctionalSpec.scala b/src/test/scala/sindi/FunctionalSpec.scala new file mode 100644 index 0000000..fe6324b --- /dev/null +++ b/src/test/scala/sindi/FunctionalSpec.scala @@ -0,0 +1,75 @@ +// _____ ___ +// / __(_)__ ___/ (_) +// _\ \/ / _ \/ _ / / +// /___/_/_//_/\_,_/_/ +// +// (c) 2011, Alois Cochard +// +// http://aloiscochard.github.com/sindi +// + +package sindi + +import org.specs2.mutable._ + +class FunctionalSpec extends Specification { + + "Sindi" should { + "throw an exception when type is not bound" in { + class Foo extends Context + new Foo().inject[String] must throwAn[RuntimeException] + } + + "bind concrete type" in { + class Foo extends Context { override val bindings: Bindings = bind[String] to "sindi" } + new Foo().inject[String] mustEqual "sindi" + } + + "bind concrete type with qualifier" in { + class Foo extends Context { override val bindings: Bindings = bind[String] to "sindi" as "sindi"} + val foo = new Foo + foo.inject[String] must throwAn[RuntimeException] + foo.injectAs[String]("sindi") mustEqual "sindi" + } + + "bind concrete type with scope" in { + class Bar + var state = 1 + class Foo extends Context { override val bindings: Bindings = bind[Bar] to new Bar scope { state } } + val foo = new Foo + val bar1 = foo.inject[Bar].hashCode + bar1 mustEqual foo.inject[Bar].hashCode + state = 2 + bar1 mustNotEqual foo.inject[Bar].hashCode + } + + "bind abstract type" in { + class Foo extends Context { override val bindings: Bindings = bind[String] to "sindi" } + new Foo().inject[AnyRef] mustEqual "sindi" + } + + "bind abstract type with FIFO priority" in { + class FooA extends Context { override val bindings = Bindings(bind[AnyRef] to "scala", + bind[String] to "sindi") } + val fooA = new FooA + fooA.inject[AnyRef] mustEqual "scala" + fooA.inject[String] mustEqual "sindi" + + class FooB extends Context { override val bindings = Bindings(bind[String] to "sindi", + bind[AnyRef] to "scala") } + val fooB = new FooB + fooB.inject[String] mustEqual "sindi" + fooB.inject[AnyRef] mustEqual "sindi" + } + + "bind parameterized type" in { + val list = List("sindi") + class Foo extends Context { override val bindings: Bindings = bind[List[String]] to list } + new Foo().inject[List[String]] mustEqual list + new Foo().inject[List[AnyRef]] mustEqual list + + } + + // TODO [aloiscochard] Test scope, qualifier, factory, Option + } +} diff --git a/src/test/scala/sindi/Sindi.scala b/src/test/scala/sindi/Sindi.scala index d98a89b..e07fa10 100644 --- a/src/test/scala/sindi/Sindi.scala +++ b/src/test/scala/sindi/Sindi.scala @@ -1,42 +1,16 @@ +// _____ ___ +// / __(_)__ ___/ (_) +// _\ \/ / _ \/ _ / / +// /___/_/_//_/\_,_/_/ +// +// (c) 2011, Alois Cochard +// +// http://aloiscochard.github.com/sindi +// + package sindi import org.specs2.mutable._ -class Foo - class SindiSpec extends Specification { - - /* - "Injector Companion" should { - "create binding from instance" in { - val foo = new Foo - val binding = Injector.bind[Foo](foo) - (binding._1 eq foo.getClass) must beTrue - (binding._2() eq foo) must beTrue - } - - "create binding from class" in { - val binding = Injector.bind[Foo](classOf[Foo]) - val fooA = binding._2() - val fooB = binding._2() - (binding._1 eq new Foo().getClass) must beTrue - (fooA ne fooB) must beTrue - } - - "create binding from function" in { - val foo = new Foo - val function = () => foo - val binding = Injector.bind[Foo](foo) - (binding._1 eq foo.getClass) must beTrue - (binding._2() eq foo) must beTrue - } - } - "Injector" should { - "accept an input size of 16 bytes only" in { - new UID((0 to 14).map(_.toByte).toList) must throwAn[IllegalArgumentException] - new UID((0 to 16).map(_.toByte).toList) must throwAn[IllegalArgumentException] - new UID((0 to 15).map(_.toByte).toList) must not be null - } - } -*/ }