scalus.testing
Members list
Packages
Type members
Classlikes
Derives an Arbitrary instance for a given type T using Magnolia.
Derives an Arbitrary instance for a given type T using Magnolia.
Attributes
- Example
-
given Arbitrary[AddrKeyHash] = ArbitraryDerivation.autoDerived[AddrKeyHash] - Supertypes
-
trait AutoDerivation[Arbitrary]trait Derivation[Arbitrary]trait SealedTraitDerivationtrait CommonDerivation[Arbitrary]class Objecttrait Matchableclass AnyShow all
- Self type
-
ArbitraryDerivation.type
Error thrown when a Scenario.check fails.
Error thrown when a Scenario.check fails.
Attributes
- Supertypes
-
trait Producttrait Equalsclass Exceptionclass Throwabletrait Serializableclass Objecttrait Matchableclass AnyShow all
State for ScalaCheck Commands testing (abstract model).
State for ScalaCheck Commands testing (abstract model).
This state is used by ScalaCheck's Commands framework to track the abstract model state during property-based testing. It wraps an ImmutableEmulator (which provides the blockchain state), the contract-specific state of type S, and an RNG seed for deterministic command generation.
Type parameters
- S
-
the contract state type
Value parameters
- contractState
-
the contract-specific state extracted from UTxOs
- emulator
-
the immutable emulator representing blockchain state
- rng
-
ScalaCheck RNG seed for deterministic generation
Attributes
- Supertypes
-
trait Serializabletrait Producttrait Equalsclass Objecttrait Matchableclass AnyShow all
Bridges ContractStepVariations with ScalaCheck's stateful testing framework.
Bridges ContractStepVariations with ScalaCheck's stateful testing framework.
This adapter enables property-based testing of smart contract interactions by:
- Using ImmutableEmulator as the abstract model (State)
- Using mutable scalus.cardano.node.Emulator as the system under test (Sut)
- Generating commands from ContractStepVariations.allActions
- Checking invariants after successful transactions
==Two Emulators Pattern==
Per ScalaCheck Commands pattern, we maintain two separate emulators:
- '''State''' (CommandsState): The abstract model - wraps ImmutableEmulator + contract state
S+ RNG seed. Used forgenCommand,nextState, andpreCondition. - '''Sut''' (scalus.cardano.node.Emulator): The system under test - the existing mutable Emulator class. Used for
run(actual execution) andpostConditionverification.
This separation allows ScalaCheck to detect discrepancies between model and implementation.
==Slot Advancement==
Slot advancement is controlled by overriding ContractStepVariations.slotDelays in the step. This returns slot delays that are included as StepAction.Wait actions alongside transaction submissions.
==JVM-Only==
This class uses Await.result for synchronous execution and is JVM-only. For cross-platform testing, use ScenarioExplorer instead.
==Example Usage==
val auctionBidStep = new ContractStepVariations[AuctionState] {
def extractState(reader: BlockchainReader)(using EC) = ???
def makeBaseTx(reader: BlockchainReader, state: AuctionState)(using EC) = ???
def variations = TxVariations.standard.default(...)
override def slotDelays(state: AuctionState) = Seq(10L, 100L)
}
// Without invariant checking
val emulator = ImmutableEmulator.withAddresses(Seq(Alice.address, Bob.address))
val commands = ContractScalaCheckCommands(emulator, auctionBidStep)()
commands.property().check()
// With invariant checking
val commandsWithInvariants = ContractScalaCheckCommands(emulator, auctionBidStep) {
(reader, state) => Future {
Prop(state.balance >= Coin.zero) && Prop(state.highestBidder.isDefined)
}
}
Type parameters
- S
-
the contract state type
Value parameters
- checkInvariants
-
async function to check invariants after successful transactions
- ec
-
execution context for async operations
- initialEmulator
-
the starting emulator state with funded addresses
- step
-
the contract step variations to test
- timeout
-
timeout for async operations (default: 30 seconds)
Attributes
- Companion
- object
- Supertypes
-
trait Commandsclass Objecttrait Matchableclass Any
Attributes
- Companion
- class
- Supertypes
-
class Objecttrait Matchableclass Any
- Self type
Bundles contract step state extraction, base template building, and variations.
Bundles contract step state extraction, base template building, and variations.
ContractStepVariations represents a single step in a contract interaction (e.g., "bid on auction", "claim HTLC"). It combines:
- State extraction from blockchain UTxOs
- Base template construction (builder + sponsor + signer)
- Transaction variations for testing
Use with ScenarioExplorer for exhaustive testing.
Type parameters
- S
-
the contract state type
Attributes
- Supertypes
-
class Objecttrait Matchableclass Any
An immutable emulator for Cardano transactions.
An immutable emulator for Cardano transactions.
Unlike Emulator, this emulator is fully immutable: every state-changing operation (submit, advanceSlot) returns a new ImmutableEmulator instance, leaving the original unchanged.
This makes it suitable for use in functional state-threading patterns such as the Scenario monad, where branching and backtracking require independent state copies.
Attributes
- Companion
- object
- Supertypes
-
trait Serializabletrait Producttrait Equalsclass Objecttrait Matchableclass AnyShow all
Attributes
- Companion
- class
- Supertypes
-
trait Producttrait Mirrorclass Objecttrait Matchableclass Any
- Self type
-
ImmutableEmulator.type
Tag for integration tests that require external services (blockchain nodes, APIs).
Tag for integration tests that require external services (blockchain nodes, APIs).
Tests tagged with this tag are excluded from default test runs. To run integration tests, use:
sbt "project/testOnly -- -n scalus.testing.IntegrationTest"
Attributes
- Supertypes
-
class Tagclass Objecttrait Matchableclass Any
- Self type
-
IntegrationTest.type
A non-deterministic, state-threading monad for testing Cardano transaction scenarios.
A non-deterministic, state-threading monad for testing Cardano transaction scenarios.
Scenario is a sealed ADT:
- Scenario.Done — terminal value with state
- Scenario.Leaf — state-dependent continuation
- Scenario.Branches — non-deterministic branching (independent states per branch)
- Scenario.WaitFuture — async suspension
- Scenario.ScenarioError — error
- Scenario.FromStream — already-evaluated stream (from fsplit/msplit)
Use async[Scenario] (from dotty-cps-async) for imperative-looking syntax.
Type parameters
- A
-
the result type
Attributes
- Companion
- object
- Supertypes
-
class Objecttrait Matchableclass Any
- Known subtypes
-
class Branches[A]class Done[A]class FromStream[A]class Leaf[A]class ScenarioError[A]class WaitFuture[A]Show all
Attributes
- Supertypes
-
class Objecttrait Matchableclass Any
- Self type
-
ScenarioCheck.type
Explores contract state space by applying actions at each step.
Explores contract state space by applying actions at each step.
ScenarioExplorer performs bounded exploration of contract interactions. At each depth it:
- Gets a snapshot reader
- Calls the step function which performs one interaction using normal Scenario operations
- Actions (Scenario.submit, Scenario.sleep) are automatically logged
- On successful step, recurses to next depth
- On check failure, captures violation with the full action path
- On other errors, propagates
The step function can use Scenario.choices/Scenario.fromCollection for branching, Scenario.submit for transactions, Scenario.sleep for time advancement, and Scenario.check for invariant verification.
Attributes
- Supertypes
-
class Objecttrait Matchableclass Any
- Self type
-
ScenarioExplorer.type
State threaded through Scenario computations.
State threaded through Scenario computations.
Value parameters
- actionLog
-
log of actions taken during exploration (newest first for O(1) append)
- emulator
-
the immutable emulator state
- rng
-
ScalaCheck RNG seed for deterministic generation
Attributes
- Companion
- object
- Supertypes
-
trait Serializabletrait Producttrait Equalsclass Objecttrait Matchableclass AnyShow all
Attributes
- Companion
- class
- Supertypes
-
trait Producttrait Mirrorclass Objecttrait Matchableclass Any
- Self type
-
ScenarioState.type
Source location captured at compile time.
Source location captured at compile time.
Attributes
- Supertypes
-
trait Serializabletrait Producttrait Equalsclass Objecttrait Matchableclass AnyShow all
Standard transaction variation patterns for property-based and exhaustive testing.
Standard transaction variation patterns for property-based and exhaustive testing.
This object provides factory methods for creating common transaction variation patterns. Each method returns a TxVariations[S] or TxSamplingVariations[S] that can be combined with other variations using the ++ operator.
Variations receive a TxTemplate containing the base transaction builder, sponsor, and signer. They modify the builder or create new transactions using the template's sponsor/signer.
==Example Usage==
val stealVariation = StandardTxVariations.removeContractOutput[AuctionState](
extractUtxo = _.utxo,
redeemer = _ => BidRedeemer.toData,
script = auctionScript
)
// Combine with other variations
val allVariations = stealVariation ++ corruptDatumVariation
// Use in testing - txTemplate provides base tx, sponsor, signer
stealVariation.enumerate(provider, state, txTemplate)
Attributes
- Supertypes
-
class Objecttrait Matchableclass Any
- Self type
-
StandardTxVariations.type
Attributes
- Companion
- trait
- Supertypes
-
trait Sumtrait Mirrorclass Objecttrait Matchableclass Any
- Self type
-
StepAction.type
Sampling-based variations for fuzz testing.
Sampling-based variations for fuzz testing.
Unlike TxVariations which enumerates all variations exhaustively, TxSamplingVariations uses a generator to sample variations. This is useful for large or continuous variation spaces.
The primary method is gen; enumerate samples from the generator.
Type parameters
- S
-
the contract state type
Attributes
- Supertypes
A transaction template bundling a builder with its sponsor and signer.
A transaction template bundling a builder with its sponsor and signer.
TxTemplate represents a transaction that is ready to be completed and signed. It bundles:
builder: the TxBuilder with inputs, outputs, etc.sponsor: the address that pays fees (provides UTxOs for fees/collateral)signer: signs the completed transaction
This allows variations to be self-contained - each variation knows who will sign it.
Attributes
- Supertypes
-
trait Serializabletrait Producttrait Equalsclass Objecttrait Matchableclass AnyShow all
Transaction variations generator for property-based and exhaustive testing.
Transaction variations generator for property-based and exhaustive testing.
TxVariations generates multiple transaction variations from a given contract state and base template. Each variation represents a different way to construct a transaction (e.g., different input selection, datum values, redeemer arguments).
The TxTemplate provides the sponsor (who pays fees) and signer, while variations build the transaction body.
Type parameters
- S
-
the contract state type, typically extracted from blockchain UTxOs
Attributes
- Companion
- object
- Supertypes
-
class Objecttrait Matchableclass Any
- Known subtypes
-
trait TxSamplingVariations[S]
Attributes
- Companion
- trait
- Supertypes
-
class Objecttrait Matchableclass Any
- Self type
-
TxVariations.type
A violation found during scenario exploration.
A violation found during scenario exploration.
Attributes
- Supertypes
-
trait Serializabletrait Producttrait Equalsclass Objecttrait Matchableclass AnyShow all