aeson-match-qq-2.0.0: test/Aeson/Match/QQ/Internal/CapturesSpec.hs
{-# LANGUAGE DataKinds #-}
{-# LANGUAGE FlexibleContexts #-}
{-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE TypeApplications #-}
module Aeson.Match.QQ.Internal.CapturesSpec (spec) where
import Data.Aeson qualified as Aeson
import Test.Hspec
import Aeson.Match.QQ.Internal.Captures
spec :: Spec
spec = do
describe "get" $ do
let captures :: Captures '["name", "age", "active"]
captures =
CCons (Aeson.String "Ada")
(CCons (Aeson.Number 37) (CCons (Aeson.Bool True) CEmpty))
it "looks up the first capture" $
extractValue @"name" captures `shouldBe` Aeson.String "Ada"
it "looks up an interior capture" $
extractValue @"age" captures `shouldBe` Aeson.Number 37
it "looks up the last capture" $
extractValue @"active" captures `shouldBe` Aeson.Bool True
it "works through a polymorphic HasCapture constraint" $
extractValue @"name" captures `shouldBe` Aeson.String "Ada"
it "returns the first value for a repeated name" $ do
let repeated :: Captures '["name", "name"]
repeated = CCons Aeson.Null (CCons (Aeson.String "Ada") CEmpty)
extractValue @"name" repeated `shouldBe` Aeson.Null
it "supports names that are not Haskell identifiers" $ do
let captures' :: Captures '["a fancy-name"]
captures' = CCons Aeson.Null CEmpty
extractValue @"a fancy-name" captures' `shouldBe` Aeson.Null