module Test.Hspec.Nix
  ( forceRight
  , roundtrips
  ) where

import Test.Hspec (Expectation, shouldBe)

roundtrips
  :: forall a b f
   . ( Applicative f
     , Eq (f a)
     , Show a
     , Show b
     , Show (f a)
     )
  => (a -> b)   -- ^ Encode
  -> (b -> f a) -- ^ Decode
  -> a
  -> Expectation
roundtrips :: forall a b (f :: * -> *).
(Applicative f, Eq (f a), Show a, Show b, Show (f a)) =>
(a -> b) -> (b -> f a) -> a -> Expectation
roundtrips a -> b
encode b -> f a
decode a
x =
  b -> f a
decode (a -> b
encode a
x) f a -> f a -> Expectation
forall a. (HasCallStack, Show a, Eq a) => a -> a -> Expectation
`shouldBe` a -> f a
forall a. a -> f a
forall (f :: * -> *) a. Applicative f => a -> f a
pure a
x

forceRight
  :: Show a
  => Either a b
  -> b
forceRight :: forall a b. Show a => Either a b -> b
forceRight = \case
  Right b
x -> b
x
  Left a
e -> [Char] -> b
forall a. HasCallStack => [Char] -> a
error ([Char] -> b) -> [Char] -> b
forall a b. (a -> b) -> a -> b
$ [Char]
"forceRight failed: " [Char] -> [Char] -> [Char]
forall a. [a] -> [a] -> [a]
++ a -> [Char]
forall a. Show a => a -> [Char]
show a
e