Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Enabled using addr_make inside AppBuilder::build #189

Merged
merged 16 commits into from
Jun 20, 2024
Merged
12 changes: 6 additions & 6 deletions src/app.rs
Original file line number Diff line number Diff line change
Expand Up @@ -67,9 +67,9 @@ pub struct App<
}

/// No-op application initialization function.
pub fn no_init<BankT, CustomT, WasmT, StakingT, DistrT, IbcT, GovT, StargateT>(
pub fn no_init<BankT, ApiT, CustomT, WasmT, StakingT, DistrT, IbcT, GovT, StargateT>(
DariuszDepta marked this conversation as resolved.
Show resolved Hide resolved
router: &mut Router<BankT, CustomT, WasmT, StakingT, DistrT, IbcT, GovT, StargateT>,
api: &dyn Api,
api: &ApiT,
storage: &mut dyn Storage,
) {
let _ = (router, api, storage);
Expand All @@ -96,7 +96,7 @@ impl BasicApp {
GovFailingModule,
StargateFailing,
>,
&dyn Api,
&MockApi,
&mut dyn Storage,
),
{
Expand All @@ -121,7 +121,7 @@ where
GovFailingModule,
StargateFailing,
>,
&dyn Api,
&MockApi,
&mut dyn Storage,
),
{
Expand Down Expand Up @@ -216,7 +216,7 @@ where
where
F: FnOnce(
&mut Router<BankT, CustomT, WasmT, StakingT, DistrT, IbcT, GovT, StargateT>,
&dyn Api,
&ApiT,
&mut dyn Storage,
) -> T,
{
Expand All @@ -228,7 +228,7 @@ where
where
F: FnOnce(
&Router<BankT, CustomT, WasmT, StakingT, DistrT, IbcT, GovT, StargateT>,
&dyn Api,
&ApiT,
&dyn Storage,
) -> T,
{
Expand Down
26 changes: 13 additions & 13 deletions src/app_builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ use std::fmt::Debug;
///
/// ```
/// # use cosmwasm_std::Empty;
/// # use cw_multi_test::{BasicAppBuilder, FailingModule, Module, no_init};
/// # use cw_multi_test::{no_init, BasicAppBuilder, FailingModule, Module};
/// # type MyHandler = FailingModule<Empty, Empty, Empty>;
/// # type MyExecC = Empty;
/// # type MyQueryC = Empty;
Expand Down Expand Up @@ -530,9 +530,9 @@ where
self
}

/// Builds final `App`. At this point all components type have to be properly related to each
/// other. If there are some generics related compilation errors, make sure that all components
/// are properly relating to each other.
/// Builds the final [App] with initialization.
///
/// At this point all component types have to be properly related to each other.
pub fn build<F>(
self,
init_fn: F,
Expand All @@ -550,11 +550,11 @@ where
StargateT: Stargate,
F: FnOnce(
&mut Router<BankT, CustomT, WasmT, StakingT, DistrT, IbcT, GovT, StargateT>,
&dyn Api,
&ApiT,
&mut dyn Storage,
),
{
let router = Router {
let mut router = Router {
wasm: self.wasm,
bank: self.bank,
custom: self.custom,
Expand All @@ -564,14 +564,14 @@ where
gov: self.gov,
stargate: self.stargate,
};

let mut app = App {
let api = self.api;
let mut storage = self.storage;
init_fn(&mut router, &api, &mut storage);
App {
router,
api: self.api,
api,
block: self.block,
storage: self.storage,
};
app.init_modules(init_fn);
app
storage,
}
DariuszDepta marked this conversation as resolved.
Show resolved Hide resolved
}
}
2 changes: 1 addition & 1 deletion src/tests/test_ibc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ fn default_ibc() {

#[test]
fn accepting_ibc() {
let mut app = AppBuilder::new()
let mut app = AppBuilder::default()
.with_ibc(IbcAcceptingModule::new())
.build(no_init);

Expand Down
6 changes: 3 additions & 3 deletions src/wasm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -433,7 +433,7 @@ where
/// # Example
///
/// ```
/// use cw_multi_test::{AppBuilder, no_init, WasmKeeper};
/// use cw_multi_test::{no_init, AppBuilder, WasmKeeper};
///
/// // create wasm keeper
/// let wasm_keeper = WasmKeeper::new();
Expand All @@ -451,7 +451,7 @@ where
///
/// ```
/// use cosmwasm_std::{Addr, Api, Storage};
/// use cw_multi_test::{AddressGenerator, AppBuilder, no_init, WasmKeeper};
/// use cw_multi_test::{no_init, AddressGenerator, AppBuilder, WasmKeeper};
/// use cw_multi_test::error::AnyResult;
/// # use cosmwasm_std::testing::MockApi;
///
Expand Down Expand Up @@ -490,7 +490,7 @@ where
///
/// ```
/// use cosmwasm_std::{Addr, Checksum};
/// use cw_multi_test::{AppBuilder, ChecksumGenerator, no_init, WasmKeeper};
/// use cw_multi_test::{no_init, AppBuilder, ChecksumGenerator, WasmKeeper};
///
/// struct MyChecksumGenerator;
///
Expand Down
1 change: 1 addition & 0 deletions tests/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ mod test_api;
mod test_app;
mod test_app_builder;
mod test_attributes;
mod test_bank;
mod test_contract_storage;
mod test_module;
mod test_prefixed_storage;
Expand Down
1 change: 1 addition & 0 deletions tests/test_app/mod.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
mod test_block_info;
mod test_initialize_app;
#[cfg(feature = "cosmwasm_1_2")]
mod test_instantiate2;
mod test_store_code;
Expand Down
21 changes: 21 additions & 0 deletions tests/test_app/test_initialize_app.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
use cw_multi_test::App;
use cw_storage_plus::Map;

const USER: &str = "user";
const USERS: Map<&str, u64> = Map::new("users");
const AMOUNT: u64 = 100;

#[test]
fn initializing_app_should_work() {
let mut app = App::default();
let mut amount = 0;
app.init_modules(|_router, api, storage| {
USERS
.save(storage, api.addr_make(USER).as_str(), &AMOUNT)
.unwrap();
});
app.read_module(|_router, api, storage| {
amount = USERS.load(storage, api.addr_make(USER).as_str()).unwrap()
});
assert_eq!(AMOUNT, amount);
}
3 changes: 1 addition & 2 deletions tests/test_app_builder/test_with_bank.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,7 @@ fn building_app_with_custom_bank_should_work() {
let bank_keeper = MyBankKeeper::new(EXECUTE_MSG, QUERY_MSG, SUDO_MSG);

// build the application with custom bank keeper
let app_builder = AppBuilder::default();
let mut app = app_builder.with_bank(bank_keeper).build(no_init);
let mut app = AppBuilder::default().with_bank(bank_keeper).build(no_init);

// prepare user addresses
let recipient_addr = app.api().addr_make("recipient");
Expand Down
3 changes: 1 addition & 2 deletions tests/test_app_builder/test_with_gov.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,7 @@ fn building_app_with_custom_gov_should_work() {
let gov_keeper = MyGovKeeper::new(EXECUTE_MSG, NO_MESSAGE, NO_MESSAGE);

// build the application with custom gov keeper
let app_builder = AppBuilder::default();
let mut app = app_builder.with_gov(gov_keeper).build(no_init);
let mut app = AppBuilder::default().with_gov(gov_keeper).build(no_init);

// prepare addresses
let sender_addr = app.api().addr_make("sender");
Expand Down
3 changes: 1 addition & 2 deletions tests/test_app_builder/test_with_ibc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,7 @@ fn building_app_with_custom_ibc_should_work() {
let ibc_keeper = MyIbcKeeper::new(EXECUTE_MSG, QUERY_MSG, NO_MESSAGE);

// build the application with custom ibc keeper
let app_builder = AppBuilder::default();
let mut app = app_builder.with_ibc(ibc_keeper).build(no_init);
let mut app = AppBuilder::default().with_ibc(ibc_keeper).build(no_init);

// prepare user addresses
let sender_addr = app.api().addr_make("sender");
Expand Down
5 changes: 3 additions & 2 deletions tests/test_app_builder/test_with_staking.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,9 @@ fn building_app_with_custom_staking_should_work() {
let stake_keeper = MyStakeKeeper::new(EXECUTE_MSG, QUERY_MSG, SUDO_MSG);

// build the application with custom stake keeper
let app_builder = AppBuilder::default();
let mut app = app_builder.with_staking(stake_keeper).build(no_init);
let mut app = AppBuilder::default()
.with_staking(stake_keeper)
.build(no_init);

// prepare addresses
let validator_addr = app.api().addr_make("validator");
Expand Down
15 changes: 9 additions & 6 deletions tests/test_app_builder/test_with_stargate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -113,8 +113,9 @@ fn building_app_with_custom_stargate_should_work() {
#[cfg(feature = "cosmwasm_2_0")]
fn building_app_with_custom_any_grpc_should_work() {
// build the application with custom stargate keeper
let app_builder = AppBuilder::default();
let mut app = app_builder.with_stargate(StargateKeeper).build(no_init);
let mut app = AppBuilder::default()
.with_stargate(StargateKeeper)
.build(no_init);

// prepare user addresses
let sender_addr = app.api().addr_make("sender");
Expand Down Expand Up @@ -144,8 +145,9 @@ fn building_app_with_custom_any_grpc_should_work() {

#[test]
fn building_app_with_accepting_stargate_should_work() {
let app_builder = AppBuilder::default();
let mut app = app_builder.with_stargate(StargateAccepting).build(no_init);
let mut app = AppBuilder::default()
.with_stargate(StargateAccepting)
.build(no_init);

// prepare user addresses
let sender_addr = app.api().addr_make("sender");
Expand Down Expand Up @@ -238,8 +240,9 @@ fn default_failing_stargate_should_work() {
#[test]
#[cfg(feature = "cosmwasm_2_0")]
fn default_failing_any_grpc_should_work() {
let app_builder = AppBuilder::default();
let mut app = app_builder.with_stargate(StargateFailing).build(no_init);
let mut app = AppBuilder::default()
.with_stargate(StargateFailing)
.build(no_init);

// prepare user addresses
let sender_addr = app.api().addr_make("sender");
Expand Down
3 changes: 1 addition & 2 deletions tests/test_app_builder/test_with_storage.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,7 @@ fn building_app_with_custom_storage_should_work() {
let label = "my-counter";

// build the application with custom storage
let app_builder = AppBuilder::default();
let mut app = app_builder
let mut app = AppBuilder::default()
.with_storage(MyStorage::default())
.build(no_init);

Expand Down
8 changes: 4 additions & 4 deletions tests/test_app_builder/test_with_wasm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -91,8 +91,7 @@ fn building_app_with_custom_wasm_should_work() {
let wasm_keeper = MyWasmKeeper::new(EXECUTE_MSG, QUERY_MSG, SUDO_MSG);

// build the application with custom wasm keeper
let app_builder = AppBuilder::default();
let mut app = app_builder.with_wasm(wasm_keeper).build(no_init);
let mut app = AppBuilder::default().with_wasm(wasm_keeper).build(no_init);

// prepare addresses
let contract_addr = app.api().addr_make("contract");
Expand Down Expand Up @@ -162,6 +161,7 @@ fn building_app_with_custom_wasm_should_work() {
fn compiling_with_wasm_keeper_should_work() {
// this verifies only compilation errors
// while our WasmKeeper does not implement Module
let app_builder = AppBuilder::default();
let _ = app_builder.with_wasm(WasmKeeper::default()).build(no_init);
let _ = AppBuilder::default()
.with_wasm(WasmKeeper::default())
.build(no_init);
}
1 change: 1 addition & 0 deletions tests/test_bank/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
mod test_init_balance;
96 changes: 96 additions & 0 deletions tests/test_bank/test_init_balance.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
use cosmwasm_std::{Coin, CustomMsg, CustomQuery, Uint128};
use cw_multi_test::{custom_app, App, AppBuilder, BasicApp};
use schemars::JsonSchema;
use serde::{Deserialize, Serialize};

const USER: &str = "user";
const DENOM: &str = "denom";
const AMOUNT: u128 = 100;

fn assert_balance(coins: Vec<Coin>) {
assert_eq!(1, coins.len());
assert_eq!(AMOUNT, coins[0].amount.u128());
assert_eq!(DENOM, coins[0].denom);
}

fn coins() -> Vec<Coin> {
vec![Coin {
denom: DENOM.to_string(),
amount: Uint128::new(AMOUNT),
}]
}

#[test]
fn initializing_balance_should_work() {
let app = AppBuilder::new().build(|router, api, storage| {
router
.bank
.init_balance(storage, &api.addr_make(USER), coins())
.unwrap();
});
assert_balance(
app.wrap()
.query_all_balances(app.api().addr_make(USER))
.unwrap(),
);
}

#[test]
fn initializing_balance_without_builder_should_work() {
let app = App::new(|router, api, storage| {
router
.bank
.init_balance(storage, &api.addr_make(USER), coins())
.unwrap();
});
assert_balance(
app.wrap()
.query_all_balances(app.api().addr_make(USER))
.unwrap(),
);
}

#[test]
fn initializing_balance_custom_app_should_work() {
#[derive(Debug, Clone, Serialize, Deserialize, JsonSchema, PartialEq, Eq)]
#[serde(rename = "snake_case")]
pub enum CustomHelperMsg {
HelperMsg,
}
impl CustomMsg for CustomHelperMsg {}

#[derive(Debug, Clone, Serialize, Deserialize, JsonSchema, PartialEq, Eq)]
#[serde(rename = "snake_case")]
pub enum CustomHelperQuery {
HelperQuery,
}
impl CustomQuery for CustomHelperQuery {}

let app: BasicApp<CustomHelperMsg, CustomHelperQuery> = custom_app(|router, api, storage| {
router
.bank
.init_balance(storage, &api.addr_make(USER), coins())
.unwrap();
});
assert_balance(
app.wrap()
.query_all_balances(app.api().addr_make(USER))
.unwrap(),
);
}

#[test]
fn initializing_balance_later_should_work() {
let mut app = App::default();
app.init_modules(|router, api, storage| {
router
.bank
.init_balance(storage, &api.addr_make(USER), coins())
.unwrap();
});
assert_balance(
app.wrap()
.query_all_balances(app.api().addr_make(USER))
.unwrap(),
);
}