From 42ded4a96c75edb508668ad7927eb443fa2f5a49 Mon Sep 17 00:00:00 2001 From: Brian Anderson Date: Fri, 10 Nov 2023 19:19:54 +0000 Subject: [PATCH] Add tests for UDTs with tuple fields. Closes #1124 --- tests/udt/src/lib.rs | 2 ++ tests/udt/src/tuples.rs | 53 +++++++++++++++++++++++++++++++++++++++++ 2 files changed, 55 insertions(+) create mode 100644 tests/udt/src/tuples.rs diff --git a/tests/udt/src/lib.rs b/tests/udt/src/lib.rs index 2b4c640b2..f3c14b27e 100644 --- a/tests/udt/src/lib.rs +++ b/tests/udt/src/lib.rs @@ -1,6 +1,8 @@ #![no_std] use soroban_sdk::{contract, contractimpl, contracttype, Vec}; +mod tuples; + #[contracttype] #[derive(Copy, Clone, Debug, Eq, PartialEq)] pub enum UdtEnum2 { diff --git a/tests/udt/src/tuples.rs b/tests/udt/src/tuples.rs new file mode 100644 index 000000000..c56bbc43e --- /dev/null +++ b/tests/udt/src/tuples.rs @@ -0,0 +1,53 @@ +use soroban_sdk::{contract, contractimpl, contracttype, Address, Vec}; + +#[contracttype] +#[derive(Clone, Debug, Eq, PartialEq)] +pub enum UdtEnum { + UdtA((u32, Address)), +} + +#[contracttype] +#[derive(Clone, Debug, Eq, PartialEq)] +pub struct UdtTuple((u32, Address)); + +#[contracttype] +#[derive(Clone, Debug, Eq, PartialEq)] +pub struct UdtStruct { + f1: (u32, Address), +} + +#[contract] +pub struct Contract; + +#[contractimpl] +impl Contract { + pub fn id(v: (UdtEnum, UdtTuple, UdtStruct)) -> (UdtEnum, UdtTuple, UdtStruct) { + (v.0, v.1, v.2) + } +} + +#[cfg(test)] +mod test { + use super::*; + use soroban_sdk::testutils::Address as _; + use soroban_sdk::{vec, xdr::ScVal, Bytes, Env, TryFromVal}; + + #[test] + fn test_tuples() { + let e = Env::default(); + let contract_id = e.register_contract(None, Contract); + let client = ContractClient::new(&e, &contract_id); + + let v1 = ( + UdtEnum::UdtA((1, Address::random(&e))), + UdtTuple((2, Address::random(&e))), + UdtStruct { + f1: (3, Address::random(&e)), + }, + ); + + let v2 = client.id(&v1); + + assert_eq!(v1, v2); + } +}