@@ -2,81 +2,60 @@
2{-# OPTIONS_GHC -fno-warn-orphans #-}
2{-# OPTIONS_GHC -fno-warn-orphans #-}
3module Main where
3module Main where
5+import Control.Applicative
10+import qualified Data.Text as T
11+import qualified Data.Text.Lazy as L
5import Test.Hspec
13import Test.Hspec
6import Test.Hspec.QuickCheck(prop)
14import Test.Hspec.QuickCheck(prop)
7import Test.QuickCheck
15import Test.QuickCheck
9import Web.PathPieces
17import Web.PathPieces
10−import qualified Data.Text as T
11−import Data.Maybe (fromJust)
13−-- import FileLocation (debug)
15instance Arbitrary T.Text where
19instance Arbitrary T.Text where
16− arbitrary = fmap T.pack arbitrary
20+ arbitrary = T.pack <$> arbitrary
22+instance Arbitrary L.Text where
23+ arbitrary = L.pack <$> arbitrary
25+instance Arbitrary Day where
26+ arbitrary = liftA3 fromGregorian (fmap abs arbitrary) arbitrary arbitrary
19main :: IO ()
28main :: IO ()
20main = hspec spec
29main = hspec spec
31+(<=>) :: Eq a => (a -> b) -> (b -> Maybe a) -> a -> Bool
32+(f <=> g) x = g (f x) == Just x
22spec :: Spec
34spec :: Spec
24 describe "PathPiece" $ do
36 describe "PathPiece" $ do
25− prop "toPathPiece <=> fromPathPiece String" $ \(p::String) ->
37+ prop "toPathPiece <=> fromPathPiece Bool" $ \(p :: Bool) -> (toPathPiece <=> fromPathPiece) p
26− case (fromPathPiece . toPathPiece) p of
38+ prop "toPathPiece <=> fromPathPiece Int" $ \(p :: Int) -> (toPathPiece <=> fromPathPiece) p
27− Nothing -> null p
39+ prop "toPathPiece <=> fromPathPiece Int8" $ \(p :: Int8) -> (toPathPiece <=> fromPathPiece) p
28− Just pConverted -> p == pConverted
40+ prop "toPathPiece <=> fromPathPiece Int16" $ \(p :: Int16) -> (toPathPiece <=> fromPathPiece) p
30− prop "toPathPiece <=> fromPathPiece Text" $ \(p::T.Text) ->
41+ prop "toPathPiece <=> fromPathPiece Int32" $ \(p :: Int32) -> (toPathPiece <=> fromPathPiece) p
31− case (fromPathPiece . toPathPiece) p of
42+ prop "toPathPiece <=> fromPathPiece Word" $ \(p :: Word) -> (toPathPiece <=> fromPathPiece) p
32− Nothing -> T.null p
43+ prop "toPathPiece <=> fromPathPiece Word8" $ \(p :: Word8) -> (toPathPiece <=> fromPathPiece) p
33− Just pConverted -> p == pConverted
35− prop "toPathPiece <=> fromPathPiece Int" $ \(p::Int) ->
44+ prop "toPathPiece <=> fromPathPiece Word16" $ \(p :: Word16) -> (toPathPiece <=> fromPathPiece) p
36− case (fromPathPiece . toPathPiece) p of
45+ prop "toPathPiece <=> fromPathPiece Word32" $ \(p :: Word32) -> (toPathPiece <=> fromPathPiece) p
37− Nothing -> False
46+ prop "toPathPiece <=> fromPathPiece Word64" $ \(p :: Word64) -> (toPathPiece <=> fromPathPiece) p
38− Just pConverted -> p == pConverted
40− prop "toPathPiece <=> fromPathPiece Bool" $ \(p::Bool) ->
47+ prop "toPathPiece <=> fromPathPiece Word64" $ \(p :: Word64) -> (toPathPiece <=> fromPathPiece) p
41− case (fromPathPiece . toPathPiece) p of
48+ prop "toPathPiece <=> fromPathPiece String" $ \(p :: String) -> (toPathPiece <=> fromPathPiece) p
42− Nothing -> False
49+ prop "toPathPiece <=> fromPathPiece Text.Strict" $ \(p :: T.Text) -> (toPathPiece <=> fromPathPiece) p
43− Just pConverted -> p == pConverted
45− prop "toPathPiece <=> fromPathPiece Maybe String" $ \(p::Maybe String) ->
50+ prop "toPathPiece <=> fromPathPiece Text.Lazy" $ \(p :: L.Text) -> (toPathPiece <=> fromPathPiece) p
46− case (fromPathPiece . toPathPiece) p of
51+ prop "toPathPiece <=> fromPathPiece Day" $ \(p :: Day) -> (toPathPiece <=> fromPathPiece) p
48− Just pConverted -> p == pConverted
53+ prop "toPathPiece <=> fromPathPiece Maybe String" $ \(p::Maybe String) -> (toPathPiece <=> fromPathPiece) p
50 describe "PathMultiPiece" $ do
55 describe "PathMultiPiece" $ do
51− prop "toPathMultiPiece <=> fromPathMultiPiece String" $ \(p::[String]) ->
56+ prop "toPathMultiPiece <=> fromPathMultiPiece String" $ \(p::[String]) -> (toPathMultiPiece <=> fromPathMultiPiece) p
52− p == (fromJust . fromPathMultiPiece . toPathMultiPiece) p
54− prop "toPathMultiPiece <=> fromPathMultiPiece Text" $ \(p::[T.Text]) ->
57+ prop "toPathMultiPiece <=> fromPathMultiPiece Text" $ \(p::[T.Text]) -> (toPathMultiPiece <=> fromPathMultiPiece) p
55− p == (fromJust . fromPathMultiPiece . toPathMultiPiece) p
58− describe "SinglePiece" $ do
59− prop "toPathPiece <=> fromPathPiece String" $ \(p::String) ->
60− case (fromPathPiece . toPathPiece) p of
62− Just pConverted -> p == pConverted
64− prop "toPathPiece <=> fromPathPiece Text" $ \(p::T.Text) ->
65− case (fromPathPiece . toPathPiece) p of
67− Just pConverted -> p == pConverted
69− prop "toPathPiece <=> fromPathPiece Int" $ \(p::Int) ->
70− case (fromPathPiece . toPathPiece) p of
72− Just pConverted -> p == pConverted
74− describe "MultiPiece" $ do
75− prop "toPathMultiPiece <=> fromPathMultiPiece String" $ \(p::[String]) ->
76− p == (fromJust . fromPathMultiPiece . toPathMultiPiece) p
78− prop "toPathMultiPiece <=> fromPathMultiPiece Text" $ \(p::[T.Text]) ->
79− p == (fromJust . fromPathMultiPiece . toPathMultiPiece) p
81 it "bad ints are rejected" $ fromPathPiece (T.pack "123hello")
59 it "bad ints are rejected" $ fromPathPiece (T.pack "123hello")
82 `shouldBe` (Nothing :: Maybe Int)
60 `shouldBe` (Nothing :: Maybe Int)