From afd2a232b18f7bfa3983a4bb89a45d25e94f34d0 Mon Sep 17 00:00:00 2001 From: Dariusz Depta Date: Wed, 19 Jun 2024 11:42:30 +0200 Subject: [PATCH 01/15] Added test. --- tests/mod.rs | 1 + tests/test_bank/mod.rs | 1 + tests/test_bank/test_init_balance.rs | 33 ++++++++++++++++++++++++++++ 3 files changed, 35 insertions(+) create mode 100644 tests/test_bank/mod.rs create mode 100644 tests/test_bank/test_init_balance.rs diff --git a/tests/mod.rs b/tests/mod.rs index 9001d161..feb7b9ea 100644 --- a/tests/mod.rs +++ b/tests/mod.rs @@ -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; diff --git a/tests/test_bank/mod.rs b/tests/test_bank/mod.rs new file mode 100644 index 00000000..dfe6392e --- /dev/null +++ b/tests/test_bank/mod.rs @@ -0,0 +1 @@ +mod test_init_balance; diff --git a/tests/test_bank/test_init_balance.rs b/tests/test_bank/test_init_balance.rs new file mode 100644 index 00000000..261056e3 --- /dev/null +++ b/tests/test_bank/test_init_balance.rs @@ -0,0 +1,33 @@ +use cosmwasm_std::testing::MockApi; +use cosmwasm_std::{Coin, Uint128}; +use cw_multi_test::AppBuilder; + +const USER: &str = "USER"; +const NATIVE_DENOM: &str = "NativeDenom"; +const AMOUNT: u128 = 100; + +#[test] +fn initializing_balance_should_work() { + let app = AppBuilder::new().build(|router, api, storage| { + let _ = api; + router + .bank + .init_balance( + storage, + &MockApi::default().addr_make(USER), + vec![Coin { + denom: NATIVE_DENOM.to_string(), + amount: Uint128::new(100), + }], + ) + .unwrap(); + }); + let api = app.api(); + let user_addr = api.addr_make(USER); + let balances = app.wrap().query_all_balances(user_addr).unwrap(); + assert_eq!(1, balances.len()); + assert_eq!( + format!("{}{}", AMOUNT, NATIVE_DENOM), + balances[0].to_string() + ); +} From 3b146c4298d5f0f652253a4fe1d6e8307f3969dd Mon Sep 17 00:00:00 2001 From: Dariusz Depta Date: Wed, 19 Jun 2024 12:12:14 +0200 Subject: [PATCH 02/15] Added ApiT to builder. --- src/app_builder.rs | 49 ++++++++++++++++++++++++++++ tests/test_bank/test_init_balance.rs | 6 ++-- 2 files changed, 51 insertions(+), 4 deletions(-) diff --git a/src/app_builder.rs b/src/app_builder.rs index ad9b0dc5..0e8aea39 100644 --- a/src/app_builder.rs +++ b/src/app_builder.rs @@ -574,4 +574,53 @@ where app.init_modules(init_fn); app } + + /// 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. + pub fn build_a( + self, + init_fn: F, + ) -> App + where + BankT: Bank, + ApiT: Api, + StorageT: Storage, + CustomT: Module, + WasmT: Wasm, + StakingT: Staking, + DistrT: Distribution, + IbcT: Ibc, + GovT: Gov, + StargateT: Stargate, + F: FnOnce( + &mut Router, + &ApiT, + &mut dyn Storage, + ), + { + let mut router = Router { + wasm: self.wasm, + bank: self.bank, + custom: self.custom, + staking: self.staking, + distribution: self.distribution, + ibc: self.ibc, + gov: self.gov, + stargate: self.stargate, + }; + + let api = self.api; + + let mut storage = self.storage; + + init_fn(&mut router, &api, &mut storage); + + App { + router, + api, + block: self.block, + storage, + } + } } diff --git a/tests/test_bank/test_init_balance.rs b/tests/test_bank/test_init_balance.rs index 261056e3..21dba54c 100644 --- a/tests/test_bank/test_init_balance.rs +++ b/tests/test_bank/test_init_balance.rs @@ -1,4 +1,3 @@ -use cosmwasm_std::testing::MockApi; use cosmwasm_std::{Coin, Uint128}; use cw_multi_test::AppBuilder; @@ -8,13 +7,12 @@ const AMOUNT: u128 = 100; #[test] fn initializing_balance_should_work() { - let app = AppBuilder::new().build(|router, api, storage| { - let _ = api; + let app = AppBuilder::new().build_a(|router, api, storage| { router .bank .init_balance( storage, - &MockApi::default().addr_make(USER), + &api.addr_make(USER), vec![Coin { denom: NATIVE_DENOM.to_string(), amount: Uint128::new(100), From 87b2793425c5b3223411218692b85ae3ee354f62 Mon Sep 17 00:00:00 2001 From: Dariusz Depta Date: Wed, 19 Jun 2024 12:55:21 +0200 Subject: [PATCH 03/15] Next try. --- Cargo.lock | 4 +- src/app.rs | 6 +-- src/app_builder.rs | 52 ++++++++++++++++--- src/tests/test_app.rs | 2 +- src/tests/test_gov.rs | 4 +- src/tests/test_ibc.rs | 4 +- src/tests/test_stargate.rs | 4 +- src/wasm.rs | 6 +-- tests/test_app/test_instantiate2.rs | 8 +-- .../test_app/test_store_code_with_creator.rs | 4 +- tests/test_app_builder/test_with_api.rs | 4 +- tests/test_app_builder/test_with_bank.rs | 4 +- tests/test_app_builder/test_with_block.rs | 6 ++- .../test_with_distribution.rs | 4 +- tests/test_app_builder/test_with_gov.rs | 4 +- tests/test_app_builder/test_with_ibc.rs | 4 +- tests/test_app_builder/test_with_staking.rs | 4 +- tests/test_app_builder/test_with_stargate.rs | 27 +++++++--- tests/test_app_builder/test_with_storage.rs | 4 +- tests/test_app_builder/test_with_wasm.rs | 10 ++-- tests/test_bank/test_init_balance.rs | 2 +- tests/test_wasm/test_with_addr_gen.rs | 12 +++-- tests/test_wasm/test_with_checksum_gen.rs | 6 ++- 23 files changed, 120 insertions(+), 65 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 416b5955..eb015e47 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -532,9 +532,9 @@ checksum = "78ca9ab1a0babb1e7d5695e3530886289c18cf2f87ec19a575a0abdce112e3a3" [[package]] name = "miniz_oxide" -version = "0.7.3" +version = "0.7.4" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "87dfd01fe195c66b572b37921ad8803d010623c0aca821bea2302239d155cdae" +checksum = "b8a240ddb74feaf34a79a7add65a741f3167852fba007066dcac1ca548d89c08" dependencies = [ "adler", ] diff --git a/src/app.rs b/src/app.rs index 39540a69..70240f3c 100644 --- a/src/app.rs +++ b/src/app.rs @@ -77,7 +77,7 @@ pub fn no_init( impl Default for BasicApp { fn default() -> Self { - Self::new(no_init) + AppBuilder::new().build_no_init() } } @@ -100,7 +100,7 @@ impl BasicApp { &mut dyn Storage, ), { - AppBuilder::new().build(init_fn) + AppBuilder::new().build_internal(init_fn) } } @@ -125,7 +125,7 @@ where &mut dyn Storage, ), { - AppBuilder::new_custom().build(init_fn) + AppBuilder::new_custom().build_internal(init_fn) } impl Querier diff --git a/src/app_builder.rs b/src/app_builder.rs index 0e8aea39..72ea52ff 100644 --- a/src/app_builder.rs +++ b/src/app_builder.rs @@ -23,7 +23,7 @@ use std::fmt::Debug; /// /// let mut app = BasicAppBuilder::::new_custom() /// .with_custom(MyHandler::default()) -/// .build(no_init); +/// .build(|_, _, _| {}); /// ``` /// This type alias is crucial for constructing a custom app with specific modules. /// It provides a streamlined approach to building and configuring an App tailored to @@ -533,7 +533,7 @@ where /// 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. - pub fn build( + pub(crate) fn build_internal( self, init_fn: F, ) -> App @@ -554,7 +554,7 @@ where &mut dyn Storage, ), { - let router = Router { + let mut router = Router { wasm: self.wasm, bank: self.bank, custom: self.custom, @@ -564,21 +564,57 @@ where gov: self.gov, stargate: self.stargate, }; + let api = self.api; + let mut storage = self.storage; + init_fn(&mut router, &api, &mut storage); + App { + router, + api, + block: self.block, + storage, + } + } - let mut app = App { + /// 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. + pub(crate) fn build_no_init( + self, + ) -> App + where + BankT: Bank, + ApiT: Api, + StorageT: Storage, + CustomT: Module, + WasmT: Wasm, + StakingT: Staking, + DistrT: Distribution, + IbcT: Ibc, + GovT: Gov, + StargateT: Stargate, + { + let router = Router { + wasm: self.wasm, + bank: self.bank, + custom: self.custom, + staking: self.staking, + distribution: self.distribution, + ibc: self.ibc, + gov: self.gov, + stargate: self.stargate, + }; + App { router, api: self.api, block: self.block, storage: self.storage, - }; - app.init_modules(init_fn); - app + } } /// 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. - pub fn build_a( + pub fn build( self, init_fn: F, ) -> App diff --git a/src/tests/test_app.rs b/src/tests/test_app.rs index 2c459e76..f3834162 100644 --- a/src/tests/test_app.rs +++ b/src/tests/test_app.rs @@ -1660,7 +1660,7 @@ mod custom_messages { let mut app = AppBuilder::new_custom() .with_custom(custom_handler) - .build(no_init); + .build(|_, _, _| {}); let sender = app.api().addr_make("sender"); let owner = app.api().addr_make("owner"); diff --git a/src/tests/test_gov.rs b/src/tests/test_gov.rs index 68897735..dc3eca48 100644 --- a/src/tests/test_gov.rs +++ b/src/tests/test_gov.rs @@ -1,5 +1,5 @@ use crate::test_helpers::gov; -use crate::{no_init, App, AppBuilder, Executor, GovAcceptingModule}; +use crate::{App, AppBuilder, Executor, GovAcceptingModule}; use cosmwasm_std::Empty; #[test] @@ -22,7 +22,7 @@ fn default_gov() { fn accepting_gov() { let mut app = AppBuilder::new() .with_gov(GovAcceptingModule::new()) - .build(no_init); + .build(|_, _, _| {}); let creator_addr = app.api().addr_make("creator"); let code = app.store_code_with_creator(creator_addr, gov::contract()); diff --git a/src/tests/test_ibc.rs b/src/tests/test_ibc.rs index ed32377e..98408af7 100644 --- a/src/tests/test_ibc.rs +++ b/src/tests/test_ibc.rs @@ -1,5 +1,5 @@ use crate::test_helpers::ibc; -use crate::{no_init, App, AppBuilder, Executor, IbcAcceptingModule}; +use crate::{App, AppBuilder, Executor, IbcAcceptingModule}; use cosmwasm_std::Empty; #[test] @@ -22,7 +22,7 @@ fn default_ibc() { fn accepting_ibc() { let mut app = AppBuilder::new() .with_ibc(IbcAcceptingModule::new()) - .build(no_init); + .build(|_, _, _| {}); let creator_addr = app.api().addr_make("creator"); let code = app.store_code_with_creator(creator_addr, ibc::contract()); diff --git a/src/tests/test_stargate.rs b/src/tests/test_stargate.rs index 01f2f975..20bdb7e7 100644 --- a/src/tests/test_stargate.rs +++ b/src/tests/test_stargate.rs @@ -1,5 +1,5 @@ use crate::test_helpers::stargate; -use crate::{no_init, App, AppBuilder, Executor, StargateAccepting}; +use crate::{App, AppBuilder, Executor, StargateAccepting}; use cosmwasm_std::Empty; #[test] @@ -34,7 +34,7 @@ fn default_failing_stargate_handler_should_work() { fn accepting_stargate_handler_should_work() { let mut app = AppBuilder::default() .with_stargate(StargateAccepting) - .build(no_init); + .build(|_, _, _| {}); // store the contract let creator_addr = app.api().addr_make("creator"); diff --git a/src/wasm.rs b/src/wasm.rs index 04493cd2..27648094 100644 --- a/src/wasm.rs +++ b/src/wasm.rs @@ -439,7 +439,7 @@ where /// let wasm_keeper = WasmKeeper::new(); /// /// // create and use the application with newly created wasm keeper - /// let mut app = AppBuilder::default().with_wasm(wasm_keeper).build(no_init); + /// let mut app = AppBuilder::default().with_wasm(wasm_keeper).build(|_, _, _| {}); /// ``` pub fn new() -> Self { Self::default() @@ -474,7 +474,7 @@ where /// let wasm_keeper = WasmKeeper::new().with_address_generator(CustomAddressGenerator); /// /// // create and use the application with customized wasm keeper - /// let mut app = AppBuilder::default().with_wasm(wasm_keeper).build(no_init); + /// let mut app = AppBuilder::default().with_wasm(wasm_keeper).build(|_, _, _| {}); /// ``` pub fn with_address_generator( mut self, @@ -505,7 +505,7 @@ where /// let wasm_keeper = WasmKeeper::new().with_checksum_generator(MyChecksumGenerator); /// /// // create and use the application with customized wasm keeper - /// let mut app = AppBuilder::default().with_wasm(wasm_keeper).build(no_init); + /// let mut app = AppBuilder::default().with_wasm(wasm_keeper).build(|_, _, _| {}); /// ``` pub fn with_checksum_generator( mut self, diff --git a/tests/test_app/test_instantiate2.rs b/tests/test_app/test_instantiate2.rs index 95fc8eed..875ac736 100644 --- a/tests/test_app/test_instantiate2.rs +++ b/tests/test_app/test_instantiate2.rs @@ -1,7 +1,7 @@ use crate::test_contracts::counter; use cosmwasm_std::testing::MockApi; use cosmwasm_std::{instantiate2_address, to_json_binary, Api, Empty, WasmMsg}; -use cw_multi_test::{no_init, AppBuilder, Executor}; +use cw_multi_test::{AppBuilder, Executor}; use cw_utils::parse_instantiate_response_data; #[test] @@ -9,7 +9,7 @@ fn instantiate2_works() { // prepare the application with custom Api and custom address generator let mut app = AppBuilder::default() .with_api(MockApi::default().with_prefix("juno")) - .build(no_init); + .build(|_, _, _| {}); // prepare addresses for sender and creator let sender = app.api().addr_make("sender"); @@ -73,7 +73,7 @@ fn instantiate2_should_work_for_multiple_salts() { // prepare the application with custom Api and custom address generator let mut app = AppBuilder::default() .with_api(MockApi::default().with_prefix("juno")) - .build(no_init); + .build(|_, _, _| {}); // prepare addresses for sender and creator let sender = app.api().addr_make("sender"); @@ -106,7 +106,7 @@ fn instantiate2_fails_for_duplicated_addresses() { // prepare the application with custom Api and custom address generator let mut app = AppBuilder::default() .with_api(MockApi::default().with_prefix("osmo")) - .build(no_init); + .build(|_, _, _| {}); // prepare addresses for sender and creator let sender = app.api().addr_make("sender"); diff --git a/tests/test_app/test_store_code_with_creator.rs b/tests/test_app/test_store_code_with_creator.rs index 73d469dc..d738c441 100644 --- a/tests/test_app/test_store_code_with_creator.rs +++ b/tests/test_app/test_store_code_with_creator.rs @@ -1,5 +1,5 @@ use crate::test_contracts::counter; -use cw_multi_test::{no_init, AppBuilder}; +use cw_multi_test::AppBuilder; use cw_multi_test::{MockApiBech32, MockApiBech32m}; #[test] @@ -7,7 +7,7 @@ fn store_code_with_custom_creator_address_should_work() { // prepare the application let mut app = AppBuilder::default() .with_api(MockApiBech32m::new("juno")) - .build(no_init); + .build(|_, _, _| {}); let creator = app.api().addr_make("zeus"); diff --git a/tests/test_app_builder/test_with_api.rs b/tests/test_app_builder/test_with_api.rs index 91ee4de9..d8bc08d4 100644 --- a/tests/test_app_builder/test_with_api.rs +++ b/tests/test_app_builder/test_with_api.rs @@ -1,6 +1,6 @@ use cosmwasm_std::testing::MockApi; use cosmwasm_std::{Api, CanonicalAddr, HexBinary}; -use cw_multi_test::{no_init, AppBuilder}; +use cw_multi_test::AppBuilder; #[test] fn building_app_with_custom_api_should_work() { @@ -12,7 +12,7 @@ fn building_app_with_custom_api_should_work() { // Bech32 address encoding with 'juno' prefix let app = AppBuilder::default() .with_api(MockApi::default().with_prefix("juno")) - .build(no_init); + .build(|_, _, _| {}); // check address validation function assert_eq!(human, app.api().addr_validate(human).unwrap().as_str()); diff --git a/tests/test_app_builder/test_with_bank.rs b/tests/test_app_builder/test_with_bank.rs index 3aba13fd..d7228b73 100644 --- a/tests/test_app_builder/test_with_bank.rs +++ b/tests/test_app_builder/test_with_bank.rs @@ -1,6 +1,6 @@ use crate::test_app_builder::MyKeeper; use cosmwasm_std::{coins, BankMsg, BankQuery}; -use cw_multi_test::{no_init, AppBuilder, Bank, BankSudo, Executor}; +use cw_multi_test::{AppBuilder, Bank, BankSudo, Executor}; type MyBankKeeper = MyKeeper; @@ -17,7 +17,7 @@ fn building_app_with_custom_bank_should_work() { // 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 = app_builder.with_bank(bank_keeper).build(|_, _, _| {}); // prepare user addresses let recipient_addr = app.api().addr_make("recipient"); diff --git a/tests/test_app_builder/test_with_block.rs b/tests/test_app_builder/test_with_block.rs index 35faed2b..18178ba7 100644 --- a/tests/test_app_builder/test_with_block.rs +++ b/tests/test_app_builder/test_with_block.rs @@ -1,5 +1,5 @@ use cosmwasm_std::{BlockInfo, Timestamp}; -use cw_multi_test::{no_init, AppBuilder}; +use cw_multi_test::AppBuilder; #[test] fn building_app_with_custom_block_should_work() { @@ -12,7 +12,9 @@ fn building_app_with_custom_block_should_work() { // build the application with custom block let app_builder = AppBuilder::default(); - let app = app_builder.with_block(block_info.clone()).build(no_init); + let app = app_builder + .with_block(block_info.clone()) + .build(|_, _, _| {}); // calling block_info should return the same block used during initialization assert_eq!(block_info, app.block_info()); diff --git a/tests/test_app_builder/test_with_distribution.rs b/tests/test_app_builder/test_with_distribution.rs index fe6a6f46..80cd229e 100644 --- a/tests/test_app_builder/test_with_distribution.rs +++ b/tests/test_app_builder/test_with_distribution.rs @@ -1,6 +1,6 @@ use crate::test_app_builder::{MyKeeper, NO_MESSAGE}; use cosmwasm_std::{DistributionMsg, Empty}; -use cw_multi_test::{no_init, AppBuilder, Distribution, Executor}; +use cw_multi_test::{AppBuilder, Distribution, Executor}; type MyDistributionKeeper = MyKeeper; @@ -18,7 +18,7 @@ fn building_app_with_custom_distribution_should_work() { let app_builder = AppBuilder::default(); let mut app = app_builder .with_distribution(distribution_keeper) - .build(no_init); + .build(|_, _, _| {}); // prepare addresses let recipient_addr = app.api().addr_make("recipient"); diff --git a/tests/test_app_builder/test_with_gov.rs b/tests/test_app_builder/test_with_gov.rs index d086674c..7a4ce2ba 100644 --- a/tests/test_app_builder/test_with_gov.rs +++ b/tests/test_app_builder/test_with_gov.rs @@ -1,6 +1,6 @@ use crate::test_app_builder::{MyKeeper, NO_MESSAGE}; use cosmwasm_std::{Empty, GovMsg, VoteOption}; -use cw_multi_test::{no_init, AppBuilder, Executor, Gov}; +use cw_multi_test::{AppBuilder, Executor, Gov}; type MyGovKeeper = MyKeeper; @@ -15,7 +15,7 @@ fn building_app_with_custom_gov_should_work() { // 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 = app_builder.with_gov(gov_keeper).build(|_, _, _| {}); // prepare addresses let sender_addr = app.api().addr_make("sender"); diff --git a/tests/test_app_builder/test_with_ibc.rs b/tests/test_app_builder/test_with_ibc.rs index a39ec0a5..c72d6eae 100644 --- a/tests/test_app_builder/test_with_ibc.rs +++ b/tests/test_app_builder/test_with_ibc.rs @@ -1,6 +1,6 @@ use crate::test_app_builder::{MyKeeper, NO_MESSAGE}; use cosmwasm_std::{Empty, IbcMsg, IbcQuery, QueryRequest}; -use cw_multi_test::{no_init, AppBuilder, Executor, Ibc}; +use cw_multi_test::{AppBuilder, Executor, Ibc}; type MyIbcKeeper = MyKeeper; @@ -16,7 +16,7 @@ fn building_app_with_custom_ibc_should_work() { // 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 = app_builder.with_ibc(ibc_keeper).build(|_, _, _| {}); // prepare user addresses let sender_addr = app.api().addr_make("sender"); diff --git a/tests/test_app_builder/test_with_staking.rs b/tests/test_app_builder/test_with_staking.rs index ac429062..dd49b848 100644 --- a/tests/test_app_builder/test_with_staking.rs +++ b/tests/test_app_builder/test_with_staking.rs @@ -1,6 +1,6 @@ use crate::test_app_builder::MyKeeper; use cosmwasm_std::{Coin, StakingMsg, StakingQuery}; -use cw_multi_test::{no_init, AppBuilder, Executor, Staking, StakingSudo}; +use cw_multi_test::{AppBuilder, Executor, Staking, StakingSudo}; type MyStakeKeeper = MyKeeper; @@ -17,7 +17,7 @@ fn building_app_with_custom_staking_should_work() { // 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 = app_builder.with_staking(stake_keeper).build(|_, _, _| {}); // prepare addresses let validator_addr = app.api().addr_make("validator"); diff --git a/tests/test_app_builder/test_with_stargate.rs b/tests/test_app_builder/test_with_stargate.rs index 51b10b80..2797b755 100644 --- a/tests/test_app_builder/test_with_stargate.rs +++ b/tests/test_app_builder/test_with_stargate.rs @@ -5,8 +5,7 @@ use cosmwasm_std::{ }; use cw_multi_test::error::AnyResult; use cw_multi_test::{ - no_init, AppBuilder, AppResponse, CosmosRouter, Executor, Stargate, StargateAccepting, - StargateFailing, + AppBuilder, AppResponse, CosmosRouter, Executor, Stargate, StargateAccepting, StargateFailing, }; use serde::de::DeserializeOwned; @@ -79,7 +78,9 @@ impl Stargate for StargateKeeper { fn building_app_with_custom_stargate_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 = app_builder + .with_stargate(StargateKeeper) + .build(|_, _, _| {}); // prepare user addresses let sender_addr = app.api().addr_make("sender"); @@ -114,7 +115,9 @@ fn building_app_with_custom_stargate_should_work() { 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 = app_builder + .with_stargate(StargateKeeper) + .build(|_, _, _| {}); // prepare user addresses let sender_addr = app.api().addr_make("sender"); @@ -145,7 +148,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 = app_builder + .with_stargate(StargateAccepting) + .build(|_, _, _| {}); // prepare user addresses let sender_addr = app.api().addr_make("sender"); @@ -173,7 +178,9 @@ fn building_app_with_accepting_stargate_should_work() { #[cfg(feature = "cosmwasm_2_0")] fn building_app_with_accepting_any_grpc_should_work() { let app_builder = AppBuilder::default(); - let mut app = app_builder.with_stargate(StargateAccepting).build(no_init); + let mut app = app_builder + .with_stargate(StargateAccepting) + .build(|_, _, _| {}); // prepare user addresses let sender_addr = app.api().addr_make("sender"); @@ -206,7 +213,9 @@ fn building_app_with_accepting_any_grpc_should_work() { #[test] fn default_failing_stargate_should_work() { let app_builder = AppBuilder::default(); - let mut app = app_builder.with_stargate(StargateFailing).build(no_init); + let mut app = app_builder + .with_stargate(StargateFailing) + .build(|_, _, _| {}); // prepare user addresses let sender_addr = app.api().addr_make("sender"); @@ -239,7 +248,9 @@ fn default_failing_stargate_should_work() { #[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 = app_builder + .with_stargate(StargateFailing) + .build(|_, _, _| {}); // prepare user addresses let sender_addr = app.api().addr_make("sender"); diff --git a/tests/test_app_builder/test_with_storage.rs b/tests/test_app_builder/test_with_storage.rs index 3290f9c1..0f0e95f2 100644 --- a/tests/test_app_builder/test_with_storage.rs +++ b/tests/test_app_builder/test_with_storage.rs @@ -1,7 +1,7 @@ use crate::test_contracts; use crate::test_contracts::counter::{CounterQueryMsg, CounterResponseMsg}; use cosmwasm_std::{to_json_binary, Empty, Order, Record, Storage, WasmMsg}; -use cw_multi_test::{no_init, AppBuilder, Executor}; +use cw_multi_test::{AppBuilder, Executor}; use std::collections::BTreeMap; use std::iter; @@ -44,7 +44,7 @@ fn building_app_with_custom_storage_should_work() { let app_builder = AppBuilder::default(); let mut app = app_builder .with_storage(MyStorage::default()) - .build(no_init); + .build(|_, _, _| {}); // prepare user addresses let owner_addr = app.api().addr_make("owner"); diff --git a/tests/test_app_builder/test_with_wasm.rs b/tests/test_app_builder/test_with_wasm.rs index 390c8684..db0ef7a3 100644 --- a/tests/test_app_builder/test_with_wasm.rs +++ b/tests/test_app_builder/test_with_wasm.rs @@ -5,8 +5,8 @@ use cosmwasm_std::{ }; use cw_multi_test::error::{bail, AnyResult}; use cw_multi_test::{ - no_init, AppBuilder, AppResponse, Contract, ContractData, CosmosRouter, Executor, Wasm, - WasmKeeper, WasmSudo, + AppBuilder, AppResponse, Contract, ContractData, CosmosRouter, Executor, Wasm, WasmKeeper, + WasmSudo, }; use once_cell::sync::Lazy; @@ -92,7 +92,7 @@ fn building_app_with_custom_wasm_should_work() { // 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 = app_builder.with_wasm(wasm_keeper).build(|_, _, _| {}); // prepare addresses let contract_addr = app.api().addr_make("contract"); @@ -163,5 +163,7 @@ 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 _ = app_builder + .with_wasm(WasmKeeper::default()) + .build(|_, _, _| {}); } diff --git a/tests/test_bank/test_init_balance.rs b/tests/test_bank/test_init_balance.rs index 21dba54c..0bb48365 100644 --- a/tests/test_bank/test_init_balance.rs +++ b/tests/test_bank/test_init_balance.rs @@ -7,7 +7,7 @@ const AMOUNT: u128 = 100; #[test] fn initializing_balance_should_work() { - let app = AppBuilder::new().build_a(|router, api, storage| { + let app = AppBuilder::new().build(|router, api, storage| { router .bank .init_balance( diff --git a/tests/test_wasm/test_with_addr_gen.rs b/tests/test_wasm/test_with_addr_gen.rs index 4ec6c829..a053c32f 100644 --- a/tests/test_wasm/test_with_addr_gen.rs +++ b/tests/test_wasm/test_with_addr_gen.rs @@ -1,7 +1,7 @@ use cosmwasm_std::testing::MockApi; use cosmwasm_std::{Addr, Api, Empty, Storage}; use cw_multi_test::error::AnyResult; -use cw_multi_test::{no_init, AddressGenerator, AppBuilder, Executor, WasmKeeper}; +use cw_multi_test::{AddressGenerator, AppBuilder, Executor, WasmKeeper}; use crate::test_contracts; @@ -10,7 +10,7 @@ fn contract_address_should_work() { // prepare application with custom API let mut app = AppBuilder::default() .with_api(MockApi::default().with_prefix("purple")) - .build(no_init); + .build(|_, _, _| {}); // prepare user addresses let creator_addr = app.api().addr_make("creator"); @@ -65,7 +65,9 @@ fn custom_address_generator_should_work() { let wasm_keeper = WasmKeeper::new().with_address_generator(CustomAddressGenerator); // prepare application with custom wasm keeper - let mut app = AppBuilder::default().with_wasm(wasm_keeper).build(no_init); + let mut app = AppBuilder::default() + .with_wasm(wasm_keeper) + .build(|_, _, _| {}); // prepare user addresses let owner_addr = app.api().addr_make("owner"); @@ -93,7 +95,7 @@ fn predictable_contract_address_should_work() { // prepare application with custom api let mut app = AppBuilder::default() .with_api(MockApi::default().with_prefix("juno")) - .build(no_init); + .build(|_, _, _| {}); let creator = app.api().addr_make("creator"); @@ -146,7 +148,7 @@ fn creating_contract_with_the_same_predictable_address_should_fail() { // prepare application with custom api let mut app = AppBuilder::default() .with_api(MockApi::default().with_prefix("juno")) - .build(no_init); + .build(|_, _, _| {}); let creator = app.api().addr_make("creator"); diff --git a/tests/test_wasm/test_with_checksum_gen.rs b/tests/test_wasm/test_with_checksum_gen.rs index 47685d31..53f90e14 100644 --- a/tests/test_wasm/test_with_checksum_gen.rs +++ b/tests/test_wasm/test_with_checksum_gen.rs @@ -1,6 +1,6 @@ use crate::test_contracts; use cosmwasm_std::{Addr, Checksum}; -use cw_multi_test::{no_init, App, AppBuilder, ChecksumGenerator, WasmKeeper}; +use cw_multi_test::{App, AppBuilder, ChecksumGenerator, WasmKeeper}; #[test] fn default_checksum_generator_should_work() { @@ -38,7 +38,9 @@ fn custom_checksum_generator_should_work() { let wasm_keeper = WasmKeeper::new().with_checksum_generator(MyChecksumGenerator); // prepare application with custom wasm keeper - let mut app = AppBuilder::default().with_wasm(wasm_keeper).build(no_init); + let mut app = AppBuilder::default() + .with_wasm(wasm_keeper) + .build(|_, _, _| {}); // prepare user addresses let creator_addr = app.api().addr_make("creator"); From 01af97f5dee780520b9062b0c3212753675ea102 Mon Sep 17 00:00:00 2001 From: Dariusz Depta Date: Wed, 19 Jun 2024 13:03:39 +0200 Subject: [PATCH 04/15] Updates. --- src/app.rs | 2 +- src/app_builder.rs | 40 ---------------------------------------- 2 files changed, 1 insertion(+), 41 deletions(-) diff --git a/src/app.rs b/src/app.rs index 70240f3c..d2966683 100644 --- a/src/app.rs +++ b/src/app.rs @@ -77,7 +77,7 @@ pub fn no_init( impl Default for BasicApp { fn default() -> Self { - AppBuilder::new().build_no_init() + Self::new(|_, _, _| {}) } } diff --git a/src/app_builder.rs b/src/app_builder.rs index 72ea52ff..b595ba09 100644 --- a/src/app_builder.rs +++ b/src/app_builder.rs @@ -575,42 +575,6 @@ where } } - /// 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. - pub(crate) fn build_no_init( - self, - ) -> App - where - BankT: Bank, - ApiT: Api, - StorageT: Storage, - CustomT: Module, - WasmT: Wasm, - StakingT: Staking, - DistrT: Distribution, - IbcT: Ibc, - GovT: Gov, - StargateT: Stargate, - { - let router = Router { - wasm: self.wasm, - bank: self.bank, - custom: self.custom, - staking: self.staking, - distribution: self.distribution, - ibc: self.ibc, - gov: self.gov, - stargate: self.stargate, - }; - App { - router, - api: self.api, - block: self.block, - storage: self.storage, - } - } - /// 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. @@ -645,13 +609,9 @@ where gov: self.gov, stargate: self.stargate, }; - let api = self.api; - let mut storage = self.storage; - init_fn(&mut router, &api, &mut storage); - App { router, api, From 562609c2a45c7aa0910c285a0afb84f9d7216c8d Mon Sep 17 00:00:00 2001 From: Dariusz Depta Date: Wed, 19 Jun 2024 13:24:54 +0200 Subject: [PATCH 05/15] Exposed the same Api in build like used in App. --- src/app.rs | 17 +++------- src/app_builder.rs | 47 +--------------------------- src/lib.rs | 4 +-- src/tests/test_app.rs | 10 +++--- src/wasm.rs | 6 ++-- tests/test_bank/test_init_balance.rs | 19 ++++++----- 6 files changed, 23 insertions(+), 80 deletions(-) diff --git a/src/app.rs b/src/app.rs index d2966683..b698bd51 100644 --- a/src/app.rs +++ b/src/app.rs @@ -66,15 +66,6 @@ pub struct App< pub(crate) block: BlockInfo, } -/// No-op application initialization function. -pub fn no_init( - router: &mut Router, - api: &dyn Api, - storage: &mut dyn Storage, -) { - let _ = (router, api, storage); -} - impl Default for BasicApp { fn default() -> Self { Self::new(|_, _, _| {}) @@ -96,11 +87,11 @@ impl BasicApp { GovFailingModule, StargateFailing, >, - &dyn Api, + &MockApi, &mut dyn Storage, ), { - AppBuilder::new().build_internal(init_fn) + AppBuilder::new().build(init_fn) } } @@ -121,11 +112,11 @@ where GovFailingModule, StargateFailing, >, - &dyn Api, + &MockApi, &mut dyn Storage, ), { - AppBuilder::new_custom().build_internal(init_fn) + AppBuilder::new_custom().build(init_fn) } impl Querier diff --git a/src/app_builder.rs b/src/app_builder.rs index b595ba09..ab18b64a 100644 --- a/src/app_builder.rs +++ b/src/app_builder.rs @@ -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::{BasicAppBuilder, FailingModule, Module}; /// # type MyHandler = FailingModule; /// # type MyExecC = Empty; /// # type MyQueryC = Empty; @@ -530,51 +530,6 @@ 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. - pub(crate) fn build_internal( - self, - init_fn: F, - ) -> App - where - BankT: Bank, - ApiT: Api, - StorageT: Storage, - CustomT: Module, - WasmT: Wasm, - StakingT: Staking, - DistrT: Distribution, - IbcT: Ibc, - GovT: Gov, - StargateT: Stargate, - F: FnOnce( - &mut Router, - &dyn Api, - &mut dyn Storage, - ), - { - let mut router = Router { - wasm: self.wasm, - bank: self.bank, - custom: self.custom, - staking: self.staking, - distribution: self.distribution, - ibc: self.ibc, - gov: self.gov, - stargate: self.stargate, - }; - let api = self.api; - let mut storage = self.storage; - init_fn(&mut router, &api, &mut storage); - App { - router, - api, - block: self.block, - storage, - } - } - /// 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. diff --git a/src/lib.rs b/src/lib.rs index e5d3f24c..be66a675 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -149,9 +149,7 @@ pub use crate::addresses::{ AddressGenerator, IntoAddr, IntoBech32, IntoBech32m, SimpleAddressGenerator, }; pub use crate::api::{MockApiBech32, MockApiBech32m}; -pub use crate::app::{ - custom_app, next_block, no_init, App, BasicApp, CosmosRouter, Router, SudoMsg, -}; +pub use crate::app::{custom_app, next_block, App, BasicApp, CosmosRouter, Router, SudoMsg}; pub use crate::app_builder::{AppBuilder, BasicAppBuilder}; pub use crate::bank::{Bank, BankKeeper, BankSudo}; pub use crate::checksums::ChecksumGenerator; diff --git a/src/tests/test_app.rs b/src/tests/test_app.rs index f3834162..bb61bf54 100644 --- a/src/tests/test_app.rs +++ b/src/tests/test_app.rs @@ -5,8 +5,8 @@ use crate::test_helpers::{caller, echo, error, hackatom, payout, reflect, Custom use crate::transactions::{transactional, StorageTransaction}; use crate::wasm::ContractData; use crate::{ - custom_app, next_block, no_init, App, AppResponse, Bank, CosmosRouter, Distribution, Executor, - Module, Router, Staking, Wasm, WasmSudo, + custom_app, next_block, App, AppResponse, Bank, CosmosRouter, Distribution, Executor, Module, + Router, Staking, Wasm, WasmSudo, }; use crate::{AppBuilder, IntoAddr}; use cosmwasm_std::testing::{mock_env, MockQuerier}; @@ -1738,7 +1738,7 @@ mod protobuf_wrapped_data { #[test] fn instantiate_with_data_works() { - let mut app = BasicApp::new(no_init); + let mut app = BasicApp::new(|_, _, _| {}); let owner = app.api().addr_make("owner"); @@ -1768,7 +1768,7 @@ mod protobuf_wrapped_data { #[test] fn instantiate_with_reply_works() { - let mut app = BasicApp::new(no_init); + let mut app = BasicApp::new(|_, _, _| {}); let owner = app.api().addr_make("owner"); @@ -1821,7 +1821,7 @@ mod protobuf_wrapped_data { #[test] fn execute_wrapped_properly() { - let mut app = BasicApp::new(no_init); + let mut app = BasicApp::new(|_, _, _| {}); let owner = app.api().addr_make("owner"); diff --git a/src/wasm.rs b/src/wasm.rs index 27648094..1275ada2 100644 --- a/src/wasm.rs +++ b/src/wasm.rs @@ -433,7 +433,7 @@ where /// # Example /// /// ``` - /// use cw_multi_test::{AppBuilder, no_init, WasmKeeper}; + /// use cw_multi_test::{AppBuilder, WasmKeeper}; /// /// // create wasm keeper /// let wasm_keeper = WasmKeeper::new(); @@ -451,7 +451,7 @@ where /// /// ``` /// use cosmwasm_std::{Addr, Api, Storage}; - /// use cw_multi_test::{AddressGenerator, AppBuilder, no_init, WasmKeeper}; + /// use cw_multi_test::{AddressGenerator, AppBuilder, WasmKeeper}; /// use cw_multi_test::error::AnyResult; /// # use cosmwasm_std::testing::MockApi; /// @@ -490,7 +490,7 @@ where /// /// ``` /// use cosmwasm_std::{Addr, Checksum}; - /// use cw_multi_test::{AppBuilder, ChecksumGenerator, no_init, WasmKeeper}; + /// use cw_multi_test::{AppBuilder, ChecksumGenerator, WasmKeeper}; /// /// struct MyChecksumGenerator; /// diff --git a/tests/test_bank/test_init_balance.rs b/tests/test_bank/test_init_balance.rs index 0bb48365..dcc8eaec 100644 --- a/tests/test_bank/test_init_balance.rs +++ b/tests/test_bank/test_init_balance.rs @@ -1,8 +1,8 @@ use cosmwasm_std::{Coin, Uint128}; use cw_multi_test::AppBuilder; -const USER: &str = "USER"; -const NATIVE_DENOM: &str = "NativeDenom"; +const USER: &str = "user"; +const DENOM: &str = "denom"; const AMOUNT: u128 = 100; #[test] @@ -14,18 +14,17 @@ fn initializing_balance_should_work() { storage, &api.addr_make(USER), vec![Coin { - denom: NATIVE_DENOM.to_string(), + denom: DENOM.to_string(), amount: Uint128::new(100), }], ) .unwrap(); }); - let api = app.api(); - let user_addr = api.addr_make(USER); - let balances = app.wrap().query_all_balances(user_addr).unwrap(); + let balances = app + .wrap() + .query_all_balances(app.api().addr_make(USER)) + .unwrap(); assert_eq!(1, balances.len()); - assert_eq!( - format!("{}{}", AMOUNT, NATIVE_DENOM), - balances[0].to_string() - ); + assert_eq!(AMOUNT, balances[0].amount.u128()); + assert_eq!(DENOM, balances[0].denom); } From 6cb296e77c0fecb3da64941d431af878031abbb2 Mon Sep 17 00:00:00 2001 From: Dariusz Depta Date: Wed, 19 Jun 2024 13:47:48 +0200 Subject: [PATCH 06/15] Updates. --- src/app_builder.rs | 29 ++++++++++++++++--- src/tests/test_app.rs | 2 +- src/tests/test_gov.rs | 2 +- src/tests/test_ibc.rs | 4 +-- src/tests/test_stargate.rs | 2 +- src/wasm.rs | 6 ++-- tests/test_app/test_instantiate2.rs | 6 ++-- .../test_app/test_store_code_with_creator.rs | 2 +- tests/test_app_builder/test_with_api.rs | 2 +- tests/test_app_builder/test_with_bank.rs | 3 +- tests/test_app_builder/test_with_block.rs | 4 +-- .../test_with_distribution.rs | 2 +- tests/test_app_builder/test_with_gov.rs | 3 +- tests/test_app_builder/test_with_ibc.rs | 3 +- tests/test_app_builder/test_with_staking.rs | 5 ++-- tests/test_app_builder/test_with_stargate.rs | 27 ++++++----------- tests/test_app_builder/test_with_storage.rs | 5 ++-- tests/test_app_builder/test_with_wasm.rs | 8 ++--- tests/test_wasm/test_with_addr_gen.rs | 10 +++---- tests/test_wasm/test_with_checksum_gen.rs | 4 +-- 20 files changed, 65 insertions(+), 64 deletions(-) diff --git a/src/app_builder.rs b/src/app_builder.rs index ab18b64a..5a7f6624 100644 --- a/src/app_builder.rs +++ b/src/app_builder.rs @@ -23,7 +23,7 @@ use std::fmt::Debug; /// /// let mut app = BasicAppBuilder::::new_custom() /// .with_custom(MyHandler::default()) -/// .build(|_, _, _| {}); +/// .build_no_init(); /// ``` /// This type alias is crucial for constructing a custom app with specific modules. /// It provides a streamlined approach to building and configuring an App tailored to @@ -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( self, init_fn: F, @@ -574,4 +574,25 @@ where storage, } } + + /// Builds the final [App] without initialization. + /// + /// At this point all component types have to be properly related to each other. + pub fn build_no_init( + self, + ) -> App + where + BankT: Bank, + ApiT: Api, + StorageT: Storage, + CustomT: Module, + WasmT: Wasm, + StakingT: Staking, + DistrT: Distribution, + IbcT: Ibc, + GovT: Gov, + StargateT: Stargate, + { + self.build(|_, _, _| {}) + } } diff --git a/src/tests/test_app.rs b/src/tests/test_app.rs index bb61bf54..21ec60b0 100644 --- a/src/tests/test_app.rs +++ b/src/tests/test_app.rs @@ -1660,7 +1660,7 @@ mod custom_messages { let mut app = AppBuilder::new_custom() .with_custom(custom_handler) - .build(|_, _, _| {}); + .build_no_init(); let sender = app.api().addr_make("sender"); let owner = app.api().addr_make("owner"); diff --git a/src/tests/test_gov.rs b/src/tests/test_gov.rs index dc3eca48..07db5899 100644 --- a/src/tests/test_gov.rs +++ b/src/tests/test_gov.rs @@ -22,7 +22,7 @@ fn default_gov() { fn accepting_gov() { let mut app = AppBuilder::new() .with_gov(GovAcceptingModule::new()) - .build(|_, _, _| {}); + .build_no_init(); let creator_addr = app.api().addr_make("creator"); let code = app.store_code_with_creator(creator_addr, gov::contract()); diff --git a/src/tests/test_ibc.rs b/src/tests/test_ibc.rs index 98408af7..2124cc31 100644 --- a/src/tests/test_ibc.rs +++ b/src/tests/test_ibc.rs @@ -20,9 +20,9 @@ fn default_ibc() { #[test] fn accepting_ibc() { - let mut app = AppBuilder::new() + let mut app = AppBuilder::default() .with_ibc(IbcAcceptingModule::new()) - .build(|_, _, _| {}); + .build_no_init(); let creator_addr = app.api().addr_make("creator"); let code = app.store_code_with_creator(creator_addr, ibc::contract()); diff --git a/src/tests/test_stargate.rs b/src/tests/test_stargate.rs index 20bdb7e7..2c9389a8 100644 --- a/src/tests/test_stargate.rs +++ b/src/tests/test_stargate.rs @@ -34,7 +34,7 @@ fn default_failing_stargate_handler_should_work() { fn accepting_stargate_handler_should_work() { let mut app = AppBuilder::default() .with_stargate(StargateAccepting) - .build(|_, _, _| {}); + .build_no_init(); // store the contract let creator_addr = app.api().addr_make("creator"); diff --git a/src/wasm.rs b/src/wasm.rs index 1275ada2..583de3ef 100644 --- a/src/wasm.rs +++ b/src/wasm.rs @@ -439,7 +439,7 @@ where /// let wasm_keeper = WasmKeeper::new(); /// /// // create and use the application with newly created wasm keeper - /// let mut app = AppBuilder::default().with_wasm(wasm_keeper).build(|_, _, _| {}); + /// let mut app = AppBuilder::default().with_wasm(wasm_keeper).build_no_init(); /// ``` pub fn new() -> Self { Self::default() @@ -474,7 +474,7 @@ where /// let wasm_keeper = WasmKeeper::new().with_address_generator(CustomAddressGenerator); /// /// // create and use the application with customized wasm keeper - /// let mut app = AppBuilder::default().with_wasm(wasm_keeper).build(|_, _, _| {}); + /// let mut app = AppBuilder::default().with_wasm(wasm_keeper).build_no_init(); /// ``` pub fn with_address_generator( mut self, @@ -505,7 +505,7 @@ where /// let wasm_keeper = WasmKeeper::new().with_checksum_generator(MyChecksumGenerator); /// /// // create and use the application with customized wasm keeper - /// let mut app = AppBuilder::default().with_wasm(wasm_keeper).build(|_, _, _| {}); + /// let mut app = AppBuilder::default().with_wasm(wasm_keeper).build_no_init(); /// ``` pub fn with_checksum_generator( mut self, diff --git a/tests/test_app/test_instantiate2.rs b/tests/test_app/test_instantiate2.rs index 875ac736..1c125aa9 100644 --- a/tests/test_app/test_instantiate2.rs +++ b/tests/test_app/test_instantiate2.rs @@ -9,7 +9,7 @@ fn instantiate2_works() { // prepare the application with custom Api and custom address generator let mut app = AppBuilder::default() .with_api(MockApi::default().with_prefix("juno")) - .build(|_, _, _| {}); + .build_no_init(); // prepare addresses for sender and creator let sender = app.api().addr_make("sender"); @@ -73,7 +73,7 @@ fn instantiate2_should_work_for_multiple_salts() { // prepare the application with custom Api and custom address generator let mut app = AppBuilder::default() .with_api(MockApi::default().with_prefix("juno")) - .build(|_, _, _| {}); + .build_no_init(); // prepare addresses for sender and creator let sender = app.api().addr_make("sender"); @@ -106,7 +106,7 @@ fn instantiate2_fails_for_duplicated_addresses() { // prepare the application with custom Api and custom address generator let mut app = AppBuilder::default() .with_api(MockApi::default().with_prefix("osmo")) - .build(|_, _, _| {}); + .build_no_init(); // prepare addresses for sender and creator let sender = app.api().addr_make("sender"); diff --git a/tests/test_app/test_store_code_with_creator.rs b/tests/test_app/test_store_code_with_creator.rs index d738c441..fcb777f8 100644 --- a/tests/test_app/test_store_code_with_creator.rs +++ b/tests/test_app/test_store_code_with_creator.rs @@ -7,7 +7,7 @@ fn store_code_with_custom_creator_address_should_work() { // prepare the application let mut app = AppBuilder::default() .with_api(MockApiBech32m::new("juno")) - .build(|_, _, _| {}); + .build_no_init(); let creator = app.api().addr_make("zeus"); diff --git a/tests/test_app_builder/test_with_api.rs b/tests/test_app_builder/test_with_api.rs index d8bc08d4..3fe01bab 100644 --- a/tests/test_app_builder/test_with_api.rs +++ b/tests/test_app_builder/test_with_api.rs @@ -12,7 +12,7 @@ fn building_app_with_custom_api_should_work() { // Bech32 address encoding with 'juno' prefix let app = AppBuilder::default() .with_api(MockApi::default().with_prefix("juno")) - .build(|_, _, _| {}); + .build_no_init(); // check address validation function assert_eq!(human, app.api().addr_validate(human).unwrap().as_str()); diff --git a/tests/test_app_builder/test_with_bank.rs b/tests/test_app_builder/test_with_bank.rs index d7228b73..54cf6461 100644 --- a/tests/test_app_builder/test_with_bank.rs +++ b/tests/test_app_builder/test_with_bank.rs @@ -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(|_, _, _| {}); + let mut app = AppBuilder::default().with_bank(bank_keeper).build_no_init(); // prepare user addresses let recipient_addr = app.api().addr_make("recipient"); diff --git a/tests/test_app_builder/test_with_block.rs b/tests/test_app_builder/test_with_block.rs index 18178ba7..6fc08b73 100644 --- a/tests/test_app_builder/test_with_block.rs +++ b/tests/test_app_builder/test_with_block.rs @@ -12,9 +12,7 @@ fn building_app_with_custom_block_should_work() { // build the application with custom block let app_builder = AppBuilder::default(); - let app = app_builder - .with_block(block_info.clone()) - .build(|_, _, _| {}); + let app = app_builder.with_block(block_info.clone()).build_no_init(); // calling block_info should return the same block used during initialization assert_eq!(block_info, app.block_info()); diff --git a/tests/test_app_builder/test_with_distribution.rs b/tests/test_app_builder/test_with_distribution.rs index 80cd229e..46003adc 100644 --- a/tests/test_app_builder/test_with_distribution.rs +++ b/tests/test_app_builder/test_with_distribution.rs @@ -18,7 +18,7 @@ fn building_app_with_custom_distribution_should_work() { let app_builder = AppBuilder::default(); let mut app = app_builder .with_distribution(distribution_keeper) - .build(|_, _, _| {}); + .build_no_init(); // prepare addresses let recipient_addr = app.api().addr_make("recipient"); diff --git a/tests/test_app_builder/test_with_gov.rs b/tests/test_app_builder/test_with_gov.rs index 7a4ce2ba..de4109f9 100644 --- a/tests/test_app_builder/test_with_gov.rs +++ b/tests/test_app_builder/test_with_gov.rs @@ -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(|_, _, _| {}); + let mut app = AppBuilder::default().with_gov(gov_keeper).build_no_init(); // prepare addresses let sender_addr = app.api().addr_make("sender"); diff --git a/tests/test_app_builder/test_with_ibc.rs b/tests/test_app_builder/test_with_ibc.rs index c72d6eae..cf088069 100644 --- a/tests/test_app_builder/test_with_ibc.rs +++ b/tests/test_app_builder/test_with_ibc.rs @@ -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(|_, _, _| {}); + let mut app = AppBuilder::default().with_ibc(ibc_keeper).build_no_init(); // prepare user addresses let sender_addr = app.api().addr_make("sender"); diff --git a/tests/test_app_builder/test_with_staking.rs b/tests/test_app_builder/test_with_staking.rs index dd49b848..a75fc979 100644 --- a/tests/test_app_builder/test_with_staking.rs +++ b/tests/test_app_builder/test_with_staking.rs @@ -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(|_, _, _| {}); + let mut app = AppBuilder::default() + .with_staking(stake_keeper) + .build_no_init(); // prepare addresses let validator_addr = app.api().addr_make("validator"); diff --git a/tests/test_app_builder/test_with_stargate.rs b/tests/test_app_builder/test_with_stargate.rs index 2797b755..331016dd 100644 --- a/tests/test_app_builder/test_with_stargate.rs +++ b/tests/test_app_builder/test_with_stargate.rs @@ -78,9 +78,7 @@ impl Stargate for StargateKeeper { fn building_app_with_custom_stargate_should_work() { // build the application with custom stargate keeper let app_builder = AppBuilder::default(); - let mut app = app_builder - .with_stargate(StargateKeeper) - .build(|_, _, _| {}); + let mut app = app_builder.with_stargate(StargateKeeper).build_no_init(); // prepare user addresses let sender_addr = app.api().addr_make("sender"); @@ -114,10 +112,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 + let mut app = AppBuilder::default() .with_stargate(StargateKeeper) - .build(|_, _, _| {}); + .build_no_init(); // prepare user addresses let sender_addr = app.api().addr_make("sender"); @@ -147,10 +144,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 + let mut app = AppBuilder::default() .with_stargate(StargateAccepting) - .build(|_, _, _| {}); + .build_no_init(); // prepare user addresses let sender_addr = app.api().addr_make("sender"); @@ -178,9 +174,7 @@ fn building_app_with_accepting_stargate_should_work() { #[cfg(feature = "cosmwasm_2_0")] fn building_app_with_accepting_any_grpc_should_work() { let app_builder = AppBuilder::default(); - let mut app = app_builder - .with_stargate(StargateAccepting) - .build(|_, _, _| {}); + let mut app = app_builder.with_stargate(StargateAccepting).build_no_init(); // prepare user addresses let sender_addr = app.api().addr_make("sender"); @@ -213,9 +207,7 @@ fn building_app_with_accepting_any_grpc_should_work() { #[test] fn default_failing_stargate_should_work() { let app_builder = AppBuilder::default(); - let mut app = app_builder - .with_stargate(StargateFailing) - .build(|_, _, _| {}); + let mut app = app_builder.with_stargate(StargateFailing).build_no_init(); // prepare user addresses let sender_addr = app.api().addr_make("sender"); @@ -247,10 +239,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 + let mut app = AppBuilder::default() .with_stargate(StargateFailing) - .build(|_, _, _| {}); + .build_no_init(); // prepare user addresses let sender_addr = app.api().addr_make("sender"); diff --git a/tests/test_app_builder/test_with_storage.rs b/tests/test_app_builder/test_with_storage.rs index 0f0e95f2..f2ece2d1 100644 --- a/tests/test_app_builder/test_with_storage.rs +++ b/tests/test_app_builder/test_with_storage.rs @@ -41,10 +41,9 @@ 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(|_, _, _| {}); + .build_no_init(); // prepare user addresses let owner_addr = app.api().addr_make("owner"); diff --git a/tests/test_app_builder/test_with_wasm.rs b/tests/test_app_builder/test_with_wasm.rs index db0ef7a3..a5b24383 100644 --- a/tests/test_app_builder/test_with_wasm.rs +++ b/tests/test_app_builder/test_with_wasm.rs @@ -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(|_, _, _| {}); + let mut app = AppBuilder::default().with_wasm(wasm_keeper).build_no_init(); // prepare addresses let contract_addr = app.api().addr_make("contract"); @@ -162,8 +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 + let _ = AppBuilder::default() .with_wasm(WasmKeeper::default()) - .build(|_, _, _| {}); + .build_no_init(); } diff --git a/tests/test_wasm/test_with_addr_gen.rs b/tests/test_wasm/test_with_addr_gen.rs index a053c32f..3a5c51e8 100644 --- a/tests/test_wasm/test_with_addr_gen.rs +++ b/tests/test_wasm/test_with_addr_gen.rs @@ -10,7 +10,7 @@ fn contract_address_should_work() { // prepare application with custom API let mut app = AppBuilder::default() .with_api(MockApi::default().with_prefix("purple")) - .build(|_, _, _| {}); + .build_no_init(); // prepare user addresses let creator_addr = app.api().addr_make("creator"); @@ -65,9 +65,7 @@ fn custom_address_generator_should_work() { let wasm_keeper = WasmKeeper::new().with_address_generator(CustomAddressGenerator); // prepare application with custom wasm keeper - let mut app = AppBuilder::default() - .with_wasm(wasm_keeper) - .build(|_, _, _| {}); + let mut app = AppBuilder::default().with_wasm(wasm_keeper).build_no_init(); // prepare user addresses let owner_addr = app.api().addr_make("owner"); @@ -95,7 +93,7 @@ fn predictable_contract_address_should_work() { // prepare application with custom api let mut app = AppBuilder::default() .with_api(MockApi::default().with_prefix("juno")) - .build(|_, _, _| {}); + .build_no_init(); let creator = app.api().addr_make("creator"); @@ -148,7 +146,7 @@ fn creating_contract_with_the_same_predictable_address_should_fail() { // prepare application with custom api let mut app = AppBuilder::default() .with_api(MockApi::default().with_prefix("juno")) - .build(|_, _, _| {}); + .build_no_init(); let creator = app.api().addr_make("creator"); diff --git a/tests/test_wasm/test_with_checksum_gen.rs b/tests/test_wasm/test_with_checksum_gen.rs index 53f90e14..fad8e574 100644 --- a/tests/test_wasm/test_with_checksum_gen.rs +++ b/tests/test_wasm/test_with_checksum_gen.rs @@ -38,9 +38,7 @@ fn custom_checksum_generator_should_work() { let wasm_keeper = WasmKeeper::new().with_checksum_generator(MyChecksumGenerator); // prepare application with custom wasm keeper - let mut app = AppBuilder::default() - .with_wasm(wasm_keeper) - .build(|_, _, _| {}); + let mut app = AppBuilder::default().with_wasm(wasm_keeper).build_no_init(); // prepare user addresses let creator_addr = app.api().addr_make("creator"); From 1f537347beceaaad08646d4a91c85b95c9ba65ec Mon Sep 17 00:00:00 2001 From: Dariusz Depta Date: Wed, 19 Jun 2024 14:25:16 +0200 Subject: [PATCH 07/15] Updates. --- tests/test_bank/test_init_balance.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/test_bank/test_init_balance.rs b/tests/test_bank/test_init_balance.rs index dcc8eaec..45faec0f 100644 --- a/tests/test_bank/test_init_balance.rs +++ b/tests/test_bank/test_init_balance.rs @@ -15,7 +15,7 @@ fn initializing_balance_should_work() { &api.addr_make(USER), vec![Coin { denom: DENOM.to_string(), - amount: Uint128::new(100), + amount: Uint128::new(AMOUNT), }], ) .unwrap(); From eeb7336c6b18afc7eaf80a0731269b1891b1d312 Mon Sep 17 00:00:00 2001 From: Dariusz Depta Date: Wed, 19 Jun 2024 15:48:55 +0200 Subject: [PATCH 08/15] Restored no_init function. --- src/app.rs | 11 +++++++- src/app_builder.rs | 25 ++----------------- src/lib.rs | 4 ++- src/tests/test_app.rs | 3 ++- src/tests/test_gov.rs | 4 +-- src/tests/test_ibc.rs | 4 +-- src/tests/test_stargate.rs | 4 +-- src/wasm.rs | 12 ++++----- tests/test_app/test_instantiate2.rs | 8 +++--- .../test_app/test_store_code_with_creator.rs | 4 +-- tests/test_app_builder/test_with_api.rs | 4 +-- tests/test_app_builder/test_with_bank.rs | 4 +-- tests/test_app_builder/test_with_block.rs | 4 +-- .../test_with_distribution.rs | 4 +-- tests/test_app_builder/test_with_gov.rs | 4 +-- tests/test_app_builder/test_with_ibc.rs | 4 +-- tests/test_app_builder/test_with_staking.rs | 4 +-- tests/test_app_builder/test_with_stargate.rs | 15 +++++------ tests/test_app_builder/test_with_storage.rs | 4 +-- tests/test_app_builder/test_with_wasm.rs | 8 +++--- tests/test_wasm/test_with_addr_gen.rs | 10 ++++---- tests/test_wasm/test_with_checksum_gen.rs | 4 +-- 22 files changed, 70 insertions(+), 78 deletions(-) diff --git a/src/app.rs b/src/app.rs index b698bd51..03b639bd 100644 --- a/src/app.rs +++ b/src/app.rs @@ -66,9 +66,18 @@ pub struct App< pub(crate) block: BlockInfo, } +/// No-op application initialization function. +pub fn no_init( + router: &mut Router, + api: &impl Api, + storage: &mut dyn Storage, +) { + let _ = (router, api, storage); +} + impl Default for BasicApp { fn default() -> Self { - Self::new(|_, _, _| {}) + Self::new(no_init) } } diff --git a/src/app_builder.rs b/src/app_builder.rs index 5a7f6624..a84619ce 100644 --- a/src/app_builder.rs +++ b/src/app_builder.rs @@ -16,14 +16,14 @@ use std::fmt::Debug; /// /// ``` /// # use cosmwasm_std::Empty; -/// # use cw_multi_test::{BasicAppBuilder, FailingModule, Module}; +/// # use cw_multi_test::{no_init, BasicAppBuilder, FailingModule, Module}; /// # type MyHandler = FailingModule; /// # type MyExecC = Empty; /// # type MyQueryC = Empty; /// /// let mut app = BasicAppBuilder::::new_custom() /// .with_custom(MyHandler::default()) -/// .build_no_init(); +/// .build(no_init); /// ``` /// This type alias is crucial for constructing a custom app with specific modules. /// It provides a streamlined approach to building and configuring an App tailored to @@ -574,25 +574,4 @@ where storage, } } - - /// Builds the final [App] without initialization. - /// - /// At this point all component types have to be properly related to each other. - pub fn build_no_init( - self, - ) -> App - where - BankT: Bank, - ApiT: Api, - StorageT: Storage, - CustomT: Module, - WasmT: Wasm, - StakingT: Staking, - DistrT: Distribution, - IbcT: Ibc, - GovT: Gov, - StargateT: Stargate, - { - self.build(|_, _, _| {}) - } } diff --git a/src/lib.rs b/src/lib.rs index be66a675..e5d3f24c 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -149,7 +149,9 @@ pub use crate::addresses::{ AddressGenerator, IntoAddr, IntoBech32, IntoBech32m, SimpleAddressGenerator, }; pub use crate::api::{MockApiBech32, MockApiBech32m}; -pub use crate::app::{custom_app, next_block, App, BasicApp, CosmosRouter, Router, SudoMsg}; +pub use crate::app::{ + custom_app, next_block, no_init, App, BasicApp, CosmosRouter, Router, SudoMsg, +}; pub use crate::app_builder::{AppBuilder, BasicAppBuilder}; pub use crate::bank::{Bank, BankKeeper, BankSudo}; pub use crate::checksums::ChecksumGenerator; diff --git a/src/tests/test_app.rs b/src/tests/test_app.rs index 21ec60b0..5aade9c6 100644 --- a/src/tests/test_app.rs +++ b/src/tests/test_app.rs @@ -1652,6 +1652,7 @@ mod wasm_queries { mod custom_messages { use super::*; + use crate::no_init; #[test] fn triggering_custom_msg() { @@ -1660,7 +1661,7 @@ mod custom_messages { let mut app = AppBuilder::new_custom() .with_custom(custom_handler) - .build_no_init(); + .build(no_init); let sender = app.api().addr_make("sender"); let owner = app.api().addr_make("owner"); diff --git a/src/tests/test_gov.rs b/src/tests/test_gov.rs index 07db5899..68897735 100644 --- a/src/tests/test_gov.rs +++ b/src/tests/test_gov.rs @@ -1,5 +1,5 @@ use crate::test_helpers::gov; -use crate::{App, AppBuilder, Executor, GovAcceptingModule}; +use crate::{no_init, App, AppBuilder, Executor, GovAcceptingModule}; use cosmwasm_std::Empty; #[test] @@ -22,7 +22,7 @@ fn default_gov() { fn accepting_gov() { let mut app = AppBuilder::new() .with_gov(GovAcceptingModule::new()) - .build_no_init(); + .build(no_init); let creator_addr = app.api().addr_make("creator"); let code = app.store_code_with_creator(creator_addr, gov::contract()); diff --git a/src/tests/test_ibc.rs b/src/tests/test_ibc.rs index 2124cc31..ab70567c 100644 --- a/src/tests/test_ibc.rs +++ b/src/tests/test_ibc.rs @@ -1,5 +1,5 @@ use crate::test_helpers::ibc; -use crate::{App, AppBuilder, Executor, IbcAcceptingModule}; +use crate::{no_init, App, AppBuilder, Executor, IbcAcceptingModule}; use cosmwasm_std::Empty; #[test] @@ -22,7 +22,7 @@ fn default_ibc() { fn accepting_ibc() { let mut app = AppBuilder::default() .with_ibc(IbcAcceptingModule::new()) - .build_no_init(); + .build(no_init); let creator_addr = app.api().addr_make("creator"); let code = app.store_code_with_creator(creator_addr, ibc::contract()); diff --git a/src/tests/test_stargate.rs b/src/tests/test_stargate.rs index 2c9389a8..01f2f975 100644 --- a/src/tests/test_stargate.rs +++ b/src/tests/test_stargate.rs @@ -1,5 +1,5 @@ use crate::test_helpers::stargate; -use crate::{App, AppBuilder, Executor, StargateAccepting}; +use crate::{no_init, App, AppBuilder, Executor, StargateAccepting}; use cosmwasm_std::Empty; #[test] @@ -34,7 +34,7 @@ fn default_failing_stargate_handler_should_work() { fn accepting_stargate_handler_should_work() { let mut app = AppBuilder::default() .with_stargate(StargateAccepting) - .build_no_init(); + .build(no_init); // store the contract let creator_addr = app.api().addr_make("creator"); diff --git a/src/wasm.rs b/src/wasm.rs index 583de3ef..b92a8d18 100644 --- a/src/wasm.rs +++ b/src/wasm.rs @@ -433,13 +433,13 @@ where /// # Example /// /// ``` - /// use cw_multi_test::{AppBuilder, WasmKeeper}; + /// use cw_multi_test::{no_init, AppBuilder, WasmKeeper}; /// /// // create wasm keeper /// let wasm_keeper = WasmKeeper::new(); /// /// // create and use the application with newly created wasm keeper - /// let mut app = AppBuilder::default().with_wasm(wasm_keeper).build_no_init(); + /// let mut app = AppBuilder::default().with_wasm(wasm_keeper).build(no_init); /// ``` pub fn new() -> Self { Self::default() @@ -451,7 +451,7 @@ where /// /// ``` /// use cosmwasm_std::{Addr, Api, Storage}; - /// use cw_multi_test::{AddressGenerator, AppBuilder, WasmKeeper}; + /// use cw_multi_test::{no_init, AddressGenerator, AppBuilder, WasmKeeper}; /// use cw_multi_test::error::AnyResult; /// # use cosmwasm_std::testing::MockApi; /// @@ -474,7 +474,7 @@ where /// let wasm_keeper = WasmKeeper::new().with_address_generator(CustomAddressGenerator); /// /// // create and use the application with customized wasm keeper - /// let mut app = AppBuilder::default().with_wasm(wasm_keeper).build_no_init(); + /// let mut app = AppBuilder::default().with_wasm(wasm_keeper).build(no_init); /// ``` pub fn with_address_generator( mut self, @@ -490,7 +490,7 @@ where /// /// ``` /// use cosmwasm_std::{Addr, Checksum}; - /// use cw_multi_test::{AppBuilder, ChecksumGenerator, WasmKeeper}; + /// use cw_multi_test::{no_init, AppBuilder, ChecksumGenerator, WasmKeeper}; /// /// struct MyChecksumGenerator; /// @@ -505,7 +505,7 @@ where /// let wasm_keeper = WasmKeeper::new().with_checksum_generator(MyChecksumGenerator); /// /// // create and use the application with customized wasm keeper - /// let mut app = AppBuilder::default().with_wasm(wasm_keeper).build_no_init(); + /// let mut app = AppBuilder::default().with_wasm(wasm_keeper).build(no_init); /// ``` pub fn with_checksum_generator( mut self, diff --git a/tests/test_app/test_instantiate2.rs b/tests/test_app/test_instantiate2.rs index 1c125aa9..95fc8eed 100644 --- a/tests/test_app/test_instantiate2.rs +++ b/tests/test_app/test_instantiate2.rs @@ -1,7 +1,7 @@ use crate::test_contracts::counter; use cosmwasm_std::testing::MockApi; use cosmwasm_std::{instantiate2_address, to_json_binary, Api, Empty, WasmMsg}; -use cw_multi_test::{AppBuilder, Executor}; +use cw_multi_test::{no_init, AppBuilder, Executor}; use cw_utils::parse_instantiate_response_data; #[test] @@ -9,7 +9,7 @@ fn instantiate2_works() { // prepare the application with custom Api and custom address generator let mut app = AppBuilder::default() .with_api(MockApi::default().with_prefix("juno")) - .build_no_init(); + .build(no_init); // prepare addresses for sender and creator let sender = app.api().addr_make("sender"); @@ -73,7 +73,7 @@ fn instantiate2_should_work_for_multiple_salts() { // prepare the application with custom Api and custom address generator let mut app = AppBuilder::default() .with_api(MockApi::default().with_prefix("juno")) - .build_no_init(); + .build(no_init); // prepare addresses for sender and creator let sender = app.api().addr_make("sender"); @@ -106,7 +106,7 @@ fn instantiate2_fails_for_duplicated_addresses() { // prepare the application with custom Api and custom address generator let mut app = AppBuilder::default() .with_api(MockApi::default().with_prefix("osmo")) - .build_no_init(); + .build(no_init); // prepare addresses for sender and creator let sender = app.api().addr_make("sender"); diff --git a/tests/test_app/test_store_code_with_creator.rs b/tests/test_app/test_store_code_with_creator.rs index fcb777f8..73d469dc 100644 --- a/tests/test_app/test_store_code_with_creator.rs +++ b/tests/test_app/test_store_code_with_creator.rs @@ -1,5 +1,5 @@ use crate::test_contracts::counter; -use cw_multi_test::AppBuilder; +use cw_multi_test::{no_init, AppBuilder}; use cw_multi_test::{MockApiBech32, MockApiBech32m}; #[test] @@ -7,7 +7,7 @@ fn store_code_with_custom_creator_address_should_work() { // prepare the application let mut app = AppBuilder::default() .with_api(MockApiBech32m::new("juno")) - .build_no_init(); + .build(no_init); let creator = app.api().addr_make("zeus"); diff --git a/tests/test_app_builder/test_with_api.rs b/tests/test_app_builder/test_with_api.rs index 3fe01bab..91ee4de9 100644 --- a/tests/test_app_builder/test_with_api.rs +++ b/tests/test_app_builder/test_with_api.rs @@ -1,6 +1,6 @@ use cosmwasm_std::testing::MockApi; use cosmwasm_std::{Api, CanonicalAddr, HexBinary}; -use cw_multi_test::AppBuilder; +use cw_multi_test::{no_init, AppBuilder}; #[test] fn building_app_with_custom_api_should_work() { @@ -12,7 +12,7 @@ fn building_app_with_custom_api_should_work() { // Bech32 address encoding with 'juno' prefix let app = AppBuilder::default() .with_api(MockApi::default().with_prefix("juno")) - .build_no_init(); + .build(no_init); // check address validation function assert_eq!(human, app.api().addr_validate(human).unwrap().as_str()); diff --git a/tests/test_app_builder/test_with_bank.rs b/tests/test_app_builder/test_with_bank.rs index 54cf6461..a6b00f4a 100644 --- a/tests/test_app_builder/test_with_bank.rs +++ b/tests/test_app_builder/test_with_bank.rs @@ -1,6 +1,6 @@ use crate::test_app_builder::MyKeeper; use cosmwasm_std::{coins, BankMsg, BankQuery}; -use cw_multi_test::{AppBuilder, Bank, BankSudo, Executor}; +use cw_multi_test::{no_init, AppBuilder, Bank, BankSudo, Executor}; type MyBankKeeper = MyKeeper; @@ -16,7 +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 mut app = AppBuilder::default().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"); diff --git a/tests/test_app_builder/test_with_block.rs b/tests/test_app_builder/test_with_block.rs index 6fc08b73..35faed2b 100644 --- a/tests/test_app_builder/test_with_block.rs +++ b/tests/test_app_builder/test_with_block.rs @@ -1,5 +1,5 @@ use cosmwasm_std::{BlockInfo, Timestamp}; -use cw_multi_test::AppBuilder; +use cw_multi_test::{no_init, AppBuilder}; #[test] fn building_app_with_custom_block_should_work() { @@ -12,7 +12,7 @@ fn building_app_with_custom_block_should_work() { // build the application with custom block let app_builder = AppBuilder::default(); - let app = app_builder.with_block(block_info.clone()).build_no_init(); + let app = app_builder.with_block(block_info.clone()).build(no_init); // calling block_info should return the same block used during initialization assert_eq!(block_info, app.block_info()); diff --git a/tests/test_app_builder/test_with_distribution.rs b/tests/test_app_builder/test_with_distribution.rs index 46003adc..fe6a6f46 100644 --- a/tests/test_app_builder/test_with_distribution.rs +++ b/tests/test_app_builder/test_with_distribution.rs @@ -1,6 +1,6 @@ use crate::test_app_builder::{MyKeeper, NO_MESSAGE}; use cosmwasm_std::{DistributionMsg, Empty}; -use cw_multi_test::{AppBuilder, Distribution, Executor}; +use cw_multi_test::{no_init, AppBuilder, Distribution, Executor}; type MyDistributionKeeper = MyKeeper; @@ -18,7 +18,7 @@ fn building_app_with_custom_distribution_should_work() { let app_builder = AppBuilder::default(); let mut app = app_builder .with_distribution(distribution_keeper) - .build_no_init(); + .build(no_init); // prepare addresses let recipient_addr = app.api().addr_make("recipient"); diff --git a/tests/test_app_builder/test_with_gov.rs b/tests/test_app_builder/test_with_gov.rs index de4109f9..f1f8e0fe 100644 --- a/tests/test_app_builder/test_with_gov.rs +++ b/tests/test_app_builder/test_with_gov.rs @@ -1,6 +1,6 @@ use crate::test_app_builder::{MyKeeper, NO_MESSAGE}; use cosmwasm_std::{Empty, GovMsg, VoteOption}; -use cw_multi_test::{AppBuilder, Executor, Gov}; +use cw_multi_test::{no_init, AppBuilder, Executor, Gov}; type MyGovKeeper = MyKeeper; @@ -14,7 +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 mut app = AppBuilder::default().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"); diff --git a/tests/test_app_builder/test_with_ibc.rs b/tests/test_app_builder/test_with_ibc.rs index cf088069..f9a77ba3 100644 --- a/tests/test_app_builder/test_with_ibc.rs +++ b/tests/test_app_builder/test_with_ibc.rs @@ -1,6 +1,6 @@ use crate::test_app_builder::{MyKeeper, NO_MESSAGE}; use cosmwasm_std::{Empty, IbcMsg, IbcQuery, QueryRequest}; -use cw_multi_test::{AppBuilder, Executor, Ibc}; +use cw_multi_test::{no_init, AppBuilder, Executor, Ibc}; type MyIbcKeeper = MyKeeper; @@ -15,7 +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 mut app = AppBuilder::default().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"); diff --git a/tests/test_app_builder/test_with_staking.rs b/tests/test_app_builder/test_with_staking.rs index a75fc979..3f49b1d4 100644 --- a/tests/test_app_builder/test_with_staking.rs +++ b/tests/test_app_builder/test_with_staking.rs @@ -1,6 +1,6 @@ use crate::test_app_builder::MyKeeper; use cosmwasm_std::{Coin, StakingMsg, StakingQuery}; -use cw_multi_test::{AppBuilder, Executor, Staking, StakingSudo}; +use cw_multi_test::{no_init, AppBuilder, Executor, Staking, StakingSudo}; type MyStakeKeeper = MyKeeper; @@ -18,7 +18,7 @@ fn building_app_with_custom_staking_should_work() { // build the application with custom stake keeper let mut app = AppBuilder::default() .with_staking(stake_keeper) - .build_no_init(); + .build(no_init); // prepare addresses let validator_addr = app.api().addr_make("validator"); diff --git a/tests/test_app_builder/test_with_stargate.rs b/tests/test_app_builder/test_with_stargate.rs index 331016dd..3bb09844 100644 --- a/tests/test_app_builder/test_with_stargate.rs +++ b/tests/test_app_builder/test_with_stargate.rs @@ -5,7 +5,8 @@ use cosmwasm_std::{ }; use cw_multi_test::error::AnyResult; use cw_multi_test::{ - AppBuilder, AppResponse, CosmosRouter, Executor, Stargate, StargateAccepting, StargateFailing, + no_init, AppBuilder, AppResponse, CosmosRouter, Executor, Stargate, StargateAccepting, + StargateFailing, }; use serde::de::DeserializeOwned; @@ -78,7 +79,7 @@ impl Stargate for StargateKeeper { fn building_app_with_custom_stargate_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 = app_builder.with_stargate(StargateKeeper).build(no_init); // prepare user addresses let sender_addr = app.api().addr_make("sender"); @@ -114,7 +115,7 @@ fn building_app_with_custom_any_grpc_should_work() { // build the application with custom stargate keeper let mut app = AppBuilder::default() .with_stargate(StargateKeeper) - .build_no_init(); + .build(no_init); // prepare user addresses let sender_addr = app.api().addr_make("sender"); @@ -146,7 +147,7 @@ fn building_app_with_custom_any_grpc_should_work() { fn building_app_with_accepting_stargate_should_work() { let mut app = AppBuilder::default() .with_stargate(StargateAccepting) - .build_no_init(); + .build(no_init); // prepare user addresses let sender_addr = app.api().addr_make("sender"); @@ -174,7 +175,7 @@ fn building_app_with_accepting_stargate_should_work() { #[cfg(feature = "cosmwasm_2_0")] fn building_app_with_accepting_any_grpc_should_work() { let app_builder = AppBuilder::default(); - let mut app = app_builder.with_stargate(StargateAccepting).build_no_init(); + let mut app = app_builder.with_stargate(StargateAccepting).build(no_init); // prepare user addresses let sender_addr = app.api().addr_make("sender"); @@ -207,7 +208,7 @@ fn building_app_with_accepting_any_grpc_should_work() { #[test] fn default_failing_stargate_should_work() { let app_builder = AppBuilder::default(); - let mut app = app_builder.with_stargate(StargateFailing).build_no_init(); + let mut app = app_builder.with_stargate(StargateFailing).build(no_init); // prepare user addresses let sender_addr = app.api().addr_make("sender"); @@ -241,7 +242,7 @@ fn default_failing_stargate_should_work() { fn default_failing_any_grpc_should_work() { let mut app = AppBuilder::default() .with_stargate(StargateFailing) - .build_no_init(); + .build(no_init); // prepare user addresses let sender_addr = app.api().addr_make("sender"); diff --git a/tests/test_app_builder/test_with_storage.rs b/tests/test_app_builder/test_with_storage.rs index f2ece2d1..6adf6909 100644 --- a/tests/test_app_builder/test_with_storage.rs +++ b/tests/test_app_builder/test_with_storage.rs @@ -1,7 +1,7 @@ use crate::test_contracts; use crate::test_contracts::counter::{CounterQueryMsg, CounterResponseMsg}; use cosmwasm_std::{to_json_binary, Empty, Order, Record, Storage, WasmMsg}; -use cw_multi_test::{AppBuilder, Executor}; +use cw_multi_test::{no_init, AppBuilder, Executor}; use std::collections::BTreeMap; use std::iter; @@ -43,7 +43,7 @@ fn building_app_with_custom_storage_should_work() { // build the application with custom storage let mut app = AppBuilder::default() .with_storage(MyStorage::default()) - .build_no_init(); + .build(no_init); // prepare user addresses let owner_addr = app.api().addr_make("owner"); diff --git a/tests/test_app_builder/test_with_wasm.rs b/tests/test_app_builder/test_with_wasm.rs index a5b24383..258894c2 100644 --- a/tests/test_app_builder/test_with_wasm.rs +++ b/tests/test_app_builder/test_with_wasm.rs @@ -5,8 +5,8 @@ use cosmwasm_std::{ }; use cw_multi_test::error::{bail, AnyResult}; use cw_multi_test::{ - AppBuilder, AppResponse, Contract, ContractData, CosmosRouter, Executor, Wasm, WasmKeeper, - WasmSudo, + no_init, AppBuilder, AppResponse, Contract, ContractData, CosmosRouter, Executor, Wasm, + WasmKeeper, WasmSudo, }; use once_cell::sync::Lazy; @@ -91,7 +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 mut app = AppBuilder::default().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"); @@ -163,5 +163,5 @@ fn compiling_with_wasm_keeper_should_work() { // while our WasmKeeper does not implement Module let _ = AppBuilder::default() .with_wasm(WasmKeeper::default()) - .build_no_init(); + .build(no_init); } diff --git a/tests/test_wasm/test_with_addr_gen.rs b/tests/test_wasm/test_with_addr_gen.rs index 3a5c51e8..4ec6c829 100644 --- a/tests/test_wasm/test_with_addr_gen.rs +++ b/tests/test_wasm/test_with_addr_gen.rs @@ -1,7 +1,7 @@ use cosmwasm_std::testing::MockApi; use cosmwasm_std::{Addr, Api, Empty, Storage}; use cw_multi_test::error::AnyResult; -use cw_multi_test::{AddressGenerator, AppBuilder, Executor, WasmKeeper}; +use cw_multi_test::{no_init, AddressGenerator, AppBuilder, Executor, WasmKeeper}; use crate::test_contracts; @@ -10,7 +10,7 @@ fn contract_address_should_work() { // prepare application with custom API let mut app = AppBuilder::default() .with_api(MockApi::default().with_prefix("purple")) - .build_no_init(); + .build(no_init); // prepare user addresses let creator_addr = app.api().addr_make("creator"); @@ -65,7 +65,7 @@ fn custom_address_generator_should_work() { let wasm_keeper = WasmKeeper::new().with_address_generator(CustomAddressGenerator); // prepare application with custom wasm keeper - let mut app = AppBuilder::default().with_wasm(wasm_keeper).build_no_init(); + let mut app = AppBuilder::default().with_wasm(wasm_keeper).build(no_init); // prepare user addresses let owner_addr = app.api().addr_make("owner"); @@ -93,7 +93,7 @@ fn predictable_contract_address_should_work() { // prepare application with custom api let mut app = AppBuilder::default() .with_api(MockApi::default().with_prefix("juno")) - .build_no_init(); + .build(no_init); let creator = app.api().addr_make("creator"); @@ -146,7 +146,7 @@ fn creating_contract_with_the_same_predictable_address_should_fail() { // prepare application with custom api let mut app = AppBuilder::default() .with_api(MockApi::default().with_prefix("juno")) - .build_no_init(); + .build(no_init); let creator = app.api().addr_make("creator"); diff --git a/tests/test_wasm/test_with_checksum_gen.rs b/tests/test_wasm/test_with_checksum_gen.rs index fad8e574..47685d31 100644 --- a/tests/test_wasm/test_with_checksum_gen.rs +++ b/tests/test_wasm/test_with_checksum_gen.rs @@ -1,6 +1,6 @@ use crate::test_contracts; use cosmwasm_std::{Addr, Checksum}; -use cw_multi_test::{App, AppBuilder, ChecksumGenerator, WasmKeeper}; +use cw_multi_test::{no_init, App, AppBuilder, ChecksumGenerator, WasmKeeper}; #[test] fn default_checksum_generator_should_work() { @@ -38,7 +38,7 @@ fn custom_checksum_generator_should_work() { let wasm_keeper = WasmKeeper::new().with_checksum_generator(MyChecksumGenerator); // prepare application with custom wasm keeper - let mut app = AppBuilder::default().with_wasm(wasm_keeper).build_no_init(); + let mut app = AppBuilder::default().with_wasm(wasm_keeper).build(no_init); // prepare user addresses let creator_addr = app.api().addr_make("creator"); From 35bdfc1e7c47d35c62300562ba5c3d1de6a1a856 Mon Sep 17 00:00:00 2001 From: Dariusz Depta Date: Wed, 19 Jun 2024 15:55:46 +0200 Subject: [PATCH 09/15] Fixed no_init with additional generic type. --- src/app.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/app.rs b/src/app.rs index 03b639bd..cacc564f 100644 --- a/src/app.rs +++ b/src/app.rs @@ -67,9 +67,9 @@ pub struct App< } /// No-op application initialization function. -pub fn no_init( +pub fn no_init( router: &mut Router, - api: &impl Api, + api: &ApiT, storage: &mut dyn Storage, ) { let _ = (router, api, storage); From f79465967d0e7d7181d89d1aa32abb236d9f9bca Mon Sep 17 00:00:00 2001 From: Dariusz Depta Date: Wed, 19 Jun 2024 15:58:27 +0200 Subject: [PATCH 10/15] Reverted some empty closures. --- src/tests/test_app.rs | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/src/tests/test_app.rs b/src/tests/test_app.rs index 5aade9c6..5f5d4851 100644 --- a/src/tests/test_app.rs +++ b/src/tests/test_app.rs @@ -1696,7 +1696,7 @@ mod custom_messages { mod protobuf_wrapped_data { use super::*; - use crate::BasicApp; + use crate::{no_init, BasicApp}; #[test] fn instantiate_wrapped_properly() { @@ -1739,7 +1739,7 @@ mod protobuf_wrapped_data { #[test] fn instantiate_with_data_works() { - let mut app = BasicApp::new(|_, _, _| {}); + let mut app = BasicApp::new(no_init); let owner = app.api().addr_make("owner"); @@ -1769,7 +1769,7 @@ mod protobuf_wrapped_data { #[test] fn instantiate_with_reply_works() { - let mut app = BasicApp::new(|_, _, _| {}); + let mut app = BasicApp::new(no_init); let owner = app.api().addr_make("owner"); @@ -1822,7 +1822,7 @@ mod protobuf_wrapped_data { #[test] fn execute_wrapped_properly() { - let mut app = BasicApp::new(|_, _, _| {}); + let mut app = BasicApp::new(no_init); let owner = app.api().addr_make("owner"); @@ -1833,7 +1833,7 @@ mod protobuf_wrapped_data { .instantiate_contract(code_id, owner.clone(), &Empty {}, &[], "label", None) .unwrap(); - // ensure the execute has the same wrapper as it should + // ensure that execute has the same wrapper as it should let msg = echo::Message:: { data: Some("hello".into()), ..echo::Message::default() From b9e3b8c83ed6e027e95dbfd9848c5dfbb4c87f7a Mon Sep 17 00:00:00 2001 From: Dariusz Depta Date: Wed, 19 Jun 2024 16:00:23 +0200 Subject: [PATCH 11/15] Reverted some unnecessary changes. --- src/tests/test_app.rs | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) diff --git a/src/tests/test_app.rs b/src/tests/test_app.rs index 5f5d4851..2c459e76 100644 --- a/src/tests/test_app.rs +++ b/src/tests/test_app.rs @@ -5,8 +5,8 @@ use crate::test_helpers::{caller, echo, error, hackatom, payout, reflect, Custom use crate::transactions::{transactional, StorageTransaction}; use crate::wasm::ContractData; use crate::{ - custom_app, next_block, App, AppResponse, Bank, CosmosRouter, Distribution, Executor, Module, - Router, Staking, Wasm, WasmSudo, + custom_app, next_block, no_init, App, AppResponse, Bank, CosmosRouter, Distribution, Executor, + Module, Router, Staking, Wasm, WasmSudo, }; use crate::{AppBuilder, IntoAddr}; use cosmwasm_std::testing::{mock_env, MockQuerier}; @@ -1652,7 +1652,6 @@ mod wasm_queries { mod custom_messages { use super::*; - use crate::no_init; #[test] fn triggering_custom_msg() { @@ -1696,7 +1695,7 @@ mod custom_messages { mod protobuf_wrapped_data { use super::*; - use crate::{no_init, BasicApp}; + use crate::BasicApp; #[test] fn instantiate_wrapped_properly() { @@ -1833,7 +1832,7 @@ mod protobuf_wrapped_data { .instantiate_contract(code_id, owner.clone(), &Empty {}, &[], "label", None) .unwrap(); - // ensure that execute has the same wrapper as it should + // ensure the execute has the same wrapper as it should let msg = echo::Message:: { data: Some("hello".into()), ..echo::Message::default() From 5008b042d40c9aea76bcfa657f51c87d12001a8d Mon Sep 17 00:00:00 2001 From: Dariusz Depta Date: Wed, 19 Jun 2024 16:49:20 +0200 Subject: [PATCH 12/15] Added tests. --- src/app.rs | 4 +- tests/test_app/mod.rs | 1 + tests/test_app/test_initialize_app.rs | 21 ++++++ tests/test_bank/test_init_balance.rs | 105 +++++++++++++++++++++++--- 4 files changed, 120 insertions(+), 11 deletions(-) create mode 100644 tests/test_app/test_initialize_app.rs diff --git a/src/app.rs b/src/app.rs index cacc564f..19f091c2 100644 --- a/src/app.rs +++ b/src/app.rs @@ -216,7 +216,7 @@ where where F: FnOnce( &mut Router, - &dyn Api, + &ApiT, &mut dyn Storage, ) -> T, { @@ -228,7 +228,7 @@ where where F: FnOnce( &Router, - &dyn Api, + &ApiT, &dyn Storage, ) -> T, { diff --git a/tests/test_app/mod.rs b/tests/test_app/mod.rs index 6592434b..00601b0d 100644 --- a/tests/test_app/mod.rs +++ b/tests/test_app/mod.rs @@ -1,4 +1,5 @@ mod test_block_info; +mod test_initialize_app; #[cfg(feature = "cosmwasm_1_2")] mod test_instantiate2; mod test_store_code; diff --git a/tests/test_app/test_initialize_app.rs b/tests/test_app/test_initialize_app.rs new file mode 100644 index 00000000..977d9947 --- /dev/null +++ b/tests/test_app/test_initialize_app.rs @@ -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); +} diff --git a/tests/test_bank/test_init_balance.rs b/tests/test_bank/test_init_balance.rs index 45faec0f..c5a478a9 100644 --- a/tests/test_bank/test_init_balance.rs +++ b/tests/test_bank/test_init_balance.rs @@ -1,10 +1,18 @@ -use cosmwasm_std::{Coin, Uint128}; -use cw_multi_test::AppBuilder; +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) { + assert_eq!(1, coins.len()); + assert_eq!(AMOUNT, coins[0].amount.u128()); + assert_eq!(DENOM, coins[0].denom); +} + #[test] fn initializing_balance_should_work() { let app = AppBuilder::new().build(|router, api, storage| { @@ -20,11 +28,90 @@ fn initializing_balance_should_work() { ) .unwrap(); }); - let balances = app - .wrap() - .query_all_balances(app.api().addr_make(USER)) - .unwrap(); - assert_eq!(1, balances.len()); - assert_eq!(AMOUNT, balances[0].amount.u128()); - assert_eq!(DENOM, balances[0].denom); + 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), + vec![Coin { + denom: DENOM.to_string(), + amount: Uint128::new(AMOUNT), + }], + ) + .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 = custom_app(|router, api, storage| { + router + .bank + .init_balance( + storage, + &api.addr_make(USER), + vec![Coin { + denom: DENOM.to_string(), + amount: Uint128::new(AMOUNT), + }], + ) + .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), + vec![Coin { + denom: DENOM.to_string(), + amount: Uint128::new(AMOUNT), + }], + ) + .unwrap(); + }); + assert_balance( + app.wrap() + .query_all_balances(app.api().addr_make(USER)) + .unwrap(), + ); } From cf1c34a4ded119ab1b7f2ceea05c6ca18115234e Mon Sep 17 00:00:00 2001 From: Dariusz Depta Date: Wed, 19 Jun 2024 16:52:10 +0200 Subject: [PATCH 13/15] Refactored tests. --- tests/test_bank/test_init_balance.rs | 43 +++++++--------------------- 1 file changed, 11 insertions(+), 32 deletions(-) diff --git a/tests/test_bank/test_init_balance.rs b/tests/test_bank/test_init_balance.rs index c5a478a9..b312b22d 100644 --- a/tests/test_bank/test_init_balance.rs +++ b/tests/test_bank/test_init_balance.rs @@ -13,19 +13,19 @@ fn assert_balance(coins: Vec) { assert_eq!(DENOM, coins[0].denom); } +fn coins() -> Vec { + 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), - vec![Coin { - denom: DENOM.to_string(), - amount: Uint128::new(AMOUNT), - }], - ) + .init_balance(storage, &api.addr_make(USER), coins()) .unwrap(); }); assert_balance( @@ -40,14 +40,7 @@ fn initializing_balance_without_builder_should_work() { let app = App::new(|router, api, storage| { router .bank - .init_balance( - storage, - &api.addr_make(USER), - vec![Coin { - denom: DENOM.to_string(), - amount: Uint128::new(AMOUNT), - }], - ) + .init_balance(storage, &api.addr_make(USER), coins()) .unwrap(); }); assert_balance( @@ -76,14 +69,7 @@ fn initializing_balance_custom_app_should_work() { let app: BasicApp = custom_app(|router, api, storage| { router .bank - .init_balance( - storage, - &api.addr_make(USER), - vec![Coin { - denom: DENOM.to_string(), - amount: Uint128::new(AMOUNT), - }], - ) + .init_balance(storage, &api.addr_make(USER), coins()) .unwrap(); }); assert_balance( @@ -99,14 +85,7 @@ fn initializing_balance_later_should_work() { app.init_modules(|router, api, storage| { router .bank - .init_balance( - storage, - &api.addr_make(USER), - vec![Coin { - denom: DENOM.to_string(), - amount: Uint128::new(AMOUNT), - }], - ) + .init_balance(storage, &api.addr_make(USER), coins()) .unwrap(); }); assert_balance( From 1e51965ecd7939ee89ed50c2dcd8e938e89a16ba Mon Sep 17 00:00:00 2001 From: Dariusz Depta Date: Thu, 20 Jun 2024 10:20:09 +0200 Subject: [PATCH 14/15] Made build function look nicer. --- src/app_builder.rs | 37 +++++++++++++++++++------------------ 1 file changed, 19 insertions(+), 18 deletions(-) diff --git a/src/app_builder.rs b/src/app_builder.rs index a84619ce..7d49815d 100644 --- a/src/app_builder.rs +++ b/src/app_builder.rs @@ -554,24 +554,25 @@ where &mut dyn Storage, ), { - let mut router = Router { - wasm: self.wasm, - bank: self.bank, - custom: self.custom, - staking: self.staking, - distribution: self.distribution, - ibc: self.ibc, - gov: self.gov, - stargate: self.stargate, - }; - let api = self.api; - let mut storage = self.storage; - init_fn(&mut router, &api, &mut storage); - App { - router, - api, + // build the final application + let mut app = App { + router: Router { + wasm: self.wasm, + bank: self.bank, + custom: self.custom, + staking: self.staking, + distribution: self.distribution, + ibc: self.ibc, + gov: self.gov, + stargate: self.stargate, + }, + api: self.api, block: self.block, - storage, - } + storage: self.storage, + }; + // execute initialization provided by the caller + app.init_modules(init_fn); + // return already initialized application + app } } From 34dadc0ca3096679fc2b4bcc4108d871207cbb6b Mon Sep 17 00:00:00 2001 From: Dariusz Depta Date: Thu, 20 Jun 2024 10:34:18 +0200 Subject: [PATCH 15/15] Placed ApiT at the beginning. --- Cargo.lock | 4 ++-- src/app.rs | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index eb015e47..ecd78733 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -789,9 +789,9 @@ checksum = "a2eb9349b6444b326872e140eb1cf5e7c522154d69e7a0ffb0fb81c06b37543f" [[package]] name = "subtle" -version = "2.5.0" +version = "2.6.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "81cdd64d312baedb58e21336b31bc043b77e01cc99033ce76ef539f78e965ebc" +checksum = "0d0208408ba0c3df17ed26eb06992cb1a1268d41b2c0e12e65203fbe3972cee5" [[package]] name = "syn" diff --git a/src/app.rs b/src/app.rs index 19f091c2..b0798d2d 100644 --- a/src/app.rs +++ b/src/app.rs @@ -67,7 +67,7 @@ pub struct App< } /// No-op application initialization function. -pub fn no_init( +pub fn no_init( router: &mut Router, api: &ApiT, storage: &mut dyn Storage,