{-# LANGUAGE AllowAmbiguousTypes #-}
{-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE RankNTypes #-}
{-# LANGUAGE ScopedTypeVariables #-}
{-# LANGUAGE TypeApplications #-}

module Test.Cardano.Data.Map.NonEmpty where

import Data.Aeson (eitherDecode)
import qualified Data.ByteString.Lazy as BSL
import qualified Data.Map.NonEmpty as NE
import Test.Cardano.Data.Arbitrary ()
import Test.Cardano.Data.JSON.Utils (roundTripJsonSpec)
import Test.Hspec

spec :: Spec
spec :: Spec
spec =
  [Char] -> Spec -> Spec
forall a. HasCallStack => [Char] -> SpecWith a -> SpecWith a
describe [Char]
"Map.NonEmpty" (Spec -> Spec) -> Spec -> Spec
forall a b. (a -> b) -> a -> b
$ do
    [Char] -> Spec -> Spec
forall a. HasCallStack => [Char] -> SpecWith a -> SpecWith a
context [Char]
"JSON round-trip" (Spec -> Spec) -> Spec -> Spec
forall a b. (a -> b) -> a -> b
$ do
      forall t.
(HasCallStack, Typeable t, Show t, Eq t, ToJSON t, FromJSON t,
 Arbitrary t) =>
Spec
roundTripJsonSpec @(NE.NonEmptyMap Int Int)
    [Char] -> Spec -> Spec
forall a. HasCallStack => [Char] -> SpecWith a -> SpecWith a
context [Char]
"JSON parsing" (Spec -> Spec) -> Spec -> Spec
forall a b. (a -> b) -> a -> b
$ do
      [Char] -> Expectation -> SpecWith (Arg Expectation)
forall a.
(HasCallStack, Example a) =>
[Char] -> a -> SpecWith (Arg a)
it [Char]
"should reject empty JSON object" (Expectation -> SpecWith (Arg Expectation))
-> Expectation -> SpecWith (Arg Expectation)
forall a b. (a -> b) -> a -> b
$ do
        let emptyJson :: ByteString
emptyJson = ByteString
"{}" :: BSL.ByteString
        case ByteString -> Either [Char] (NonEmptyMap Int Int)
forall a. FromJSON a => ByteString -> Either [Char] a
eitherDecode ByteString
emptyJson :: Either String (NE.NonEmptyMap Int Int) of
          Left [Char]
err -> [Char]
err [Char] -> [Char] -> Expectation
forall a. (HasCallStack, Show a, Eq a) => [a] -> [a] -> Expectation
`shouldContain` [Char]
"Empty map found, expected non-empty"
          Right NonEmptyMap Int Int
_ -> HasCallStack => [Char] -> Expectation
[Char] -> Expectation
expectationFailure [Char]
"Expected parsing to fail for empty map, but it succeeded"