Skip to content
New issue

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

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

Already on GitHub? Sign in to your account

Support calling ctors with tuples #1340

Merged
merged 6 commits into from
Sep 18, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 13 additions & 9 deletions soroban-sdk/src/env.rs
Original file line number Diff line number Diff line change
Expand Up @@ -453,9 +453,9 @@ impl Env {
use crate::{
auth,
testutils::{
budget::Budget, Address as _, AuthSnapshot, AuthorizedInvocation, ContractFunctionSet,
EventsSnapshot, Generators, Ledger as _, MockAuth, MockAuthContract, Snapshot,
StellarAssetContract, StellarAssetIssuer,
budget::Budget, Address as _, AuthSnapshot, AuthorizedInvocation, ConstructorArgs,
ContractFunctionSet, EventsSnapshot, Generators, Ledger as _, MockAuth, MockAuthContract,
Snapshot, StellarAssetContract, StellarAssetIssuer,
},
Bytes, BytesN,
};
Expand Down Expand Up @@ -622,7 +622,7 @@ impl Env {
contract_id: impl Into<Option<&'a Address>>,
contract: T,
) -> Address {
self.register_contract_with_constructor(contract_id, contract, crate::vec![&self])
self.register_contract_with_constructor(contract_id, contract, ())
}

/// Register a contract with the [Env] for testing.
Expand All @@ -645,7 +645,7 @@ impl Env {
///
/// ### Examples
/// ```
/// use soroban_sdk::{contract, contractimpl, BytesN, Env, Symbol, IntoVal};
/// use soroban_sdk::{contract, contractimpl, BytesN, Env, Symbol};
///
/// #[contract]
/// pub struct Contract;
Expand All @@ -662,14 +662,18 @@ impl Env {
/// # fn main() {
/// let env = Env::default();
/// let contract_id = env.register_contract_with_constructor(
/// None, Contract, (123_u32,).into_val(&env));
/// None, Contract, (123_u32,));
/// }
/// ```
pub fn register_contract_with_constructor<'a, T: ContractFunctionSet + 'static>(
pub fn register_contract_with_constructor<
'a,
T: ContractFunctionSet + 'static,
A: ConstructorArgs,
>(
&self,
contract_id: impl Into<Option<&'a Address>>,
contract: T,
constructor_args: Vec<Val>,
constructor_args: A,
) -> Address {
struct InternalContractFunctionSet<T: ContractFunctionSet>(pub(crate) T);
impl<T: ContractFunctionSet> internal::ContractFunctionSet for InternalContractFunctionSet<T> {
Expand Down Expand Up @@ -703,7 +707,7 @@ impl Env {
.register_test_contract_with_constructor(
contract_id.to_object(),
Rc::new(InternalContractFunctionSet(contract)),
constructor_args.to_object(),
constructor_args.into_val(self).to_object(),
)
.unwrap();
contract_id
Expand Down
33 changes: 32 additions & 1 deletion soroban-sdk/src/testutils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,42 @@ use soroban_env_host::TryIntoVal;

pub mod storage;

use crate::{xdr, Env, Val, Vec};
use crate::{xdr, Env, IntoVal, Val, Vec};
use soroban_ledger_snapshot::LedgerSnapshot;

pub use crate::env::EnvTestConfig;

pub trait ConstructorArgs: IntoVal<Env, Vec<Val>> {}

impl<T> ConstructorArgs for Vec<T> {}

macro_rules! impl_constructor_args_for_tuple {
( $($typ:ident $idx:tt)* ) => {
impl<$($typ),*> ConstructorArgs for ($($typ,)*)
where
$($typ: IntoVal<Env, Val>),*
{
}
};
}

// 0 topics
impl ConstructorArgs for () {}
// 1-13 topics
impl_constructor_args_for_tuple! { T0 0 }
impl_constructor_args_for_tuple! { T0 0 T1 1 }
impl_constructor_args_for_tuple! { T0 0 T1 1 T2 2 }
impl_constructor_args_for_tuple! { T0 0 T1 1 T2 2 T3 3 }
impl_constructor_args_for_tuple! { T0 0 T1 1 T2 2 T3 3 T4 4 }
impl_constructor_args_for_tuple! { T0 0 T1 1 T2 2 T3 3 T4 4 T5 5 }
impl_constructor_args_for_tuple! { T0 0 T1 1 T2 2 T3 3 T4 4 T5 5 T6 6 }
impl_constructor_args_for_tuple! { T0 0 T1 1 T2 2 T3 3 T4 4 T5 5 T6 6 T7 7 }
impl_constructor_args_for_tuple! { T0 0 T1 1 T2 2 T3 3 T4 4 T5 5 T6 6 T7 7 T8 8 }
impl_constructor_args_for_tuple! { T0 0 T1 1 T2 2 T3 3 T4 4 T5 5 T6 6 T7 7 T8 8 T9 9 }
impl_constructor_args_for_tuple! { T0 0 T1 1 T2 2 T3 3 T4 4 T5 5 T6 6 T7 7 T8 8 T9 9 T10 10 }
impl_constructor_args_for_tuple! { T0 0 T1 1 T2 2 T3 3 T4 4 T5 5 T6 6 T7 7 T8 8 T9 9 T10 10 T11 11 }
impl_constructor_args_for_tuple! { T0 0 T1 1 T2 2 T3 3 T4 4 T5 5 T6 6 T7 7 T8 8 T9 9 T10 10 T11 11 T12 12 }

#[derive(Default, Debug, Clone, PartialEq, Eq, serde::Serialize, serde::Deserialize)]
#[serde(rename_all = "snake_case")]
pub struct Snapshot {
Expand Down
16 changes: 4 additions & 12 deletions tests/constructor/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
#![no_std]
#[cfg(test)]
use soroban_sdk::IntoVal;
use soroban_sdk::{contract, contractimpl, contracttype, Env};

#[contract]
Expand Down Expand Up @@ -39,8 +37,7 @@ impl Contract {
#[test]
fn test_constructor() {
let env = Env::default();
let contract_id =
env.register_contract_with_constructor(None, Contract, (100_u32, 1000_i64).into_val(&env));
let contract_id = env.register_contract_with_constructor(None, Contract, (100_u32, 1000_i64));
let client = ContractClient::new(&env, &contract_id);
assert_eq!(client.get_data(&DataKey::Persistent(100)), Some(1000));
assert_eq!(client.get_data(&DataKey::Temp(200)), Some(2000));
Expand All @@ -62,24 +59,19 @@ fn test_passing_no_constructor_arguments_causes_panic() {
#[should_panic(expected = "constructor invocation has failed with error")]
fn test_missing_constructor_arguments_causes_panic() {
let env = Env::default();
let _ = env.register_contract_with_constructor(None, Contract, (100_u32,).into_val(&env));
let _ = env.register_contract_with_constructor(None, Contract, (100_u32,));
}

#[test]
#[should_panic(expected = "constructor invocation has failed with error")]
fn test_passing_extra_constructor_arguments_causes_panic() {
let env = Env::default();
let _ = env.register_contract_with_constructor(
None,
Contract,
(100_u32, 1000_i64, 123_u32).into_val(&env),
);
let _ = env.register_contract_with_constructor(None, Contract, (100_u32, 1000_i64, 123_u32));
}

#[test]
#[should_panic(expected = "constructor invocation has failed with error")]
fn test_passing_incorrectly_typed_constructor_arguments_causes_panic() {
let env = Env::default();
let _ =
env.register_contract_with_constructor(None, Contract, (100_u32, 1000_u32).into_val(&env));
let _ = env.register_contract_with_constructor(None, Contract, (100_u32, 1000_u32));
}
Loading
Loading