packages feed

time-w3c-0.1: tests/ParsecParserTest.hs

module ParsecParserTest
    ( testData
    )
    where

import Data.Time
import Data.Time.W3C hiding (parse)
import Data.Time.W3C.Parser.Parsec
import Test.HUnit
import Text.Parsec


parseW3C :: String -> Either String W3CDateTime
parseW3C src = case parse w3cDateTime "" src of
                 Left  err -> Left (show err)
                 Right w3c -> Right w3c

parseW3C' :: String -> Maybe W3CDateTime
parseW3C' src = case parse w3cDateTime "" src of
                  Left  _   -> Nothing
                  Right w3c -> Just w3c


testData :: [Test]
testData = [ parseW3C "2010"
             ~?=
             Right (W3CDateTime 2010 Nothing Nothing Nothing Nothing Nothing Nothing)

           , parseW3C "2010-12"
             ~?=
             Right (W3CDateTime 2010 (Just 12) Nothing Nothing Nothing Nothing Nothing)

           , parseW3C "2010-12-31"
             ~?=
             Right (W3CDateTime 2010 (Just 12) (Just 31) Nothing Nothing Nothing Nothing)

           , parseW3C "2010-12-31T01:23Z"
             ~?=
             Right (W3CDateTime 2010 (Just 12) (Just 31) (Just 1) (Just 23) Nothing (Just (hoursToTimeZone 0)))

           , parseW3C "2010-12-31T01:23:45+09:00"
             ~?=
             Right (W3CDateTime 2010 (Just 12) (Just 31) (Just 1) (Just 23) (Just 45) (Just (hoursToTimeZone 9)))

           , parseW3C "2010-12-31T01:23:45.666666+09:00"
             ~?=
             Right (W3CDateTime 2010 (Just 12) (Just 31) (Just 1) (Just 23) (Just 45.666666) (Just (hoursToTimeZone 9)))

           , parseW3C' ""
             ~?=
             Nothing

           , parseW3C' "99-01-01"
             ~?=
             Nothing

           , parseW3C' "2010-01-01T"
             ~?=
             Nothing

           , parseW3C' "2010-01-01T10Z"
             ~?=
             Nothing

           , parseW3C' "2010-01-01T11:22"
             ~?=
             Nothing

           , parseW3C' "2010-01-01T11:22:33"
             ~?=
             Nothing

           , parseW3C' "2010-01-01T11:22:33.Z"
             ~?=
             Nothing
           ]