|
| 1 | +package scalachecklib |
| 2 | + |
| 3 | +import org.scalatest.Matchers |
| 4 | +import org.scalatest.prop.Checkers |
| 5 | + |
| 6 | +/** scalacheck-datetime is a library for helping use datetime libraries with ScalaCheck |
| 7 | + * |
| 8 | + * The motivation behind this library is to provide a simple, easy way to provide generated date and time instances |
| 9 | + * that are useful to your own domain. |
| 10 | + * |
| 11 | + * For SBT, you can add the dependency to your project’s build file: |
| 12 | + * |
| 13 | + * {{{ |
| 14 | + * resolvers += Resolver.sonatypeRepo("releases") |
| 15 | + * |
| 16 | + * "com.fortysevendeg" %% "scalacheck-datetime" % "0.2.0" % "test" |
| 17 | + * }}} |
| 18 | + * |
| 19 | + * Please, visit the [[https://47deg.github.io/scalacheck-datetime homepage]] for more information |
| 20 | + * |
| 21 | + * @param name scalacheck-datetime |
| 22 | + */ |
| 23 | +object ScalacheckDatetimeSection extends Checkers with Matchers with org.scalaexercises.definitions.Section { |
| 24 | + |
| 25 | + /** ==Usage== |
| 26 | + * |
| 27 | + * To arbitrarily generate dates and times, you need to have the `Arbitrary` in scope for your date/time class. |
| 28 | + * Assuming Joda Time: |
| 29 | + */ |
| 30 | + def usage(res0: Boolean) = { |
| 31 | + |
| 32 | + import com.fortysevendeg.scalacheck.datetime.joda.ArbitraryJoda._ |
| 33 | + import org.joda.time.DateTime |
| 34 | + import org.scalacheck.Prop.forAll |
| 35 | + |
| 36 | + check { |
| 37 | + forAll { dt: DateTime => |
| 38 | + (dt.getDayOfMonth >= 1 && dt.getDayOfMonth <= 31) == res0 |
| 39 | + } |
| 40 | + } |
| 41 | + } |
| 42 | + |
| 43 | + /** ==A note on imports== |
| 44 | + * |
| 45 | + * For all of the examples given in this document, you can substitute `jdk8` for `joda` and vice-versa, |
| 46 | + * depending on which library you would like to generate instances for. |
| 47 | + * |
| 48 | + * ==Implementation== |
| 49 | + * |
| 50 | + * The infrastructure behind the generation of date/time instances for any given date/time library, |
| 51 | + * which may take ranges into account, is done using a fairly simple typeclass, which has the type signature |
| 52 | + * `ScalaCheckDateTimeInfra[D, R]`. That is to say, as long as there is an implicit `ScalaCheckDateTimeInfra` |
| 53 | + * instance in scope for a given date/time type `D` (such as Joda’s `DateTime`) and a range type `R` |
| 54 | + * (such as Joda’s `Period`), then the code will compile and be able to provide generated date/time instances. |
| 55 | + * |
| 56 | + * As stated, currently there are two instances, `ScalaCheckDateTimeInfra[DateTime, Period]` for Joda Time and |
| 57 | + * `ScalaCheckDateTimeInfra[ZonedDateTime, Duration]` for Java SE 8’s Date and Time. |
| 58 | + * |
| 59 | + * ==Granularity== |
| 60 | + * |
| 61 | + * If you wish to restrict the precision of the generated instances, this library refers to that as <i>granularity</i>. |
| 62 | + * |
| 63 | + * You can constrain the granularity to: |
| 64 | + * |
| 65 | + * <ul> |
| 66 | + * <li>Seconds</li> |
| 67 | + * <li>Minutes</li> |
| 68 | + * <li>Hours</li> |
| 69 | + * <li>Days</li> |
| 70 | + * <li>Years</li> |
| 71 | + * </ul> |
| 72 | + * |
| 73 | + * When a value is constrained, the time fields are set to zero, and the rest to the first day of the month, |
| 74 | + * or day of the year. For example, if you constrain a field to be years, the generated instance will be midnight |
| 75 | + * exactly, on the first day of January. |
| 76 | + * |
| 77 | + * To constrain a generated type, you simply need to provide an import for the typeclass for your date/time and |
| 78 | + * range, and also an import for the granularity. As an example, this time using Java SE 8's `java.time` package: |
| 79 | + */ |
| 80 | + def granularity(res0: Int, res1: Int, res2: Int, res3: Int, res4: Int) = { |
| 81 | + |
| 82 | + import java.time._ |
| 83 | + import com.fortysevendeg.scalacheck.datetime.jdk8.ArbitraryJdk8._ |
| 84 | + import com.fortysevendeg.scalacheck.datetime.jdk8.granularity.years |
| 85 | + import org.scalacheck.Prop.forAll |
| 86 | + |
| 87 | + check { |
| 88 | + forAll { zdt: ZonedDateTime => |
| 89 | + zdt.getMonth == Month.JANUARY |
| 90 | + (zdt.getDayOfMonth == res0) && |
| 91 | + (zdt.getHour == res1) && |
| 92 | + (zdt.getMinute == res2) && |
| 93 | + (zdt.getSecond == res3) && |
| 94 | + (zdt.getNano == res4) |
| 95 | + } |
| 96 | + } |
| 97 | + |
| 98 | + } |
| 99 | + |
| 100 | + /** ==Creating Ranges== |
| 101 | + * |
| 102 | + * You can generate date/time instances only within a certain range, using the `genDateTimeWithinRange` in the |
| 103 | + * `GenDateTime` class. The function takes two parameters, the date/time instances as a base from which to generate |
| 104 | + * new date/time instances, and a range for the generated instances. |
| 105 | + * |
| 106 | + * If the range is positive, it will be in the future from the base date/time, negative in the past. |
| 107 | + * |
| 108 | + * Showing this usage with Joda Time: |
| 109 | + */ |
| 110 | + def ranges(res0: Int) = { |
| 111 | + |
| 112 | + import org.joda.time._ |
| 113 | + import com.fortysevendeg.scalacheck.datetime.instances.joda._ |
| 114 | + import com.fortysevendeg.scalacheck.datetime.GenDateTime.genDateTimeWithinRange |
| 115 | + import org.scalacheck.Prop.forAll |
| 116 | + |
| 117 | + val from = new DateTime(2016, 1, 1, 0, 0) |
| 118 | + val range = Period.years(1) |
| 119 | + |
| 120 | + check { |
| 121 | + forAll(genDateTimeWithinRange(from, range)) { dt => |
| 122 | + dt.getYear == res0 |
| 123 | + } |
| 124 | + } |
| 125 | + } |
| 126 | + |
| 127 | + /** ==Using Granularity and Ranges Together== |
| 128 | + * |
| 129 | + * As you would expect, it is possible to use the granularity and range concepts together. |
| 130 | + * This example should not show anything surprising by now: |
| 131 | + */ |
| 132 | + def granularityAndRanges(res0: Int, res1: Int, res2: Int, res3: Int, res4: Int) = { |
| 133 | + |
| 134 | + import org.joda.time._ |
| 135 | + import com.fortysevendeg.scalacheck.datetime.instances.joda._ |
| 136 | + import com.fortysevendeg.scalacheck.datetime.GenDateTime.genDateTimeWithinRange |
| 137 | + import com.fortysevendeg.scalacheck.datetime.joda.granularity.days |
| 138 | + import org.scalacheck.Prop.forAll |
| 139 | + |
| 140 | + val from = new DateTime(2016, 1, 1, 0, 0) |
| 141 | + val range = Period.years(1) |
| 142 | + |
| 143 | + check { |
| 144 | + forAll(genDateTimeWithinRange(from, range)) { dt => |
| 145 | + (dt.getYear == res0) && |
| 146 | + (dt.getHourOfDay == res1) && |
| 147 | + (dt.getMinuteOfHour == res2) && |
| 148 | + (dt.getSecondOfMinute == res3) && |
| 149 | + (dt.getMillisOfSecond == res4) |
| 150 | + } |
| 151 | + } |
| 152 | + } |
| 153 | +} |
0 commit comments