Skip to content

Commit dd8aad1

Browse files
committed
test: wip: add foundation for property testing
1 parent d4da9df commit dd8aad1

File tree

7 files changed

+139
-1
lines changed

7 files changed

+139
-1
lines changed

Makefile

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,14 @@
1-
.PHONY: build, repl, format, hoogle, all_scripts, auction_metadata_validator, standing_bid_validator
1+
.PHONY: build test repl format hoogle all_scripts auction_metadata_validator standing_bid_validator
22

33
hs-sources := $(shell fd --no-ignore-parent -ehs)
44
cabal-sources := $(shell fd --no-ignore-parent -ecabal)
55

66
build:
77
cabal v2-build all
88

9+
test:
10+
cabal v2-test
11+
912
repl:
1013
cabal v2-repl hydra-auction-onchain --ghc-options '-Wno-missing-import-lists'
1114

hydra-auction-onchain.cabal

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -147,3 +147,24 @@ executable hydra-auction-onchain-exe
147147
other-modules:
148148
hs-source-dirs: app
149149
ghc-options: -threaded -rtsopts -with-rtsopts=-N
150+
151+
test-suite hydra-auction-onchain-test
152+
import: common-lang
153+
type: exitcode-stdio-1.0
154+
hs-source-dirs: test
155+
main-is: Spec.hs
156+
other-modules:
157+
Spec.HydraAuctionOnchain.Helpers
158+
Spec.HydraAuctionOnchain.Types.AuctionTerms
159+
Spec.HydraAuctionOnchain.Validators.StandingBid
160+
161+
build-depends:
162+
, hydra-auction-onchain
163+
, plutarch
164+
, plutus-ledger-api
165+
, plutus-tx
166+
, pretty-simple
167+
, QuickCheck
168+
, tasty
169+
, tasty-quickcheck
170+
, text

src/HydraAuctionOnchain/Scripts.hs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
module HydraAuctionOnchain.Scripts
22
( auctionMetadataValidatorScript
33
, auctionMetadataValidatorUntyped
4+
, compileScript
45
, standingBidValidatorScript
56
, standingBidValidatorUntyped
67
, writeScript

test/Spec.hs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
module Main (main) where
2+
3+
import Spec.HydraAuctionOnchain.Validators.StandingBid qualified as StandingBid (spec)
4+
import Test.Tasty (defaultMain, testGroup)
5+
6+
main :: IO ()
7+
main =
8+
defaultMain $
9+
testGroup
10+
"hydra-auction-onchain"
11+
[ StandingBid.spec
12+
]
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
module Spec.HydraAuctionOnchain.Helpers
2+
( shouldFail
3+
) where
4+
5+
import Data.Text.Lazy qualified as TL (unpack)
6+
import Plutarch (Script)
7+
import Plutarch.Evaluate (evalScript)
8+
import Test.Tasty.QuickCheck (Property, counterexample, property)
9+
import Text.Pretty.Simple (pShow)
10+
11+
shouldFail :: Script -> Property
12+
shouldFail script =
13+
case result of
14+
Left _ -> property True
15+
Right _ ->
16+
counterexample "Expected failure, but succeeded instead."
17+
. counterexample (showLogs logs)
18+
. property
19+
$ False
20+
where
21+
(result, _exUnits, logs) = evalScript script
22+
23+
showLogs :: Show a => [a] -> String
24+
showLogs = \case
25+
[] -> "No logs found. Did you forget to compile with tracing on?"
26+
logs -> TL.unpack $ pShow logs
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
{-# LANGUAGE TemplateHaskell #-}
2+
{-# LANGUAGE UndecidableInstances #-}
3+
{-# OPTIONS_GHC -Wno-orphans #-}
4+
5+
module Spec.HydraAuctionOnchain.Types.AuctionTerms
6+
( AuctionTerms (..)
7+
) where
8+
9+
import HydraAuctionOnchain.Types.AuctionTerms (PAuctionTerms)
10+
import Plutarch.DataRepr (DerivePConstantViaData (DerivePConstantViaData))
11+
import Plutarch.Lift (PConstantDecl, PUnsafeLiftDecl (PLifted))
12+
import PlutusLedgerApi.V2 (BuiltinByteString, POSIXTime, PubKeyHash, Value)
13+
import PlutusTx (makeIsDataIndexed)
14+
15+
data AuctionTerms = AuctionTerms
16+
{ at'AuctionLot :: Value
17+
, at'SellerPkh :: PubKeyHash
18+
, at'SellerVk :: BuiltinByteString
19+
, at'Delegates :: [PubKeyHash]
20+
, at'BiddingStart :: POSIXTime
21+
, at'BiddingEnd :: POSIXTime
22+
, at'PurchaseDeadline :: POSIXTime
23+
, at'Cleanup :: POSIXTime
24+
, at'AuctionFeePerDelegate :: Integer
25+
, at'StartingBid :: Integer
26+
, at'MinBidIncrement :: Integer
27+
, at'MinDepositAmount :: Integer
28+
}
29+
deriving stock (Show, Eq)
30+
31+
makeIsDataIndexed ''AuctionTerms [('AuctionTerms, 0)]
32+
33+
deriving via
34+
(DerivePConstantViaData AuctionTerms PAuctionTerms)
35+
instance
36+
(PConstantDecl AuctionTerms)
37+
38+
instance PUnsafeLiftDecl PAuctionTerms where
39+
type PLifted PAuctionTerms = AuctionTerms
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
module Spec.HydraAuctionOnchain.Validators.StandingBid (spec) where
2+
3+
import HydraAuctionOnchain.Scripts (compileScript)
4+
import HydraAuctionOnchain.Validators.StandingBid (standingBidValidator)
5+
import Plutarch (Script)
6+
import PlutusLedgerApi.V2 (CurrencySymbol, ScriptContext)
7+
import Spec.HydraAuctionOnchain.Helpers (shouldFail)
8+
import Spec.HydraAuctionOnchain.Types.AuctionTerms (AuctionTerms)
9+
import Test.Tasty (TestTree, testGroup)
10+
import Test.Tasty.QuickCheck (Property, testProperty)
11+
12+
spec :: TestTree
13+
spec =
14+
testGroup
15+
"StandingBid"
16+
[ testProperty "Fails when own input is missing" prop_ownInputMissing_fails
17+
]
18+
19+
prop_ownInputMissing_fails :: Property
20+
prop_ownInputMissing_fails = shouldFail undefined
21+
22+
compile
23+
:: CurrencySymbol
24+
-> AuctionTerms
25+
-> StandingBidState
26+
-> StandingBidRedeemer
27+
-> ScriptContext
28+
-> Script
29+
compile auctionCs auctionTerms datum redeemer =
30+
compileScript $
31+
popaque $
32+
standingBidValidator
33+
# pconstant auctionCs
34+
# pconstant auctionTerms
35+
# pconstant datum
36+
# pconstant redeemer

0 commit comments

Comments
 (0)