From e9263934277ce2d719c13949971740866bfa1462 Mon Sep 17 00:00:00 2001 From: Heather Miller Date: Sun, 2 Jun 2013 09:04:24 +0200 Subject: [PATCH] Adding more JSON tests, including a failing JSON test due to #10 --- .../pickling/binary-check-array-int.scala | 17 - .../binary-check-case-class-int-string.scala | 20 -- .../pickling/json-non-primitive-field.scala | 19 ++ .../scala/pickling/pickling-binary-spec.scala | 148 -------- .../scala/pickling/pickling-json-spec.scala | 142 -------- .../test/scala/pickling/pickling-spec.scala | 316 ++++++++++++++++++ 6 files changed, 335 insertions(+), 327 deletions(-) delete mode 100644 core/src/test/scala/pickling/binary-check-array-int.scala delete mode 100644 core/src/test/scala/pickling/binary-check-case-class-int-string.scala create mode 100644 core/src/test/scala/pickling/json-non-primitive-field.scala delete mode 100644 core/src/test/scala/pickling/pickling-binary-spec.scala delete mode 100644 core/src/test/scala/pickling/pickling-json-spec.scala create mode 100644 core/src/test/scala/pickling/pickling-spec.scala diff --git a/core/src/test/scala/pickling/binary-check-array-int.scala b/core/src/test/scala/pickling/binary-check-array-int.scala deleted file mode 100644 index 726bb9d965..0000000000 --- a/core/src/test/scala/pickling/binary-check-array-int.scala +++ /dev/null @@ -1,17 +0,0 @@ -package scala.pickling.test - -import scala.pickling._ -import binary._ - -import org.scalacheck.Properties -import org.scalacheck.Prop.forAll - -object BinaryArrayIntSpecification extends Properties("Array[Int]") { - - property("pickle/unpickle") = forAll((ia: Array[Int]) => { - val pickle: BinaryPickle = ia.pickle - val readArr = pickle.unpickle[Array[Int]] - readArr.sameElements(ia) - }) - -} diff --git a/core/src/test/scala/pickling/binary-check-case-class-int-string.scala b/core/src/test/scala/pickling/binary-check-case-class-int-string.scala deleted file mode 100644 index 83a27b5bf3..0000000000 --- a/core/src/test/scala/pickling/binary-check-case-class-int-string.scala +++ /dev/null @@ -1,20 +0,0 @@ -package scala.pickling.test - -import scala.pickling._ -import binary._ - -import org.scalacheck.Properties -import org.scalacheck.Prop.forAll - -object BinaryCaseClassIntStringSpecification extends Properties("case class Int String") { - - case class Person(name: String, age: Int) - - property("pickle/unpickle") = forAll((name: String) => { - val p = Person(name, 43) - val pickle = p.pickle - val up = pickle.unpickle[Person] - p == up - }) - -} diff --git a/core/src/test/scala/pickling/json-non-primitive-field.scala b/core/src/test/scala/pickling/json-non-primitive-field.scala new file mode 100644 index 0000000000..e9cb6866e4 --- /dev/null +++ b/core/src/test/scala/pickling/json-non-primitive-field.scala @@ -0,0 +1,19 @@ +package scala.pickling.json.non.primitive.field + +import org.scalatest.FunSuite + import scala.pickling._ + import json._ + +case class Person(name: String, age: Int) +case class Philipp(mother: Person) + +class JSONNonPrimitiveFieldTest extends FunSuite { + test("main") { + val gudrun = Person("Gudrun", 62) + val p = Philipp(gudrun) + val pckl = p.pickle + + assert(pckl.value.mkString("[", ",", "]") === "[0,0,0,49,115,99,97,108,97,46,112,105,99,107,108,105,110,103,46,98,105,110,97,114,121,46,110,111,110,46,112,114,105,109,105,116,105,118,101,46,102,105,101,108,100,46,80,104,105,108,105,112,112,-1,0,0,0,6,71,117,100,114,117,110,0,0,0,62]") + assert(pckl.unpickle[Philipp] === p) + } +} diff --git a/core/src/test/scala/pickling/pickling-binary-spec.scala b/core/src/test/scala/pickling/pickling-binary-spec.scala deleted file mode 100644 index 84af6bddb2..0000000000 --- a/core/src/test/scala/pickling/pickling-binary-spec.scala +++ /dev/null @@ -1,148 +0,0 @@ -package scala.pickling.test - -import scala.pickling._ -import binary._ - -import org.scalacheck.{Properties, Prop, Arbitrary, Gen} -import Gen._ -import Arbitrary.arbitrary - -object PicklingBinarySpec extends Properties("pickling-binary") { - - sealed abstract class Base - final class C(s: String) extends Base { override def toString = "C" } - final class D(i: Int) extends Base { override def toString = "D" } - - implicit val arbitraryBase: Arbitrary[Base] = Arbitrary[Base]( - oneOf(arbitrary[String].map(s => new C(s)), - arbitrary[Int].map(i => new D(i)))) - - sealed abstract class CaseBase - case class CaseC(s: String) extends CaseBase - case class CaseD(i: Int) extends CaseBase - - implicit val arbitraryCaseBase: Arbitrary[CaseBase] = Arbitrary[CaseBase]( - oneOf(arbitrary[String].map(CaseC(_)), - arbitrary[Int].map(CaseD(_)))) - - // subsumes test base.scala - property("Base") = Prop forAll { (b: Base) => - val pickle: BinaryPickle = b.pickle - val b1 = pickle.unpickle[Base] - b1.toString == b.toString - } - - property("CaseBase") = Prop forAll { (x: CaseBase) => - val pickle: BinaryPickle = x.pickle - val x1 = pickle.unpickle[CaseBase] - x1 == x - } - - case class WithIntArray(a: Array[Int]) - - implicit val arbitraryWithIntArray: Arbitrary[WithIntArray] = - Arbitrary[WithIntArray](arbitrary[Array[Int]].map(WithIntArray(_))) - - property("case class with Array[Int] field") = Prop forAll { (x: WithIntArray) => - val pickle = x.pickle - val x1 = pickle.unpickle[WithIntArray] - x1 == x - true - } - - def emptyOrUnicode(s: String) = - s == "" || s.exists(_.toInt > 255) - - /* The following two tests subsume test tuple2-primitive.scala */ - - property("(String, Int)") = Prop forAll { (p: (String, Int)) => - if (emptyOrUnicode(p._1)) true //FIXME - else { - val pickle: BinaryPickle = p.pickle - val p1 = pickle.unpickle[(String, Int)] - p1 == p - } - } - - property("(String, Int, String)") = Prop forAll { (t: (String, Int, String)) => - if (emptyOrUnicode(t._1) || emptyOrUnicode(t._3)) true //FIXME - else { - val pickle: BinaryPickle = t.pickle - val t1 = pickle.unpickle[(String, Int, String)] - t1 == t - } - } - - property("(Int, (String, Int), Int)") = Prop forAll { (t: (Int, (String, Int), Int)) => - if (emptyOrUnicode(t._2._1)) true //FIXME - else { - val pickle: BinaryPickle = t.pickle - val t1 = pickle.unpickle[(Int, (String, Int), Int)] - t1 == t - } - } - - // subsumes test option-primitive.scala - property("Option[Int]") = Prop forAll { (opt: Option[Int]) => - val pickle: BinaryPickle = opt.pickle - val opt1 = pickle.unpickle[Option[Int]] - opt1 == opt - } - - property("Option[String]") = Prop forAll { (opt: Option[String]) => - val pickle: BinaryPickle = opt.pickle - val opt1 = pickle.unpickle[Option[String]] - opt1 == opt - } - - property("(Option[String], String)") = Prop forAll { (t: (Option[String], String)) => - if (emptyOrUnicode(t._2)) true //FIXME - else { - val pickle: BinaryPickle = t.pickle - val t1 = pickle.unpickle[(Option[String], String)] - t1 == t - } - } - - property("Option[Option[Int]]") = Prop forAll { (opt: Option[Option[Int]]) => - val pickle: BinaryPickle = opt.pickle - val opt1 = pickle.unpickle[Option[Option[Int]]] - opt1 == opt - } - - property("Option[CaseBase]") = Prop forAll { (opt: Option[CaseBase]) => - val pickle: BinaryPickle = opt.pickle - val opt1 = pickle.unpickle[Option[CaseBase]] - opt1 == opt - } - - property("Int") = Prop forAll { (x: Int) => - val pickle: BinaryPickle = x.pickle - val x1 = pickle.unpickle[Int] - x1 == x - } - - property("String") = Prop forAll { (x: String) => - val pickle: BinaryPickle = x.pickle - val x1 = pickle.unpickle[String] - x1 == x - } - - property("Long") = Prop forAll { (x: Long) => - val pickle = x.pickle - val x1 = pickle.unpickle[Long] - x1 == x - } - - property("Short") = Prop forAll { (x: Short) => - val pickle = x.pickle - val x1 = pickle.unpickle[Short] - x1 == x - } - - property("Boolean") = Prop forAll { (x: Boolean) => - val pickle = x.pickle - val x1 = pickle.unpickle[Boolean] - x1 == x - } -} diff --git a/core/src/test/scala/pickling/pickling-json-spec.scala b/core/src/test/scala/pickling/pickling-json-spec.scala deleted file mode 100644 index 4121d665bb..0000000000 --- a/core/src/test/scala/pickling/pickling-json-spec.scala +++ /dev/null @@ -1,142 +0,0 @@ -package scala.pickling.test - -import scala.pickling._ -import json._ - -import org.scalacheck.{Properties, Prop, Arbitrary, Gen} -import Gen._ -import Arbitrary.arbitrary - -object PicklingJsonSpec extends Properties("pickling-json") { - - sealed abstract class Base - final class C(s: String) extends Base { override def toString = "C" } - final class D(i: Int) extends Base { override def toString = "D" } - - implicit val arbitraryBase: Arbitrary[Base] = Arbitrary[Base]( - oneOf(arbitrary[String].map(s => new C(s)), - arbitrary[Int].map(i => new D(i)))) - - sealed abstract class CaseBase - case class CaseC(s: String) extends CaseBase - case class CaseD(i: Int) extends CaseBase - - implicit val arbitraryCaseBase: Arbitrary[CaseBase] = Arbitrary[CaseBase]( - oneOf(arbitrary[String].map(CaseC(_)), - arbitrary[Int].map(CaseD(_)))) - - // subsumes test base.scala - property("Base") = Prop forAll { (b: Base) => - val pickle: JSONPickle = b.pickle - val b1 = pickle.unpickle[Base] - b1.toString == b.toString - } - - property("CaseBase") = Prop forAll { (x: CaseBase) => - val pickle: JSONPickle = x.pickle - val x1 = pickle.unpickle[CaseBase] - x1 == x - } - - def emptyOrUnicode(s: String) = - s == "" || s.exists(_.toInt > 255) - - /* The following two tests subsume test tuple2-primitive.scala */ - - property("(String, Int)") = Prop forAll { (p: (String, Int)) => - if (emptyOrUnicode(p._1)) true //FIXME - else { - val pickle: JSONPickle = p.pickle - val p1 = pickle.unpickle[(String, Int)] - p1 == p - } - } - - property("(String, Int, String)") = Prop forAll { (t: (String, Int, String)) => - if (emptyOrUnicode(t._1) || emptyOrUnicode(t._3)) true //FIXME - else { - val pickle: JSONPickle = t.pickle - val t1 = pickle.unpickle[(String, Int, String)] - t1 == t - } - } - - property("(Int, (String, Int), Int)") = Prop forAll { (t: (Int, (String, Int), Int)) => - if (emptyOrUnicode(t._2._1)) true //FIXME - else { - val pickle: JSONPickle = t.pickle - val t1 = pickle.unpickle[(Int, (String, Int), Int)] - t1 == t - } - } - - // subsumes test option-primitive.scala - property("Option[Int]") = Prop forAll { (opt: Option[Int]) => - val pickle: JSONPickle = opt.pickle - val opt1 = pickle.unpickle[Option[Int]] - opt1 == opt - } - - property("Option[String]") = Prop forAll { (opt: Option[String]) => - val pickle: JSONPickle = opt.pickle - val opt1 = pickle.unpickle[Option[String]] - opt1 == opt - } - - property("(Option[String], String)") = Prop forAll { (t: (Option[String], String)) => - if (emptyOrUnicode(t._2)) true //FIXME - else { - val pickle: JSONPickle = t.pickle - val t1 = pickle.unpickle[(Option[String], String)] - t1 == t - } - } - - property("Option[Option[Int]]") = Prop forAll { (opt: Option[Option[Int]]) => - val pickle: JSONPickle = opt.pickle - val opt1 = pickle.unpickle[Option[Option[Int]]] - opt1 == opt - } - - property("Option[CaseBase]") = Prop forAll { (opt: Option[CaseBase]) => - val pickle: JSONPickle = opt.pickle - val opt1 = pickle.unpickle[Option[CaseBase]] - opt1 == opt - } - - property("Int") = Prop forAll { (x: Int) => - val pickle: JSONPickle = x.pickle - val x1 = pickle.unpickle[Int] - x1 == x - } - - property("String") = Prop forAll { (x: String) => - val pickle: JSONPickle = x.pickle - val x1 = pickle.unpickle[String] - x1 == x - } - - property("Long") = Prop forAll { (x: Long) => - val pickle: JSONPickle = x.pickle - val x1 = pickle.unpickle[Long] - x1 == x - } - - property("Short") = Prop forAll { (x: Short) => - val pickle: JSONPickle = x.pickle - val x1 = pickle.unpickle[Short] - x1 == x - } - - property("Boolean") = Prop forAll { (x: Boolean) => - val pickle: JSONPickle = x.pickle - val x1 = pickle.unpickle[Boolean] - x1 == x - } - - property("Byte") = Prop forAll { (x: Byte) => - val pickle: JSONPickle = x.pickle - val x1 = pickle.unpickle[Byte] - x1 == x - } -} diff --git a/core/src/test/scala/pickling/pickling-spec.scala b/core/src/test/scala/pickling/pickling-spec.scala new file mode 100644 index 0000000000..7c5b339be5 --- /dev/null +++ b/core/src/test/scala/pickling/pickling-spec.scala @@ -0,0 +1,316 @@ +package scala.pickling.test + +import scala.pickling._ + +import org.scalacheck.{Properties, Prop, Arbitrary, Gen} +import org.scalacheck.Prop.forAll +import Gen._ +import Arbitrary.arbitrary + +object PicklingSpec { + sealed abstract class Base + final class C(s: String) extends Base { override def toString = "C" } + final class D(i: Int) extends Base { override def toString = "D" } + + implicit val arbitraryBase: Arbitrary[Base] = Arbitrary[Base]( + oneOf(arbitrary[String].map(s => new C(s)), + arbitrary[Int].map(i => new D(i)))) + + sealed abstract class CaseBase + case class CaseC(s: String) extends CaseBase + case class CaseD(i: Int) extends CaseBase + + implicit val arbitraryCaseBase: Arbitrary[CaseBase] = Arbitrary[CaseBase]( + oneOf(arbitrary[String].map(CaseC(_)), + arbitrary[Int].map(CaseD(_)))) + + case class WithIntArray(a: Array[Int]) + + implicit val arbitraryWithIntArray: Arbitrary[WithIntArray] = + Arbitrary[WithIntArray](arbitrary[Array[Int]].map(WithIntArray(_))) + + case class Person(name: String, age: Int) +} + +/* ~~~~~~~~~~~~~~~~~~~~~ + * START OF JSON SPEC! + * ~~~~~~~~~~~~~~~~~~~~~*/ +object PicklingJsonSpec extends Properties("pickling-json") { + import json._ + import PicklingSpec._ + + // subsumes test base.scala + property("Base") = Prop forAll { (b: Base) => + val pickle: JSONPickle = b.pickle + val b1 = pickle.unpickle[Base] + b1.toString == b.toString + } + + property("CaseBase") = Prop forAll { (x: CaseBase) => + val pickle: JSONPickle = x.pickle + val x1 = pickle.unpickle[CaseBase] + x1 == x + } + + def emptyOrUnicode(s: String) = + s == "" || s.exists(_.toInt > 255) + + /* The following two tests subsume test tuple2-primitive.scala */ + + property("(String, Int)") = Prop forAll { (p: (String, Int)) => + if (emptyOrUnicode(p._1)) true //FIXME + else { + val pickle: JSONPickle = p.pickle + val p1 = pickle.unpickle[(String, Int)] + p1 == p + } + } + + property("(String, Int, String)") = Prop forAll { (t: (String, Int, String)) => + if (emptyOrUnicode(t._1) || emptyOrUnicode(t._3)) true //FIXME + else { + val pickle: JSONPickle = t.pickle + val t1 = pickle.unpickle[(String, Int, String)] + t1 == t + } + } + + property("(Int, (String, Int), Int)") = Prop forAll { (t: (Int, (String, Int), Int)) => + if (emptyOrUnicode(t._2._1)) true //FIXME + else { + val pickle: JSONPickle = t.pickle + val t1 = pickle.unpickle[(Int, (String, Int), Int)] + t1 == t + } + } + + // subsumes test option-primitive.scala + property("Option[Int]") = Prop forAll { (opt: Option[Int]) => + val pickle: JSONPickle = opt.pickle + val opt1 = pickle.unpickle[Option[Int]] + opt1 == opt + } + + property("Option[String]") = Prop forAll { (opt: Option[String]) => + val pickle: JSONPickle = opt.pickle + val opt1 = pickle.unpickle[Option[String]] + opt1 == opt + } + + property("(Option[String], String)") = Prop forAll { (t: (Option[String], String)) => + if (emptyOrUnicode(t._2)) true //FIXME + else { + val pickle: JSONPickle = t.pickle + val t1 = pickle.unpickle[(Option[String], String)] + t1 == t + } + } + + property("Option[Option[Int]]") = Prop forAll { (opt: Option[Option[Int]]) => + val pickle: JSONPickle = opt.pickle + val opt1 = pickle.unpickle[Option[Option[Int]]] + opt1 == opt + } + + property("Option[CaseBase]") = Prop forAll { (opt: Option[CaseBase]) => + val pickle: JSONPickle = opt.pickle + val opt1 = pickle.unpickle[Option[CaseBase]] + opt1 == opt + } + + property("Int") = Prop forAll { (x: Int) => + val pickle: JSONPickle = x.pickle + val x1 = pickle.unpickle[Int] + x1 == x + } + + property("String") = Prop forAll { (x: String) => + val pickle: JSONPickle = x.pickle + val x1 = pickle.unpickle[String] + x1 == x + } + + property("Long") = Prop forAll { (x: Long) => + val pickle: JSONPickle = x.pickle + val x1 = pickle.unpickle[Long] + x1 == x + } + + property("Short") = Prop forAll { (x: Short) => + val pickle: JSONPickle = x.pickle + val x1 = pickle.unpickle[Short] + x1 == x + } + + property("Boolean") = Prop forAll { (x: Boolean) => + val pickle: JSONPickle = x.pickle + val x1 = pickle.unpickle[Boolean] + x1 == x + } + + property("Byte") = Prop forAll { (x: Byte) => + val pickle: JSONPickle = x.pickle + val x1 = pickle.unpickle[Byte] + x1 == x + } + + property("Array") = forAll((ia: Array[Int]) => { + val pickle: JSONPickle = ia.pickle + val readArr = pickle.unpickle[Array[Int]] + readArr.sameElements(ia) + }) + + property("CaseClassIntString") = forAll((name: String) => { + val p = Person(name, 43) + val pickle: JSONPickle = p.pickle + val up = pickle.unpickle[Person] + p == up + }) + + property("case class with Array[Int] field") = Prop forAll { (x: WithIntArray) => + val pickle: JSONPickle = x.pickle + val x1 = pickle.unpickle[WithIntArray] + x1 == x + true + } +} + + +/* ~~~~~~~~~~~~~~~~~~~~~ + * START OF BINARY SPEC! + * ~~~~~~~~~~~~~~~~~~~~~*/ +object PicklingBinarySpec extends Properties("pickling-binary") { + import binary._ + import PicklingSpec._ + + // subsumes test base.scala + property("Base") = Prop forAll { (b: Base) => + val pickle: BinaryPickle = b.pickle + val b1 = pickle.unpickle[Base] + b1.toString == b.toString + } + + property("CaseBase") = Prop forAll { (x: CaseBase) => + val pickle: BinaryPickle = x.pickle + val x1 = pickle.unpickle[CaseBase] + x1 == x + } + + def emptyOrUnicode(s: String) = + s == "" || s.exists(_.toInt > 255) + + /* The following two tests subsume test tuple2-primitive.scala */ + + property("(String, Int)") = Prop forAll { (p: (String, Int)) => + if (emptyOrUnicode(p._1)) true //FIXME + else { + val pickle: BinaryPickle = p.pickle + val p1 = pickle.unpickle[(String, Int)] + p1 == p + } + } + + property("(String, Int, String)") = Prop forAll { (t: (String, Int, String)) => + if (emptyOrUnicode(t._1) || emptyOrUnicode(t._3)) true //FIXME + else { + val pickle: BinaryPickle = t.pickle + val t1 = pickle.unpickle[(String, Int, String)] + t1 == t + } + } + + property("(Int, (String, Int), Int)") = Prop forAll { (t: (Int, (String, Int), Int)) => + if (emptyOrUnicode(t._2._1)) true //FIXME + else { + val pickle: BinaryPickle = t.pickle + val t1 = pickle.unpickle[(Int, (String, Int), Int)] + t1 == t + } + } + + // subsumes test option-primitive.scala + property("Option[Int]") = Prop forAll { (opt: Option[Int]) => + val pickle: BinaryPickle = opt.pickle + val opt1 = pickle.unpickle[Option[Int]] + opt1 == opt + } + + property("Option[String]") = Prop forAll { (opt: Option[String]) => + val pickle: BinaryPickle = opt.pickle + val opt1 = pickle.unpickle[Option[String]] + opt1 == opt + } + + property("(Option[String], String)") = Prop forAll { (t: (Option[String], String)) => + if (emptyOrUnicode(t._2)) true //FIXME + else { + val pickle: BinaryPickle = t.pickle + val t1 = pickle.unpickle[(Option[String], String)] + t1 == t + } + } + + property("Option[Option[Int]]") = Prop forAll { (opt: Option[Option[Int]]) => + val pickle: BinaryPickle = opt.pickle + val opt1 = pickle.unpickle[Option[Option[Int]]] + opt1 == opt + } + + property("Option[CaseBase]") = Prop forAll { (opt: Option[CaseBase]) => + val pickle: BinaryPickle = opt.pickle + val opt1 = pickle.unpickle[Option[CaseBase]] + opt1 == opt + } + + property("Int") = Prop forAll { (x: Int) => + val pickle: BinaryPickle = x.pickle + val x1 = pickle.unpickle[Int] + x1 == x + } + + property("String") = Prop forAll { (x: String) => + val pickle: BinaryPickle = x.pickle + val x1 = pickle.unpickle[String] + x1 == x + } + + property("Long") = Prop forAll { (x: Long) => + val pickle: BinaryPickle = x.pickle + val x1 = pickle.unpickle[Long] + x1 == x + } + + property("Short") = Prop forAll { (x: Short) => + val pickle: BinaryPickle = x.pickle + val x1 = pickle.unpickle[Short] + x1 == x + } + + property("Boolean") = Prop forAll { (x: Boolean) => + val pickle: BinaryPickle = x.pickle + val x1 = pickle.unpickle[Boolean] + x1 == x + } + + // TODO add a test for Byte (binary)!! + + property("Array") = forAll((ia: Array[Int]) => { + val pickle: BinaryPickle = ia.pickle + val readArr = pickle.unpickle[Array[Int]] + readArr.sameElements(ia) + }) + + property("CaseClassIntString") = forAll((name: String) => { + val p = Person(name, 43) + val pickle: BinaryPickle = p.pickle + val up = pickle.unpickle[Person] + p == up + }) + + property("case class with Array[Int] field") = Prop forAll { (x: WithIntArray) => + val pickle: BinaryPickle = x.pickle + val x1 = pickle.unpickle[WithIntArray] + x1 == x + true + } +}