git.iko.soy/typeable/servant
on master help sign in
Add framingRender examples
17f9237Oleg Grenrus
servant/servant.cabal+1
@@ -150,6 +150,7 @@ test-suite spec
150 , base-compat
150 , base-compat
151 , aeson
151 , aeson
152 , bytestring
152 , bytestring
153+ , mtl
153 , servant
154 , servant
154 , string-conversions
155 , string-conversions
155 , text
156 , text
servant/test/Servant/API/StreamSpec.hs+15
@@ -1,6 +1,8 @@
1{-# LANGUAGE OverloadedStrings #-}
1{-# LANGUAGE OverloadedStrings #-}
2module Servant.API.StreamSpec where
2module Servant.API.StreamSpec where
3
3
4+import Control.Monad.Except
5+ (runExcept)
4import qualified Data.Aeson as Aeson
6import qualified Data.Aeson as Aeson
5import qualified Data.ByteString as BS
7import qualified Data.ByteString as BS
6import qualified Data.ByteString.Lazy as LBS
8import qualified Data.ByteString.Lazy as LBS
@@ -32,6 +34,11 @@ spec = describe "Servant.API.Stream" $ do
32
34
33 describe "NewlineFraming" $ do
35 describe "NewlineFraming" $ do
34 let tp = framingUnrender (Proxy :: Proxy NewlineFraming) (Right . LBS.toStrict)
36 let tp = framingUnrender (Proxy :: Proxy NewlineFraming) (Right . LBS.toStrict)
37+ let re = framingRender (Proxy :: Proxy NewlineFraming) id
38+
39+ it "framingRender examples" $ do
40+ runRenderFrames re [] `shouldBe` Right ""
41+ runRenderFrames re ["foo", "bar", "baz"] `shouldBe` Right "foo\nbar\nbaz"
35
42
36 it "framingUnrender examples" $ do
43 it "framingUnrender examples" $ do
37 let expected n = map Right [fromString ("foo" ++ show (n :: Int)), "bar", "baz"]
44 let expected n = map Right [fromString ("foo" ++ show (n :: Int)), "bar", "baz"]
@@ -51,6 +58,11 @@ spec = describe "Servant.API.Stream" $ do
51
58
52 describe "NetstringFraming" $ do
59 describe "NetstringFraming" $ do
53 let tp = framingUnrender (Proxy :: Proxy NetstringFraming) (Right . LBS.toStrict)
60 let tp = framingUnrender (Proxy :: Proxy NetstringFraming) (Right . LBS.toStrict)
61+ let re = framingRender (Proxy :: Proxy NetstringFraming) id
62+
63+ it "framingRender examples" $ do
64+ runRenderFrames re [] `shouldBe` Right ""
65+ runRenderFrames re ["foo", "bar", "baz"] `shouldBe` Right "3:foo,3:bar,3:baz,"
54
66
55 it "framingUnrender examples" $ do
67 it "framingUnrender examples" $ do
56 let expected n = map Right [fromString ("foo" ++ show (n :: Int)), "bar", "baz"]
68 let expected n = map Right [fromString ("foo" ++ show (n :: Int)), "bar", "baz"]
@@ -74,6 +86,9 @@ roundtrip
74roundtrip render unrender xs =
86roundtrip render unrender xs =
75 map Right xs === runUnrenderFrames (unrender . fmap LBS.toStrict . render) xs
87 map Right xs === runUnrenderFrames (unrender . fmap LBS.toStrict . render) xs
76
88
89+runRenderFrames :: (SourceT Identity a -> SourceT Identity LBS.ByteString) -> [a] -> Either String LBS.ByteString
90+runRenderFrames f = fmap mconcat . runExcept . runSourceT . f . source
91+
77runUnrenderFrames :: (SourceT Identity b -> SourceT Identity a) -> [b] -> [Either String a]
92runUnrenderFrames :: (SourceT Identity b -> SourceT Identity a) -> [b] -> [Either String a]
78runUnrenderFrames f = go . Effect . flip unSourceT return . f . source where
93runUnrenderFrames f = go . Effect . flip unSourceT return . f . source where
79 go :: StepT Identity a -> [Either String a]
94 go :: StepT Identity a -> [Either String a]