Skip to content

Commit

Permalink
Use ValidContract arbitrary instance when required
Browse files Browse the repository at this point in the history
  • Loading branch information
paluh authored and nhenin committed Apr 11, 2024
1 parent 093d307 commit e84bffa
Show file tree
Hide file tree
Showing 7 changed files with 22 additions and 10 deletions.
8 changes: 4 additions & 4 deletions marlowe-cli/tests/Spec/Format.hs
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,9 @@ module Spec.Format (

import Data.Text (pack)
import Language.Marlowe.CLI.Format
import Language.Marlowe.Core.V1.Semantics.Types
import Language.Marlowe.Core.V1.Semantics.Types ()
import Language.Marlowe.Pretty
import Spec.Marlowe.Semantics.Arbitrary ()
import Spec.Marlowe.Semantics.Arbitrary (ValidContractStructure (ValidContractStructure))
import Test.Tasty (TestTree, testGroup)
import Test.Tasty.QuickCheck (Property, testProperty, (===))
import Text.Megaparsec
Expand All @@ -38,6 +38,6 @@ testPrettyUnPretty :: TestTree
testPrettyUnPretty =
testProperty "Pretty print and parse contract" prop

prop :: Contract -> Property
prop c =
prop :: ValidContractStructure -> Property
prop (ValidContractStructure c) =
pure c === runParser contractParser "string" (pack . show $ pretty c)
5 changes: 3 additions & 2 deletions marlowe-object/gen/Language/Marlowe/Object/Gen.hs
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,11 @@ import Control.Monad (replicateM)
import qualified Data.ByteString as BS
import Data.Foldable (Foldable (fold))
import Data.Function ((&))
import Data.Functor ((<&>))
import Language.Marlowe.Object.Bundler (BundlerT (..))
import Language.Marlowe.Object.Link
import Language.Marlowe.Object.Types
import Spec.Marlowe.Semantics.Arbitrary ()
import Spec.Marlowe.Semantics.Arbitrary (unValidContractStructure)
import Test.Gen.Cardano.Api.Typed (genAddressShelley)
import Test.QuickCheck hiding (label)
import Test.QuickCheck.Hedgehog (hedgehog)
Expand All @@ -35,7 +36,7 @@ instance Arbitrary LinkedObject where
arbitrary =
oneof
[ LinkedAction <$> arbitrary
, LinkedContract <$> arbitrary
, LinkedContract <$> (arbitrary <&> unValidContractStructure)
, LinkedObservation <$> arbitrary
, LinkedParty <$> arbitrary
, LinkedToken <$> arbitrary
Expand Down
1 change: 1 addition & 0 deletions marlowe-object/marlowe-object.cabal
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,7 @@ test-suite marlowe-object-test
, binary ^>=0.8.8
, hspec
, marlowe-object:{marlowe-object, gen}
, marlowe-test
, QuickCheck >=2.14 && <3
, quickcheck-classes
, unordered-containers ^>=0.2.19
Expand Down
3 changes: 2 additions & 1 deletion marlowe-object/test/Language/Marlowe/Object/TypesSpec.hs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import Data.Foldable (traverse_)
import Data.Proxy (Proxy (..))
import Language.Marlowe.Object.Gen ()
import Language.Marlowe.Object.Types
import Spec.Marlowe.Semantics.Arbitrary (ValidContractStructure (..))
import Test.Hspec
import Test.Hspec.QuickCheck (prop)
import Test.QuickCheck hiding (Success, label)
Expand All @@ -28,7 +29,7 @@ spec = do
checkLaws $ showReadLaws $ Proxy @LabelledObject

describe "Contract" do
prop "Can decode JSON generated from core contracts" \contract ->
prop "Can decode JSON generated from core contracts" \(ValidContractStructure contract) ->
fromJSON (toJSON contract) === Success (fromCoreContract contract)

binaryLaws :: forall a. (Binary a, Show a, Arbitrary a, Eq a) => Proxy a -> Laws
Expand Down
11 changes: 10 additions & 1 deletion marlowe-runtime-web/test/Spec.hs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import Data.Aeson.Encode.Pretty (encodePrettyToTextBuilder)
import qualified Data.ByteString as BS
import Data.Coerce (coerce)
import Data.Data (Typeable)
import Data.Functor ((<&>))
import qualified Data.HashMap.Strict.InsOrd as IOHM
import Data.Kind (Type)
import Data.OpenApi (
Expand Down Expand Up @@ -83,6 +84,9 @@ import Servant.API (
)
import Servant.OpenApi (validateEveryToJSONWithPatternChecker)
import Spec.Marlowe.Semantics.Arbitrary ()
import Servant.API
import Servant.OpenApi
import Spec.Marlowe.Semantics.Arbitrary (ValidContractStructure (unValidContractStructure))
import Spec.Marlowe.Semantics.Next.Arbitrary ()
import Test.Hspec (Spec, describe, hspec, it, shouldBe)
import Test.Hspec.Golden (defaultGolden)
Expand Down Expand Up @@ -584,7 +588,12 @@ instance Arbitrary Web.Party where
shrink = genericShrink

instance Arbitrary Web.ContractOrSourceId where
arbitrary = ContractOrSourceId <$> oneof [Right <$> arbitrary, Left <$> resize 6 arbitrary]
arbitrary =
ContractOrSourceId
<$> oneof
[ Right <$> arbitrary
, Left <$> resize 6 (arbitrary <&> unValidContractStructure)
]
shrink = genericShrink

instance Arbitrary Web.PostTransactionsRequest where
Expand Down
2 changes: 1 addition & 1 deletion marlowe-test/marlowe-test.cabal
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,7 @@ library
if flag(trace-plutus)
cpp-options: -DTRACE_PLUTUS

test-suite marlowe-test
executable test-suite
import: lang
hs-source-dirs: test
type: exitcode-stdio-1.0
Expand Down
2 changes: 1 addition & 1 deletion marlowe-test/src/Spec/Marlowe/Semantics/Arbitrary.hs
Original file line number Diff line number Diff line change
Expand Up @@ -1341,7 +1341,7 @@ instance (Arbitrary a) => Arbitrary (Transaction a) where
<> [transaction{txInput = input} | input <- shrink txInput]
<> [transaction{txOutput = output} | output <- shrink txOutput]

newtype ValidContractStructure = ValidContractStructure Contract
newtype ValidContractStructure = ValidContractStructure {unValidContractStructure :: Contract}
deriving (Eq, Show)

instance Arbitrary ValidContractStructure where
Expand Down

0 comments on commit e84bffa

Please sign in to comment.