Skip to content

Commit

Permalink
feat: multitests does not need #[contract(module=...)]
Browse files Browse the repository at this point in the history
  • Loading branch information
kulikthebird committed Mar 5, 2024
1 parent 37cc1dd commit 57a63a9
Show file tree
Hide file tree
Showing 33 changed files with 307 additions and 449 deletions.
3 changes: 2 additions & 1 deletion examples/contracts/custom/src/multitest/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,10 @@ use crate::contract::sv::multitest_utils::CodeId;

use super::custom_module::CustomModule;

use crate::cw1::sv::test_utils::Cw1;
use crate::contract::sv::multitest_utils::ContractImpl;

use cosmwasm_std::CosmosMsg;
use cw1::sv::test_utils::Cw1Proxy;

#[test]
fn test_custom() {
Expand Down
4 changes: 3 additions & 1 deletion examples/contracts/cw1-subkeys/src/multitest/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@ use sylvia::multitest::App;
use crate::contract::sv::multitest_utils::CodeId;
use crate::contract::{CONTRACT_NAME, CONTRACT_VERSION};

use crate::contract::sv::multitest_utils::ContractImpl;
use cw1::sv::test_utils::Cw1Proxy;

const ATOM: &str = "atom";

/// Helper function for comparing vectors or another slice-like object as they would represent
Expand Down Expand Up @@ -370,7 +373,6 @@ mod permissions {
}

mod cw1_execute {
use crate::cw1::sv::test_utils::Cw1;
use cosmwasm_std::BankMsg;

use super::*;
Expand Down
5 changes: 3 additions & 2 deletions examples/contracts/cw1-whitelist/src/multitest.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,13 @@ mod test {
use whitelist::responses::AdminListResponse;

use crate::contract::sv::multitest_utils::CodeId;
use crate::cw1::sv::test_utils::Cw1;
use crate::error::ContractError;
use crate::whitelist::sv::test_utils::Whitelist;
use assert_matches::assert_matches;
use sylvia::multitest::App;

use cw1::sv::test_utils::Cw1Proxy;
use whitelist::sv::test_utils::WhitelistProxy;

#[test]
fn proxy_freeze_message() {
let app = App::default();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,15 @@ use cw_multi_test::next_block;
use cw_utils::Expiration;
use sylvia::multitest::App;

use crate::allowances::sv::test_utils::Cw20Allowances;
use crate::contract::sv::multitest_utils::CodeId;
use crate::contract::InstantiateMsgData;
use crate::error::ContractError;
use crate::multitest::receiver_contract::sv::multitest_utils::CodeId as ReceiverCodeId;
use crate::responses::Cw20Coin;

use crate::contract::sv::multitest_utils::ContractImpl;
use cw20_allowances::sv::test_utils::Cw20AllowancesProxy;

#[test]
fn increase_decrease_allowances() {
let app = App::default();
Expand Down
4 changes: 2 additions & 2 deletions examples/contracts/cw20-base/src/multitest/base_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,12 @@ use cw20_allowances::responses::{AllAllowancesResponse, SpenderAllowanceInfo};
use cw_utils::Expiration;
use sylvia::multitest::App;

use crate::allowances::sv::test_utils::Cw20Allowances;
use crate::contract::sv::multitest_utils::CodeId;
use crate::contract::sv::multitest_utils::{CodeId, ContractImpl};
use crate::contract::InstantiateMsgData;
use crate::error::ContractError;
use crate::multitest::receiver_contract::sv::multitest_utils::CodeId as ReceiverCodeId;
use crate::responses::{BalanceResponse, Cw20Coin, TokenInfoResponse};
use cw20_allowances::sv::test_utils::Cw20AllowancesProxy;

#[test]
fn basic() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use sylvia::multitest::App;
use crate::contract::sv::multitest_utils::CodeId;
use crate::contract::{InstantiateMarketingInfo, InstantiateMsgData};
use crate::error::ContractError;
use crate::marketing::sv::test_utils::Cw20Marketing;
use cw20_marketing::sv::test_utils::Cw20MarketingProxy;

const PNG_HEADER: [u8; 8] = [0x89, b'P', b'N', b'G', 0x0d, 0x0a, 0x1a, 0x0a];

Expand Down
4 changes: 2 additions & 2 deletions examples/contracts/cw20-base/src/multitest/minting_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@ use cosmwasm_std::{StdError, Uint128};
use cw20_minting::responses::MinterResponse;
use sylvia::multitest::App;

use crate::contract::sv::multitest_utils::CodeId;
use crate::contract::sv::multitest_utils::{CodeId, ContractImpl};
use crate::contract::InstantiateMsgData;
use crate::error::ContractError;
use crate::minting::sv::test_utils::Cw20Minting;
use crate::responses::{Cw20Coin, TokenInfoResponse};
use cw20_minting::sv::test_utils::Cw20MintingProxy;

#[test]
fn mintable() {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#[cfg(test)]
mod test {
use crate::contract::sv::multitest_utils::CodeId;
use crate::contract::sv::multitest_utils::{CodeId, ContractImpl};
use crate::contract::sv::{ContractExecMsg, ExecMsg};
use crate::messages::{CustomExecMsg, SudoMsg, UserExecMsg};
use cosmwasm_std::Addr;
Expand Down
1 change: 1 addition & 0 deletions examples/contracts/generic_contract/src/contract.rs
Original file line number Diff line number Diff line change
Expand Up @@ -189,6 +189,7 @@ where
#[cfg(test)]
mod tests {
use super::sv::multitest_utils::CodeId;
use crate::contract::sv::multitest_utils::ContractImpl;
use sylvia::multitest::App;
use sylvia::types::{SvCustomMsg, SvCustomQuery};

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -111,8 +111,8 @@ impl<

#[cfg(test)]
mod tests {
use super::sv::test_utils::CustomAndGeneric;
use crate::contract::sv::multitest_utils::CodeId;
use custom_and_generic::sv::test_utils::CustomAndGenericProxy;
use sylvia::multitest::App;
use sylvia::types::{SvCustomMsg, SvCustomQuery};

Expand Down
2 changes: 1 addition & 1 deletion examples/contracts/generic_contract/src/cw1.rs
Original file line number Diff line number Diff line change
Expand Up @@ -54,9 +54,9 @@ impl<

#[cfg(test)]
mod tests {
use super::sv::test_utils::Cw1;
use crate::contract::sv::multitest_utils::CodeId;
use cosmwasm_std::{CosmosMsg, Empty};
use cw1::sv::test_utils::Cw1Proxy;
use sylvia::multitest::App;
use sylvia::types::{SvCustomMsg, SvCustomQuery};

Expand Down
2 changes: 1 addition & 1 deletion examples/contracts/generic_contract/src/generic.rs
Original file line number Diff line number Diff line change
Expand Up @@ -123,9 +123,9 @@ where

#[cfg(test)]
mod tests {
use super::sv::test_utils::Generic;
use crate::contract::sv::multitest_utils::CodeId;
use cosmwasm_std::CosmosMsg;
use generic::sv::test_utils::GenericProxy;
use sylvia::multitest::App;
use sylvia::types::{SvCustomMsg, SvCustomQuery};

Expand Down
6 changes: 3 additions & 3 deletions examples/contracts/generic_iface_on_contract/src/contract.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,9 @@ mod tests {
use sylvia::types::{SvCustomMsg, SvCustomQuery};

use super::NonGenericContract;
use crate::custom_and_generic::sv::test_utils::CustomAndGeneric;
use crate::cw1::sv::test_utils::Cw1;
use crate::generic::sv::test_utils::Generic;
use custom_and_generic::sv::test_utils::CustomAndGenericProxy;
use cw1::sv::test_utils::Cw1Proxy;
use generic::sv::test_utils::GenericProxy;

#[test]
fn mt_helpers() {
Expand Down
1 change: 1 addition & 0 deletions examples/contracts/generics_forwarded/src/contract.rs
Original file line number Diff line number Diff line change
Expand Up @@ -198,6 +198,7 @@ where
#[cfg(test)]
mod tests {
use super::sv::multitest_utils::CodeId;
use crate::contract::sv::multitest_utils::ContractImpl;
use sylvia::multitest::App;
use sylvia::types::{SvCustomMsg, SvCustomQuery};

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -131,9 +131,9 @@ where

#[cfg(test)]
mod tests {
use super::sv::test_utils::CustomAndGeneric;
use crate::contract::sv::multitest_utils::CodeId;
use cosmwasm_std::CosmosMsg;
use custom_and_generic::sv::test_utils::CustomAndGenericProxy;
use sylvia::multitest::App;
use sylvia::types::{SvCustomMsg, SvCustomQuery};

Expand Down
2 changes: 1 addition & 1 deletion examples/contracts/generics_forwarded/src/cw1.rs
Original file line number Diff line number Diff line change
Expand Up @@ -76,9 +76,9 @@ where

#[cfg(test)]
mod tests {
use super::sv::test_utils::Cw1;
use crate::contract::sv::multitest_utils::CodeId;
use cosmwasm_std::{CosmosMsg, Empty};
use cw1::sv::test_utils::Cw1Proxy;
use sylvia::multitest::App;
use sylvia::types::{SvCustomMsg, SvCustomQuery};

Expand Down
2 changes: 1 addition & 1 deletion examples/contracts/generics_forwarded/src/generic.rs
Original file line number Diff line number Diff line change
Expand Up @@ -129,9 +129,9 @@ where

#[cfg(test)]
mod tests {
use super::sv::test_utils::Generic;
use crate::contract::sv::multitest_utils::CodeId;
use cosmwasm_std::CosmosMsg;
use generic::sv::test_utils::GenericProxy;
use sylvia::multitest::App;
use sylvia::types::{SvCustomMsg, SvCustomQuery};

Expand Down
39 changes: 1 addition & 38 deletions sylvia-derive/src/associated_types.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
use proc_macro2::{Ident, TokenStream};
use quote::quote;
use syn::{
parse_quote, ImplItem, ImplItemType, ItemImpl, ItemTrait, TraitItem, TraitItemType, Type,
WhereClause, WherePredicate,
};
use syn::{parse_quote, ItemTrait, TraitItem, TraitItemType, WhereClause, WherePredicate};

pub const ERROR_TYPE: &str = "Error";
pub const EXEC_TYPE: &str = "ExecC";
Expand Down Expand Up @@ -97,40 +94,6 @@ impl<'a> AssociatedTypes<'a> {
}
}

#[derive(Default)]
pub struct ImplAssociatedTypes<'a>(Vec<&'a ImplItemType>);

impl<'a> ImplAssociatedTypes<'a> {
pub fn new(source: &'a ItemImpl) -> Self {
let associated_types: Vec<_> = source
.items
.iter()
.filter_map(|item| match item {
ImplItem::Type(ty) => Some(ty),
_ => None,
})
.collect();

Self(associated_types)
}

pub fn as_types(&self) -> Vec<&Type> {
self.filtered().map(|associated| &associated.ty).collect()
}

pub fn as_item_types(&self) -> Vec<&ImplItemType> {
self.filtered().copied().collect()
}

pub fn filtered(&self) -> impl Iterator<Item = &&ImplItemType> {
self.0.iter().filter(|associated| {
!RESERVED_TYPES
.iter()
.any(|reserved| reserved == &associated.ident.to_string().as_str())
})
}
}

pub trait ItemType {
fn as_name(&self) -> &Ident;
fn as_where_predicate(&self) -> WherePredicate;
Expand Down
50 changes: 8 additions & 42 deletions sylvia-derive/src/input.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,7 @@ use crate::message::{
};
use crate::multitest::{ContractMtHelpers, ImplMtHelpers};
use crate::parser::attributes::msg::MsgType;
use crate::parser::{
ContractArgs, ContractErrorAttr, Custom, OverrideEntryPoint, ParsedSylviaAttributes,
};
use crate::parser::{ContractErrorAttr, Custom, OverrideEntryPoint, ParsedSylviaAttributes};
use crate::querier::{ContractQuerier, TraitQuerier};
use crate::variant_descs::AsVariantDescs;

Expand Down Expand Up @@ -73,6 +71,8 @@ impl<'a> TraitInput<'a> {

let interface_messages = InterfaceApi::new(item, associated_types, custom).emit();

let multitest_helpers = self.emit_multitest_helpers();

quote! {
pub mod sv {
use super::*;
Expand All @@ -81,6 +81,8 @@ impl<'a> TraitInput<'a> {
#querier

#interface_messages

#multitest_helpers
}
}
}
Expand Down Expand Up @@ -122,52 +124,16 @@ impl<'a> TraitInput<'a> {
)
.emit()
}
}

/// Preprocessed `contract` macro input for impl trait block
pub struct ImplTraitInput<'a> {
attributes: &'a ContractArgs,
item: &'a ItemImpl,
generics: Vec<&'a GenericParam>,
interfaces: Interfaces,
}

impl<'a> ImplTraitInput<'a> {
pub fn new(attributes: &'a ContractArgs, item: &'a ItemImpl) -> Self {
let generics = item.generics.params.iter().collect();
let interfaces = Interfaces::new(item);

Self {
attributes,
item,
generics,
interfaces,
}
}

pub fn process(&self) -> TokenStream {
let multitest_helpers = self.emit_multitest_helpers();

quote! {
pub mod sv {
use super::*;

#multitest_helpers
}
}
}

fn emit_multitest_helpers(&self) -> TokenStream {
if !cfg!(feature = "mt") {
return quote! {};
}

let Self {
item, interfaces, ..
} = self;
let generic_params = &self.generics;
let Self { item, .. } = self;
let associated_types = &self.associated_types;

ImplMtHelpers::new(item, generic_params, interfaces, &self.attributes.module).emit()
ImplMtHelpers::new(item, associated_types).emit()
}
}

Expand Down
20 changes: 0 additions & 20 deletions sylvia-derive/src/interfaces.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
use proc_macro2::{Ident, TokenStream};
use proc_macro_error::emit_error;
use quote::quote;
use syn::spanned::Spanned;
use syn::{GenericArgument, ItemImpl};
Expand Down Expand Up @@ -138,23 +137,4 @@ impl Interfaces {
.flat_map(|interface| &interface.generics)
.collect()
}

pub fn get_only_interface(&self) -> Option<&ContractMessageAttr> {
let interfaces = &self.interfaces;
match interfaces.len() {
0 => None,
1 => Some(&interfaces[0]),
_ => {
let first = &interfaces[0];
for redefined in &interfaces[1..] {
emit_error!(
redefined.module, "The attribute `sv::messages` is redefined";
note = first.module.span() => "Previous definition of the attribute `sv::messages`";
note = "Only one `sv::messages` attribute can exist on an interface implementation on contract"
);
}
None
}
}
}
}
9 changes: 4 additions & 5 deletions sylvia-derive/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use input::{ImplInput, ImplTraitInput, TraitInput};
use input::{ImplInput, TraitInput};
use proc_macro::TokenStream;
use proc_macro2::TokenStream as TokenStream2;
use proc_macro_error::proc_macro_error;
Expand Down Expand Up @@ -235,11 +235,10 @@ pub fn contract(attr: TokenStream, item: TokenStream) -> TokenStream {
fn contract_impl(attr: TokenStream2, item: TokenStream2) -> TokenStream2 {
fn inner(attr: TokenStream2, item: TokenStream2) -> syn::Result<TokenStream2> {
let input: ItemImpl = parse2(item)?;
let expanded = if input.trait_.is_some() {
let attrs: parser::ContractArgs = parse2(attr)?;
ImplTraitInput::new(&attrs, &input).process()
} else {
let expanded = if attr.is_empty() {
ImplInput::new(&input).process()
} else {
quote! {}
};
let input = StripInput.fold_item_impl(input);

Expand Down
Loading

0 comments on commit 57a63a9

Please sign in to comment.