Skip to content

Commit 473a163

Browse files
committed
Add EnvsOptions to make block_time, initial_height, initial_time, chain_id
configurable
1 parent 4e78fa6 commit 473a163

File tree

2 files changed

+45
-10
lines changed

2 files changed

+45
-10
lines changed

packages/std/src/testing/mock.rs

Lines changed: 43 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -441,6 +441,7 @@ pub fn mock_env() -> Env {
441441
/// }
442442
/// ```
443443
pub struct Envs {
444+
chain_id: String,
444445
contract_address: Addr,
445446
/// The number of nanoseconds between two consecutive blocks
446447
block_time: u64,
@@ -449,17 +450,45 @@ pub struct Envs {
449450
envs_produced: u64,
450451
}
451452

453+
pub struct EnvsOptions {
454+
bech32_prefix: &'static str, /* static due to MockApi's Copy requirement. No better idea for now. */
455+
block_time: u64,
456+
// The height before the first `make` call
457+
initial_height: u64,
458+
// The block time before the first `make` call
459+
initial_time: Timestamp,
460+
chain_id: String,
461+
}
462+
463+
impl Default for EnvsOptions {
464+
fn default() -> Self {
465+
EnvsOptions {
466+
bech32_prefix: BECH32_PREFIX,
467+
block_time: 5_000_000_000, // 5s
468+
initial_height: 12_344,
469+
initial_time: Timestamp::from_nanos(1_571_797_419_879_305_533).minus_seconds(5),
470+
chain_id: "cosmos-testnet-14002".to_string(),
471+
}
472+
}
473+
}
474+
452475
impl Envs {
453-
pub fn new(
454-
bech32_prefix: &'static str, /* static due to MockApi's Copy requirement. No better idea for now. */
455-
) -> Self {
456-
let api = MockApi::default().with_prefix(bech32_prefix);
476+
pub fn new(bech32_prefix: &'static str) -> Self {
477+
Self::with_options(EnvsOptions {
478+
bech32_prefix,
479+
..Default::default()
480+
})
481+
}
482+
483+
pub fn with_options(options: EnvsOptions) -> Self {
484+
let api = MockApi::default().with_prefix(options.bech32_prefix);
457485
Envs {
486+
chain_id: options.chain_id,
458487
// Default values here for compatibility with old `mock_env` function. They could be changed to anything else if there is a good reason.
459488
contract_address: api.addr_make("cosmos2contract"),
460-
block_time: 5_000_000_000, // 5s
461-
last_height: 12_344,
462-
last_time: Timestamp::from_nanos(1_571_797_419_879_305_533).minus_seconds(5),
489+
block_time: options.block_time,
490+
last_height: options.initial_height,
491+
last_time: options.initial_time,
463492
envs_produced: 0,
464493
}
465494
}
@@ -480,7 +509,7 @@ impl Envs {
480509
block: BlockInfo {
481510
height,
482511
time,
483-
chain_id: "cosmos-testnet-14002".to_string(),
512+
chain_id: self.chain_id.clone(),
484513
},
485514
transaction: Some(TransactionInfo { index: 3 }),
486515
contract: ContractInfo {
@@ -490,6 +519,12 @@ impl Envs {
490519
}
491520
}
492521

522+
impl Default for Envs {
523+
fn default() -> Self {
524+
Envs::with_options(EnvsOptions::default())
525+
}
526+
}
527+
493528
// The iterator implementation ends in case of overflows to avoid panics.
494529
// Using this is recommended for very long running test suites.
495530
impl Iterator for Envs {

packages/std/src/testing/mod.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,8 @@ pub use mock::DistributionQuerier;
1919
pub use mock::StakingQuerier;
2020
pub use mock::{
2121
mock_dependencies, mock_dependencies_with_balance, mock_dependencies_with_balances, mock_env,
22-
mock_wasmd_attr, BankQuerier, Envs, MockApi, MockQuerier, MockQuerierCustomHandlerResult,
23-
MOCK_CONTRACT_ADDR,
22+
mock_wasmd_attr, BankQuerier, Envs, EnvsOptions, MockApi, MockQuerier,
23+
MockQuerierCustomHandlerResult, MOCK_CONTRACT_ADDR,
2424
};
2525
#[cfg(feature = "ibc2")]
2626
pub use mock::{mock_ibc2_packet_recv, mock_ibc2_packet_timeout};

0 commit comments

Comments
 (0)