@@ -16,7 +16,7 @@ import Test.Hspec
16import Test.Hspec.QuickCheck(prop)
16import Test.Hspec.QuickCheck(prop)
17import Test.QuickCheck
17import Test.QuickCheck
19−import Web.PathPieces
19+import Web.HttpApiData
21instance Arbitrary T.Text where
21instance Arbitrary T.Text where
22 arbitrary = T.pack <$> arbitrary
22 arbitrary = T.pack <$> arbitrary
@@ -36,21 +36,18 @@ instance Arbitrary Version where
36main :: IO ()
36main :: IO ()
37main = hspec spec
37main = hspec spec
39−(<=>) :: Eq a => (a -> b) -> (b -> Maybe a) -> a -> Bool
39+(<=>) :: Eq a => (a -> b) -> (b -> Either T.Text a) -> a -> Bool
40−(f <=> g) x = g (f x) == Just x
40+(f <=> g) x = g (f x) == Right x
42data Proxy a = Proxy
42data Proxy a = Proxy
44−checkPathPiece :: forall a. (Eq a, PathPiece a, Show a, Arbitrary a) => Proxy a -> String -> Spec
44+checkUrlPiece :: forall a. (Eq a, ToHttpApiData a, FromHttpApiData a, Show a, Arbitrary a) => Proxy a -> String -> Spec
45−checkPathPiece _ name = prop ("toPathPiece <=> fromPathPiece " ++ name) (toPathPiece <=> fromPathPiece :: a -> Bool)
45+checkUrlPiece _ name = prop name (toUrlPiece <=> parseUrlPiece :: a -> Bool)
47−checkPathMultiPiece :: forall a. (Eq a, PathMultiPiece a, Show a, Arbitrary a) => Proxy a -> String -> Spec
48−checkPathMultiPiece _ name = prop ("toPathMultiPiece <=> fromPathMultiPiece " ++ name) (toPathMultiPiece <=> fromPathMultiPiece :: a -> Bool)
50data RandomCase a = RandomCase [Bool] a
47data RandomCase a = RandomCase [Bool] a
52−instance PathPiece a => Show (RandomCase a) where
49+instance ToHttpApiData a => Show (RandomCase a) where
53− show rc@(RandomCase _ x) = show (toPathPiece rc) ++ " (original: " ++ show (toPathPiece x) ++ ")"
50+ show rc@(RandomCase _ x) = show (toUrlPiece rc) ++ " (original: " ++ show (toUrlPiece x) ++ ")"
55instance Eq a => Eq (RandomCase a) where
52instance Eq a => Eq (RandomCase a) where
56 RandomCase _ x == RandomCase _ y = x == y
53 RandomCase _ x == RandomCase _ y = x == y
@@ -60,51 +57,52 @@ instance Arbitrary a => Arbitrary (RandomCase a) where
61 nonempty = liftA2 (:) arbitrary arbitrary
58 nonempty = liftA2 (:) arbitrary arbitrary
63−instance PathPiece a => PathPiece (RandomCase a) where
60+instance ToHttpApiData a => ToHttpApiData (RandomCase a) where
64− toPathPiece (RandomCase us x) = T.pack (zipWith (\u -> if u then toUpper else toLower) (cycle us) (T.unpack (toPathPiece x)))
61+ toUrlPiece (RandomCase us x) = T.pack (zipWith (\u -> if u then toUpper else toLower) (cycle us) (T.unpack (toUrlPiece x)))
65− fromPathPiece s = RandomCase [] <$> fromPathPiece s
62+
63+instance FromHttpApiData a => FromHttpApiData (RandomCase a) where
64+ parseUrlPiece s = RandomCase [] <$> parseUrlPiece s
67−-- | Check case insensitivity for @fromPathPiece@.
66+-- | Check case insensitivity for @fromUrlPiece@.
68−checkPathPieceI :: forall a. (Eq a, PathPiece a, Show a, Arbitrary a) => Proxy a -> String -> Spec
67+checkUrlPieceI :: forall a. (Eq a, ToHttpApiData a, FromHttpApiData a, Show a, Arbitrary a) => Proxy a -> String -> Spec
69−checkPathPieceI _ = checkPathPiece (Proxy :: Proxy (RandomCase a))
68+checkUrlPieceI _ = checkUrlPiece (Proxy :: Proxy (RandomCase a))
71spec :: Spec
70spec :: Spec
73− describe "PathPiece" $ do
72+ describe "toUrlPiece <=> fromUrlPiece" $ do
74− checkPathPiece (Proxy :: Proxy ()) "()"
73+ checkUrlPiece (Proxy :: Proxy ()) "()"
75− checkPathPiece (Proxy :: Proxy Char) "Char"
74+ checkUrlPiece (Proxy :: Proxy Char) "Char"
76− checkPathPieceI (Proxy :: Proxy Bool) "Bool"
75+ checkUrlPieceI (Proxy :: Proxy Bool) "Bool"
77− checkPathPieceI (Proxy :: Proxy Ordering) "Ordering"
76+ checkUrlPieceI (Proxy :: Proxy Ordering) "Ordering"
78− checkPathPiece (Proxy :: Proxy Int) "Int"
77+ checkUrlPiece (Proxy :: Proxy Int) "Int"
79− checkPathPiece (Proxy :: Proxy Int8) "Int8"
78+ checkUrlPiece (Proxy :: Proxy Int8) "Int8"
80− checkPathPiece (Proxy :: Proxy Int16) "Int16"
79+ checkUrlPiece (Proxy :: Proxy Int16) "Int16"
81− checkPathPiece (Proxy :: Proxy Int32) "Int32"
80+ checkUrlPiece (Proxy :: Proxy Int32) "Int32"
82− checkPathPiece (Proxy :: Proxy Int64) "Int64"
81+ checkUrlPiece (Proxy :: Proxy Int64) "Int64"
83− checkPathPiece (Proxy :: Proxy Integer) "Integer"
82+ checkUrlPiece (Proxy :: Proxy Integer) "Integer"
84− checkPathPiece (Proxy :: Proxy Word) "Word"
83+ checkUrlPiece (Proxy :: Proxy Word) "Word"
85− checkPathPiece (Proxy :: Proxy Word8) "Word8"
84+ checkUrlPiece (Proxy :: Proxy Word8) "Word8"
86− checkPathPiece (Proxy :: Proxy Word16) "Word16"
85+ checkUrlPiece (Proxy :: Proxy Word16) "Word16"
87− checkPathPiece (Proxy :: Proxy Word32) "Word32"
86+ checkUrlPiece (Proxy :: Proxy Word32) "Word32"
88− checkPathPiece (Proxy :: Proxy Word64) "Word64"
87+ checkUrlPiece (Proxy :: Proxy Word64) "Word64"
89− checkPathPiece (Proxy :: Proxy String) "String"
88+ checkUrlPiece (Proxy :: Proxy String) "String"
90− checkPathPiece (Proxy :: Proxy T.Text) "Text.Strict"
89+ checkUrlPiece (Proxy :: Proxy T.Text) "Text.Strict"
91− checkPathPiece (Proxy :: Proxy L.Text) "Text.Lazy"
90+ checkUrlPiece (Proxy :: Proxy L.Text) "Text.Lazy"
92− checkPathPiece (Proxy :: Proxy Day) "Day"
91+ checkUrlPiece (Proxy :: Proxy Day) "Day"
93− checkPathPiece (Proxy :: Proxy Version) "Version"
92+ checkUrlPiece (Proxy :: Proxy Version) "Version"
95− checkPathPiece (Proxy :: Proxy (Maybe String)) "Maybe String"
94+ checkUrlPiece (Proxy :: Proxy (Maybe String)) "Maybe String"
96− checkPathPieceI (Proxy :: Proxy (Maybe Integer)) "Maybe Integer"
95+ checkUrlPieceI (Proxy :: Proxy (Maybe Integer)) "Maybe Integer"
97− checkPathPiece (Proxy :: Proxy (Either Integer T.Text)) "Either Integer Text"
96+ checkUrlPiece (Proxy :: Proxy (Either Integer T.Text)) "Either Integer Text"
98− checkPathPieceI (Proxy :: Proxy (Either Version Day)) "Either Version Day"
97+ checkUrlPieceI (Proxy :: Proxy (Either Version Day)) "Either Version Day"
100− describe "PathMultiPiece" $ do
101− checkPathMultiPiece (Proxy :: Proxy [String]) "[String]"
102− checkPathMultiPiece (Proxy :: Proxy [T.Text]) "[Text]"
104 it "bad integers are rejected" $ do
99 it "bad integers are rejected" $ do
105− fromPathPiece (T.pack "123hello") `shouldBe` (Nothing :: Maybe Int)
100+ fromUrlPiece (T.pack "123hello") `shouldBe` (Nothing :: Maybe Int)
107 it "bounds checking works" $ do
102 it "bounds checking works" $ do
108− fromPathPiece (T.pack "256") `shouldBe` (Nothing :: Maybe Int8)
103+ fromUrlPiece (T.pack "256") `shouldBe` (Nothing :: Maybe Int8)
109− fromPathPiece (T.pack "-10") `shouldBe` (Nothing :: Maybe Word)
104+ fromUrlPiece (T.pack "-10") `shouldBe` (Nothing :: Maybe Word)
106+ fromUrlPiece :: FromHttpApiData a => T.Text -> Maybe a
107+ fromUrlPiece = either (const Nothing) Just . parseUrlPiece