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

53 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 :: Int) :. EmptyConfig
cfg2 = (1 :: Int) :. cfg1
2015-12-26 15:30:39 +01:00
it "gets the config if a matching one exists" $ do
getConfigEntry (Proxy :: Proxy ()) cfg1 `shouldBe` (0 :: Int)
2015-12-26 15:30:39 +01:00
it "gets the first matching config" $ do
getConfigEntry (Proxy :: Proxy ()) cfg2 `shouldBe` (1 :: Int)
2015-12-26 15:30:39 +01:00
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' :. (Tag 'b' :: Tagged "second" Char) :. EmptyConfig
getConfigEntry (Proxy :: Proxy ()) cfg `shouldBe` 'a'
getConfigEntry (Proxy :: Proxy "second") cfg `shouldBe` 'b'
it "does not typecheck if type does not exist" $ do
let x = getConfigEntry (Proxy :: Proxy ()) cfg1 :: Bool
shouldNotTypecheck x
it "does not typecheck if tag does not exist" $ do
let cfg = (Tag 'a' :: Tagged "foo" Char) :. EmptyConfig
x = getConfigEntry (Proxy :: Proxy "bar") cfg :: Char
shouldNotTypecheck x
2016-01-08 18:15:01 +01:00
context "Show instance" $ do
2016-01-10 16:50:17 +01:00
let cfg = 1 :. 2 :. EmptyConfig
2016-01-08 18:15:01 +01:00
it "has a Show instance" $ do
2016-01-10 16:50:17 +01:00
show cfg `shouldBe` "1 :. 2 :. EmptyConfig"
2016-01-08 18:15:01 +01:00
it "bracketing works" $ do
2016-01-10 16:50:17 +01:00
show (Just cfg) `shouldBe` "Just (1 :. 2 :. EmptyConfig)"
2016-01-08 18:15:01 +01:00
it "bracketing works with operators" $ do
2016-01-10 16:50:17 +01:00
let cfg = (1 :. 'a' :. EmptyConfig) :<|> ('b' :. True :. EmptyConfig)
show cfg `shouldBe` "(1 :. 'a' :. EmptyConfig) :<|> ('b' :. True :. EmptyConfig)"