-
Notifications
You must be signed in to change notification settings - Fork 19
/
Main.purs
100 lines (75 loc) · 4 KB
/
Main.purs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
module Test.Main where
import Prelude
import Control.Monad.Eff (Eff)
import Control.Monad.Eff.Console (CONSOLE, log)
import Control.Monad.Except (runExcept)
import Data.DateTime as DT
import Data.Enum (toEnum)
import Data.Either (isRight)
import Data.Foreign (F, Foreign)
import Data.JSDate as JSD
import Data.Maybe (Maybe(..), fromJust)
import Partial.Unsafe (unsafePartial)
import Global (nan)
import Test.Assert (ASSERT, assert)
foreign import myDate :: Foreign
main :: forall eff. Eff (console :: CONSOLE, assert :: ASSERT, locale :: JSD.LOCALE | eff) Unit
main = do
log "Checking that readDate will read JS date values..."
assert $ isRight $ runExcept $ JSD.readDate myDate :: F JSD.JSDate
log "Checking that a UTC date constructed with sensible values is valid..."
assert $ JSD.isValid $ JSD.jsdate defaultDateRecord
log "Checking that any UTC date constructed with a NaN value is invalid..."
assert $ not $ JSD.isValid $ JSD.jsdate defaultDateRecord { year = nan }
assert $ not $ JSD.isValid $ JSD.jsdate defaultDateRecord { month = nan }
assert $ not $ JSD.isValid $ JSD.jsdate defaultDateRecord { day = nan }
assert $ not $ JSD.isValid $ JSD.jsdate defaultDateRecord { hour = nan }
assert $ not $ JSD.isValid $ JSD.jsdate defaultDateRecord { minute = nan }
assert $ not $ JSD.isValid $ JSD.jsdate defaultDateRecord { second = nan }
assert $ not $ JSD.isValid $ JSD.jsdate defaultDateRecord { millisecond = nan }
log "Checking that a date constructed from a string with sensible values is valid..."
assert <<< JSD.isValid =<< JSD.parse "2011-10-10T14:48:00"
assert <<< JSD.isValid =<< JSD.parse "Thu, 01 Jan 1970 00:00:00 GMT-0400"
log "Checking that a local date constructed with sensible values is valid..."
assert <<< JSD.isValid =<< JSD.jsdateLocal defaultDateRecord
log "Checking that any local date constructed with a NaN value is invalid..."
assert <<< not <<< JSD.isValid =<< JSD.jsdateLocal defaultDateRecord { year = nan }
assert <<< not <<< JSD.isValid =<< JSD.jsdateLocal defaultDateRecord { month = nan }
assert <<< not <<< JSD.isValid =<< JSD.jsdateLocal defaultDateRecord { day = nan }
assert <<< not <<< JSD.isValid =<< JSD.jsdateLocal defaultDateRecord { hour = nan }
assert <<< not <<< JSD.isValid =<< JSD.jsdateLocal defaultDateRecord { minute = nan }
assert <<< not <<< JSD.isValid =<< JSD.jsdateLocal defaultDateRecord { second = nan }
assert <<< not <<< JSD.isValid =<< JSD.jsdateLocal defaultDateRecord { millisecond = nan }
log "Check that a roundtrip conversion of a dates results in the input"
assert $ JSD.toDateTime (JSD.fromDateTime dateTime) == Just dateTime
assert $ JSD.toDateTime (JSD.fromDateTime ancientDateTime) == Just ancientDateTime
assert $ JSD.toDateTime (JSD.fromDateTime bottom) == Just bottom
assert $ JSD.toDateTime (JSD.fromDateTime top) == Just top
log "Check that equal dates test equal"
assert $ JSD.fromDateTime dateTime == JSD.fromDateTime dateTime
assert $ JSD.fromDateTime ancientDateTime == JSD.fromDateTime ancientDateTime
log "Check that unequal dates do not test equal"
assert $ JSD.fromDateTime dateTime /= JSD.fromDateTime ancientDateTime
log "Check that dates are chronologically ordered"
assert $ JSD.fromDateTime dateTime `compare` JSD.fromDateTime dateTime == EQ
assert $ JSD.fromDateTime dateTime `compare` JSD.fromDateTime ancientDateTime == GT
assert $ JSD.fromDateTime ancientDateTime `compare` JSD.fromDateTime dateTime == LT
log "All tests done"
where
defaultDateRecord =
{ year: 2016.0
, month: 5.0
, day: 3.0
, hour: 2.0
, minute: 21.0
, second: 43.0
, millisecond: 678.0
}
date = unsafePartial $ fromJust $
DT.canonicalDate <$> toEnum 2016 <*> pure DT.June <*> toEnum 3
time = unsafePartial $ fromJust $
DT.Time <$> toEnum 2 <*> toEnum 21 <*> toEnum 43 <*> toEnum 678
dateTime = DT.DateTime date time
ancientDate = unsafePartial $ fromJust $
DT.canonicalDate <$> toEnum 1 <*> pure DT.January <*> toEnum 1
ancientDateTime = DT.DateTime ancientDate time