diff --git a/plutus-ledger-api/plutus-ledger-api.cabal b/plutus-ledger-api/plutus-ledger-api.cabal index e5cf081b9a8..a7a877fe18f 100644 --- a/plutus-ledger-api/plutus-ledger-api.cabal +++ b/plutus-ledger-api/plutus-ledger-api.cabal @@ -131,6 +131,7 @@ library plutus-ledger-api-testlib PlutusLedgerApi.Test.V1.Value PlutusLedgerApi.Test.V2.Data.EvaluationContext PlutusLedgerApi.Test.V2.EvaluationContext + PlutusLedgerApi.Test.V2.OutputDatum PlutusLedgerApi.Test.V3.Data.EvaluationContext PlutusLedgerApi.Test.V3.EvaluationContext diff --git a/plutus-ledger-api/test/Spec.hs b/plutus-ledger-api/test/Spec.hs index 772faf953f5..1ec71bd829b 100644 --- a/plutus-ledger-api/test/Spec.hs +++ b/plutus-ledger-api/test/Spec.hs @@ -20,6 +20,7 @@ import Spec.Interval qualified import Spec.ScriptDecodeError qualified import Spec.V1.Data.Value qualified as Data.Value import Spec.V1.Value qualified as Value +import Spec.V2.OutputDatum qualified as OutputDatum import Spec.Versions qualified import Test.Tasty @@ -136,6 +137,8 @@ tests = testGroup "plutus-ledger-api" , Spec.CostModelParams.tests , Spec.ContextDecoding.tests , Value.test_Value + , Value.test_FaceValue + , OutputDatum.testOutputDatum ] , testGroup "Data" [ Spec.Data.Eval.tests diff --git a/plutus-ledger-api/test/Spec/V1/Value.hs b/plutus-ledger-api/test/Spec/V1/Value.hs index 422bc740752..cad7c8230cc 100644 --- a/plutus-ledger-api/test/Spec/V1/Value.hs +++ b/plutus-ledger-api/test/Spec/V1/Value.hs @@ -122,6 +122,11 @@ test_split = testProperty "split" . scaleTestsBy 7 $ \value -> let (valueL, valueR) = split value in Numeric.negate valueL <> valueR <=> value +test_roundTripFaceValue :: TestTree +test_roundTripFaceValue = testProperty "unFaceValue followed by FaceValue returns the original" $ + \(faceValue :: FaceValue) -> + unFaceValue (FaceValue $ unFaceValue faceValue) === unFaceValue faceValue + test_Value :: TestTree test_Value = testGroup "Value" [ test_laws @@ -130,3 +135,8 @@ test_Value = testGroup "Value" , test_shuffle , test_split ] + +test_FaceValue :: TestTree +test_FaceValue = testGroup "FaceValue" + [ test_roundTripFaceValue + ] diff --git a/plutus-ledger-api/test/Spec/V2/OutputDatum.hs b/plutus-ledger-api/test/Spec/V2/OutputDatum.hs new file mode 100644 index 00000000000..ac6c9717af0 --- /dev/null +++ b/plutus-ledger-api/test/Spec/V2/OutputDatum.hs @@ -0,0 +1,17 @@ +module Spec.V2.OutputDatum where + +import PlutusLedgerApi.Test.V2.OutputDatum () +import Test.Tasty +import Test.Tasty.QuickCheck + +-- Define a test case for Arbitrary instance +arbitraryTest :: TestTree +arbitraryTest = testProperty "Arbitrary instance exists" $ \(_) -> + let _ = arbitrary :: Gen OutputDatum + in True + +-- Define the main test suite +testOutputDatum :: TestTree +testOutputDatum = testGroup "OutputDatum" + [arbitraryTest + ] diff --git a/plutus-ledger-api/testlib/PlutusLedgerApi/Test/V1/Value.hs b/plutus-ledger-api/testlib/PlutusLedgerApi/Test/V1/Value.hs index 90afa8a1ee4..c4e0688ffb1 100644 --- a/plutus-ledger-api/testlib/PlutusLedgerApi/Test/V1/Value.hs +++ b/plutus-ledger-api/testlib/PlutusLedgerApi/Test/V1/Value.hs @@ -69,6 +69,9 @@ instance Arbitrary FaceValue where , (1, FaceValue . fromIntegral <$> arbitrary @Int) ] +instance Show FaceValue where + show (FaceValue value) = show value + -- | A wrapper for satisfying an @Arbitrary a@ constraint without implementing an 'Arbitrary' -- instance for @a@. newtype NoArbitrary a = NoArbitrary diff --git a/plutus-ledger-api/testlib/PlutusLedgerApi/Test/V2/OutputDatum.hs b/plutus-ledger-api/testlib/PlutusLedgerApi/Test/V2/OutputDatum.hs new file mode 100644 index 00000000000..d0dc3ad1719 --- /dev/null +++ b/plutus-ledger-api/testlib/PlutusLedgerApi/Test/V2/OutputDatum.hs @@ -0,0 +1,28 @@ +{-# LANGUAGE LambdaCase #-} + +module PlutusLedgerApi.Test.V2.OutputDatum where + +import PlutusLedgerApi.V2 +import Test.QuickCheck + +instance Arbitrary OutputDatum where + {-# INLINEABLE arbitrary #-} + arbitrary = + oneof + [ pure NoOutputDatum + , OutputDatumHash <$> arbitrary + , OutputDatum <$> arbitrary + ] + {-# INLINEABLE shrink #-} + -- We only shrink the OutputDatum case, since the others wouldn't shrink + -- anyway. + shrink = \case + OutputDatum d -> OutputDatum <$> shrink d + _ -> [] + +instance CoArbitrary OutputDatum where + {-# INLINEABLE coarbitrary #-} + coarbitrary = \case + NoOutputDatum -> variant (0 :: Int) + OutputDatumHash dh -> variant (1 :: Int) . coarbitrary dh + OutputDatum d -> variant (2 :: Int) . coarbitrary d