@@ -2,6 +2,7 @@ module Text.Markdown.SlamDown.Syntax.TextBox
22 ( TimeValue
33 , DateValue
44 , DateTimeValue
5+ , TimePrecision (..)
56 , TextBox (..)
67 , transTextBox
78 , traverseTextBox
@@ -16,13 +17,15 @@ import Prelude
1617import Data.Function (on )
1718import Data.HugeNum as HN
1819import Data.Identity (Identity (..), runIdentity )
20+ import Data.Maybe (Maybe (..))
1921
2022import Test.StrongCheck.Arbitrary as SCA
2123import Test.StrongCheck.Gen as Gen
2224
2325type TimeValue =
2426 { hours ∷ Int
2527 , minutes ∷ Int
28+ , seconds ∷ Maybe Int
2629 }
2730
2831newtype TimeValueP = TimeValueP TimeValue
@@ -55,12 +58,16 @@ instance arbitraryTimeValueP ∷ SCA.Arbitrary TimeValueP where
5558 arbitrary = do
5659 hours ← Gen .chooseInt 0.0 12.0
5760 minutes ← Gen .chooseInt 0.0 60.0
58- pure $ TimeValueP { hours , minutes }
61+ secs ← Gen .chooseInt 0.0 60.0
62+ b <- (_ < 0.5 ) <$> Gen .choose 0.0 1.0
63+ let seconds = if b then Nothing else Just secs
64+ pure $ TimeValueP { hours , minutes , seconds }
5965
6066instance coarbitraryTimeValueP :: SCA.Coarbitrary TimeValueP where
61- coarbitrary (TimeValueP { hours, minutes }) gen = do
67+ coarbitrary (TimeValueP { hours, minutes, seconds }) gen = do
6268 SCA .coarbitrary hours gen
6369 SCA .coarbitrary minutes gen
70+ SCA .coarbitrary seconds gen
6471
6572type DateValue =
6673 { month ∷ Int
@@ -153,12 +160,33 @@ instance coarbitraryDateTimeValueP :: SCA.Coarbitrary DateTimeValueP where
153160 SCA .coarbitrary (DateValueP date) gen
154161 SCA .coarbitrary (TimeValueP time) gen
155162
163+ data TimePrecision
164+ = Minutes
165+ | Seconds
166+
167+ derive instance eqTimePrecision :: Eq TimePrecision
168+ derive instance ordTimePrecision :: Ord TimePrecision
169+
170+ instance showTimePrecision :: Show TimePrecision where
171+ show Minutes = " Minutes"
172+ show Seconds = " Seconds"
173+
174+ instance arbitraryTimePrecision ∷ SCA.Arbitrary TimePrecision where
175+ arbitrary =
176+ Gen .chooseInt 0.0 1.0 <#> case _ of
177+ 0 → Minutes
178+ _ → Seconds
179+
180+ instance coarbitraryTimePrecision :: SCA.Coarbitrary TimePrecision where
181+ coarbitrary Minutes = SCA .coarbitrary 1
182+ coarbitrary Seconds = SCA .coarbitrary 2
183+
156184data TextBox f
157185 = PlainText (f String )
158186 | Numeric (f HN.HugeNum )
159187 | Date (f DateValue )
160- | Time (f TimeValue )
161- | DateTime (f DateTimeValue )
188+ | Time TimePrecision (f TimeValue )
189+ | DateTime TimePrecision (f DateTimeValue )
162190
163191transTextBox
164192 ∷ ∀ f g
@@ -180,17 +208,17 @@ traverseTextBox eta t =
180208 PlainText def → PlainText <$> eta def
181209 Numeric def → Numeric <$> eta def
182210 Date def → Date <$> eta def
183- Time def → Time <$> eta def
184- DateTime def → DateTime <$> eta def
211+ Time prec def → Time prec <$> eta def
212+ DateTime prec def → DateTime prec <$> eta def
185213
186214instance showTextBox ∷ (Functor f , Show (f String ), Show (f HN.HugeNum ), Show (f TimeValueP ), Show (f DateValueP ), Show (f DateTimeValueP )) ⇒ Show (TextBox f ) where
187215 show =
188216 case _ of
189217 PlainText def → " (PlainText " <> show def <> " )"
190218 Numeric def → " (Numeric " <> show def <> " )"
191219 Date def → " (Date " <> show (DateValueP <$> def) <> " )"
192- Time def → " (Time " <> show (TimeValueP <$> def) <> " )"
193- DateTime def → " (DateTime " <> show (DateTimeValueP <$> def) <> " )"
220+ Time prec def → " (Time " <> show prec <> " " <> show (TimeValueP <$> def) <> " )"
221+ DateTime prec def → " (DateTime " <> show prec <> " " <> show (DateTimeValueP <$> def) <> " )"
194222
195223instance ordTextBox ∷ (Functor f , Ord (f String ), Ord (f HN.HugeNum ), Ord (f TimeValueP ), Ord (f DateValueP ), Ord (f DateTimeValueP )) ⇒ Ord (TextBox f ) where
196224 compare =
@@ -207,20 +235,20 @@ instance ordTextBox ∷ (Functor f, Ord (f String), Ord (f HN.HugeNum), Ord (f T
207235 Date _, _ → LT
208236 _, Date _ → GT
209237
210- Time t1, Time t2 → on compare (map TimeValueP ) t1 t2
211- Time _, _ → LT
212- _, Time _ → GT
238+ Time prec1 t1, Time prec2 t2 → compare prec1 prec2 <> on compare (map TimeValueP ) t1 t2
239+ Time _ _ , _ → LT
240+ _, Time _ _ → GT
213241
214- DateTime d1, DateTime d2 → on compare (map DateTimeValueP ) d1 d2
242+ DateTime prec1 d1, DateTime prec2 d2 → compare prec1 prec2 <> on compare (map DateTimeValueP ) d1 d2
215243
216244instance eqTextBox ∷ (Functor f , Eq (f String ), Eq (f HN.HugeNum ), Eq (f TimeValueP ), Eq (f DateValueP ), Eq (f DateTimeValueP )) ⇒ Eq (TextBox f ) where
217245 eq =
218246 case _, _ of
219247 PlainText d1, PlainText d2 → d1 == d2
220248 Numeric d1, Numeric d2 → d1 == d2
221249 Date d1, Date d2 → on eq (map DateValueP ) d1 d2
222- Time d1, Time d2 → on eq (map TimeValueP ) d1 d2
223- DateTime d1, DateTime d2 → on eq (map DateTimeValueP ) d1 d2
250+ Time prec1 d1, Time prec2 d2 → prec1 == prec2 && on eq (map TimeValueP ) d1 d2
251+ DateTime prec1 d1, DateTime prec2 d2 → prec1 == prec2 && on eq (map DateTimeValueP ) d1 d2
224252 _, _ → false
225253
226254instance arbitraryTextBox ∷ (Functor f , SCA.Arbitrary (f String ), SCA.Arbitrary (f Number ), SCA.Arbitrary (f TimeValueP ), SCA.Arbitrary (f DateValueP ), SCA.Arbitrary (f DateTimeValueP )) ⇒ SCA.Arbitrary (TextBox f ) where
@@ -230,8 +258,8 @@ instance arbitraryTextBox ∷ (Functor f, SCA.Arbitrary (f String), SCA.Arbitrar
230258 0 → PlainText <$> SCA .arbitrary
231259 1 → Numeric <<< map HN .fromNumber <$> SCA .arbitrary
232260 2 → Date <<< map getDateValueP <$> SCA .arbitrary
233- 3 → Time <<< map getTimeValueP <$> SCA .arbitrary
234- 4 → DateTime <<< map getDateTimeValueP <$> SCA .arbitrary
261+ 3 → Time <$> SCA .arbitrary <*> ( map getTimeValueP <$> SCA .arbitrary)
262+ 4 → DateTime <$> SCA .arbitrary <*> ( map getDateTimeValueP <$> SCA .arbitrary)
235263 _ → PlainText <$> SCA .arbitrary
236264
237265instance coarbitraryTextBox :: (Functor f , SCA.Coarbitrary (f String ), SCA.Coarbitrary (f Number ), SCA.Coarbitrary (f DateValueP ), SCA.Coarbitrary (f TimeValueP ), SCA.Coarbitrary (f DateTimeValueP )) ⇒ SCA.Coarbitrary (TextBox f ) where
@@ -240,5 +268,9 @@ instance coarbitraryTextBox :: (Functor f, SCA.Coarbitrary (f String), SCA.Coarb
240268 PlainText d -> SCA .coarbitrary d
241269 Numeric d -> SCA .coarbitrary $ HN .toNumber <$> d
242270 Date d -> SCA .coarbitrary $ DateValueP <$> d
243- Time d -> SCA .coarbitrary $ TimeValueP <$> d
244- DateTime d -> SCA .coarbitrary $ DateTimeValueP <$> d
271+ Time prec d -> do
272+ SCA .coarbitrary prec
273+ SCA .coarbitrary $ TimeValueP <$> d
274+ DateTime prec d -> do
275+ SCA .coarbitrary prec
276+ SCA .coarbitrary $ DateTimeValueP <$> d
0 commit comments