diff options
author | Eduardo Julian | 2019-02-05 19:09:31 -0400 |
---|---|---|
committer | Eduardo Julian | 2019-02-05 19:09:31 -0400 |
commit | 47b97c128bde837fa803a605f3e011a3e9ddd71c (patch) | |
tree | 5e8a84d1b1812ec4a157d4049c778ec2e4e434c4 /stdlib/source/test/lux/time | |
parent | be5710d104e6ee085dcb9d871be0b80305e48f8b (diff) |
Integrated tests into normal source code.
Diffstat (limited to 'stdlib/source/test/lux/time')
-rw-r--r-- | stdlib/source/test/lux/time/date.lux | 147 | ||||
-rw-r--r-- | stdlib/source/test/lux/time/duration.lux | 60 | ||||
-rw-r--r-- | stdlib/source/test/lux/time/instant.lux | 99 |
3 files changed, 306 insertions, 0 deletions
diff --git a/stdlib/source/test/lux/time/date.lux b/stdlib/source/test/lux/time/date.lux new file mode 100644 index 000000000..d89ccccc8 --- /dev/null +++ b/stdlib/source/test/lux/time/date.lux @@ -0,0 +1,147 @@ +(.module: + [lux #* + [control + [monad (#+ Monad do)] + pipe] + [data + ["." error]] + [math + ["r" random ("random/." monad)]] + [time + ["@." instant] + ["@" date]]] + lux/test + [// + ["_." instant]]) + +(def: month + (r.Random @.Month) + (r.either (r.either (r.either (random/wrap #@.January) + (r.either (random/wrap #@.February) + (random/wrap #@.March))) + (r.either (random/wrap #@.April) + (r.either (random/wrap #@.May) + (random/wrap #@.June)))) + (r.either (r.either (random/wrap #@.July) + (r.either (random/wrap #@.August) + (random/wrap #@.September))) + (r.either (random/wrap #@.October) + (r.either (random/wrap #@.November) + (random/wrap #@.December)))))) + +(context: "(Month) Equivalence." + (<| (times 100) + (do @ + [sample month + #let [(^open "@/.") @.equivalence]] + (test "Every value equals itself." + (@/= sample sample))))) + +(context: "(Month) Order." + (<| (times 100) + (do @ + [reference month + sample month + #let [(^open "@/.") @.order]] + (test "Valid Order." + (and (or (@/< reference sample) + (@/>= reference sample)) + (or (@/> reference sample) + (@/<= reference sample))))))) + +(context: "(Month) Enum." + (<| (times 100) + (do @ + [sample month + #let [(^open "@/.") @.enum]] + (test "Valid Enum." + (and (not (@/= (@/succ sample) + sample)) + (not (@/= (@/pred sample) + sample)) + (|> sample @/succ @/pred (@/= sample)) + (|> sample @/pred @/succ (@/= sample))))))) + +(def: day + (r.Random @.Day) + (r.either (r.either (r.either (random/wrap #@.Sunday) + (random/wrap #@.Monday)) + (r.either (random/wrap #@.Tuesday) + (random/wrap #@.Wednesday))) + (r.either (r.either (random/wrap #@.Thursday) + (random/wrap #@.Friday)) + (random/wrap #@.Saturday)))) + +(context: "(Day) Equivalence." + (<| (times 100) + (do @ + [sample day + #let [(^open "@/.") @.equivalence]] + (test "Every value equals itself." + (@/= sample sample))))) + +(context: "(Day) Order." + (<| (times 100) + (do @ + [reference day + sample day + #let [(^open "@/.") @.order]] + (test "Valid Order." + (and (or (@/< reference sample) + (@/>= reference sample)) + (or (@/> reference sample) + (@/<= reference sample))))))) + +(context: "(Day) Enum." + (<| (times 100) + (do @ + [sample day + #let [(^open "@/.") @.enum]] + (test "Valid Enum." + (and (not (@/= (@/succ sample) + sample)) + (not (@/= (@/pred sample) + sample)) + (|> sample @/succ @/pred (@/= sample)) + (|> sample @/pred @/succ (@/= sample))))))) + +(def: #export date + (r.Random @.Date) + (|> _instant.instant (:: r.monad map @instant.date))) + +(context: "(Date) Equivalence." + (<| (times 100) + (do @ + [sample date + #let [(^open "@/.") @.equivalence]] + (test "Every value equals itself." + (@/= sample sample))))) + +(context: "(Date) Order." + (<| (times 100) + (do @ + [reference date + sample date + #let [(^open "@/.") @.order]] + (test "Valid Order." + (and (or (@/< reference sample) + (@/>= reference sample)) + (or (@/> reference sample) + (@/<= reference sample))))))) + +(context: "(Date) Codec" + (<| (seed 6623983470548808292) + ## (times 100) + (do @ + [sample date + #let [(^open "@/.") @.equivalence + (^open "@/.") @.codec]] + (test "Can encode/decode dates." + (|> sample + @/encode + @/decode + (case> (#error.Success decoded) + (@/= sample decoded) + + (#error.Failure error) + #0)))))) diff --git a/stdlib/source/test/lux/time/duration.lux b/stdlib/source/test/lux/time/duration.lux new file mode 100644 index 000000000..3aba23203 --- /dev/null +++ b/stdlib/source/test/lux/time/duration.lux @@ -0,0 +1,60 @@ +(.module: + [lux #* + [io] + [control + [monad (#+ do Monad)]] + [data + ["E" error]] + [math + ["r" random]] + [time + ["@" duration]]] + lux/test) + +(def: #export duration + (r.Random @.Duration) + (|> r.int (:: r.monad map @.from-millis))) + +(context: "Conversion." + (<| (times 100) + (do @ + [millis r.int] + (test "Can convert from/to milliseconds." + (|> millis @.from-millis @.to-millis (i/= millis)))))) + +(context: "Equivalence." + (<| (times 100) + (do @ + [sample duration + #let [(^open "@/.") @.equivalence]] + (test "Every duration equals itself." + (@/= sample sample))))) + +(context: "Order." + (<| (times 100) + (do @ + [reference duration + sample duration + #let [(^open "@/.") @.order]] + (test "Can compare times." + (and (or (@/< reference sample) + (@/>= reference sample)) + (or (@/> reference sample) + (@/<= reference sample))))))) + +(context: "Arithmetic." + (<| (times 100) + (do @ + [sample (|> duration (:: @ map (@.frame @.day))) + frame duration + factor (|> r.int (:: @ map (|>> (i/% +10) (i/max +1)))) + #let [(^open "@/.") @.order]] + ($_ seq + (test "Can scale a duration." + (|> sample (@.scale-up factor) (@.query sample) (i/= factor))) + (test "Scaling a duration by one does not change it." + (|> sample (@.scale-up +1) (@/= sample))) + (test "Merging with the empty duration changes nothing." + (|> sample (@.merge @.empty) (@/= sample))) + (test "Merging a duration with it's opposite yields an empty duration." + (|> sample (@.merge (@.scale-up -1 sample)) (@/= @.empty))))))) diff --git a/stdlib/source/test/lux/time/instant.lux b/stdlib/source/test/lux/time/instant.lux new file mode 100644 index 000000000..c9d7aad55 --- /dev/null +++ b/stdlib/source/test/lux/time/instant.lux @@ -0,0 +1,99 @@ +(.module: + [lux #* + [io] + [control + [monad (#+ do Monad)] + pipe] + [data + ["." text + format] + [error]] + [math + ["r" random]] + [time + ["@" instant] + ["@d" duration] + ["@date" date]]] + lux/test + [// + ["_." duration]]) + +(def: boundary Int +99_999_999_999_999) + +(def: #export instant + (r.Random @.Instant) + (|> r.int (:: r.monad map (|>> (i/% boundary) @.from-millis)))) + +(context: "Conversion." + (<| (times 100) + (do @ + [millis r.int] + (test "Can convert from/to milliseconds." + (|> millis @.from-millis @.to-millis (i/= millis)))))) + +(context: "Equivalence." + (<| (times 100) + (do @ + [sample instant + #let [(^open "@/.") @.equivalence]] + (test "Every instant equals itself." + (@/= sample sample))))) + +(context: "Order" + (<| (times 100) + (do @ + [reference instant + sample instant + #let [(^open "@/.") @.order]] + (test "Can compare instants." + (and (or (@/< reference sample) + (@/>= reference sample)) + (or (@/> reference sample) + (@/<= reference sample))))))) + +(context: "Enum" + (<| (times 100) + (do @ + [sample instant + #let [(^open "@/.") @.enum]] + (test "Valid Enum." + (and (not (@/= (@/succ sample) + sample)) + (not (@/= (@/pred sample) + sample)) + (|> sample @/succ @/pred (@/= sample)) + (|> sample @/pred @/succ (@/= sample))))))) + +(context: "Arithmetic" + (<| (times 100) + (do @ + [sample instant + span _duration.duration + #let [(^open "@/.") @.equivalence + (^open "@d/.") @d.equivalence]] + ($_ seq + (test "The span of a instant and itself has an empty duration." + (|> sample (@.span sample) (@d/= @d.empty))) + (test "Can shift a instant by a duration." + (|> sample (@.shift span) (@.span sample) (@d/= span))) + (test "Can obtain the time-span between the epoch and an instant." + (|> sample @.relative @.absolute (@/= sample))) + (test "All instants are relative to the epoch." + (|> @.epoch (@.shift (@.relative sample)) (@/= sample))))))) + +## (context: "Codec" +## (<| (seed 9863552679229274604) +## ## (times 100) +## (do @ +## [sample instant +## #let [(^open "@/.") @.equivalence +## (^open "@/.") @.codec]] +## (test "Can encode/decode instants." +## (|> sample +## @/encode +## @/decode +## (case> (#error.Success decoded) +## (@/= sample decoded) + +## (#error.Failure error) +## #0)))))) |