Skip to content

Commit

Permalink
test
Browse files Browse the repository at this point in the history
  • Loading branch information
kellen committed Aug 23, 2024
1 parent e7e5080 commit 141abd2
Show file tree
Hide file tree
Showing 4 changed files with 180 additions and 49 deletions.
5 changes: 3 additions & 2 deletions build.sbt
Original file line number Diff line number Diff line change
Expand Up @@ -388,7 +388,8 @@ lazy val shared = project
commonSettings,
crossScalaVersions := Seq(scala3, scala213, scala212),
moduleName := "magnolify-shared",
description := "Shared code for Magnolify"
description := "Shared code for Magnolify",
libraryDependencies += "org.scalacheck" %% "scalacheck" % scalacheckVersion % Test
)

// shared code for unit tests
Expand All @@ -408,7 +409,7 @@ lazy val test = project
lazy val scalacheck = project
.in(file("scalacheck"))
.dependsOn(
shared,
shared % "test->test,compile->compile",
test % "test->test"
)
.settings(
Expand Down
52 changes: 5 additions & 47 deletions scalacheck/src/test/scala/magnolify/scalacheck/TestArbitrary.scala
Original file line number Diff line number Diff line change
Expand Up @@ -17,18 +17,16 @@
package magnolify.scalacheck

import magnolify.scalacheck.semiauto.ArbitraryDerivation
import magnolify.shared.UnsafeEnum
import magnolify.test.ADT._
import magnolify.shared.{TimeArbitrary, UnsafeEnum}
import magnolify.test.ADT.*
import magnolify.test.JavaEnums
import magnolify.test.Simple._
import org.joda.{time => joda}
import org.scalacheck._
import magnolify.test.Simple.*
import org.scalacheck.*

import java.net.URI
import java.nio.ByteBuffer
import java.time._

object TestArbitrary {
object TestArbitrary extends TimeArbitrary {
// null
implicit lazy val arbNull: Arbitrary[Null] = Arbitrary(Gen.const(null))

Expand All @@ -44,46 +42,6 @@ object TestArbitrary {
Arbitrary.arbitrary[Array[Byte]].map(ByteBuffer.wrap)
}

// java-time
implicit lazy val arbInstant: Arbitrary[Instant] =
Arbitrary(Gen.posNum[Long].map(Instant.ofEpochMilli))
implicit lazy val arbLocalDate: Arbitrary[LocalDate] =
Arbitrary(Gen.chooseNum(0L, 365L * 100).map(LocalDate.ofEpochDay))
implicit lazy val arbLocalTime: Arbitrary[LocalTime] =
Arbitrary(arbInstant.arbitrary.map(_.atZone(ZoneOffset.UTC).toLocalTime))
implicit lazy val arbLocalDateTime: Arbitrary[LocalDateTime] =
Arbitrary(arbInstant.arbitrary.map(_.atZone(ZoneOffset.UTC).toLocalDateTime))
implicit lazy val arbOffsetTime: Arbitrary[OffsetTime] =
Arbitrary(arbInstant.arbitrary.map(_.atOffset(ZoneOffset.UTC).toOffsetTime))
implicit lazy val arbDuration: Arbitrary[Duration] =
Arbitrary(Gen.posNum[Long].map(Duration.ofMillis))

// joda-time
implicit val arbJodaDate: Arbitrary[joda.LocalDate] = Arbitrary {
Arbitrary.arbitrary[LocalDate].map { ld =>
new joda.LocalDate(ld.getYear, ld.getMonthValue, ld.getDayOfMonth)
}
}
implicit val arbJodaDateTime: Arbitrary[joda.DateTime] = Arbitrary {
Arbitrary.arbitrary[Instant].map { i =>
new joda.DateTime(i.toEpochMilli, joda.DateTimeZone.UTC)
}
}
implicit val arbJodaLocalTime: Arbitrary[joda.LocalTime] = Arbitrary {
Arbitrary.arbitrary[LocalTime].map { lt =>
joda.LocalTime.fromMillisOfDay(lt.toNanoOfDay / 1000)
}
}
implicit val arbJodaLocalDateTime: Arbitrary[joda.LocalDateTime] = Arbitrary {
Arbitrary.arbitrary[LocalDateTime].map { ldt =>
joda.LocalDateTime.parse(ldt.toString)
}
}
implicit val arbJodaDuration: Arbitrary[joda.Duration] =
Arbitrary(Gen.posNum[Long].map(joda.Duration.millis))
implicit val arbJodaInstant: Arbitrary[joda.Instant] =
Arbitrary(Gen.posNum[Long].map(l => new joda.Instant(l)))

// enum
implicit lazy val arbJavaEnum: Arbitrary[JavaEnums.Color] =
Arbitrary(Gen.oneOf(JavaEnums.Color.values.toSeq))
Expand Down
47 changes: 47 additions & 0 deletions shared/src/test/scala/magnolify/shared/TimeArbitrary.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
package magnolify.shared

import org.joda.time as joda
import org.scalacheck.{Arbitrary, Gen}

import java.time.{Duration, Instant, LocalDate, LocalDateTime, LocalTime, OffsetTime, ZoneOffset}

trait TimeArbitrary {
implicit lazy val arbInstant: Arbitrary[Instant] =
Arbitrary(Gen.posNum[Long].map(Instant.ofEpochMilli))
implicit lazy val arbLocalDate: Arbitrary[LocalDate] =
Arbitrary(Gen.chooseNum(0L, 365L * 100).map(LocalDate.ofEpochDay))
implicit lazy val arbLocalTime: Arbitrary[LocalTime] =
Arbitrary(arbInstant.arbitrary.map(_.atZone(ZoneOffset.UTC).toLocalTime))
implicit lazy val arbLocalDateTime: Arbitrary[LocalDateTime] =
Arbitrary(arbInstant.arbitrary.map(_.atZone(ZoneOffset.UTC).toLocalDateTime))
implicit lazy val arbOffsetTime: Arbitrary[OffsetTime] =
Arbitrary(arbInstant.arbitrary.map(_.atOffset(ZoneOffset.UTC).toOffsetTime))
implicit lazy val arbDuration: Arbitrary[Duration] =
Arbitrary(Gen.posNum[Long].map(Duration.ofMillis))

implicit val arbJodaDate: Arbitrary[joda.LocalDate] = Arbitrary {
Arbitrary.arbitrary[LocalDate].map { ld =>
new joda.LocalDate(ld.getYear, ld.getMonthValue, ld.getDayOfMonth)
}
}
implicit val arbJodaDateTime: Arbitrary[joda.DateTime] = Arbitrary {
Arbitrary.arbitrary[Instant].map { i =>
new joda.DateTime(i.toEpochMilli, joda.DateTimeZone.UTC)
}
}
implicit val arbJodaLocalTime: Arbitrary[joda.LocalTime] = Arbitrary {
Arbitrary.arbitrary[LocalTime].map { lt =>
joda.LocalTime.fromMillisOfDay(lt.toNanoOfDay / 1000)
}
}
implicit val arbJodaLocalDateTime: Arbitrary[joda.LocalDateTime] = Arbitrary {
Arbitrary.arbitrary[LocalDateTime].map { ldt =>
joda.LocalDateTime.parse(ldt.toString)
}
}
implicit val arbJodaDuration: Arbitrary[joda.Duration] =
Arbitrary(Gen.posNum[Long].map(joda.Duration.millis))
implicit val arbJodaInstant: Arbitrary[joda.Instant] =
Arbitrary(Gen.posNum[Long].map(l => new joda.Instant(l)))

}
125 changes: 125 additions & 0 deletions shared/src/test/scala/magnolify/shared/TimeSpec.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,125 @@
package magnolify.shared

import org.joda.time as joda
import org.scalacheck.*
import org.scalacheck.Prop.forAll

class TimeSpec extends Properties("Time") with TimeArbitrary {
import Time._

case class Convert[T, U: Arbitrary, V: Arbitrary](
name: String,
javaTo: T => U,
javaFrom: U => T,
jodaTo: T => V,
jodaFrom: V => T
) {
def java =
property(name) = forAll((u: U) => (javaFrom andThen javaTo)(u) == u)
def joda =
property(s"$name-joda") = forAll((v: V) => (jodaFrom andThen jodaTo)(v) == v)
def roundtrip =
property(s"$name-roundtrip") =
forAll((u: U) => (javaFrom andThen jodaTo andThen jodaFrom andThen javaTo)(u) == u)
}

val conversions: List[Convert[?, ?, ?]] = List(
Convert(
"millis-instant",
millisToInstant,
millisFromInstant,
millisToJodaInstant,
millisFromJodaInstant
),
Convert(
"millis-localtime",
millisToLocalTime,
millisFromLocalTime,
millisToJodaLocalTime,
millisFromJodaLocalTime
),
Convert(
"millis-localdatetime",
millisToLocalDateTime,
millisFromLocalDateTime,
millisToJodaLocalDateTime,
millisFromJodaLocalDateTime
),
Convert(
"millis-duration",
millisToDuration,
millisFromDuration,
millisToJodaDuration,
millisFromJodaDuration
),
Convert(
"micros-instant",
microsToInstant,
microsFromInstant,
microsToJodaInstant,
microsFromJodaInstant
),
Convert(
"micros-localtime",
microsToLocalTime,
microsFromLocalTime,
microsToJodaLocalTime,
microsFromJodaLocalTime
),
Convert(
"micros-localdatetime",
microsToLocalDateTime,
microsFromLocalDateTime,
microsToJodaLocalDateTime,
microsFromJodaLocalDateTime
),
Convert(
"micros-duration",
microsToDuration,
microsFromDuration,
microsToJodaDuration,
microsFromJodaDuration
),
Convert(
"nanos-instant",
nanosToInstant,
nanosFromInstant,
nanosToJodaInstant,
nanosFromJodaInstant
),
Convert(
"nanos-localtime",
nanosToLocalTime,
nanosFromLocalTime,
nanosToJodaLocalTime,
nanosFromJodaLocalTime
),
Convert(
"nanos-localdatetime",
nanosToLocalDateTime,
nanosFromLocalDateTime,
nanosToJodaLocalDateTime,
nanosFromJodaLocalDateTime
),
Convert(
"nanos-duration",
nanosToDuration,
nanosFromDuration,
nanosToJodaDuration,
nanosFromJodaDuration
)
)

conversions.foreach { c =>
c.java
c.joda
c.roundtrip
}

property(s"millis-datetime-joda") =
forAll((v: joda.DateTime) => (millisFromJodaDateTime andThen millisToJodaDateTime)(v) == v)
property(s"micros-datetime-joda") =
forAll((v: joda.DateTime) => (microsFromJodaDateTime andThen microsToJodaDateTime)(v) == v)
property(s"nanos-datetime-joda") =
forAll((v: joda.DateTime) => (nanosFromJodaDateTime andThen nanosToJodaDateTime)(v) == v)
}

0 comments on commit 141abd2

Please sign in to comment.