aeson-match-qq-2.0.0: test/Aeson/Match/QQSpec.hs
{-# LANGUAGE DataKinds #-}
{-# LANGUAGE DuplicateRecordFields #-}
{-# LANGUAGE GeneralisedNewtypeDeriving #-}
{-# LANGUAGE OverloadedLists #-}
{-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE QuasiQuotes #-}
{-# LANGUAGE TemplateHaskell #-}
{-# LANGUAGE TypeApplications #-}
module Aeson.Match.QQSpec (spec) where
import Data.Aeson qualified as Aeson
import Aeson.Value.QQ qualified as Value (qq)
import Data.Either (isLeft)
import Data.List.NonEmpty (NonEmpty(..))
import Data.HashMap.Strict qualified as HashMap
import Data.String (fromString)
import Data.Text (Text)
import Test.Hspec
import Aeson.Match.QQ
import SpecHelper.NoCaptureQQ qualified as AST (qq)
spec :: Spec
spec = do
describe "match" $ do
let
text :: Int -> Text
text =
fromString . show
it "specs" $ do
[qq| _ |] `shouldMatch` [Value.qq| {foo: 4, bar: 7} |]
[qq| _ : any |] `shouldMatch` [Value.qq| {foo: 4, bar: 7} |]
[qq| null |] `shouldMatch` [Value.qq| null |]
[qq| true |] `shouldMatch` [Value.qq| true |]
[qq| false |] `shouldMatch` [Value.qq| false |]
[qq| 4 |] `shouldMatch` [Value.qq| 4 |]
[qq| "foo" |] `shouldMatch` [Value.qq| "foo" |]
[qq| (ci) "foo" |] `shouldMatch` [Value.qq| "Foo" |]
[qq| "" |] `shouldMatch` [Value.qq| "" |]
[qq| "foo#{text 4}" |] `shouldMatch` [Value.qq| "foo4" |]
[qq| "#{text 7}bar" |] `shouldMatch` [Value.qq| "7bar" |]
[qq| "#{text 4}#{text 7}" |] `shouldMatch` [Value.qq| "47" |]
[qq| (ci) "foo#{text 4}" |] `shouldMatch` [Value.qq| "FOO4" |]
[qq| (ci) "foo#{\"bar\"}" |] `shouldMatch` [Value.qq| "FoObAr" |]
[qq| [1, 2, 3] |] `shouldMatch` [Value.qq| [1, 2, 3] |]
[qq| [1, _ : number, 3, ...] |] `shouldMatch` [Value.qq| [1, 2, 3, 4] |]
[qq| [1, _ : string] |] `shouldMatch` [Value.qq| [1, "foo"] |]
[qq| [1, _ : ci-string] |] `shouldMatch` [Value.qq| [1, "foo"] |]
[qq| [1, _ : unordered-array] |] `shouldMatch` [Value.qq| [1, ["foo"]] |]
[qq| (unordered) [] |] `shouldMatch` [Value.qq| [] |]
[qq| (unordered) [1, 2, 3] |] `shouldMatch` [Value.qq| [1, 2, 3] |]
[qq| (unordered) [1, 2, 3] |] `shouldMatch` [Value.qq| [2, 3, 1] |]
[qq| (unordered) [1, 2, 2] |] `shouldMatch` [Value.qq| [2, 2, 1] |]
[qq| (unordered) [1, _, 2] |] `shouldMatch` [Value.qq| [2, 2, 1] |]
[qq| (unordered) [1, 2, ...] |] `shouldMatch` [Value.qq| [2, 3, 1] |]
[qq| (unordered) [1, 2, ...] |] `shouldMatch` [Value.qq| [2, 2, 1] |]
[qq| {foo: 4, bar: 7} |] `shouldMatch` [Value.qq| {foo: 4, bar: 7} |]
[qq| {foo: 4, bar: 7, ...} |] `shouldMatch` [Value.qq| {foo: 4, bar: 7, baz: 11} |]
[qq| #{1 + 2 :: Int} |] `shouldMatch` [Value.qq| 3 |]
[qq| {foo: _ : number, bar: 7} |] `shouldMatch` [Value.qq| {foo: 4, bar: 7} |]
[qq| {foo: _ : number?, bar: 7} |] `shouldMatch` [Value.qq| {foo: null, bar: 7} |]
[qq| null |] `shouldNotMatch` [Value.qq| 4 |]
[qq| true |] `shouldNotMatch` [Value.qq| false |]
[qq| false |] `shouldNotMatch` [Value.qq| true |]
[qq| 4 |] `shouldNotMatch` [Value.qq| 7 |]
[qq| "foo" |] `shouldNotMatch` [Value.qq| "bar" |]
[qq| (ci) "foo" |] `shouldNotMatch` [Value.qq| "Bar" |]
[qq| [1, 2, 3] |] `shouldNotMatch` [Value.qq| [1, 2, 3, 4] |]
[qq| [1, 2, 3, ...] |] `shouldNotMatch` [Value.qq| [1, 2] |]
[qq| [1, _ : string] |] `shouldNotMatch` [Value.qq| [1, 2] |]
[qq| [1, 2, 3, ...] |] `shouldNotMatch` [Value.qq| [1, 2, 4] |]
[qq| (unordered) [1, 2, 3] |] `shouldNotMatch` [Value.qq| [1, 2, 4] |]
[qq| (unordered) [1, 2, 3, 4] |] `shouldNotMatch` [Value.qq| [1, 2, 3] |]
[qq| (unordered) [1, 2] |] `shouldNotMatch` [Value.qq| [1, 2, 2] |]
[qq| {foo: 4, bar: 7} |] `shouldNotMatch` [Value.qq| {foo: 7, bar: 4} |]
[qq| {foo: 4, bar: 7} |] `shouldNotMatch` [Value.qq| {foo: 4, baz: 7} |]
[qq| {foo: 4, bar: 7, ...} |] `shouldNotMatch` [Value.qq| {foo: 4, baz: 11} |]
[qq| #{1 + 2 :: Int} |] `shouldNotMatch` [Value.qq| 4 |]
[qq| {foo: _ : number, bar: 7} |] `shouldNotMatch` [Value.qq| {foo: "foo", bar: 7} |]
[qq| {foo: _ : number, bar: 7} |] `shouldNotMatch` [Value.qq| {foo: null, bar: 7} |]
[qq|
{ foo: _ : string
, bar: 7
} |] `shouldNotMatch` [Value.qq| {foo: null, bar: 7} |]
[qq| {foo: 4, foo: 7} |] `shouldNotMatch` [Value.qq| {foo: 4} |]
[qq| {foo: 4, foo: 7} |] `shouldNotMatch` [Value.qq| {foo: 7} |]
it "paths" $ do
match [qq| {foo: {bar: {baz: [1, 4]}}} |] [Value.qq| {foo: {bar: {baz: [1, 7]}}} |] `shouldBe`
throwE (Mismatch MkMismatch
{ path = ["foo", "bar", "baz", Idx 1]
, matcher = [AST.qq| 4 |]
, given = [Value.qq| 7 |]
})
context "named holes" $ do
it "matches" $
match [qq| {foo: _hole} |] [Value.qq| {foo: {bar: {baz: [1, 4]}}} |] `shouldBe`
pure (CCons [Value.qq| {bar: {baz: [1, 4]}} |] CEmpty)
it "supports named lookup and JSON decoding through the public API" $ do
let userMatcher :: Matcher '["age", "name"]
userMatcher = [qq| {name: _name : string, age: _age : number} |]
result = match userMatcher [Value.qq| {name: "Ada", age: 37} |]
fmap (extractValue @"name") result `shouldBe` Right (Aeson.String "Ada")
fmap (extractDecoded @"age") result `shouldBe` Right (Right (37 :: Int))
context "unordered array" $
it "matches" $ do
match [qq| (unordered) [1, _hole] |] [Value.qq| [2, 1] |] `shouldBe`
pure (CCons [Value.qq| 2 |] CEmpty)
match [qq| (unordered) [{foo: _hole}, ...] |] [Value.qq| [{foo: 2}, 1] |] `shouldBe`
pure (CCons [Value.qq| 2 |] CEmpty)
context "duplicate keys" $ do
it "matches" $ do
match [qq| {foo: _name, foo: 4} |] [Value.qq| {foo: 4} |] `shouldBe`
pure (CCons [Value.qq| 4 |] CEmpty)
match [qq| {foo: [_a, 7], foo: [4, _b]} |] [Value.qq| {foo: [4, 7]} |] `shouldBe`
pure (CCons [Value.qq| 4 |] (CCons [Value.qq| 7 |] CEmpty))
it "doesn't match" $ do
match [qq| {foo: _name, foo: 7} |] [Value.qq| {foo: 4} |] `shouldBe`
throwE (Mismatch MkMismatch
{ path = [Key "foo"]
, matcher = [AST.qq| 7 |]
, given = Aeson.Number 4
})
describe "repro" $ do
-- https://github.com/supki/aeson-match-qq/issues/7
it "#7" $ do
match [qq| {foo: _} |] [Value.qq| {} |] `shouldBe`
throwE (MissingPathElem MkMissingPathElem
{ path = []
, missing = "foo"
})
match [qq| [_] |] [Value.qq| [] |] `shouldBe`
throwE (MissingPathElem MkMissingPathElem
{ path = []
, missing = Idx 0
})
-- https://github.com/supki/aeson-match-qq/issues/10
it "#10" $ do
[AST.qq| {foo: []} |] `shouldBe`
Object
(Box
{ values = [("foo", [Array (Box {values = [], extra = False})])]
, extra = False
})
[AST.qq| [{}] |] `shouldBe`
Array
(Box
{ values = [Object (Box {values = [], extra = False})]
, extra = False
})
-- https://github.com/supki/aeson-match-qq/issues/12
it "#12" $ do
[AST.qq| [ ... ] |] `shouldBe` [AST.qq| _ : array |]
[AST.qq| (unordered) [ ... ] |] `shouldBe` [AST.qq| _ : unordered-array |]
[AST.qq| { ... } |] `shouldBe` [AST.qq| _ : object |]
-- https://github.com/supki/aeson-match-qq/issues/13
it "#13" $
[qq| "Слава Україні" |] `shouldMatch` [Value.qq| "Слава Україні" |]
-- https://github.com/supki/aeson-match-qq/issues/18
it "#18" $ do
-- string ~ string
match [qq| "foo" |] [Value.qq| "bar" |] `shouldBe`
throwE (Mismatch MkMismatch
{ path = []
, matcher = String "foo"
, given = Aeson.String "bar"
})
-- string !~ number
match [qq| "foo" |] [Value.qq| 4 |] `shouldBe`
throwE (Mistype MkTypeMismatch
{ path = []
, expected = StringT
, matcher = String "foo"
, given = Aeson.Number 4
})
-- string !~ null
match [qq| "foo" |] [Value.qq| null |] `shouldBe`
throwE (Mistype MkTypeMismatch
{ path = []
, expected = StringT
, matcher = String "foo"
, given = Aeson.Null
})
-- null !~ number
match [qq| null |] [Value.qq| 4 |] `shouldBe`
throwE (Mismatch MkMismatch
{ path = []
, matcher = Null
, given = Aeson.Number 4
})
-- https://github.com/supki/aeson-match-qq/issues/26
it "#26" $
match [qq|
{ foo: _hole
}
|] [Value.qq| {foo: {bar: {baz: [1, 4]}}} |] `shouldBe`
pure (CCons [Value.qq| {bar: {baz: [1, 4]}} |] CEmpty)
-- https://github.com/supki/aeson-match-qq/issues/28
it "#28" $ do
let
Left (err :| _) =
match [qq| [{foo: 4, bar: 7}] |] [Value.qq| {foo: 4, bar: 7}|]
prettyError err `shouldBe`
" error: type of value does not match\n\
\expected: array\n\
\ actual: object\n\
\ path: .\n\
\ matcher: [qq|\n\
\ [ { bar: 7\n\
\ , foo: 4\n\
\ }\n\
\ ]\n\
\ |]\n\
\ given: { \"bar\": 7\n\
\ , \"foo\": 4\n\
\ }"
-- https://github.com/supki/aeson-match-qq/issues/29
it "#29" $ do
parse [] "_ : not-a-known-type" `shouldBe`
Left "unknown type in hole signature: Failed reading: empty"
-- https://github.com/supki/aeson-match-qq/issues/32
it "#32" $ do
parse [] "null some garbage" `shouldBe`
Left "trailing garbage after a Matcher value: endOfInput"
describe "pretty-printing" $
it "pretty" $ do
prettyError (Mismatch MkMismatch
{ path = [Key "foo", Idx 0, Key "bar"]
, matcher = String "foo"
, given = Aeson.String "bar"
}) `shouldBe`
" error: value does not match\n\
\ path: .foo[0].bar\n\
\matcher: [qq|\n\
\ \"foo\"\n\
\ |]\n\
\ given: \"bar\""
prettyError (Mistype MkTypeMismatch
{ path = [Key "foo", Idx 0, Key "bar"]
, expected = StringT
, matcher = String "foo"
, given = Aeson.Number 4
}) `shouldBe`
" error: type of value does not match\n\
\expected: string\n\
\ actual: number\n\
\ path: .foo[0].bar\n\
\ matcher: [qq|\n\
\ \"foo\"\n\
\ |]\n\
\ given: 4"
prettyError (MissingPathElem MkMissingPathElem
{ path = [Key "foo", Idx 0, Key "bar"]
, missing = Idx 1
}) `shouldBe`
" error: missing key or index\n\
\ path: .foo[0].bar\n\
\missing: [1]"
prettyError (ExtraArrayValues MkExtraArrayValues
{ path = [Key "foo", Idx 0, Key "bar"]
, values = [Aeson.String "foo", Aeson.Number 4]
}) `shouldBe`
" error: extra array values\n\
\ path: .foo[0].bar\n\
\ values: \"foo\"\n\
\ 4"
prettyError (ExtraObjectValues MkExtraObjectValues
{ path = [Key "foo", Idx 0, Key "bar"]
, values = HashMap.fromList [("k0", Aeson.String "foo"), ("k1", Aeson.Number 4)]
}) `shouldBe`
" error: extra object values\n\
\ path: .foo[0].bar\n\
\ values: key: .k0\n\
\ value: \"foo\"\n\
\ key: .k1\n\
\ value: 4"
newtype ToEncoding a = ToEncoding { unToEncoding :: a }
deriving (Show, Eq, Num)
instance Aeson.ToJSON a => Aeson.ToJSON (ToEncoding a) where
toJSON =
error "ToJSON is undefined"
toEncoding =
Aeson.toEncoding . unToEncoding
shouldMatch :: HasCallStack => Matcher names -> Aeson.Value -> Expectation
shouldMatch a b =
fmap (const ()) (match a b) `shouldBe` Right ()
shouldNotMatch :: HasCallStack => Matcher names -> Aeson.Value -> Expectation
shouldNotMatch a b =
match a b `shouldSatisfy` isLeft
throwE :: e -> Either (NonEmpty e) a
throwE =
Left . pure