config: added Show instance
This commit is contained in:
parent
5c863b2043
commit
66628ca043
2 changed files with 28 additions and 3 deletions
|
@ -34,14 +34,21 @@ data Config a where
|
|||
EmptyConfig :: Config '[]
|
||||
ConsConfig :: x -> Config xs -> Config (x ': xs)
|
||||
|
||||
instance Show (Config '[]) where
|
||||
show EmptyConfig = "EmptyConfig"
|
||||
instance (Show a, Show (Config as)) => Show (Config (ConfigEntry tag a ': as)) where
|
||||
showsPrec outerPrecedence (ConsConfig (ConfigEntry a) as) =
|
||||
showParen (outerPrecedence > 5) $
|
||||
shows a . showString " .: " . shows as
|
||||
|
||||
instance Eq (Config '[]) where
|
||||
_ == _ = True
|
||||
instance (Eq a, Eq (Config as)) => Eq (Config (a ' : as)) where
|
||||
instance (Eq a, Eq (Config as)) => Eq (Config (a ': as)) where
|
||||
ConsConfig x1 y1 == ConsConfig x2 y2 = x1 == x2 && y1 == y2
|
||||
|
||||
(.:) :: x -> Config xs -> Config (ConfigEntry tag x ': xs)
|
||||
e .: cfg = ConsConfig (ConfigEntry e) cfg
|
||||
infixr 4 .:
|
||||
infixr 5 .:
|
||||
|
||||
class HasConfigEntry (cfg :: [*]) (a :: k) (val :: *) | cfg a -> val where
|
||||
getConfigEntry :: proxy a -> Config cfg -> val
|
||||
|
|
|
@ -3,9 +3,10 @@
|
|||
module Servant.Server.Internal.ConfigSpec (spec) where
|
||||
|
||||
import Data.Proxy (Proxy (..))
|
||||
import Test.Hspec (Spec, describe, it, shouldBe)
|
||||
import Test.Hspec (Spec, describe, it, shouldBe, pending, context)
|
||||
import Test.ShouldNotTypecheck (shouldNotTypecheck)
|
||||
|
||||
import Servant.API
|
||||
import Servant.Server.Internal.Config
|
||||
|
||||
spec :: Spec
|
||||
|
@ -26,6 +27,23 @@ getConfigEntrySpec = describe "getConfigEntry" $ do
|
|||
|
||||
getConfigEntry (Proxy :: Proxy "a") cfg2 `shouldBe` 1
|
||||
|
||||
it "allows to distinguish between different config entries with the same type by tag" $ do
|
||||
let cfg = 'a' .: 'b' .: EmptyConfig :: Config '[ConfigEntry 1 Char, ConfigEntry 2 Char]
|
||||
print cfg
|
||||
getConfigEntry (Proxy :: Proxy 1) cfg `shouldBe` 'a'
|
||||
|
||||
context "Show instance" $ do
|
||||
let cfg = 1 .: 2 .: EmptyConfig
|
||||
it "has a Show instance" $ do
|
||||
show cfg `shouldBe` "1 .: 2 .: EmptyConfig"
|
||||
|
||||
it "bracketing works" $ do
|
||||
show (Just cfg) `shouldBe` "Just (1 .: 2 .: EmptyConfig)"
|
||||
|
||||
it "bracketing works with operators" $ do
|
||||
let cfg = (1 .: 'a' .: EmptyConfig) :<|> ('b' .: True .: EmptyConfig)
|
||||
show cfg `shouldBe` "(1 .: 'a' .: EmptyConfig) :<|> ('b' .: True .: EmptyConfig)"
|
||||
|
||||
it "does not typecheck if key does not exist" $ do
|
||||
|
||||
let x = getConfigEntry (Proxy :: Proxy "b") cfg1 :: Int
|
||||
|
|
Loading…
Reference in a new issue