Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Slightly less trivial working tests

  • Loading branch information...
commit 86a1cf8e64ad425017648a29dc2601014bb3ac44 1 parent 222234a
@jcoveney jcoveney authored
View
1  .gitignore
@@ -29,6 +29,7 @@
/.cache
/.idea
/.settings
+.idea_modules
# bak files produced by ./cleanup-commit
*.bak
View
2  bijection-core/src/main/scala/com/twitter/bijection/Bijection.scala
@@ -57,7 +57,7 @@ trait Bijection[A, B] extends (A => B) with Serializable { self =>
/**
* Abstract class to ease Bijection creation from Java.
*/
-abstract class BijectionImpl[A, B] extends Bijection[A, B] {
+abstract class AbstractBijection[A, B] extends Bijection[A, B] {
override def apply(a: A): B
override def invert(b: B): A
View
49 bijection-core/src/test/java/com/twitter/bijection/HelloWorld.java
@@ -1,49 +0,0 @@
-package com.twitter.bijection;
-
-import org.junit.Test;
-import scala.Function1;
-
-import static junit.framework.Assert.assertEquals;
-
-/**
- * Created with IntelliJ IDEA.
- * User: jcoveney
- * Date: 1/11/13
- * Time: 2:05 PM
- * To change this template use File | Settings | File Templates.
- */
-public class HelloWorld {
- @Test
- public void test() {
- roundTrip(new StringToLong(), "21", 21L);
- }
-
- //TODO also test that composition works
-
- private <T1, T2> void roundTrip(Bijection<T1,T2> bij, T1 d1, T2 d2) {
- Bijection<T2,T1> inv = bij.inverse();
-
- assertEquals(d2, bij.apply(d1));
- assertEquals(d2, inv.invert(d1));
-
- assertEquals(d1, bij.invert(bij.apply(d1)));
- assertEquals(d1, inv.apply(bij.apply(d1)));
-
-
-
-
-
- }
-
- public static class StringToLong extends BijectionImpl<String,Long> {
- @Override
- public Long apply(String a) {
- return Long.parseLong(a);
- }
-
- @Override
- public String invert(Long b) {
- return b.toString();
- }
- }
-}
View
78 bijection-core/src/test/java/com/twitter/bijection/TestBijectionInJava.java
@@ -0,0 +1,78 @@
+package com.twitter.bijection;
+
+import org.junit.Test;
+
+import static junit.framework.Assert.assertEquals;
+
+/**
+ * Bijection is as useful in Java as in Scala, so these tests ensure correct
+ * functionality while providing an example of use and implementation from Java.
+ */
+public class TestBijectionInJava {
+ @Test
+ public void testBasicBijection() {
+ Bijection<String,Long> s2l = new StringToLong();
+
+ for (long l = -1000; l < 1000; l++) {
+ String s = Long.valueOf(l).toString();
+ roundTrip(s2l, s, l);
+ roundTrip(s2l.inverse(), l, s);
+
+ roundTrip(s2l.andThen(s2l.inverse()).inverse(), s, s);
+ roundTrip(s2l.inverse().compose(s2l), s, s);
+ roundTrip(s2l.inverse().compose(s2l).inverse(), s, s);
+ }
+ }
+
+ @Test
+ public void testBase64Bijection() {
+ Bijection$.MODULE$.bytes2Base64(); //TODO make access to this cleaner from Java
+ }
+
+ @Test
+ public void testBase64BijectionGzip() {
+
+ }
+
+ private <T1, T2> void roundTrip(Bijection<T1,T2> bij, T1 d1, T2 d2) {
+ Bijection<T2,T1> inv = bij.inverse();
+
+ assertEquals(d2, bij.apply(d1));
+ assertEquals(d2, inv.invert(d1));
+
+ assertEquals(d1, bij.invert(bij.apply(d1)));
+ assertEquals(d1, bij.invert(inv.invert(d1)));
+ assertEquals(d1, inv.apply(bij.apply(d1)));
+ assertEquals(d1, inv.apply(inv.invert(d1)));
+
+ assertEquals(d1, bij.invert(d2));
+ assertEquals(d1, inv.apply(d2));
+
+ assertEquals(d2, bij.apply(bij.invert(d2)));
+ assertEquals(d2, bij.apply(inv.apply(d2)));
+ assertEquals(d2, inv.invert(bij.invert(d2)));
+ assertEquals(d2, inv.invert(inv.apply(d2)));
+
+ assertEquals(d1, bij.andThen(bij.inverse()).apply(d1));
+ assertEquals(d1, bij.andThen(bij.inverse()).inverse().apply(d1));
+ assertEquals(d1, bij.inverse().compose(bij).apply(d1));
+ assertEquals(d1, (bij.inverse().compose(bij).inverse().apply(d1)));
+
+ assertEquals(d2, inv.andThen(inv.inverse()).apply(d2));
+ assertEquals(d2, inv.andThen(inv.inverse()).inverse().apply(d2));
+ assertEquals(d2, inv.inverse().compose(inv).apply(d2));
+ assertEquals(d2, (inv.inverse().compose(inv).inverse().apply(d2)));
+ }
+
+ public static class StringToLong extends AbstractBijection<String,Long> {
+ @Override
+ public Long apply(String a) {
+ return Long.parseLong(a);
+ }
+
+ @Override
+ public String invert(Long b) {
+ return b.toString();
+ }
+ }
+}
View
8 bijection-core/src/test/scala/com/twitter/bijection/JavaTests.scala
@@ -0,0 +1,8 @@
+package com.twitter.bijection
+
+import org.scalatest.junit.JUnitSuite
+
+/**
+ * This is a dummy class to ensure "sbt test" runs the JUnit tests.
+ */
+class JavaTests extends TestBijectionInJava with JUnitSuite
View
6 project/Build.scala
@@ -86,7 +86,11 @@ object BijectionBuild extends Build {
settings = sharedSettings
).settings(
name := "bijection-core",
- libraryDependencies ++= Seq("commons-codec" % "commons-codec" % "1.7", "com.novocode" % "junit-interface" % "0.10-M1" % "test")
+ libraryDependencies ++= Seq(
+ "commons-codec" % "commons-codec" % "1.7",
+ "com.novocode" % "junit-interface" % "0.10-M1" % "test",
+ "org.scalatest" %% "scalatest" % "1.6.1" % "test"
+ )
)
lazy val bijectionProtobuf = Project(
Please sign in to comment.
Something went wrong with that request. Please try again.