2687b800839b99fa438b5281afe6e610bb31dcf9
[packages/time.git] / test / main / Test / Format / ISO8601.hs
1 {-# OPTIONS -fno-warn-orphans #-}
2 module Test.Format.ISO8601(testISO8601) where
3
4 import Data.Ratio
5 import Data.Time
6 import Data.Time.Format.ISO8601
7 import Test.QuickCheck.Property
8 import Test.Tasty
9 import Test.Tasty.HUnit
10 import Test.Tasty.QuickCheck hiding (reason)
11 import Test.TestUtil
12 import Test.Arbitrary()
13
14
15 deriving instance Eq ZonedTime
16
17 readShowProperty :: (Eq a,Show a) => Format a -> a -> Property
18 readShowProperty fmt val = case formatShowM fmt val of
19 Nothing -> property Discard
20 Just str -> let
21 found = formatParseM fmt str
22 expected = Just val
23 in property $ if expected == found then succeeded else
24 failed {reason = show str ++ ": expected " ++ (show expected) ++ ", found " ++ (show found)}
25
26 readBoth :: NameTest t => (FormatExtension -> t) -> [TestTree]
27 readBoth fmts =
28 [
29 nameTest "extended" $ fmts ExtendedFormat,
30 nameTest "basic" $ fmts BasicFormat
31 ]
32
33 readShowProperties :: (Eq a,Show a,Arbitrary a) => (FormatExtension -> Format a) -> [TestTree]
34 readShowProperties fmts = readBoth $ \fe -> readShowProperty $ fmts fe
35
36 newtype Durational t = MkDurational t
37
38 instance Show t => Show (Durational t) where
39 show (MkDurational t) = show t
40
41 instance Arbitrary (Durational CalendarDiffDays) where
42 arbitrary = do
43 mm <- choose (-10000,10000)
44 dd <- choose (-40,40)
45 return $ MkDurational $ CalendarDiffDays mm dd
46
47 instance Arbitrary (Durational CalendarDiffTime) where
48 arbitrary = let
49 limit = 40 * 86400
50 picofactor = 10 ^ (12 :: Int)
51 in do
52 mm <- choose (-10000,10000)
53 ss <- choose (negate limit * picofactor, limit * picofactor)
54 return $ MkDurational $ CalendarDiffTime mm $ fromRational $ ss % picofactor
55
56 testReadShowFormat :: TestTree
57 testReadShowFormat = nameTest "read-show format"
58 [
59 nameTest "calendarFormat" $ readShowProperties $ calendarFormat,
60 nameTest "yearMonthFormat" $ readShowProperty $ yearMonthFormat,
61 nameTest "yearFormat" $ readShowProperty $ yearFormat,
62 nameTest "centuryFormat" $ readShowProperty $ centuryFormat,
63 nameTest "expandedCalendarFormat" $ readShowProperties $ expandedCalendarFormat 6,
64 nameTest "expandedYearMonthFormat" $ readShowProperty $ expandedYearMonthFormat 6,
65 nameTest "expandedYearFormat" $ readShowProperty $ expandedYearFormat 6,
66 nameTest "expandedCenturyFormat" $ readShowProperty $ expandedCenturyFormat 4,
67 nameTest "ordinalDateFormat" $ readShowProperties $ ordinalDateFormat,
68 nameTest "expandedOrdinalDateFormat" $ readShowProperties $ expandedOrdinalDateFormat 6,
69 nameTest "weekDateFormat" $ readShowProperties $ weekDateFormat,
70 nameTest "yearWeekFormat" $ readShowProperties $ yearWeekFormat,
71 nameTest "expandedWeekDateFormat" $ readShowProperties $ expandedWeekDateFormat 6,
72 nameTest "expandedYearWeekFormat" $ readShowProperties $ expandedYearWeekFormat 6,
73 nameTest "timeOfDayFormat" $ readShowProperties $ timeOfDayFormat,
74 nameTest "hourMinuteFormat" $ readShowProperties $ hourMinuteFormat,
75 nameTest "hourFormat" $ readShowProperty $ hourFormat,
76 nameTest "withTimeDesignator" $ readShowProperties $ \fe -> withTimeDesignator $ timeOfDayFormat fe,
77 nameTest "withUTCDesignator" $ readShowProperties $ \fe -> withUTCDesignator $ timeOfDayFormat fe,
78 nameTest "timeOffsetFormat" $ readShowProperties $ timeOffsetFormat,
79 nameTest "timeOfDayAndOffsetFormat" $ readShowProperties $ timeOfDayAndOffsetFormat,
80 nameTest "localTimeFormat" $ readShowProperties $ \fe -> localTimeFormat (calendarFormat fe) (timeOfDayFormat fe),
81 nameTest "zonedTimeFormat" $ readShowProperties $ \fe -> zonedTimeFormat (calendarFormat fe) (timeOfDayFormat fe) fe,
82 nameTest "utcTimeFormat" $ readShowProperties $ \fe -> utcTimeFormat (calendarFormat fe) (timeOfDayFormat fe),
83 nameTest "dayAndTimeFormat" $ readShowProperties $ \fe -> dayAndTimeFormat (calendarFormat fe) (timeOfDayFormat fe),
84 nameTest "timeAndOffsetFormat" $ readShowProperties $ \fe -> timeAndOffsetFormat (timeOfDayFormat fe) fe,
85 nameTest "durationDaysFormat" $ readShowProperty $ durationDaysFormat,
86 nameTest "durationTimeFormat" $ readShowProperty $ durationTimeFormat,
87 nameTest "alternativeDurationDaysFormat" $ readBoth $ \fe (MkDurational t) -> readShowProperty (alternativeDurationDaysFormat fe) t,
88 nameTest "alternativeDurationTimeFormat" $ readBoth $ \fe (MkDurational t) -> readShowProperty (alternativeDurationTimeFormat fe) t,
89 nameTest "intervalFormat" $ readShowProperties $ \fe -> intervalFormat (localTimeFormat (calendarFormat fe) (timeOfDayFormat fe)) durationTimeFormat,
90 nameTest "recurringIntervalFormat" $ readShowProperties $ \fe -> recurringIntervalFormat (localTimeFormat (calendarFormat fe) (timeOfDayFormat fe)) durationTimeFormat
91 ]
92
93 testShowFormat :: String -> Format t -> String -> t -> TestTree
94 testShowFormat name fmt str t = nameTest (name ++ ": " ++ str) $
95 assertEqual "" (Just str) $ formatShowM fmt t
96
97 testShowFormats :: TestTree
98 testShowFormats = nameTest "show format"
99 [
100 testShowFormat "durationDaysFormat" durationDaysFormat "P0D" $ CalendarDiffDays 0 0,
101 testShowFormat "durationDaysFormat" durationDaysFormat "P4Y" $ CalendarDiffDays 48 0,
102 testShowFormat "durationDaysFormat" durationDaysFormat "P7M" $ CalendarDiffDays 7 0,
103 testShowFormat "durationDaysFormat" durationDaysFormat "P5D" $ CalendarDiffDays 0 5,
104 testShowFormat "durationDaysFormat" durationDaysFormat "P2Y3M81D" $ CalendarDiffDays 27 81,
105 testShowFormat "durationTimeFormat" durationTimeFormat "P0D" $ CalendarDiffTime 0 0,
106 testShowFormat "durationTimeFormat" durationTimeFormat "P4Y" $ CalendarDiffTime 48 0,
107 testShowFormat "durationTimeFormat" durationTimeFormat "P7M" $ CalendarDiffTime 7 0,
108 testShowFormat "durationTimeFormat" durationTimeFormat "P5D" $ CalendarDiffTime 0 $ 5 * nominalDay,
109 testShowFormat "durationTimeFormat" durationTimeFormat "P2Y3M81D" $ CalendarDiffTime 27 $ 81 * nominalDay,
110 testShowFormat "durationTimeFormat" durationTimeFormat "PT2H" $ CalendarDiffTime 0 $ 7200,
111 testShowFormat "durationTimeFormat" durationTimeFormat "PT3M" $ CalendarDiffTime 0 $ 180,
112 testShowFormat "durationTimeFormat" durationTimeFormat "PT12S" $ CalendarDiffTime 0 $ 12,
113 testShowFormat "durationTimeFormat" durationTimeFormat "PT1M18.77634S" $ CalendarDiffTime 0 $ 78.77634,
114 testShowFormat "durationTimeFormat" durationTimeFormat "PT2H1M18.77634S" $ CalendarDiffTime 0 $ 7278.77634,
115 testShowFormat "durationTimeFormat" durationTimeFormat "P5DT2H1M18.77634S" $ CalendarDiffTime 0 $ 5 * nominalDay + 7278.77634,
116 testShowFormat "durationTimeFormat" durationTimeFormat "P7Y10M5DT2H1M18.77634S" $ CalendarDiffTime 94 $ 5 * nominalDay + 7278.77634,
117 testShowFormat "durationTimeFormat" durationTimeFormat "P7Y10MT2H1M18.77634S" $ CalendarDiffTime 94 $ 7278.77634,
118 testShowFormat "durationTimeFormat" durationTimeFormat "P8YT2H1M18.77634S" $ CalendarDiffTime 96 $ 7278.77634,
119 testShowFormat "alternativeDurationDaysFormat" (alternativeDurationDaysFormat ExtendedFormat) "P0001-00-00" $ CalendarDiffDays 12 0,
120 testShowFormat "alternativeDurationTimeFormat" (alternativeDurationTimeFormat ExtendedFormat) "P0000-00-01T00:00:00" $ CalendarDiffTime 0 86400,
121 testShowFormat "intervalFormat etc."
122 (intervalFormat (localTimeFormat (calendarFormat ExtendedFormat) (timeOfDayFormat ExtendedFormat)) durationTimeFormat)
123 "2015-06-13T21:13:56/P1Y2M7DT5H33M2.34S"
124 (LocalTime (fromGregorian 2015 6 13) (TimeOfDay 21 13 56),CalendarDiffTime 14 $ 7 * nominalDay + 5 * 3600 + 33 * 60 + 2.34),
125 testShowFormat "recurringIntervalFormat etc."
126 (recurringIntervalFormat (localTimeFormat (calendarFormat ExtendedFormat) (timeOfDayFormat ExtendedFormat)) durationTimeFormat)
127 "R74/2015-06-13T21:13:56/P1Y2M7DT5H33M2.34S"
128 (74,LocalTime (fromGregorian 2015 6 13) (TimeOfDay 21 13 56),CalendarDiffTime 14 $ 7 * nominalDay + 5 * 3600 + 33 * 60 + 2.34),
129 testShowFormat "recurringIntervalFormat etc."
130 (recurringIntervalFormat (calendarFormat ExtendedFormat) durationDaysFormat)
131 "R74/2015-06-13/P1Y2M7D"
132 (74,fromGregorian 2015 6 13,CalendarDiffDays 14 7),
133 testShowFormat "timeOffsetFormat"
134 iso8601Format
135 "-06:30"
136 (minutesToTimeZone (-390)),
137 testShowFormat "timeOffsetFormat"
138 iso8601Format
139 "+00:00"
140 (minutesToTimeZone 0),
141 testShowFormat "timeOffsetFormat"
142 (timeOffsetFormat BasicFormat)
143 "+0000"
144 (minutesToTimeZone 0),
145 testShowFormat "timeOffsetFormat"
146 iso8601Format
147 "+00:10"
148 (minutesToTimeZone 10),
149 testShowFormat "timeOffsetFormat"
150 iso8601Format
151 "-00:10"
152 (minutesToTimeZone (-10)),
153 testShowFormat "timeOffsetFormat"
154 iso8601Format
155 "+01:35"
156 (minutesToTimeZone 95),
157 testShowFormat "timeOffsetFormat"
158 iso8601Format
159 "-01:35"
160 (minutesToTimeZone (-95)),
161 testShowFormat "timeOffsetFormat"
162 (timeOffsetFormat BasicFormat)
163 "+0135"
164 (minutesToTimeZone 95),
165 testShowFormat "timeOffsetFormat"
166 (timeOffsetFormat BasicFormat)
167 "-0135"
168 (minutesToTimeZone (-95)),
169 testShowFormat "zonedTimeFormat"
170 iso8601Format
171 "2024-07-06T08:45:56.553-06:30"
172 (ZonedTime (LocalTime (fromGregorian 2024 07 06) (TimeOfDay 8 45 56.553)) (minutesToTimeZone (-390))),
173 testShowFormat "zonedTimeFormat"
174 iso8601Format
175 "2024-07-06T08:45:56.553+06:30"
176 (ZonedTime (LocalTime (fromGregorian 2024 07 06) (TimeOfDay 8 45 56.553)) (minutesToTimeZone 390)),
177 testShowFormat "utcTimeFormat"
178 iso8601Format
179 "2024-07-06T08:45:56.553Z"
180 (UTCTime (fromGregorian 2024 07 06) (timeOfDayToTime $ TimeOfDay 8 45 56.553)),
181 testShowFormat "utcTimeFormat"
182 iso8601Format
183 "2028-12-31T23:59:60.9Z"
184 (UTCTime (fromGregorian 2028 12 31) (timeOfDayToTime $ TimeOfDay 23 59 60.9)),
185 testShowFormat "weekDateFormat"
186 (weekDateFormat ExtendedFormat)
187 "1994-W52-7"
188 (fromGregorian 1995 1 1),
189 testShowFormat "weekDateFormat"
190 (weekDateFormat ExtendedFormat)
191 "1995-W01-1"
192 (fromGregorian 1995 1 2),
193 testShowFormat "weekDateFormat"
194 (weekDateFormat ExtendedFormat)
195 "1996-W52-7"
196 (fromGregorian 1996 12 29),
197 testShowFormat "weekDateFormat"
198 (weekDateFormat ExtendedFormat)
199 "1997-W01-2"
200 (fromGregorian 1996 12 31),
201 testShowFormat "weekDateFormat"
202 (weekDateFormat ExtendedFormat)
203 "1997-W01-3"
204 (fromGregorian 1997 1 1),
205 testShowFormat "weekDateFormat"
206 (weekDateFormat ExtendedFormat)
207 "1974-W32-6"
208 (fromGregorian 1974 8 10),
209 testShowFormat "weekDateFormat"
210 (weekDateFormat BasicFormat)
211 "1974W326"
212 (fromGregorian 1974 8 10),
213 testShowFormat "weekDateFormat"
214 (weekDateFormat ExtendedFormat)
215 "1995-W05-6"
216 (fromGregorian 1995 2 4),
217 testShowFormat "weekDateFormat"
218 (weekDateFormat BasicFormat)
219 "1995W056"
220 (fromGregorian 1995 2 4),
221 testShowFormat "weekDateFormat"
222 (expandedWeekDateFormat 6 ExtendedFormat)
223 "+001995-W05-6"
224 (fromGregorian 1995 2 4),
225 testShowFormat "weekDateFormat"
226 (expandedWeekDateFormat 6 BasicFormat)
227 "+001995W056"
228 (fromGregorian 1995 2 4),
229 testShowFormat "ordinalDateFormat"
230 (ordinalDateFormat ExtendedFormat)
231 "1846-235"
232 (fromGregorian 1846 8 23),
233 testShowFormat "ordinalDateFormat"
234 (ordinalDateFormat BasicFormat)
235 "1844236"
236 (fromGregorian 1844 8 23),
237 testShowFormat "ordinalDateFormat"
238 (expandedOrdinalDateFormat 5 ExtendedFormat)
239 "+01846-235"
240 (fromGregorian 1846 8 23)
241 ]
242
243 testISO8601 :: TestTree
244 testISO8601 = nameTest "ISO8601"
245 [
246 testShowFormats,
247 testReadShowFormat
248 ]