{-# LANGUAGE NamedFieldPuns #-}
{-# OPTIONS_GHC -Wno-orphans #-}

module Test.Ouroboros.Network.NodeToClient.Version (tests) where

import Ouroboros.Network.CodecCBORTerm
import Ouroboros.Network.Magic
import Ouroboros.Network.NodeToClient.Version

import Test.QuickCheck
import Test.Tasty (TestTree, testGroup)
import Test.Tasty.QuickCheck (testProperty)


tests :: TestTree
tests :: TestTree
tests = TestName -> [TestTree] -> TestTree
testGroup TestName
"Ouroboros.Network.NodeToClient.Version"
    [ TestName -> (VersionAndVersionData -> Bool) -> TestTree
forall a. Testable a => TestName -> a -> TestTree
testProperty TestName
"nodeToClientCodecCBORTerm" VersionAndVersionData -> Bool
prop_nodeToClientCodec
    ]

data VersionAndVersionData =
    VersionAndVersionData NodeToClientVersion NodeToClientVersionData
  deriving Int -> VersionAndVersionData -> ShowS
[VersionAndVersionData] -> ShowS
VersionAndVersionData -> TestName
(Int -> VersionAndVersionData -> ShowS)
-> (VersionAndVersionData -> TestName)
-> ([VersionAndVersionData] -> ShowS)
-> Show VersionAndVersionData
forall a.
(Int -> a -> ShowS) -> (a -> TestName) -> ([a] -> ShowS) -> Show a
$cshowsPrec :: Int -> VersionAndVersionData -> ShowS
showsPrec :: Int -> VersionAndVersionData -> ShowS
$cshow :: VersionAndVersionData -> TestName
show :: VersionAndVersionData -> TestName
$cshowList :: [VersionAndVersionData] -> ShowS
showList :: [VersionAndVersionData] -> ShowS
Show

instance Arbitrary VersionAndVersionData where
    arbitrary :: Gen VersionAndVersionData
arbitrary =
      NodeToClientVersion
-> NodeToClientVersionData -> VersionAndVersionData
VersionAndVersionData
        (NodeToClientVersion
 -> NodeToClientVersionData -> VersionAndVersionData)
-> Gen NodeToClientVersion
-> Gen (NodeToClientVersionData -> VersionAndVersionData)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> [NodeToClientVersion] -> Gen NodeToClientVersion
forall a. [a] -> Gen a
elements [ NodeToClientVersion
forall a. Bounded a => a
minBound .. NodeToClientVersion
forall a. Bounded a => a
maxBound]
        Gen (NodeToClientVersionData -> VersionAndVersionData)
-> Gen NodeToClientVersionData -> Gen VersionAndVersionData
forall a b. Gen (a -> b) -> Gen a -> Gen b
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> (NetworkMagic -> Bool -> NodeToClientVersionData
NodeToClientVersionData (NetworkMagic -> Bool -> NodeToClientVersionData)
-> (Word32 -> NetworkMagic)
-> Word32
-> Bool
-> NodeToClientVersionData
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Word32 -> NetworkMagic
NetworkMagic (Word32 -> Bool -> NodeToClientVersionData)
-> Gen Word32 -> Gen (Bool -> NodeToClientVersionData)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Gen Word32
forall a. Arbitrary a => Gen a
arbitrary Gen (Bool -> NodeToClientVersionData)
-> Gen Bool -> Gen NodeToClientVersionData
forall a b. Gen (a -> b) -> Gen a -> Gen b
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> Gen Bool
forall a. Arbitrary a => Gen a
arbitrary)

prop_nodeToClientCodec :: VersionAndVersionData -> Bool
prop_nodeToClientCodec :: VersionAndVersionData -> Bool
prop_nodeToClientCodec (VersionAndVersionData NodeToClientVersion
vNumber NodeToClientVersionData
vData) =
      case Term -> Either Text NodeToClientVersionData
decodeTerm (NodeToClientVersionData -> Term
encodeTerm NodeToClientVersionData
vData) of
        Right NodeToClientVersionData
vData' -> NodeToClientVersionData -> NetworkMagic
networkMagic NodeToClientVersionData
vData' NetworkMagic -> NetworkMagic -> Bool
forall a. Eq a => a -> a -> Bool
== NodeToClientVersionData -> NetworkMagic
networkMagic NodeToClientVersionData
vData
        Left {}      -> Bool
False
    where
      CodecCBORTerm { NodeToClientVersionData -> Term
encodeTerm :: NodeToClientVersionData -> Term
encodeTerm :: forall fail a. CodecCBORTerm fail a -> a -> Term
encodeTerm, Term -> Either Text NodeToClientVersionData
decodeTerm :: Term -> Either Text NodeToClientVersionData
decodeTerm :: forall fail a. CodecCBORTerm fail a -> Term -> Either fail a
decodeTerm } = NodeToClientVersion -> CodecCBORTerm Text NodeToClientVersionData
nodeToClientCodecCBORTerm NodeToClientVersion
vNumber