Skip to content
This repository was archived by the owner on Oct 4, 2020. It is now read-only.

Commit f99b46a

Browse files
authored
Merge pull request #42 from garyb/datetime
Add Arb* newtypes for Date/Time/DateTime
2 parents 26551f6 + 8679d89 commit f99b46a

File tree

2 files changed

+80
-1
lines changed

2 files changed

+80
-1
lines changed

bower.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
"purescript-free": "^3.0.0",
2525
"purescript-machines": "^2.0.0",
2626
"purescript-random": "^2.0.0",
27-
"purescript-arrays": "^3.0.0"
27+
"purescript-arrays": "^3.0.0",
28+
"purescript-datetime": "^2.1.1"
2829
}
2930
}
Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
module Test.StrongCheck.Data.ArbDateTime where
2+
3+
import Prelude
4+
5+
import Data.DateTime as DT
6+
import Data.Enum (fromEnum, toEnum)
7+
import Data.Maybe (fromMaybe)
8+
import Data.Newtype (class Newtype)
9+
10+
import Test.StrongCheck.Arbitrary (class Arbitrary, class Coarbitrary, arbitrary, coarbitrary)
11+
import Test.StrongCheck.Gen (chooseInt)
12+
13+
newtype ArbTime = ArbTime DT.Time
14+
15+
runArbTime ArbTime DT.Time
16+
runArbTime (ArbTime x) = x
17+
18+
derive instance newtypeArbTimeNewtype ArbTime _
19+
20+
instance arbitraryArbTimeArbitrary ArbTime where
21+
arbitrary = do
22+
hour ← chooseInt 0 23
23+
minute ← chooseInt 0 59
24+
second ← chooseInt 0 59
25+
millisecond ← chooseInt 0 999
26+
pure $ ArbTime $ DT.Time
27+
(fromMaybe bottom (toEnum hour))
28+
(fromMaybe bottom (toEnum minute))
29+
(fromMaybe bottom (toEnum second))
30+
(fromMaybe bottom (toEnum millisecond))
31+
32+
instance coarbitraryArbTimeCoarbitrary ArbTime where
33+
coarbitrary (ArbTime t) = do
34+
coarbitrary $ fromEnum (DT.hour t)
35+
coarbitrary $ fromEnum (DT.minute t)
36+
coarbitrary $ fromEnum (DT.second t)
37+
coarbitrary $ fromEnum (DT.millisecond t)
38+
39+
newtype ArbDate = ArbDate DT.Date
40+
41+
runArbDate ArbDate DT.Date
42+
runArbDate (ArbDate x) = x
43+
44+
derive instance newtypeArbDateNewtype ArbDate _
45+
46+
instance arbitraryArbDateArbitrary ArbDate where
47+
arbitrary = do
48+
year ← chooseInt 1950 2050
49+
month ← chooseInt 1 12
50+
day ← chooseInt 1 31
51+
pure $ ArbDate $ DT.canonicalDate
52+
(fromMaybe bottom (toEnum year))
53+
(fromMaybe bottom (toEnum month))
54+
(fromMaybe bottom (toEnum day))
55+
56+
instance coarbitraryArbDateCoarbitrary ArbDate where
57+
coarbitrary (ArbDate dt) = do
58+
coarbitrary $ fromEnum (DT.year dt)
59+
coarbitrary $ fromEnum (DT.month dt)
60+
coarbitrary $ fromEnum (DT.day dt)
61+
62+
newtype ArbDateTime = ArbDateTime DT.DateTime
63+
64+
runArbDateTime ArbDateTime DT.DateTime
65+
runArbDateTime (ArbDateTime x) = x
66+
67+
derive instance newtypeArbDateTimeNewtype ArbDateTime _
68+
69+
instance arbitraryArbDateTimeArbitrary ArbDateTime where
70+
arbitrary = do
71+
date <- runArbDate <$> arbitrary
72+
time <- runArbTime <$> arbitrary
73+
pure $ ArbDateTime $ DT.DateTime date time
74+
75+
instance coarbitraryArbDateTimeCoarbitrary ArbDateTime where
76+
coarbitrary (ArbDateTime dt) = do
77+
coarbitrary $ ArbDate (DT.date dt)
78+
coarbitrary $ ArbTime (DT.time dt)

0 commit comments

Comments
 (0)