forc_test/
setup.rs

1use fuel_tx as tx;
2use fuel_vm::{self as vm};
3
4/// Result of preparing a deployment transaction setup for a contract.
5pub type ContractDeploymentSetup = (tx::ContractId, vm::checked_transaction::Checked<tx::Create>);
6
7/// Required test setup for package types that requires a deployment.
8#[derive(Debug, Clone)]
9pub enum DeploymentSetup {
10    Script(ScriptTestSetup),
11    Contract(ContractTestSetup),
12}
13
14impl DeploymentSetup {
15    /// Returns the storage for this test setup
16    fn storage(&self) -> &vm::storage::MemoryStorage {
17        match self {
18            DeploymentSetup::Script(script_setup) => &script_setup.storage,
19            DeploymentSetup::Contract(contract_setup) => &contract_setup.storage,
20        }
21    }
22
23    /// Return the root contract id if this is a contract setup.
24    fn root_contract_id(&self) -> Option<tx::ContractId> {
25        match self {
26            DeploymentSetup::Script(_) => None,
27            DeploymentSetup::Contract(contract_setup) => Some(contract_setup.root_contract_id),
28        }
29    }
30}
31
32/// The storage and the contract id (if a contract is being tested) for a test.
33#[derive(Debug, Clone)]
34pub enum TestSetup {
35    WithDeployment(DeploymentSetup),
36    WithoutDeployment(vm::storage::MemoryStorage),
37}
38
39impl TestSetup {
40    /// Returns the storage for this test setup
41    pub fn storage(&self) -> &vm::storage::MemoryStorage {
42        match self {
43            TestSetup::WithDeployment(deployment_setup) => deployment_setup.storage(),
44            TestSetup::WithoutDeployment(storage) => storage,
45        }
46    }
47
48    /// Produces an iterator yielding contract ids of contract dependencies for this test setup.
49    pub fn contract_dependency_ids(&self) -> impl Iterator<Item = &tx::ContractId> + '_ {
50        match self {
51            TestSetup::WithDeployment(deployment_setup) => match deployment_setup {
52                DeploymentSetup::Script(script_setup) => {
53                    script_setup.contract_dependency_ids.iter()
54                }
55                DeploymentSetup::Contract(contract_setup) => {
56                    contract_setup.contract_dependency_ids.iter()
57                }
58            },
59            TestSetup::WithoutDeployment(_) => [].iter(),
60        }
61    }
62
63    /// Return the root contract id if this is a contract setup.
64    pub fn root_contract_id(&self) -> Option<tx::ContractId> {
65        match self {
66            TestSetup::WithDeployment(deployment_setup) => deployment_setup.root_contract_id(),
67            TestSetup::WithoutDeployment(_) => None,
68        }
69    }
70
71    /// Produces an iterator yielding all contract ids required to be included in the transaction
72    /// for this test setup.
73    pub fn contract_ids(&self) -> impl Iterator<Item = tx::ContractId> + '_ {
74        self.contract_dependency_ids()
75            .cloned()
76            .chain(self.root_contract_id())
77    }
78}
79
80/// The data collected to test a contract.
81#[derive(Debug, Clone)]
82pub struct ContractTestSetup {
83    pub storage: vm::storage::MemoryStorage,
84    pub contract_dependency_ids: Vec<tx::ContractId>,
85    pub root_contract_id: tx::ContractId,
86}
87
88/// The data collected to test a script.
89#[derive(Debug, Clone)]
90pub struct ScriptTestSetup {
91    pub storage: vm::storage::MemoryStorage,
92    pub contract_dependency_ids: Vec<tx::ContractId>,
93}