Skip to content

Commit

Permalink
Migrate tests to PureScript Spec to align with other projects
Browse files Browse the repository at this point in the history
  • Loading branch information
jhbertra committed Dec 2, 2021
1 parent 4f4eb56 commit 1115023
Show file tree
Hide file tree
Showing 12 changed files with 294 additions and 241 deletions.
2 changes: 1 addition & 1 deletion marlowe-playground-client/spago.dhall
Original file line number Diff line number Diff line change
Expand Up @@ -58,9 +58,9 @@ You can edit this file as you like.
, "routing-duplex"
, "servant-support"
, "simple-json"
, "spec"
, "strings"
, "tailrec"
, "test-unit"
, "transformers"
, "tuples"
, "type-equality"
Expand Down
36 changes: 20 additions & 16 deletions marlowe-playground-client/test/BridgeTests.purs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ module BridgeTests
) where

import Prologue

import Control.Monad.Error.Class (class MonadError)
import Data.Argonaut.Decode
( class DecodeJson
, JsonDecodeError
Expand All @@ -15,6 +17,7 @@ import Data.Map as Map
import Data.String.Regex (replace)
import Data.String.Regex.Flags (RegexFlags(..))
import Data.String.Regex.Unsafe (unsafeRegex)
import Effect.Aff (Error)
import Effect.Aff.Class (class MonadAff)
import Effect.Class (liftEffect)
import Language.Haskell.Interpreter (CompilationError)
Expand All @@ -35,28 +38,28 @@ import Marlowe.Semantics
)
import Node.Encoding (Encoding(UTF8))
import Node.FS.Sync as FS
import Test.Unit (TestSuite, Test, failure, success, suite, test)
import Test.Unit.Assert (equal)
import Test.Spec (Spec, describe, it)
import Test.Spec.Assertions (fail, shouldEqual)

all :: TestSuite
all :: Spec Unit
all =
suite "JSON Serialization" do
describe "JSON Serialization" do
jsonHandling
serializationTest

jsonHandling :: TestSuite
jsonHandling :: Spec Unit
jsonHandling = do
test "Json handling" do
it "Json handling" do
response1 :: Either JsonDecodeError String <- decodeFile
"test/evaluation_response1.json"
assertRight response1
error1 :: Either JsonDecodeError (Array CompilationError) <- decodeFile
"test/evaluation_error1.json"
assertRight error1

serializationTest :: TestSuite
serializationTest :: Spec Unit
serializationTest =
test "Contract Serialization" do
it "Contract Serialization" do
-- A simple test that runs the Escrow contract to completion
let
ada = Token "" ""
Expand Down Expand Up @@ -145,17 +148,18 @@ serializationTest =
)

expectedState = replace rx "" expectedStateJson
equal expectedState jsonState
equal (Right contract) (lmap printJsonDecodeError $ parseDecodeJson json)
equal (Right contract)
shouldEqual expectedState jsonState
shouldEqual (Right contract)
(lmap printJsonDecodeError $ parseDecodeJson json)
shouldEqual (Right contract)
(lmap printJsonDecodeError $ parseDecodeJson bridgedJson)
equal (Right state)
shouldEqual (Right state)
(lmap printJsonDecodeError $ parseDecodeJson bridgedStateJson)

assertRight :: forall a. Either JsonDecodeError a -> Test
assertRight (Left err) = failure (printJsonDecodeError err)

assertRight (Right _) = success
assertRight
:: forall m a. MonadError Error m => Either JsonDecodeError a -> m Unit
assertRight (Left err) = fail (printJsonDecodeError err)
assertRight (Right _) = pure unit

decodeFile
:: forall m a
Expand Down
30 changes: 15 additions & 15 deletions marlowe-playground-client/test/Main.purs
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
module Test.Main where

import Prologue

import BridgeTests as BridgeTests
import Data.BigInt.Argonaut (withJsonPatch)
import Effect (Effect)
import Effect.Aff (launchAff_)
import Effect.Class (liftEffect)
import Marlowe.BlocklyTests as BlocklyTests
import Marlowe.ContractTests as ContractTests
import Marlowe.DeinstantiatorTests as DeinstantiatorTests
Expand All @@ -14,22 +14,22 @@ import Marlowe.Holes.TemplateTest as HolesTemplateTest
import Marlowe.Holes.TimeoutTest as HolesTimeoutTest
import Marlowe.LintTests as LintTests
import Marlowe.ParserTests as ParserTests
import Test.Unit.Main (runTest)
import Test.Spec (around_)
import Test.Spec.Reporter (consoleReporter)
import Test.Spec.Runner (runSpec)

foreign import forDeps :: Effect Unit

main :: Effect Unit
main =
launchAff_
$ withJsonPatch do
liftEffect
$ runTest do
BridgeTests.all
ParserTests.all
ContractTests.all
BlocklyTests.all
LintTests.all
DeinstantiatorTests.all
HolesSemanticTest.all
HolesTemplateTest.all
HolesTimeoutTest.all
launchAff_ do
runSpec [ consoleReporter ] $ around_ withJsonPatch $ do
BridgeTests.all
ParserTests.all
ContractTests.all
BlocklyTests.all
LintTests.all
DeinstantiatorTests.all
HolesSemanticTest.all
HolesTemplateTest.all
HolesTimeoutTest.all
8 changes: 4 additions & 4 deletions marlowe-playground-client/test/Marlowe/BlocklyTests.purs
Original file line number Diff line number Diff line change
Expand Up @@ -23,13 +23,13 @@ import Marlowe.GenWithHoles (GenWithHoles, contractQuickCheck)
import Marlowe.Holes (Contract, Term)
import Marlowe.Parser as Parser
import Test.QuickCheck (Result, (===))
import Test.Unit (TestSuite, suite, test)
import Test.Spec (Spec, describe, it)
import Text.Extra (stripParens)

all :: TestSuite
all :: Spec Unit
all =
suite "Marlowe.Blockly" do
test "codeToBlocklyToCode" $ contractQuickCheck
describe "Marlowe.Blockly" do
it "codeToBlocklyToCode" $ contractQuickCheck
(GenerationOptions { withHoles: true, withExtendedConstructs: true })
codeToBlocklyToCode

Expand Down
59 changes: 30 additions & 29 deletions marlowe-playground-client/test/Marlowe/ContractTests.purs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
module Marlowe.ContractTests where

import Prologue

import Control.Bind (bindFlipped)
import Control.Monad.Gen (class MonadGen, chooseInt, elements, oneOf)
import Control.Monad.Rec.Class (class MonadRec)
Expand Down Expand Up @@ -57,14 +58,14 @@ import Simulator.State (applyInput, getAllActions, moveToSlot, startSimulation)
import Simulator.Types (ActionInput(..))
import Test.QuickCheck (Result(..))
import Test.QuickCheck.Gen (Gen)
import Test.Unit (TestSuite, suite, test)
import Test.Unit.Assert (equal)
import Test.Unit.QuickCheck (quickCheck)
import Test.Spec (Spec, describe, it)
import Test.Spec.Assertions (shouldEqual)
import Test.Spec.QuickCheck (quickCheck)
import Text.Pretty (pretty)

all :: TestSuite
all :: Spec Unit
all =
suite "Contract Tests" do
describe "Contract Tests" do
examplesMatch
escrowSimpleFlow
exampleContractsHaveNoErrors
Expand All @@ -78,29 +79,29 @@ toTerm contract = unsafePartial $ fromJust $ hush $ parseContract $ show $
contractToExtended :: String -> Maybe EM.Contract
contractToExtended = fromTerm <=< hush <<< parseContract

examplesMatch :: TestSuite
examplesMatch :: Spec Unit
examplesMatch =
suite "Purescript and Haskell examples match" do
test "Simple escrow"
$ equal (Just Escrow.fullExtendedContract)
describe "Purescript and Haskell examples match" do
it "Simple escrow"
$ shouldEqual (Just Escrow.fullExtendedContract)
(contractToExtended Contracts.escrow)
test "Escrow with collateral"
$ equal (Just EscrowWithCollateral.fullExtendedContract)
it "Escrow with collateral"
$ shouldEqual (Just EscrowWithCollateral.fullExtendedContract)
(contractToExtended Contracts.escrowWithCollateral)
test "Zero coupon bond"
$ equal (Just ZeroCouponBond.fullExtendedContract)
it "Zero coupon bond"
$ shouldEqual (Just ZeroCouponBond.fullExtendedContract)
(contractToExtended Contracts.zeroCouponBond)
test "Coupon bond guaranteed"
$ equal (Just CouponBondGuaranteed.extendedContract)
it "Coupon bond guaranteed"
$ shouldEqual (Just CouponBondGuaranteed.extendedContract)
(contractToExtended Contracts.couponBondGuaranteed)
test "Swap"
$ equal (Just Swap.fullExtendedContract)
it "Swap"
$ shouldEqual (Just Swap.fullExtendedContract)
(contractToExtended Contracts.swap)
test "Contract for differences"
$ equal (Just ContractForDifferences.extendedContract)
it "Contract for differences"
$ shouldEqual (Just ContractForDifferences.extendedContract)
(contractToExtended Contracts.contractForDifferences)
test "Contract for differences with oracle"
$ equal (Just ContractForDifferencesWithOracle.extendedContract)
it "Contract for differences with oracle"
$ shouldEqual (Just ContractForDifferencesWithOracle.extendedContract)
(contractToExtended Contracts.contractForDifferencesWithOracle)

seller :: Party
Expand Down Expand Up @@ -256,9 +257,9 @@ filledContractForDifferencesWithOracle =
-- TODO: We should combine this test with the ones defined in Marlowe.Holes.SemanticTest
-- so that we can have a single definition of contracts and flows, and then test what we care in each one. In semantic
-- test we care that the compute transaction of term and semantic are the same, in here we care about the output of the simulation.
escrowSimpleFlow :: TestSuite
escrowSimpleFlow :: Spec Unit
escrowSimpleFlow =
test "Escrow" do
it "Escrow" do
-- A simple test that runs the Escrow contract to completion
let
deposit = IDeposit seller buyer ada (BigInt.fromInt 450)
Expand All @@ -281,14 +282,14 @@ escrowSimpleFlow =
(_marloweState <<< _Head <<< _executionState <<< _SimulationRunning)
finalState
executionState ^. _transactionError
equal Nothing txError
equal (Just Close) (fromTerm =<< finalContract)
shouldEqual Nothing txError
shouldEqual (Just Close) (fromTerm =<< finalContract)
pure unit

--
exampleContractsHaveNoErrors :: TestSuite
exampleContractsHaveNoErrors :: Spec Unit
exampleContractsHaveNoErrors =
suite "Provided Examples don't throw errors nor have warnings" do
describe "Provided Examples don't throw errors nor have warnings" do
contractHasNoErrors "Simple Escrow" filledEscrow
contractHasNoErrors "Escrow with collateral" filledEscrowWithCollateral
contractHasNoErrors "Zero coupon bond" filledZeroCouponBond
Expand All @@ -300,9 +301,9 @@ exampleContractsHaveNoErrors =

-- This is a property based test that checks that for a given contract, the possible actions available
-- during the simulation don't throw errors nor warnings.
contractHasNoErrors :: String -> Term T.Contract -> TestSuite
contractHasNoErrors :: String -> Term T.Contract -> Spec Unit
contractHasNoErrors contractName contract =
test contractName
it contractName
$ quickCheck
$ evalStateT property mkState
where
Expand Down
27 changes: 14 additions & 13 deletions marlowe-playground-client/test/Marlowe/DeinstantiatorTests.purs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
module Marlowe.DeinstantiatorTests where

import Prologue

import Data.BigInt.Argonaut (fromInt)
import Data.Map as Map
import Data.Maybe (maybe)
Expand All @@ -9,15 +10,15 @@ import Examples.PureScript.Escrow as Escrow
import Examples.PureScript.ZeroCouponBond as ZeroCouponBond
import Marlowe.Deinstantiate (findTemplate)
import Marlowe.Extended (toCore)
import Marlowe.Template (TemplateContent(..), fillTemplate)
import Marlowe.Semantics (Contract)
import Test.Unit (TestSuite, suite, test)
import Test.Unit.Assert (assertFalse, equal)
import Marlowe.Template (TemplateContent(..), fillTemplate)
import Test.Spec (Spec, describe, it)
import Test.Spec.Assertions (shouldEqual, shouldSatisfy)

all :: TestSuite
all :: Spec Unit
all =
suite "Deinstantiator Tests" do
test "Escrow" do
describe "Deinstantiator Tests" do
it "Escrow" do
let
mFilledEscrow :: Maybe Contract
mFilledEscrow =
Expand All @@ -39,11 +40,11 @@ all =
)
Escrow.contractTemplate.extendedContract
)
assertFalse "Could not instantiate Escrow contract"
(mFilledEscrow == Nothing)
equal (Just Escrow.contractTemplate)
shouldSatisfy (mFilledEscrow == Nothing) not
shouldEqual
(Just Escrow.contractTemplate)
(maybe Nothing findTemplate mFilledEscrow)
test "Zero Coupon Bond" do
it "Zero Coupon Bond" do
let
mFilledZeroCouponBond :: Maybe Contract
mFilledZeroCouponBond =
Expand All @@ -64,7 +65,7 @@ all =
)
ZeroCouponBond.contractTemplate.extendedContract
)
assertFalse "Could not instantiate Zero Coupon Bond contract"
(mFilledZeroCouponBond == Nothing)
equal (Just ZeroCouponBond.contractTemplate)
shouldSatisfy (mFilledZeroCouponBond == Nothing) not
shouldEqual
(Just ZeroCouponBond.contractTemplate)
(maybe Nothing findTemplate mFilledZeroCouponBond)
9 changes: 5 additions & 4 deletions marlowe-playground-client/test/Marlowe/GenWithHoles.purs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
module Marlowe.GenWithHoles where

import Prologue

import Control.Lazy (class Lazy)
import Control.Monad.Gen
( class MonadGen
Expand All @@ -17,11 +18,11 @@ import Control.Monad.Reader
, runReaderT
)
import Control.Monad.Rec.Class (class MonadRec)
import Effect.Aff (Aff)
import Marlowe.Gen (GenerationOptions)
import Test.QuickCheck (class Testable)
import Test.QuickCheck.Gen (Gen)
import Test.Unit (Test)
import Test.Unit.QuickCheck (quickCheck)
import Marlowe.Gen (GenerationOptions)
import Test.Spec.QuickCheck (quickCheck)

-- TODO: rename to GenContract or similar
newtype GenWithHoles a
Expand Down Expand Up @@ -74,6 +75,6 @@ contractQuickCheck
. Testable prop
=> GenerationOptions
-> GenWithHoles prop
-> Test
-> Aff Unit
contractQuickCheck options g = quickCheck $ runReaderT (unGenWithHoles g)
options
Loading

0 comments on commit 1115023

Please sign in to comment.