From acd67d75aab3f2350b488fe6402bc0a20e476a18 Mon Sep 17 00:00:00 2001 From: Michael Orlitzky Date: Tue, 22 Jul 2014 21:22:25 -0400 Subject: [PATCH] Add more tests for existing picklers. --- src/TSN/Picklers.hs | 93 +++++++++++++++++++++++++++++++++++++++++---- 1 file changed, 86 insertions(+), 7 deletions(-) diff --git a/src/TSN/Picklers.hs b/src/TSN/Picklers.hs index a8b0567..a64db8a 100644 --- a/src/TSN/Picklers.hs +++ b/src/TSN/Picklers.hs @@ -69,6 +69,21 @@ date_format_padded = "%0m/%0d/%Y" -- | (Un)pickle a UTCTime without the time portion. -- +-- /Examples/: +-- +-- This should parse: +-- +-- >>> let tn = text_node "2/15/1983" +-- >>> unpickleDoc xp_date tn +-- Just 1983-02-15 00:00:00 UTC +-- +-- But for some reason, it can also parse a leading zero in the +-- month. Whatever. This isn't required behavior. +-- +-- >>> let tn = text_node "02/15/1983" +-- >>> unpickleDoc xp_date tn +-- Just 1983-02-15 00:00:00 UTC +-- xp_date :: PU UTCTime xp_date = (to_date, from_date) `xpWrapMaybe` xpText @@ -83,6 +98,12 @@ xp_date = -- | (Un)pickle a UTCTime without the time portion. The day/month are -- padded to two characters with zeros. -- +-- Examples: +-- +-- >>> let tn = text_node "02/15/1983" +-- >>> unpickleDoc xp_date_padded tn +-- Just 1983-02-15 00:00:00 UTC +-- xp_date_padded :: PU UTCTime xp_date_padded = (to_date, from_date) `xpWrapMaybe` xpText @@ -119,6 +140,7 @@ format_commas :: Int -> String format_commas x = reverse (intercalate "," $ chunksOf 3 $ reverse $ show x) + -- | Parse \ from an 'AutoRaceResultsListing'. These are -- essentially 'Int's, but they look like, -- @@ -128,6 +150,16 @@ format_commas x = -- -- * \TBA\ -- +-- Examples: +-- +-- >>> let tn = text_node "1,000,191" +-- >>> unpickleDoc xp_earnings tn +-- Just (Just 1000191) +-- +-- >>> let tn = text_node "TBA" +-- >>> unpickleDoc xp_earnings tn +-- Just Nothing +-- xp_earnings :: PU (Maybe Int) xp_earnings = (to_earnings, from_earnings) `xpWrap` xpText @@ -145,14 +177,26 @@ xp_earnings = from_earnings (Just i) = format_commas i + -- | (Un)pickle an unpadded 'UTCTime'. Used for example on the -- \ elements in an 'AutoRaceResults' message. -- -- Examples: -- --- * \6/1/2014 1:00:00 PM\ +-- >>> let tn = text_node "6/1/2014 1:00:00 PM" +-- >>> unpickleDoc xp_datetime tn +-- Just 2014-06-01 13:00:00 UTC -- --- * \5/24/2014 2:45:00 PM\ +-- >>> let tn = text_node "5/24/2014 2:45:00 PM" +-- >>> unpickleDoc xp_datetime tn +-- Just 2014-05-24 14:45:00 UTC +-- +-- Padded! For some reason it works with only one zero in front. I +-- dunno man. NOT required (or even desired?) behavior. +-- +-- >>> let tn = text_node "05/24/2014 2:45:00 PM" +-- >>> unpickleDoc xp_datetime tn +-- Just 2014-05-24 14:45:00 UTC -- xp_datetime :: PU UTCTime xp_datetime = @@ -170,12 +214,19 @@ xp_datetime = -- | (Un)pickle a UTCTime from a weather forecast's gamedate. Example -- input looks like, -- --- \ --- -- When unpickling we get rid of the suffixes \"st\", \"nd\", \"rd\", and -- \"th\". During pickling, we add them back based on the last digit -- of the date. -- +-- Examples: +-- +-- >>> let tn = text_node "Monday, December 30th" +-- >>> let (Just gd) = unpickleDoc xp_gamedate tn +-- >>> gd +-- 1970-12-30 00:00:00 UTC +-- >>> pickleDoc xp_gamedate gd +-- NTree (XTag "/" []) [NTree (XText "Wednesday, December 30th") []] +-- xp_gamedate :: PU UTCTime xp_gamedate = (to_gamedate, from_gamedate) `xpWrapMaybe` xpText @@ -218,7 +269,22 @@ xp_gamedate = --- | (Un)pickle a UTCTime without the date portion. +-- | (Un)pickle a UTCTime without the date portion. Doesn't work if +-- the fields aren't zero-padded to two characters. +-- +-- /Examples/: +-- +-- Padded, should work: +-- +-- >>> let tn = text_node "04:35 PM" +-- >>> unpickleDoc xp_time tn +-- Just 1970-01-01 16:35:00 UTC +-- +-- Unpadded, should fail: +-- +-- >>> let tn = text_node "4:35 PM" +-- >>> unpickleDoc xp_time tn +-- Nothing -- xp_time :: PU UTCTime xp_time = @@ -235,9 +301,22 @@ xp_time = -- 'xp_time' in that it uses periods in the AM/PM part, i.e. \"A.M.\" -- and \"P.M.\" It also doesn't use padding for the \"hours\" part. -- --- Examples: +-- /Examples/: +-- +-- A standard example of the correct form: +-- +-- >>> let tn = text_node "11:30 A.M." +-- >>> let (Just result) = unpickleDoc xp_time_dots tn +-- >>> result +-- 1970-01-01 11:30:00 UTC +-- >>> pickleDoc xp_time_dots result +-- NTree (XTag "/" []) [NTree (XText "11:30 A.M.") []] +-- +-- Another miracle, it still parses with a leading zero! -- --- * \11:30 A.M.\ +-- >>> let tn = text_node "01:30 A.M." +-- >>> unpickleDoc xp_time_dots tn +-- Just 1970-01-01 01:30:00 UTC -- xp_time_dots :: PU UTCTime xp_time_dots = -- 2.44.2