servant/servant-server/test/Servant/Server/Internal/ConfigSpec.hs

55 lines
1.9 KiB
Haskell
Raw Normal View History

2015-12-26 15:30:39 +01:00
{-# LANGUAGE DataKinds #-}
{-# OPTIONS_GHC -fdefer-type-errors #-}
module Servant.Server.Internal.ConfigSpec (spec) where
import Data.Proxy (Proxy (..))
2016-01-08 18:15:01 +01:00
import Test.Hspec (Spec, describe, it, shouldBe, pending, context)
2015-12-26 15:30:39 +01:00
import Test.ShouldNotTypecheck (shouldNotTypecheck)
2016-01-08 18:15:01 +01:00
import Servant.API
2015-12-26 15:30:39 +01:00
import Servant.Server.Internal.Config
spec :: Spec
spec = do
getConfigEntrySpec
getConfigEntrySpec :: Spec
getConfigEntrySpec = describe "getConfigEntry" $ do
let cfg1 = 0 .:. EmptyConfig :: Config '[ConfigEntry "a" Int]
cfg2 = 1 .:. cfg1 :: Config '[ConfigEntry "a" Int, ConfigEntry "a" Int]
2015-12-26 15:30:39 +01:00
it "gets the config if a matching one exists" $ do
getConfigEntry (Proxy :: Proxy "a") cfg1 `shouldBe` 0
it "gets the first matching config" $ do
getConfigEntry (Proxy :: Proxy "a") cfg2 `shouldBe` 1
2016-01-08 18:15:01 +01:00
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]
2016-01-08 18:15:01 +01:00
getConfigEntry (Proxy :: Proxy 1) cfg `shouldBe` 'a'
context "Show instance" $ do
let cfg = 1 .:. 2 .:. EmptyConfig
2016-01-08 18:15:01 +01:00
it "has a Show instance" $ do
show cfg `shouldBe` "1 .:. 2 .:. EmptyConfig"
2016-01-08 18:15:01 +01:00
it "bracketing works" $ do
show (Just cfg) `shouldBe` "Just (1 .:. 2 .:. EmptyConfig)"
2016-01-08 18:15:01 +01:00
it "bracketing works with operators" $ do
let cfg = (1 .:. 'a' .:. EmptyConfig) :<|> ('b' .:. True .:. EmptyConfig)
show cfg `shouldBe` "(1 .:. 'a' .:. EmptyConfig) :<|> ('b' .:. True .:. EmptyConfig)"
2016-01-08 18:15:01 +01:00
2015-12-26 15:30:39 +01:00
it "does not typecheck if key does not exist" $ do
let x = getConfigEntry (Proxy :: Proxy "b") cfg1 :: Int
shouldNotTypecheck x
it "does not typecheck if key maps to a different type" $ do
let x = getConfigEntry (Proxy :: Proxy "a") cfg1 :: String
shouldNotTypecheck x