-
Notifications
You must be signed in to change notification settings - Fork 26
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
180 additions
and
49 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
47 changes: 47 additions & 0 deletions
47
shared/src/test/scala/magnolify/shared/TimeArbitrary.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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))) | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} |