Skip to content

Commit

Permalink
fix the unit test
Browse files Browse the repository at this point in the history
  • Loading branch information
konnov committed May 13, 2024
1 parent eaa8be6 commit 3e38725
Show file tree
Hide file tree
Showing 2 changed files with 103 additions and 12 deletions.
30 changes: 27 additions & 3 deletions ContractExamples/contracts/setter/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -193,21 +193,45 @@ impl SetterContract {
env.storage().instance().extend_ttl(50, 100)
}

pub fn get_vec(env: Env) -> Vec<i32> {
fn get_vec_internal(env: &Env) -> Vec<i32> {
env.storage().instance().get(&MY_VEC).unwrap_or(vec![&env])
}

pub fn get_vec(env: Env) -> Vec<i32> {
Self::get_vec_internal(&env)
}

pub fn vec_push(env: Env, i: i32) {
let mut vs = Self::get_vec_internal(&env);
vs.push_back(i);
env.storage().instance().set(&MY_VEC, &vs);
}

pub fn set_map(env: Env, v: Map<u32, i32>) -> () {
env.storage().instance().set(&MY_MAP, &v);
log!(&env, "myMap: {}", v);
// bump the lifetime
env.storage().instance().extend_ttl(50, 100)
}

pub fn get_map(env: Env) -> Map<u32, i32> {
fn get_map_internal(env: &Env) -> Map<u32, i32> {
env.storage().instance().get(&MY_MAP).unwrap_or(Map::from_array(&env, [(0, 0); 0]))
}

pub fn get_map(env: Env) -> Map<u32, i32> {
Self::get_map_internal(&env)
}

pub fn map_set(env: Env, key: u32, value: i32) {
let mut map = Self::get_map_internal(&env);
map.set(key, value);
env.storage().instance().set(&MY_MAP, &map)
}

pub fn map_get(env: Env, key: u32) -> i32 {
Self::get_map_internal(&env).get(key).unwrap_or(0i32)
}

pub fn set_address(env: Env, v: Address) -> () {
env.storage().instance().set(&MY_ADDR, &v);
log!(&env, "myAddress: {}", v);
Expand All @@ -216,7 +240,7 @@ impl SetterContract {
}

pub fn get_address(env: Env) -> Option<Address> {
env.storage().instance().get(&MY_VEC)
env.storage().instance().get(&MY_ADDR)
}

pub fn set_my_struct(env: Env, v: MyStruct) -> () {
Expand Down
85 changes: 76 additions & 9 deletions ContractExamples/contracts/setter/src/test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

use super::*;
use soroban_sdk::{
testutils::{Address as _},
testutils::Address as _,
Address, Env
};

Expand All @@ -15,15 +15,82 @@ fn test() {

let contract_id = env.register_contract(None, SetterContract);
// <X>Client is automagically created.
let client = MegaContractClient::new(&env, &contract_id);
let client = SetterContractClient::new(&env, &contract_id);

assert!(client.get_addrs().is_empty());
assert_eq!(client.get_bool(), false);
assert_eq!(client.set_bool(&true), false);
assert_eq!(client.get_bool(), true);

assert_eq!(client.next_val_and_save_addr(&addr),2);
assert_eq!(client.next_val_and_save_addr(&addr),4);
assert_eq!(client.next_val_and_save_addr(&addr),8);
assert_eq!(client.next_val_and_save_addr(&addr),1);
assert_eq!(client.next_val_and_save_addr(&addr),2);
assert_eq!(client.get_u32(), 0u32);
assert_eq!(client.set_u32(&42u32), 0);
assert_eq!(client.get_u32(), 42);

assert!(client.get_addrs().get(2u32).map_or(0, |a| a.len()) == 2);
assert_eq!(client.get_i32(), 0i32);
assert_eq!(client.set_i32(&42i32), 0);
assert_eq!(client.get_i32(), 42);

assert_eq!(client.get_u64(), 0u64);
assert_eq!(client.set_u64(&42u64), 0);
assert_eq!(client.get_u64(), 42);

assert_eq!(client.get_i64(), 0i64);
assert_eq!(client.set_i64(&42i64), 0);
assert_eq!(client.get_i64(), 42);

assert_eq!(client.get_u128(), 0u128);
assert_eq!(client.set_u128(&42u128), 0);
assert_eq!(client.get_u128(), 42);

assert_eq!(client.get_i128(), 0i128);
assert_eq!(client.set_i128(&42i128), 0);
assert_eq!(client.get_i128(), 42);

client.set_sym(&symbol_short!("FOO"));
assert_eq!(client.get_sym(), symbol_short!("FOO"));

client.set_bytes(&Bytes::from_array(&env, &[ 1u8, 2u8, 3u8]));
assert_eq!(client.get_bytes(), Bytes::from_array(&env, &[ 1u8, 2u8, 3u8]));

let bytes32: BytesN<32> = BytesN::from_array(&env, &[
1u8, 2u8, 3u8, 4u8, 5u8, 6u8, 7u8, 8u8, 9u8, 10u8, 11u8, 12u8, 13u8,
14u8, 15u8, 16u8, 17u8, 18u8, 19u8, 20u8, 21u8, 22u8, 23u8, 24u8, 25u8,
26u8, 27u8, 28u8, 29u8, 30u8, 31u8, 32u8
]);
client.set_bytes32(&bytes32);
assert_eq!(client.get_bytes32(), bytes32);

client.map_set(&2, &3);
client.map_set(&5, &6);

assert_eq!(client.map_get(&2), 3);
assert_eq!(client.map_get(&5), 6);
assert_eq!(client.map_get(&7), 0);

client.set_map(&Map::from_array(&env, [(3u32, 4i32), (6u32, 8i32)]));
assert_eq!(client.map_get(&2), 0);
assert_eq!(client.map_get(&3), 4);
assert_eq!(client.map_get(&5), 0);
assert_eq!(client.map_get(&6), 8);
assert_eq!(client.map_get(&7), 0);

client.vec_push(&2);
client.vec_push(&3);
client.vec_push(&4);

assert_eq!(client.get_vec(), vec![&env, 2, 3, 4]);

client.set_vec(&vec![&env, 5, 6, 7]);
assert_eq!(client.get_vec(), vec![&env, 5, 6, 7]);

client.set_address(&addr);
assert_eq!(client.get_address(), Some(addr.clone()));

client.set_my_struct(&MyStruct { a: 3u32, b: 4i128 });
assert_eq!(client.get_my_struct(), MyStruct { a: 3u32, b: 4i128 });

client.set_my_enum(&MyEnum::A);
assert_eq!(client.get_my_enum(), MyEnum::A);

client.set_my_enum(&MyEnum::B(123u32));
assert_eq!(client.get_my_enum(), MyEnum::B(123u32));
}

0 comments on commit 3e38725

Please sign in to comment.