1
1
module Test.Main where
2
2
3
3
import Prelude
4
+ import Control.Monad.Aff.Console as AffC
5
+ import Data.Date as D
6
+ import Data.DateTime as DTi
7
+ import Data.Formatter.DateTime as FDT
8
+ import Data.Formatter.Number as FN
9
+ import Data.Time as T
10
+ import Debug.Trace as DT
11
+ import Control.Monad.Aff (Aff , Canceler , runAff )
12
+ import Control.Monad.Aff.Class (liftAff )
4
13
import Control.Monad.Eff (Eff )
5
- import Control.Monad.Eff.Console (CONSOLE , log )
6
-
14
+ import Control.Monad.Eff.Console (CONSOLE )
15
+ import Control.Monad.Eff.Exception (EXCEPTION , error )
16
+ import Control.Monad.Error.Class (throwError )
17
+ import Control.Monad.State (StateT , put , get , execStateT )
18
+ import Data.DateTime (DateTime )
19
+ import Data.Either (Either (..), either )
20
+ import Data.Enum (toEnum )
7
21
import Data.Functor.Mu (roll )
8
- import Data.Formatter.Number as FN
9
- import Data.Formatter.DateTime as FDT
10
22
import Data.Maybe (fromMaybe )
11
- import Data.Either (Either (..))
12
- import Data.Enum (toEnum )
13
23
14
- import Data.DateTime as DTi
15
- import Data.Date as D
16
- import Data.Time as T
17
- import Debug.Trace as DT
24
+ type Tests e a = StateT Boolean (Aff (exception :: EXCEPTION , console :: CONSOLE | e )) a
25
+
26
+
27
+ execTests :: forall a e c .
28
+ StateT a (Aff ( process :: PROCESS | e )) c ->
29
+ a ->
30
+ Eff (process :: PROCESS | e ) (Canceler ( process :: PROCESS | e ))
31
+ execTests fn state = runAff (\s -> exit 1 ) (\s -> exit 0 ) (execStateT fn state)
32
+
33
+
34
+ log :: forall e . String -> Tests e Unit
35
+ log message = liftAff $ AffC .log message
36
+
37
+
38
+ foreign import data PROCESS :: !
39
+ foreign import exit :: Int -> forall e . Eff (process :: PROCESS | e ) Unit
18
40
19
41
20
42
fnOne ∷ FN.Formatter
@@ -36,6 +58,7 @@ fnTwo =
36
58
, sign: true
37
59
}
38
60
61
+
39
62
fnThree ∷ FN.Formatter
40
63
fnThree =
41
64
{ comma: false
@@ -46,9 +69,18 @@ fnThree =
46
69
}
47
70
48
71
49
- numeral ∷ ∀ e . Eff (console ∷ CONSOLE |e ) Unit
50
- numeral = do
51
- log $ " NUMERAL TESTS\n\n "
72
+ fdtOne ∷ FDT.Formatter
73
+ fdtOne =
74
+ roll $ FDT.Placeholder " format string is "
75
+ $ roll $ FDT.YearFull
76
+ $ roll $ FDT.Placeholder " -"
77
+ $ roll $ FDT.MonthShort
78
+ $ roll FDT.End
79
+
80
+
81
+ numeralTests :: forall e . Tests e Unit
82
+ numeralTests = do
83
+ log $ " \n NUMERAL TESTS\n "
52
84
53
85
log $ " \n PRINT FORMATTER"
54
86
log $ FN .printFormatter fnOne
@@ -84,31 +116,84 @@ numeral = do
84
116
log $ " \n UNFORMAT NUMBER"
85
117
DT .traceAnyA $ FN .unformatNumber " 0.00" " 12.00"
86
118
87
- fdtOne ∷ FDT.Formatter
88
- fdtOne =
89
- roll $ FDT.Placeholder " format string is "
90
- $ roll $ FDT.YearFull
91
- $ roll $ FDT.Placeholder " -"
92
- $ roll $ FDT.MonthShort
93
- $ roll FDT.End
94
119
95
- testDateTime ∷ DTi.DateTime
96
- testDateTime =
120
+ -- April 12th 2017 at 11:34:34:234
121
+ -- 4/12/2017
122
+ makeDateTime ∷ Int -> DTi.DateTime
123
+ makeDateTime year =
97
124
DTi.DateTime
98
- (D .canonicalDate (fromMaybe bottom $ toEnum 1234 ) D.April (fromMaybe bottom $ toEnum 12 ))
125
+ (D .canonicalDate (fromMaybe bottom $ toEnum year ) D.April (fromMaybe bottom $ toEnum 12 ))
99
126
(T.Time
100
127
(fromMaybe bottom $ toEnum 11 )
101
128
(fromMaybe bottom $ toEnum 34 )
102
129
(fromMaybe bottom $ toEnum 34 )
103
130
(fromMaybe bottom $ toEnum 234 ))
104
131
105
- timeTest ∷ ∀ e . Eff (console ∷ CONSOLE |e ) Unit
106
- timeTest = do
107
- log $ " \n\n DATETIME FORMATTER"
108
- log " \n PRINT FORMATTER"
109
- log $ FDT .printFormatter fdtOne
132
+ testDateTime :: DTi.DateTime
133
+ testDateTime = makeDateTime 2017
134
+
135
+
136
+ assert :: forall e . String -> String -> Boolean -> Tests e Unit
137
+ assert _ success true = log $ " ✓ - Passed - " <> success
138
+ assert fail _ false = do
139
+ log $ " ☠ - Failed because " <> fail
140
+ put false
141
+
142
+
143
+ failTest :: forall e . String -> Tests e Unit
144
+ failTest message = do
145
+ log message
146
+ put false
110
147
148
+
149
+ assertFormatting :: forall e . String -> String -> DateTime -> Tests e Unit
150
+ assertFormatting target' format dateTime = do
151
+ let result = FDT .formatDateTime format dateTime
152
+ let target = Right target'
153
+ assert
154
+ ((show result) <> " does not equal " <> (show target))
155
+ ((show result) <> " equals " <> (show target))
156
+ (result == target)
157
+
158
+
159
+ timeTest :: forall e . Tests e Unit
160
+ timeTest = do
161
+ log " - Data.Formatter.DateTime.formatDateTime"
162
+
163
+ -- var a = moment(
164
+ -- 'April 12th 2017 at 11:34:34:234',
165
+ -- 'MMMM Do YYYY [at] HH:mm:ss:SSS'
166
+ -- );
167
+ -- a.format('MMMM Do YYYY [at] HH:mm:ss:SSS')
168
+ -- testDateTime = April 12th 2017 at 11:34:34:234
169
+ assertFormatting " 04/12/2017" " MM/DD/YYYY" testDateTime
170
+ assertFormatting " April" " MMMM" testDateTime
171
+ assertFormatting " 2017-12-04" " YYYY-DD-MM" testDateTime
172
+ assertFormatting " 2017-Apr" " YYYY-MMM" testDateTime
173
+
174
+ -- This should probably be am (lowercase), if the desired
175
+ -- functionality of the library is to mirror momentjs
176
+ assertFormatting " 11:34:34:234 AM" " hh:mm:ss:SSS a" testDateTime
177
+ assertFormatting " 17" " YY" testDateTime
178
+ log " --- Format 20017 with YY"
179
+ assertFormatting " 17" " YY" (makeDateTime 20017 )
180
+ log " --- Format 0 with YY"
181
+ assertFormatting " 00" " YY" (makeDateTime 0 )
182
+ log " --- Format -1 with YY"
183
+ assertFormatting " 01" " YY" (makeDateTime (-1 ))
184
+
185
+ log " - Data.Formatter.DateTime.unformatDateTime "
186
+
187
+ let dt = FDT .unformatDateTime " YYYY-DD-MM SSS" " 2017-12-04 234"
188
+ either
189
+ (const $ failTest " Could not parse 017-12-04 234" )
190
+ (assertFormatting " 2017-12-04 234" " YYYY-DD-MM SSS" )
191
+ dt
192
+
193
+ formattingTests :: forall e . Tests e Unit
194
+ formattingTests = do
111
195
log $ " \n PARSE FORMAT STRING"
196
+
112
197
DT .traceAnyA $ FDT .parseFormatString " YYYY-MM-DD"
113
198
DT .traceAnyA $ FDT .parseFormatString " YY-Q-dddd HH:mm Z"
114
199
@@ -123,8 +208,15 @@ timeTest = do
123
208
log $ " \n UNFORMATDATETIME"
124
209
DT .traceAnyA $ FDT .unformatDateTime " YYYY-DD-MM SSS" " 3456-09-10 333"
125
210
126
-
127
- main ∷ forall e . Eff (console ∷ CONSOLE | e ) Unit
128
- main = do
129
- timeTest
130
- numeral
211
+ main :: forall e .
212
+ Eff ( process :: PROCESS , exception :: EXCEPTION , console :: CONSOLE | e )
213
+ (Canceler ( process :: PROCESS , exception :: EXCEPTION , console :: CONSOLE | e ))
214
+ main = execTests tests true
215
+ where
216
+ tests = do
217
+ log " Testing time functions..."
218
+ timeTest
219
+ passed <- get
220
+ when (passed /= true ) (throwError (error " Tests did not pass." ))
221
+ -- numeralTests
222
+ -- formattingTests
0 commit comments