added Scalatest spec. related changes in pom

master
Abhinav Sarkar 2010-06-02 03:05:45 +05:30
parent 6a3ae6ac23
commit 322d33358f
6 changed files with 223 additions and 8 deletions

View File

@ -2,8 +2,8 @@
<classpath>
<classpathentry kind="src" output="target/classes" path="src/main/java"/>
<classpathentry kind="src" output="target/test-classes" path="src/test/java"/>
<classpathentry kind="src" path="src/test/scala"/>
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-1.6"/>
<classpathentry kind="con" path="org.maven.ide.eclipse.MAVEN2_CLASSPATH_CONTAINER"/>
<classpathentry kind="con" path="org.eclipse.jdt.junit.JUNIT_CONTAINER/4"/>
<classpathentry kind="output" path="target/classes"/>
</classpath>

View File

@ -1,5 +1,17 @@
<?xml version="1.0" encoding="UTF-8"?>
<module org.jetbrains.idea.maven.project.MavenProjectsManager.isMavenModule="true" type="JAVA_MODULE" version="4">
<component name="FacetManager">
<facet type="Scala" name="Scala">
<configuration>
<option name="takeFromSettings" value="true" />
<option name="myScalaSdkJarPaths">
<array>
<option value="$MAVEN_REPOSITORY$/org/scala-lang/scala-library/2.7.7/scala-library-2.7.7.jar" />
</array>
</option>
</configuration>
</facet>
</component>
<component name="NewModuleRootManager" LANGUAGE_LEVEL="JDK_1_6" inherit-compiler-output="false">
<output url="file://$MODULE_DIR$/target/classes" />
<output-test url="file://$MODULE_DIR$/target/test-classes" />
@ -7,10 +19,14 @@
<sourceFolder url="file://$MODULE_DIR$/target/generated-sources/annotations" isTestSource="false" />
<sourceFolder url="file://$MODULE_DIR$/src/main/java" isTestSource="false" />
<sourceFolder url="file://$MODULE_DIR$/src/test/java" isTestSource="true" />
<sourceFolder url="file://$MODULE_DIR$/src/test/scala" isTestSource="true" />
<excludeFolder url="file://$MODULE_DIR$/target/classes" />
<excludeFolder url="file://$MODULE_DIR$/target/test-classes" />
</content>
<orderEntry type="inheritedJdk" />
<orderEntry type="sourceFolder" forTests="false" />
<orderEntry type="library" exported="" name="Maven: org.scala-lang:scala-library:2.7.7" level="project" />
<orderEntry type="library" scope="TEST" name="Maven: org.scalatest:scalatest:1.0" level="project" />
<orderEntry type="library" scope="TEST" name="Maven: junit:junit:4.4" level="project" />
</component>
</module>

75
pom.xml
View File

@ -12,6 +12,7 @@
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<scala.version>2.7.7</scala.version>
</properties>
<build>
@ -25,10 +26,80 @@
<target>1.6</target>
</configuration>
</plugin>
<plugin>
<groupId>org.scala-tools</groupId>
<artifactId>maven-scala-plugin</artifactId>
<version>2.12</version>
<executions>
<execution>
<goals>
<goal>compile</goal>
<goal>testCompile</goal>
</goals>
</execution>
</executions>
<configuration>
<scalaVersion>${scala.version}</scalaVersion>
<includes>
<include>**/*.scala</include>
</includes>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.5</version>
<configuration>
<reportFormat>plain</reportFormat>
<includes>
<include>**/*Spec.class</include>
</includes>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-report-plugin</artifactId>
<version>2.5</version>
<executions>
<execution>
<phase>test</phase>
<goals>
<goal>report-only</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
<pluginRepositories>
<pluginRepository>
<id>scala-tools.org</id>
<name>Scala-tools Maven2 Repository</name>
<url>http://scala-tools.org/repo-releases</url>
</pluginRepository>
</pluginRepositories>
<repositories>
<repository>
<id>scala-tools.org</id>
<name>Scala-Tools Maven2 Repository</name>
<url>http://scala-tools.org/repo-releases</url>
</repository>
</repositories>
<dependencies>
<dependency>
<groupId>org.scala-lang</groupId>
<artifactId>scala-library</artifactId>
<version>${scala.version}</version>
</dependency>
<dependency>
<groupId>org.scalatest</groupId>
<artifactId>scalatest</artifactId>
<version>1.0</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
@ -36,5 +107,5 @@
<scope>test</scope>
</dependency>
</dependencies>
</project>

View File

@ -58,7 +58,7 @@ public final class Just<T> extends Maybe<T> {
@Override
public String toString() {
return value.toString();
return "Just(" + value.toString() + ")";
}
}

View File

@ -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<T> {
*
* @see Maybe#maybe
*/
public <V> Maybe<V> callIfSomething(final ContextualCallable<T,V> callable) {
public <V> Maybe<V> callIfSomething(
final ContextualCallable<? super T,V> callable) {
if (isSomething()) {
callable.setContext(get());
try {
@ -190,7 +191,8 @@ public abstract class Maybe<T> {
*
* @throws RuntimeException See the description.
*/
public void runIfSomething(final ContextualRunnable<T> runnable) {
public void runIfSomething(
final ContextualRunnable<? super T> runnable) {
if (isSomething()) {
runnable.setContext(get());
try {

View File

@ -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]
}
}
}
}