From: PHO Date: Wed, 28 Sep 2011 16:36:52 +0000 (+0900) Subject: Tests for Data.Time.Asctime X-Git-Tag: RELEASE-0.2~6 X-Git-Url: http://git.cielonegro.org/gitweb.cgi?a=commitdiff_plain;h=e322e3c;p=time-http.git Tests for Data.Time.Asctime Ditz-issue: c8c594f249504e28212f18a8a5c6b8a708b99f79 --- diff --git a/Data/Time/Asctime.hs b/Data/Time/Asctime.hs index 449fb12..0814e45 100644 --- a/Data/Time/Asctime.hs +++ b/Data/Time/Asctime.hs @@ -5,7 +5,7 @@ -- -- ANSI C's asctime() format looks like: -- --- @Wdy Mon DD HH:MM:SS YYYY@ +-- @Wdy Mon [D]D HH:MM:SS YYYY@ -- -- The exact syntax is as follows: -- @@ -15,7 +15,7 @@ -- > month ::= "Jan" | "Feb" | "Mar" | "Apr" -- > | "May" | "Jun" | "Jul" | "Aug" -- > | "Sep" | "Oct" | "Nov" | "Dec" --- > day ::= 2DIGIT +-- > day ::= 2DIGIT | SP 1DIGIT -- > time ::= 2DIGIT ':' 2DIGIT [':' 2DIGIT] -- > year ::= 4DIGIT -- diff --git a/Data/Time/Asctime/Internal.hs b/Data/Time/Asctime/Internal.hs index f707759..1681fc0 100644 --- a/Data/Time/Asctime/Internal.hs +++ b/Data/Time/Asctime/Internal.hs @@ -19,10 +19,10 @@ import Data.Time.HTTP.Common -- |Parse an ANSI C's @asctime()@ string. asctime ∷ Parser LocalTime asctime = do weekDay ← shortWeekDayNameP - _ ← string ", " + _ ← char ' ' month ← shortMonthNameP _ ← char ' ' - day ← read2 + day ← read2' _ ← char ' ' hour ← read2 _ ← char ':' @@ -46,10 +46,10 @@ toAsciiBuilder localTime timeOfDay = localTimeOfDay localTime in shortWeekDayName week - ⊕ A.toAsciiBuilder "⊕ " + ⊕ A.toAsciiBuilder " " ⊕ shortMonthName month ⊕ A.toAsciiBuilder " " - ⊕ show2 day + ⊕ show2' day ⊕ A.toAsciiBuilder " " ⊕ show2 (todHour timeOfDay) ⊕ A.toAsciiBuilder ":" diff --git a/Data/Time/HTTP/Common.hs b/Data/Time/HTTP/Common.hs index cf5412c..1cfa89e 100644 --- a/Data/Time/HTTP/Common.hs +++ b/Data/Time/HTTP/Common.hs @@ -15,11 +15,13 @@ module Data.Time.HTTP.Common , longMonthName , longMonthNameP - , show2 , show4 + , show2 + , show2' - , read2 , read4 + , read2 + , read2' , show4digitsTZ , read4digitsTZ @@ -211,6 +213,14 @@ show2 = A.unsafeFromBuilder ∘ go | i ≥ 0 ∧ i < 100 = BT.integral i | otherwise = error ("show2: the integer i must satisfy 0 <= i < 100: " ⧺ show i) +show2' ∷ Integral i ⇒ i → AsciiBuilder +{-# INLINE show2' #-} +show2' = A.unsafeFromBuilder ∘ go + where + go i | i ≥ 0 ∧ i < 10 = B.fromByteString " " ⊕ BT.digit i + | i ≥ 0 ∧ i < 100 = BT.integral i + | otherwise = error ("show2': the integer i must satisfy 0 <= i < 100: " ⧺ show i) + read4 ∷ Num n ⇒ Parser n {-# INLINEABLE read4 #-} read4 = do n1 ← digit' @@ -225,6 +235,12 @@ read2 = do n1 ← digit' n2 ← digit' return (n1 * 10 + n2) +read2' ∷ Num n ⇒ Parser n +{-# INLINEABLE read2' #-} +read2' = do n1 ← (char ' ' *> pure 0) <|> digit' + n2 ← digit' + return (n1 * 10 + n2) + digit' ∷ Num n ⇒ Parser n digit' = fromC <$> P.digit diff --git a/GNUmakefile b/GNUmakefile index 6473d6f..b51a53d 100644 --- a/GNUmakefile +++ b/GNUmakefile @@ -1,3 +1,4 @@ CONFIGURE_ARGS = -O2 --enable-tests +RUN_COMMAND = time ./dist/build/test-time-http/test-time-http include cabal-package.mk diff --git a/Test/Time/HTTP.hs b/Test/Time/HTTP.hs index 97c5da8..bb60928 100644 --- a/Test/Time/HTTP.hs +++ b/Test/Time/HTTP.hs @@ -1,8 +1,15 @@ {-# LANGUAGE - UnicodeSyntax + OverloadedStrings + , UnicodeSyntax + , ViewPatterns #-} module Main (main) where +import Control.Applicative +import Control.Applicative.Unicode +import Data.Time +import qualified Data.Time.Asctime as Asctime import System.Exit +import Prelude.Unicode import Test.QuickCheck main ∷ IO () @@ -17,6 +24,24 @@ runTest prop Failure {} → exitFailure NoExpectedFailure {} → exitFailure +instance Arbitrary Day where + arbitrary = ModifiedJulianDay <$> arbitrary + +instance Arbitrary TimeOfDay where + arbitrary + = do h ← choose (0, 23) + m ← choose (0, 59) + s ← choose (0, 60) + return $ TimeOfDay h m (fromIntegral (s ∷ Int)) + +instance Arbitrary LocalTime where + arbitrary = LocalTime <$> arbitrary ⊛ arbitrary + tests ∷ [Property] -tests = [ +tests = [ -- Asctime + property ( Asctime.fromAscii "Sun Nov 6 08:49:37 1994" + ≡ Right (LocalTime (ModifiedJulianDay 49662) (TimeOfDay 8 49 37)) ) + , property ( Asctime.toAscii (LocalTime (ModifiedJulianDay 49662) (TimeOfDay 8 49 37)) + ≡ "Sun Nov 6 08:49:37 1994" ) + , property $ \lt → Right lt ≡ Asctime.fromAscii (Asctime.toAscii lt) ] diff --git a/time-http.cabal b/time-http.cabal index 75b9904..d8c1c2a 100644 --- a/time-http.cabal +++ b/time-http.cabal @@ -42,9 +42,9 @@ Library Build-depends: ascii == 0.0.*, + attoparsec == 0.9.*, blaze-builder == 0.3.*, blaze-textual == 0.2.*, - attoparsec == 0.9.*, base == 4.*, base-unicode-symbols == 0.2.*, time == 1.2.* @@ -62,9 +62,9 @@ Test-Suite test-time-http Build-depends: QuickCheck == 2.4.*, ascii == 0.0.*, + attoparsec == 0.9.*, blaze-builder == 0.3.*, blaze-textual == 0.2.*, - attoparsec == 0.9.*, base == 4.*, base-unicode-symbols == 0.2.*, time == 1.2.*