Skip to content

Commit

Permalink
Merge pull request #1155 from multiversx/scenario-static-api-2
Browse files Browse the repository at this point in the history
StaticApi in tests
  • Loading branch information
andrei-marinica authored Jul 11, 2023
2 parents 0de82bf + d8cd4f3 commit d16b362
Show file tree
Hide file tree
Showing 10 changed files with 43 additions and 70 deletions.
6 changes: 3 additions & 3 deletions contracts/examples/multisig/src/action.rs
Original file line number Diff line number Diff line change
Expand Up @@ -56,13 +56,13 @@ pub struct ActionFullInfo<M: ManagedTypeApi> {

#[cfg(test)]
mod test {
use multiversx_sc_scenario::DebugApi;
use multiversx_sc_scenario::api::StaticApi;

use super::Action;

#[test]
fn test_is_pending() {
assert!(!Action::<DebugApi>::Nothing.is_pending());
assert!(Action::<DebugApi>::ChangeQuorum(5).is_pending());
assert!(!Action::<StaticApi>::Nothing.is_pending());
assert!(Action::<StaticApi>::ChangeQuorum(5).is_pending());
}
}
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
use multiversx_sc::types::{SCResult, StaticSCError};
use multiversx_sc_scenario::*;
use multiversx_sc_scenario::api::StaticApi;

use alloc_features::macro_features_legacy::MacroFeaturesLegacy;

/// Likely to be removed soon.
#[test]
fn test_sc_error() {
let _ = DebugApi::dummy();
let bf = alloc_features::contract_obj::<DebugApi>();
let bf = alloc_features::contract_obj::<StaticApi>();
let result = bf.return_sc_error();
assert_eq!(
SCResult::Err(StaticSCError::from(&b"return_sc_error"[..])),
Expand Down
Original file line number Diff line number Diff line change
@@ -1,21 +1,19 @@
use multiversx_sc::types::{BoxedBytes, ManagedAddress, ManagedBuffer};
use multiversx_sc_scenario::*;
use multiversx_sc_scenario::api::StaticApi;

use alloc_features::managed_buffer_features_alloc::ManagedBufferFeatures;

#[test]
#[should_panic]
fn test_managed_buffer_set_slice_should_panic() {
let _ = DebugApi::dummy();
let bf = alloc_features::contract_obj::<DebugApi>();
let bf = alloc_features::contract_obj::<StaticApi>();
let buffer = bf.mbuffer_from_slice(&[1, 2, 3][..]);
let _ = bf.mbuffer_set_slice(buffer, 4, &[5]);
}

#[test]
fn test_managed_buffer_set_slice() {
let _ = DebugApi::dummy();
let bf = alloc_features::contract_obj::<DebugApi>();
let bf = alloc_features::contract_obj::<StaticApi>();
let buffer = bf.mbuffer_from_slice(&[1, 2, 3][..]);

let set_result = bf.mbuffer_set_slice(buffer, 1, &[5][..]);
Expand All @@ -25,8 +23,7 @@ fn test_managed_buffer_set_slice() {

#[test]
fn test_managed_buffer_from() {
let _ = DebugApi::dummy();
let bf = alloc_features::contract_obj::<DebugApi>();
let bf = alloc_features::contract_obj::<StaticApi>();
let result = bf.mbuffer_from_slice(&[1, 2, 3][..]);
assert_eq!(ManagedBuffer::from(&[1, 2, 3][..]), result);
let result = bf.mbuffer_from_boxed_bytes(BoxedBytes::from(&[4, 5, 6][..]));
Expand All @@ -35,7 +32,6 @@ fn test_managed_buffer_from() {

#[test]
fn test_managed_address_from() {
let _ = DebugApi::dummy();
let bf = alloc_features::contract_obj::<DebugApi>();
let bf = alloc_features::contract_obj::<StaticApi>();
assert_eq!(ManagedAddress::zero(), bf.managed_address_from(&[0u8; 32]));
}
Original file line number Diff line number Diff line change
@@ -1,20 +1,18 @@
use multiversx_sc::types::{BigInt, BigUint, ManagedBuffer};
use multiversx_sc_scenario::*;
use multiversx_sc_scenario::api::StaticApi;

use basic_features::{big_num_methods::BigIntMethods, big_num_operators::BigIntOperators};

#[test]
fn test_big_uint_zero() {
let _ = DebugApi::dummy();
let bf = basic_features::contract_obj::<DebugApi>();
let bf = basic_features::contract_obj::<StaticApi>();
let result = bf.big_uint_zero();
assert_eq!(BigUint::zero(), result);
}

#[test]
fn test_big_uint_from() {
let _ = DebugApi::dummy();
let bf = basic_features::contract_obj::<DebugApi>();
let bf = basic_features::contract_obj::<StaticApi>();
let result = bf.big_uint_from_u64_1(5);
assert_eq!(BigUint::from(5u32), result);
let result = bf.big_uint_from_u64_2(5);
Expand All @@ -27,39 +25,36 @@ fn test_big_uint_from() {

#[test]
fn test_big_int_zero() {
let _ = DebugApi::dummy();
let bf = basic_features::contract_obj::<DebugApi>();
let bf = basic_features::contract_obj::<StaticApi>();
let result = bf.big_int_zero();
assert_eq!(BigInt::zero(), result);
}

#[test]
fn test_big_int_from() {
let _ = DebugApi::dummy();
let bf = basic_features::contract_obj::<DebugApi>();
let bf = basic_features::contract_obj::<StaticApi>();
let result = bf.big_int_from_i64_1(5);
assert_eq!(BigInt::from(5), result);
let result = bf.big_int_from_i64_2(6);
assert_eq!(BigInt::from(6), result);
}

fn check_big_uint_bitwise_and(a: u64, b: u64) {
let bf = basic_features::contract_obj::<DebugApi>();
let bf = basic_features::contract_obj::<StaticApi>();
let result = bf.bit_and_big_uint(BigUint::from(a), BigUint::from(b));
assert_eq!(BigUint::from(a & b), result);
}

#[test]
fn test_big_uint_bitwise_and() {
let _ = DebugApi::dummy();
check_big_uint_bitwise_and(1, 1);
check_big_uint_bitwise_and(5, 7);
check_big_uint_bitwise_and(0, 1023);
check_big_uint_bitwise_and(0, 0);
}

fn check_big_uint_shift(a: u64, b: usize) {
let bf = basic_features::contract_obj::<DebugApi>();
let bf = basic_features::contract_obj::<StaticApi>();
let result = bf.shl_big_uint(BigUint::from(a), b);
assert_eq!(BigUint::from(a << b), result);
let result = bf.shr_big_uint(BigUint::from(a), b);
Expand All @@ -68,7 +63,6 @@ fn check_big_uint_shift(a: u64, b: usize) {

#[test]
fn test_big_uint_bitwise_shift() {
let _ = DebugApi::dummy();
check_big_uint_shift(1, 3);
check_big_uint_shift(256, 0);
check_big_uint_shift(1023, 5);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,20 +1,18 @@
use multiversx_sc::types::{ManagedAddress, ManagedBuffer};
use multiversx_sc_scenario::*;
use multiversx_sc_scenario::{api::StaticApi, *};

use basic_features::managed_buffer_features::ManagedBufferFeatures;

#[test]
fn test_managed_buffer_new_empty() {
let _ = DebugApi::dummy();
let bf = basic_features::contract_obj::<DebugApi>();
let bf = basic_features::contract_obj::<StaticApi>();
let result = bf.mbuffer_new();
assert_eq!(ManagedBuffer::new(), result);
}

#[test]
fn test_managed_address_zero() {
let _ = DebugApi::dummy();
let bf = basic_features::contract_obj::<DebugApi>();
let bf = basic_features::contract_obj::<StaticApi>();
let result = bf.managed_address_zero();
assert_eq!(ManagedAddress::zero(), result);
}
Original file line number Diff line number Diff line change
@@ -1,20 +1,18 @@
use multiversx_sc::types::{BigUint, ManagedVec};
use multiversx_sc_scenario::*;
use multiversx_sc_scenario::{api::StaticApi, *};

use basic_features::managed_vec_features::ManagedVecFeatures;

#[test]
fn test_managed_vec_new() {
let _ = DebugApi::dummy();
let bf = basic_features::contract_obj::<DebugApi>();
let bf = basic_features::contract_obj::<StaticApi>();
let result = bf.managed_vec_new();
assert_eq!(ManagedVec::new(), result);
}

#[test]
fn test_managed_vec_eq() {
let _ = DebugApi::dummy();
let bf = basic_features::contract_obj::<DebugApi>();
let bf = basic_features::contract_obj::<StaticApi>();

let mut mv1 = ManagedVec::new();
mv1.push(BigUint::from(1u32));
Expand All @@ -37,8 +35,7 @@ fn test_managed_vec_eq() {

#[test]
fn test_managed_vec_set() {
let _ = DebugApi::dummy();
let bf = basic_features::contract_obj::<DebugApi>();
let bf = basic_features::contract_obj::<StaticApi>();

let mut mv1 = ManagedVec::new();
mv1.push(BigUint::from(1u32));
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
use multiversx_sc::types::SparseArray;
use multiversx_sc_scenario::DebugApi;
use multiversx_sc_scenario::api::StaticApi;

#[test]
fn sparse_array_test() {
let _ = DebugApi::dummy();
let mut array = SparseArray::<DebugApi, 100>::new(5);
let mut array = SparseArray::<StaticApi, 100>::new(5);
assert_eq!(array.len(), 5);
assert_eq!(array.as_raw_slice(), vec![0, 0, 0, 0, 0].as_slice());

Expand All @@ -26,14 +25,12 @@ fn sparse_array_test() {
#[should_panic]
#[test]
fn sparse_array_create_over_capacity_test() {
let _ = DebugApi::dummy();
let _ = SparseArray::<DebugApi, 100>::new(101);
let _ = SparseArray::<StaticApi, 100>::new(101);
}

#[should_panic]
#[test]
fn sparse_array_get_invalid_index_test() {
let _ = DebugApi::dummy();
let array = SparseArray::<DebugApi, 100>::new(5);
let array = SparseArray::<StaticApi, 100>::new(5);
let _ = array.get(5);
}
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
use multiversx_sc::types::{EgldOrEsdtTokenIdentifier, ManagedBuffer, TokenIdentifier};
use multiversx_sc_scenario::*;
use multiversx_sc_scenario::{api::StaticApi, *};

use basic_features::token_identifier_features::TokenIdentifierFeatures;

#[test]
fn test_token_identifier_egld() {
let _ = DebugApi::dummy();
let bf = basic_features::contract_obj::<DebugApi>();
let bf = basic_features::contract_obj::<StaticApi>();
let result = bf.token_identifier_egld();
assert_eq!(EgldOrEsdtTokenIdentifier::egld(), result);
}
Expand All @@ -15,8 +14,7 @@ fn test_token_identifier_egld() {
/// For a complete suite of test cases, see `multiversx-sc-scenario/tests/managed_token_identifier_test.rs`.
#[test]
fn test_token_identifier_is_valid() {
let _ = DebugApi::dummy();
let bf = basic_features::contract_obj::<DebugApi>();
let bf = basic_features::contract_obj::<StaticApi>();
let result = bf.token_identifier_is_valid_1(EgldOrEsdtTokenIdentifier::esdt(
TokenIdentifier::from(&b"ALC-6258d2"[..]),
));
Expand All @@ -33,9 +31,7 @@ fn test_token_identifier_is_valid() {

#[test]
fn test_token_identifier_compare() {
let _ = DebugApi::dummy();

let token_id = TokenIdentifier::<DebugApi>::from(&b"ALC-6258d2"[..]);
let token_id = TokenIdentifier::<StaticApi>::from(&b"ALC-6258d2"[..]);
let esdt_token_id = EgldOrEsdtTokenIdentifier::esdt(token_id.clone());
let wrong_esdt_token_id =
EgldOrEsdtTokenIdentifier::esdt(TokenIdentifier::from(&b"AAA-111111"[..]));
Expand Down
4 changes: 2 additions & 2 deletions contracts/feature-tests/use-module/tests/use_module_test.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
use multiversx_sc::contract_base::CallableContract;
use multiversx_sc_scenario::*;
use multiversx_sc_scenario::api::DebugApi;

#[test]
fn test_function_selector() {
let _ = DebugApi::dummy();
DebugApi::dummy();
let use_module = use_module::contract_obj::<DebugApi>();

assert!(!use_module.call("invalid_endpoint"));
Expand Down
22 changes: 9 additions & 13 deletions framework/scenario/tests/static_buffer_ref_test.rs
Original file line number Diff line number Diff line change
@@ -1,52 +1,48 @@
use multiversx_sc::types::{LockableStaticBuffer, StaticBufferRef};
use multiversx_sc_scenario::api::DebugApi;
use multiversx_sc_scenario::api::StaticApi;

#[test]
fn test_try_extend_from_slice() {
let _ = DebugApi::dummy();
let mut s = StaticBufferRef::<DebugApi>::try_new(b"z").unwrap();
let mut s = StaticBufferRef::<StaticApi>::try_new(b"z").unwrap();
assert!(s.try_extend_from_slice(b"abc"));
assert!(s.try_extend_from_slice(b"def"));
assert!(s.contents_eq(b"zabcdef"));
}

#[test]
fn test_lock_unlock() {
let _ = DebugApi::dummy();
{
let s = StaticBufferRef::<DebugApi>::try_new(b"first").unwrap();
let s = StaticBufferRef::<StaticApi>::try_new(b"first").unwrap();
assert!(s.contents_eq(b"first"));
// should unlock here
}

let s = StaticBufferRef::<DebugApi>::try_new(b"another").unwrap();
assert!(StaticBufferRef::<DebugApi>::try_new(b"no, locked").is_none());
let s = StaticBufferRef::<StaticApi>::try_new(b"another").unwrap();
assert!(StaticBufferRef::<StaticApi>::try_new(b"no, locked").is_none());
assert!(s.contents_eq(b"another"));
}

#[test]
fn test_extend_past_buffer_limits() {
let _ = DebugApi::dummy();
let mut s = StaticBufferRef::<DebugApi>::try_new(&[]).unwrap();
let mut s = StaticBufferRef::<StaticApi>::try_new(&[]).unwrap();
assert!(s.try_extend_from_slice(&[22; LockableStaticBuffer::capacity() - 1]));
assert!(s.try_extend_from_slice(&[33; 1]));
assert!(!s.try_extend_from_slice(&[44; 1]));
}

fn new_should_fail() {
let buffer_option = StaticBufferRef::<DebugApi>::try_new(b"test");
let buffer_option = StaticBufferRef::<StaticApi>::try_new(b"test");
assert!(buffer_option.is_none());
}

fn new_should_succeed() {
let buffer_option = StaticBufferRef::<DebugApi>::try_new(b"test");
let buffer_option = StaticBufferRef::<StaticApi>::try_new(b"test");
assert!(buffer_option.is_some());
}

#[test]
fn test_lock_2() {
let _ = DebugApi::dummy();
let buffer_option = StaticBufferRef::<DebugApi>::try_new(b"locking_test");
let buffer_option = StaticBufferRef::<StaticApi>::try_new(b"locking_test");
new_should_fail();
assert!(buffer_option.is_some());
let s1_buffer = buffer_option.unwrap();
Expand Down

0 comments on commit d16b362

Please sign in to comment.