git.iko.soy/typeable/servant
on master help sign in
Add framingRender examples
commit17f92379804622e75a24389fbe76c8361fcfa13c authorOleg Grenrus <oleg.grenrus@iki.fi> dateSat 10 Nov 2018, 01:20 (8 years ago) parentce83e4b
2 files changed+16
servant/servant.cabal+1 servant/test/Servant/API/StreamSpec.hs+15
servant/servant.cabal+1
@@ -150,6 +150,7 @@ test-suite spec
150150 , base-compat
151151 , aeson
152152 , bytestring
153+ , mtl
153154 , servant
154155 , string-conversions
155156 , text
servant/test/Servant/API/StreamSpec.hs+15
@@ -1,6 +1,8 @@
11{-# LANGUAGE OverloadedStrings #-}
22module Servant.API.StreamSpec where
33
4+import Control.Monad.Except
5+ (runExcept)
46import qualified Data.Aeson as Aeson
57import qualified Data.ByteString as BS
68import qualified Data.ByteString.Lazy as LBS
@@ -32,6 +34,11 @@ spec = describe "Servant.API.Stream" $ do
3234
3335 describe "NewlineFraming" $ do
3436 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"
3542
3643 it "framingUnrender examples" $ do
3744 let expected n = map Right [fromString ("foo" ++ show (n :: Int)), "bar", "baz"]
@@ -51,6 +58,11 @@ spec = describe "Servant.API.Stream" $ do
5158
5259 describe "NetstringFraming" $ do
5360 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,"
5466
5567 it "framingUnrender examples" $ do
5668 let expected n = map Right [fromString ("foo" ++ show (n :: Int)), "bar", "baz"]
@@ -74,6 +86,9 @@ roundtrip
7486roundtrip render unrender xs =
7587 map Right xs === runUnrenderFrames (unrender . fmap LBS.toStrict . render) xs
7688
89+runRenderFrames :: (SourceT Identity a -> SourceT Identity LBS.ByteString) -> [a] -> Either String LBS.ByteString
90+runRenderFrames f = fmap mconcat . runExcept . runSourceT . f . source
91+
7792runUnrenderFrames :: (SourceT Identity b -> SourceT Identity a) -> [b] -> [Either String a]
7893runUnrenderFrames f = go . Effect . flip unSourceT return . f . source where
7994 go :: StepT Identity a -> [Either String a]