|
| 1 | +package refined4s.modules.chimney.derivation.types |
| 2 | + |
| 3 | +import hedgehog.* |
| 4 | +import hedgehog.runner.* |
| 5 | +import io.scalaland.chimney |
| 6 | +import io.scalaland.chimney.dsl.* |
| 7 | +import refined4s.types.TimeGens |
| 8 | +import refined4s.types.time.* |
| 9 | + |
| 10 | +/** @author Kevin Lee |
| 11 | + * @since 2025-09-01 |
| 12 | + */ |
| 13 | +trait timeSpec { |
| 14 | + |
| 15 | + protected val timeTypeClasses: refined4s.modules.chimney.derivation.types.time |
| 16 | + import timeTypeClasses.given |
| 17 | + |
| 18 | + def allTests: List[Test] = List( |
| 19 | + // Month |
| 20 | + property("test from Month to Int", testFromMonthToInt), |
| 21 | + property("test from Int to Month", testToIntToMonth), |
| 22 | + // Day |
| 23 | + property("test from Day to Int", testFromDayToInt), |
| 24 | + property("test from Int to Day", testToIntToDay), |
| 25 | + // Hour |
| 26 | + property("test from Hour to Int", testFromHourToInt), |
| 27 | + property("test from Int to Hour", testToIntToHour), |
| 28 | + // Minute |
| 29 | + property("test from Minute to Int", testFromMinuteToInt), |
| 30 | + property("test from Int to Minute", testToIntToMinute), |
| 31 | + // Second |
| 32 | + property("test from Second to Int", testFromSecondToInt), |
| 33 | + property("test from Int to Second", testToIntToSecond), |
| 34 | + // Millis |
| 35 | + property("test from Millis to Int", testFromMillisToInt), |
| 36 | + property("test from Int to Millis", testToIntToMillis), |
| 37 | + ) |
| 38 | + |
| 39 | + def testFromMonthToInt: Property = |
| 40 | + for { |
| 41 | + month <- TimeGens.genMonth.log("month") |
| 42 | + } yield { |
| 43 | + val input = month |
| 44 | + |
| 45 | + val expected = month.value |
| 46 | + val actual = input.into[Int].transform |
| 47 | + |
| 48 | + actual ==== expected |
| 49 | + } |
| 50 | + |
| 51 | + def testToIntToMonth: Property = |
| 52 | + for { |
| 53 | + month <- TimeGens.genMonth.log("month") |
| 54 | + } yield { |
| 55 | + val input = month.value |
| 56 | + |
| 57 | + val expected = chimney.partial.Result.fromValue(month) |
| 58 | + |
| 59 | + val actual = input.intoPartial[Month].transform |
| 60 | + |
| 61 | + actual ==== expected |
| 62 | + } |
| 63 | + |
| 64 | + def testFromDayToInt: Property = |
| 65 | + for { |
| 66 | + day <- TimeGens.genDay.log("day") |
| 67 | + } yield { |
| 68 | + val input = day |
| 69 | + |
| 70 | + val expected = day.value |
| 71 | + val actual = input.into[Int].transform |
| 72 | + |
| 73 | + actual ==== expected |
| 74 | + } |
| 75 | + |
| 76 | + def testToIntToDay: Property = |
| 77 | + for { |
| 78 | + day <- TimeGens.genDay.log("day") |
| 79 | + } yield { |
| 80 | + val input = day.value |
| 81 | + |
| 82 | + val expected = chimney.partial.Result.fromValue(day) |
| 83 | + |
| 84 | + val actual = input.intoPartial[Day].transform |
| 85 | + |
| 86 | + actual ==== expected |
| 87 | + } |
| 88 | + |
| 89 | + def testFromHourToInt: Property = |
| 90 | + for { |
| 91 | + hour <- TimeGens.genHour.log("hour") |
| 92 | + } yield { |
| 93 | + val input = hour |
| 94 | + |
| 95 | + val expected = hour.value |
| 96 | + val actual = input.into[Int].transform |
| 97 | + |
| 98 | + actual ==== expected |
| 99 | + } |
| 100 | + |
| 101 | + def testToIntToHour: Property = |
| 102 | + for { |
| 103 | + hour <- TimeGens.genHour.log("hour") |
| 104 | + } yield { |
| 105 | + val input = hour.value |
| 106 | + |
| 107 | + val expected = chimney.partial.Result.fromValue(hour) |
| 108 | + |
| 109 | + val actual = input.intoPartial[Hour].transform |
| 110 | + |
| 111 | + actual ==== expected |
| 112 | + } |
| 113 | + |
| 114 | + def testFromMinuteToInt: Property = |
| 115 | + for { |
| 116 | + minute <- TimeGens.genMinute.log("minute") |
| 117 | + } yield { |
| 118 | + val input = minute |
| 119 | + |
| 120 | + val expected = minute.value |
| 121 | + val actual = input.into[Int].transform |
| 122 | + |
| 123 | + actual ==== expected |
| 124 | + } |
| 125 | + |
| 126 | + def testToIntToMinute: Property = |
| 127 | + for { |
| 128 | + minute <- TimeGens.genMinute.log("minute") |
| 129 | + } yield { |
| 130 | + val input = minute.value |
| 131 | + |
| 132 | + val expected = chimney.partial.Result.fromValue(minute) |
| 133 | + |
| 134 | + val actual = input.intoPartial[Minute].transform |
| 135 | + |
| 136 | + actual ==== expected |
| 137 | + } |
| 138 | + |
| 139 | + def testFromSecondToInt: Property = |
| 140 | + for { |
| 141 | + second <- TimeGens.genSecond.log("second") |
| 142 | + } yield { |
| 143 | + val input = second |
| 144 | + |
| 145 | + val expected = second.value |
| 146 | + val actual = input.into[Int].transform |
| 147 | + |
| 148 | + actual ==== expected |
| 149 | + } |
| 150 | + |
| 151 | + def testToIntToSecond: Property = |
| 152 | + for { |
| 153 | + second <- TimeGens.genSecond.log("second") |
| 154 | + } yield { |
| 155 | + val input = second.value |
| 156 | + |
| 157 | + val expected = chimney.partial.Result.fromValue(second) |
| 158 | + |
| 159 | + val actual = input.intoPartial[Second].transform |
| 160 | + |
| 161 | + actual ==== expected |
| 162 | + } |
| 163 | + |
| 164 | + def testFromMillisToInt: Property = |
| 165 | + for { |
| 166 | + millis <- TimeGens.genMillis.log("millis") |
| 167 | + } yield { |
| 168 | + val input = millis |
| 169 | + |
| 170 | + val expected = millis.value |
| 171 | + val actual = input.into[Int].transform |
| 172 | + |
| 173 | + actual ==== expected |
| 174 | + } |
| 175 | + |
| 176 | + def testToIntToMillis: Property = |
| 177 | + for { |
| 178 | + millis <- TimeGens.genMillis.log("millis") |
| 179 | + } yield { |
| 180 | + val input = millis.value |
| 181 | + |
| 182 | + val expected = chimney.partial.Result.fromValue(millis) |
| 183 | + |
| 184 | + val actual = input.intoPartial[Millis].transform |
| 185 | + |
| 186 | + actual ==== expected |
| 187 | + } |
| 188 | + |
| 189 | +} |
| 190 | +object timeSpec extends Properties, timeSpec { |
| 191 | + |
| 192 | + override protected object timeTypeClasses extends refined4s.modules.chimney.derivation.types.time |
| 193 | + |
| 194 | + override def tests: List[Test] = allTests |
| 195 | + |
| 196 | +} |
0 commit comments