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

module Test.Cardano.Data.Set.NonEmpty where

import Data.Aeson
import qualified Data.ByteString.Lazy as BSL
import qualified Data.Set.NonEmpty as NE
import Test.Cardano.Data.Arbitrary ()
import Test.Cardano.Data.JSON.Utils (roundTripJsonSpec)
import Test.Hspec
import Prelude hiding (elem, filter, lookup, null)

spec :: Spec
spec :: Spec
spec =
  [Char] -> Spec -> Spec
forall a. HasCallStack => [Char] -> SpecWith a -> SpecWith a
describe [Char]
"Set.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.NonEmptySet 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 array" (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] (NonEmptySet Int)
forall a. FromJSON a => ByteString -> Either [Char] a
eitherDecode ByteString
emptyJson :: Either String (NE.NonEmptySet Int) of
          Left [Char]
err -> [Char]
err [Char] -> [Char] -> Expectation
forall a. (HasCallStack, Show a, Eq a) => [a] -> [a] -> Expectation
`shouldContain` [Char]
"Empty set found, expected non-empty"
          Right NonEmptySet Int
_ -> HasCallStack => [Char] -> Expectation
[Char] -> Expectation
expectationFailure [Char]
"Expected parsing to fail for empty set, but it succeeded"