git.iko.soy/typeable/http-api-data
on master help sign in
Update Spec test suite to use Web.HttpApiData
commit6a475a5369b8d3a8000a2fe12ebf74cf5b939035 authorNickolay Kudasov <nickolay.kudasov@gmail.com> dateWed 7 Oct 2015, 16:14 (11 years ago) parentba18233
1 file changed+47−49
test/Spec.hs+47−49
test/Spec.hs+47−49
@@ -16,7 +16,7 @@ import Test.Hspec
1616import Test.Hspec.QuickCheck(prop)
1717import Test.QuickCheck
1818
19−import Web.PathPieces
19+import Web.HttpApiData
2020
2121instance Arbitrary T.Text where
2222 arbitrary = T.pack <$> arbitrary
@@ -36,21 +36,18 @@ instance Arbitrary Version where
3636main :: IO ()
3737main = hspec spec
3838
39−(<=>) :: Eq a => (a -> b) -> (b -> Maybe a) -> a -> Bool
40−(f <=> g) x = g (f x) == Just x
39+(<=>) :: Eq a => (a -> b) -> (b -> Either T.Text a) -> a -> Bool
40+(f <=> g) x = g (f x) == Right x
4141
4242data Proxy a = Proxy
4343
44−checkPathPiece :: forall a. (Eq a, PathPiece a, Show a, Arbitrary a) => Proxy a -> String -> Spec
45−checkPathPiece _ name = prop ("toPathPiece <=> fromPathPiece " ++ name) (toPathPiece <=> fromPathPiece :: a -> Bool)
46−
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)
44+checkUrlPiece :: forall a. (Eq a, ToHttpApiData a, FromHttpApiData a, Show a, Arbitrary a) => Proxy a -> String -> Spec
45+checkUrlPiece _ name = prop name (toUrlPiece <=> parseUrlPiece :: a -> Bool)
4946
5047data RandomCase a = RandomCase [Bool] a
5148
52−instance PathPiece a => Show (RandomCase a) where
53− show rc@(RandomCase _ x) = show (toPathPiece rc) ++ " (original: " ++ show (toPathPiece x) ++ ")"
49+instance ToHttpApiData a => Show (RandomCase a) where
50+ show rc@(RandomCase _ x) = show (toUrlPiece rc) ++ " (original: " ++ show (toUrlPiece x) ++ ")"
5451
5552instance Eq a => Eq (RandomCase a) where
5653 RandomCase _ x == RandomCase _ y = x == y
@@ -60,51 +57,52 @@ instance Arbitrary a => Arbitrary (RandomCase a) where
6057 where
6158 nonempty = liftA2 (:) arbitrary arbitrary
6259
63−instance PathPiece a => PathPiece (RandomCase a) where
64− toPathPiece (RandomCase us x) = T.pack (zipWith (\u -> if u then toUpper else toLower) (cycle us) (T.unpack (toPathPiece x)))
65− fromPathPiece s = RandomCase [] <$> fromPathPiece s
60+instance ToHttpApiData a => ToHttpApiData (RandomCase a) where
61+ toUrlPiece (RandomCase us x) = T.pack (zipWith (\u -> if u then toUpper else toLower) (cycle us) (T.unpack (toUrlPiece x)))
62+
63+instance FromHttpApiData a => FromHttpApiData (RandomCase a) where
64+ parseUrlPiece s = RandomCase [] <$> parseUrlPiece s
6665
67−-- | Check case insensitivity for @fromPathPiece@.
68−checkPathPieceI :: forall a. (Eq a, PathPiece a, Show a, Arbitrary a) => Proxy a -> String -> Spec
69−checkPathPieceI _ = checkPathPiece (Proxy :: Proxy (RandomCase a))
66+-- | Check case insensitivity for @fromUrlPiece@.
67+checkUrlPieceI :: forall a. (Eq a, ToHttpApiData a, FromHttpApiData a, Show a, Arbitrary a) => Proxy a -> String -> Spec
68+checkUrlPieceI _ = checkUrlPiece (Proxy :: Proxy (RandomCase a))
7069
7170spec :: Spec
7271spec = do
73− describe "PathPiece" $ do
74− checkPathPiece (Proxy :: Proxy ()) "()"
75− checkPathPiece (Proxy :: Proxy Char) "Char"
76− checkPathPieceI (Proxy :: Proxy Bool) "Bool"
77− checkPathPieceI (Proxy :: Proxy Ordering) "Ordering"
78− checkPathPiece (Proxy :: Proxy Int) "Int"
79− checkPathPiece (Proxy :: Proxy Int8) "Int8"
80− checkPathPiece (Proxy :: Proxy Int16) "Int16"
81− checkPathPiece (Proxy :: Proxy Int32) "Int32"
82− checkPathPiece (Proxy :: Proxy Int64) "Int64"
83− checkPathPiece (Proxy :: Proxy Integer) "Integer"
84− checkPathPiece (Proxy :: Proxy Word) "Word"
85− checkPathPiece (Proxy :: Proxy Word8) "Word8"
86− checkPathPiece (Proxy :: Proxy Word16) "Word16"
87− checkPathPiece (Proxy :: Proxy Word32) "Word32"
88− checkPathPiece (Proxy :: Proxy Word64) "Word64"
89− checkPathPiece (Proxy :: Proxy String) "String"
90− checkPathPiece (Proxy :: Proxy T.Text) "Text.Strict"
91− checkPathPiece (Proxy :: Proxy L.Text) "Text.Lazy"
92− checkPathPiece (Proxy :: Proxy Day) "Day"
93− checkPathPiece (Proxy :: Proxy Version) "Version"
94−
95− checkPathPiece (Proxy :: Proxy (Maybe String)) "Maybe String"
96− checkPathPieceI (Proxy :: Proxy (Maybe Integer)) "Maybe Integer"
97− checkPathPiece (Proxy :: Proxy (Either Integer T.Text)) "Either Integer Text"
98− checkPathPieceI (Proxy :: Proxy (Either Version Day)) "Either Version Day"
99−
100− describe "PathMultiPiece" $ do
101− checkPathMultiPiece (Proxy :: Proxy [String]) "[String]"
102− checkPathMultiPiece (Proxy :: Proxy [T.Text]) "[Text]"
72+ describe "toUrlPiece <=> fromUrlPiece" $ do
73+ checkUrlPiece (Proxy :: Proxy ()) "()"
74+ checkUrlPiece (Proxy :: Proxy Char) "Char"
75+ checkUrlPieceI (Proxy :: Proxy Bool) "Bool"
76+ checkUrlPieceI (Proxy :: Proxy Ordering) "Ordering"
77+ checkUrlPiece (Proxy :: Proxy Int) "Int"
78+ checkUrlPiece (Proxy :: Proxy Int8) "Int8"
79+ checkUrlPiece (Proxy :: Proxy Int16) "Int16"
80+ checkUrlPiece (Proxy :: Proxy Int32) "Int32"
81+ checkUrlPiece (Proxy :: Proxy Int64) "Int64"
82+ checkUrlPiece (Proxy :: Proxy Integer) "Integer"
83+ checkUrlPiece (Proxy :: Proxy Word) "Word"
84+ checkUrlPiece (Proxy :: Proxy Word8) "Word8"
85+ checkUrlPiece (Proxy :: Proxy Word16) "Word16"
86+ checkUrlPiece (Proxy :: Proxy Word32) "Word32"
87+ checkUrlPiece (Proxy :: Proxy Word64) "Word64"
88+ checkUrlPiece (Proxy :: Proxy String) "String"
89+ checkUrlPiece (Proxy :: Proxy T.Text) "Text.Strict"
90+ checkUrlPiece (Proxy :: Proxy L.Text) "Text.Lazy"
91+ checkUrlPiece (Proxy :: Proxy Day) "Day"
92+ checkUrlPiece (Proxy :: Proxy Version) "Version"
93+
94+ checkUrlPiece (Proxy :: Proxy (Maybe String)) "Maybe String"
95+ checkUrlPieceI (Proxy :: Proxy (Maybe Integer)) "Maybe Integer"
96+ checkUrlPiece (Proxy :: Proxy (Either Integer T.Text)) "Either Integer Text"
97+ checkUrlPieceI (Proxy :: Proxy (Either Version Day)) "Either Version Day"
10398
10499 it "bad integers are rejected" $ do
105− fromPathPiece (T.pack "123hello") `shouldBe` (Nothing :: Maybe Int)
100+ fromUrlPiece (T.pack "123hello") `shouldBe` (Nothing :: Maybe Int)
106101
107102 it "bounds checking works" $ do
108− fromPathPiece (T.pack "256") `shouldBe` (Nothing :: Maybe Int8)
109− fromPathPiece (T.pack "-10") `shouldBe` (Nothing :: Maybe Word)
103+ fromUrlPiece (T.pack "256") `shouldBe` (Nothing :: Maybe Int8)
104+ fromUrlPiece (T.pack "-10") `shouldBe` (Nothing :: Maybe Word)
105+ where
106+ fromUrlPiece :: FromHttpApiData a => T.Text -> Maybe a
107+ fromUrlPiece = either (const Nothing) Just . parseUrlPiece
110108