From 322d33358faa5bec21cbad381f242fd5d7803e36 Mon Sep 17 00:00:00 2001 From: Abhinav Sarkar Date: Wed, 2 Jun 2010 03:05:45 +0530 Subject: [PATCH] added Scalatest spec. related changes in pom --- .classpath | 2 +- nulllessj.iml | 16 +++ pom.xml | 75 ++++++++++- .../net/abhinavsarkar/nulllessj/Just.java | 2 +- .../net/abhinavsarkar/nulllessj/Maybe.java | 10 +- .../nulllessj/NulllessjSpec.scala | 126 ++++++++++++++++++ 6 files changed, 223 insertions(+), 8 deletions(-) create mode 100644 src/test/scala/net/abhinavsarkar/nulllessj/NulllessjSpec.scala diff --git a/.classpath b/.classpath index 3a4681c..42acbf3 100644 --- a/.classpath +++ b/.classpath @@ -2,8 +2,8 @@ + - diff --git a/nulllessj.iml b/nulllessj.iml index 66a6ba1..e1bc531 100644 --- a/nulllessj.iml +++ b/nulllessj.iml @@ -1,5 +1,17 @@ + + + + + + + @@ -7,10 +19,14 @@ + + + + diff --git a/pom.xml b/pom.xml index 26ba274..988c974 100644 --- a/pom.xml +++ b/pom.xml @@ -12,6 +12,7 @@ UTF-8 + 2.7.7 @@ -25,10 +26,80 @@ 1.6 + + org.scala-tools + maven-scala-plugin + 2.12 + + + + compile + testCompile + + + + + ${scala.version} + + **/*.scala + + + + + org.apache.maven.plugins + maven-surefire-plugin + 2.5 + + plain + + **/*Spec.class + + + + + org.apache.maven.plugins + maven-surefire-report-plugin + 2.5 + + + test + + report-only + + + + - + + + + scala-tools.org + Scala-tools Maven2 Repository + http://scala-tools.org/repo-releases + + + + + + scala-tools.org + Scala-Tools Maven2 Repository + http://scala-tools.org/repo-releases + + + + + org.scala-lang + scala-library + ${scala.version} + + + org.scalatest + scalatest + 1.0 + test + junit junit @@ -36,5 +107,5 @@ test - + diff --git a/src/main/java/net/abhinavsarkar/nulllessj/Just.java b/src/main/java/net/abhinavsarkar/nulllessj/Just.java index 6ac8706..ff9f16e 100644 --- a/src/main/java/net/abhinavsarkar/nulllessj/Just.java +++ b/src/main/java/net/abhinavsarkar/nulllessj/Just.java @@ -58,7 +58,7 @@ public final class Just extends Maybe { @Override public String toString() { - return value.toString(); + return "Just(" + value.toString() + ")"; } } diff --git a/src/main/java/net/abhinavsarkar/nulllessj/Maybe.java b/src/main/java/net/abhinavsarkar/nulllessj/Maybe.java index a231b0c..f8571cd 100644 --- a/src/main/java/net/abhinavsarkar/nulllessj/Maybe.java +++ b/src/main/java/net/abhinavsarkar/nulllessj/Maybe.java @@ -1,9 +1,9 @@ package net.abhinavsarkar.nulllessj; -import static net.abhinavsarkar.nulllessj.Just.just; - import java.util.concurrent.Callable; +import static net.abhinavsarkar.nulllessj.Just.just; + /** * A container class for representing a value which 'maybe' present. * This is an abstract class with two concrete subclasses: {@link Just} @@ -130,7 +130,8 @@ public abstract class Maybe { * * @see Maybe#maybe */ - public Maybe callIfSomething(final ContextualCallable callable) { + public Maybe callIfSomething( + final ContextualCallable callable) { if (isSomething()) { callable.setContext(get()); try { @@ -190,7 +191,8 @@ public abstract class Maybe { * * @throws RuntimeException See the description. */ - public void runIfSomething(final ContextualRunnable runnable) { + public void runIfSomething( + final ContextualRunnable runnable) { if (isSomething()) { runnable.setContext(get()); try { diff --git a/src/test/scala/net/abhinavsarkar/nulllessj/NulllessjSpec.scala b/src/test/scala/net/abhinavsarkar/nulllessj/NulllessjSpec.scala new file mode 100644 index 0000000..ca44fa0 --- /dev/null +++ b/src/test/scala/net/abhinavsarkar/nulllessj/NulllessjSpec.scala @@ -0,0 +1,126 @@ +package net.abhinavsarkar.nulllessj + +import org.scalatest.WordSpec +import org.scalatest.junit.{ShouldMatchersForJUnit,JUnitRunner} +import org.junit.runner.RunWith +import java.util.concurrent.Callable +import java.lang.String + +@RunWith(classOf[JUnitRunner]) +class NulllessjSpec extends WordSpec with ShouldMatchersForJUnit { + + "Nothing" should { + "return true for isNothing " in { + (new Nothing() isNothing) should be (true) + } + "return false for isSomething " in { + (new Nothing() isSomething) should be (false) + } + "have same hashcode for all instances " in { + (new Nothing() hashCode) should be (new Nothing() hashCode) + } + "have all instances equal " in { + (new Nothing) should be (new Nothing) + } + } + "Nothing" when { + "getting value from it" should { + "throw NothingException " in { + evaluating { (new Nothing) get } should produce [NothingException] + } + } + "getting value from it with default value" should { + "return the default value " in { + ((new Nothing) getOrElse "a") should be ("a") + } + } + } + + "Just" should { + "return false for isNothing " in { + (new Just(new {}) isNothing) should be (false) + } + "return true for isSomething " in { + (new Just(new {}) isSomething) should be (true) + } + } + "Just" when { + "created with a null value" should { + "throw NothingException " in { + evaluating { new Just(null) } should produce [NothingException] + } + } + "getting value from it" should { + "return the wrapped value " in { + (new Just("a") get) should be ("a") + } + } + "getting value from it with default value" should { + "return the wrapped value " in { + (new Just("a") getOrElse "b") should be ("a") + } + } + } + + val callable = new Callable[String] { def call() = "abc" } + val runnable = new Runnable { def run { throw new RuntimeException } } + val contextualCallable = new ContextualCallable[Object, String] { + def call() = "abc" + } + val contextualRunnable = new ContextualRunnable[Object] { + def run { throw new RuntimeException } + } + + "Maybe" when { + "created with a null value" should { + "return a Nothing " in { + Maybe.maybe(null).getClass should be (classOf[Nothing[_]]) + } + "have 'NONE' kind " in { + Maybe.maybe(null).kind should be (Maybe.MaybeKind.NONE) + } + "call the callable in 'callIfNothing' method " in { + Maybe.maybe(null).callIfNothing(callable).get should be ("abc") + } + "run the runnable in 'runIfNothing' method " in { + evaluating { + Maybe.maybe(null).runIfNothing(runnable)} should + produce [RuntimeException] + } + "not call the callable in 'callIfSomething' method " in { + evaluating { Maybe.maybe(null) + .callIfSomething(contextualCallable).get } should + produce [NothingException] + } + "not run the runnable in 'runIfSomething' method " in { + Maybe.maybe(null).runIfSomething(contextualRunnable) + } + } + "created with a non-null value" should { + "return a Just " in { + Maybe.maybe(new {}).getClass should be (classOf[Just[_]]) + } + "have 'SOME' kind " in { + Maybe.maybe(new {}).kind should be (Maybe.MaybeKind.SOME) + } + "not call the callable in 'callIfNothing' method " in { + evaluating { + Maybe.maybe(new {}).callIfNothing(callable).get } should + produce [NothingException] + } + "not run the runnable in 'runIfNothing' method " in { + Maybe.maybe(new {}).runIfNothing(runnable) + } + "call the callable in 'callIfSomething' method " in { + Maybe.maybe(new {}) + .callIfSomething(contextualCallable).get should be ("abc") + } + "run the runnable in 'runIfSomething' method " in { + evaluating { Maybe.maybe(new {}) + .runIfSomething(contextualRunnable) } should + produce [RuntimeException] + } + } + } + +}