Skip to content

Commit

Permalink
unified syntax - expect value
Browse files Browse the repository at this point in the history
  • Loading branch information
andrei-marinica committed Apr 17, 2024
1 parent a0b1760 commit 0330c4f
Show file tree
Hide file tree
Showing 6 changed files with 94 additions and 21 deletions.
33 changes: 16 additions & 17 deletions contracts/examples/adder/tests/adder_blackbox_test.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
use multiversx_sc_scenario::imports::*;
use num_bigint::BigUint;

use adder::*;

Expand All @@ -20,16 +19,12 @@ fn world() -> ScenarioWorld {
#[test]
fn adder_blackbox() {
let mut world = world();
let owner_address = "address:owner";
let adder_contract = ContractInfo::<adder::Proxy<StaticApi>>::new("sc:adder");

world.start_trace();

world.set_state_step(
SetStateStep::new()
.put_account(owner_address, Account::new().nonce(1))
.new_address(owner_address, 1, "sc:adder"),
);
world.account(OWNER).nonce(1);

world.set_state_step(SetStateStep::new().new_address(OWNER, 1, SC_ADDER));

let new_address = world
.tx()
Expand All @@ -40,16 +35,15 @@ fn adder_blackbox() {
.returns(ReturnsNewAddress)
.run();

assert_eq!(new_address, adder_contract.to_address());
assert_eq!(new_address, SC_ADDER.to_address());

let value = world
world
.query()
.to(SC_ADDER)
.typed(adder_proxy::AdderProxy)
.sum()
.returns(ReturnsResultConv::<BigUint>::new())
.returns(ExpectValue(5u32))
.run();
assert_eq!(value, BigUint::from(5u32));

world
.tx()
Expand All @@ -59,13 +53,18 @@ fn adder_blackbox() {
.add(1u32)
.run();

world
.query()
.to(SC_ADDER)
.typed(adder_proxy::AdderProxy)
.sum()
.returns(ExpectValue(6u32))
.run();

world.check_state_step(
CheckStateStep::new()
.put_account(owner_address, CheckAccount::new())
.put_account(
&adder_contract,
CheckAccount::new().check_storage("str:sum", "6"),
),
.put_account(OWNER, CheckAccount::new())
.put_account(SC_ADDER, CheckAccount::new().check_storage("str:sum", "6")),
);

world.write_scenario_trace("trace1.scen.json");
Expand Down
16 changes: 13 additions & 3 deletions framework/base/src/types/interaction/expr/sc_expr.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
use core::ptr;

use crate::types::{
AnnotatedValue, ManagedAddress, ManagedBuffer, TxEnv, TxFrom, TxFromSpecified, TxTo,
TxToSpecified,
use crate::{
proxy_imports::Address,

Check failure on line 4 in framework/base/src/types/interaction/expr/sc_expr.rs

View workflow job for this annotation

GitHub Actions / Template tool test - current (unreleased) templates

unresolved import `crate::proxy_imports::Address`

Check failure on line 4 in framework/base/src/types/interaction/expr/sc_expr.rs

View workflow job for this annotation

GitHub Actions / Template tool test - current (unreleased) templates

unresolved import `crate::proxy_imports::Address`

Check failure on line 4 in framework/base/src/types/interaction/expr/sc_expr.rs

View workflow job for this annotation

GitHub Actions / Template tool test - current (unreleased) templates

unresolved import `crate::proxy_imports::Address`

Check failure on line 4 in framework/base/src/types/interaction/expr/sc_expr.rs

View workflow job for this annotation

GitHub Actions / Template tool test - current (unreleased) templates

unresolved import `crate::proxy_imports::Address`

Check failure on line 4 in framework/base/src/types/interaction/expr/sc_expr.rs

View workflow job for this annotation

GitHub Actions / Template tool test - current (unreleased) templates

unresolved import `crate::proxy_imports::Address`

Check failure on line 4 in framework/base/src/types/interaction/expr/sc_expr.rs

View workflow job for this annotation

GitHub Actions / Contracts / Wasm tests

unresolved import `crate::proxy_imports::Address`
types::{
AnnotatedValue, ManagedAddress, ManagedBuffer, TxEnv, TxFrom, TxFromSpecified, TxTo,
TxToSpecified,
},
};

const SC_PREFIX: &str = "sc:";
Expand All @@ -28,6 +31,13 @@ where
}
}

impl<'a> ScExpr<'a> {
pub fn to_address(&self) -> Address {
let expr: [u8; 32] = self.eval_to_array();
expr.into()
}
}

impl<'a, Env> TxFrom<Env> for ScExpr<'a>
where
Env: TxEnv,
Expand Down
4 changes: 4 additions & 0 deletions framework/scenario/src/facade/expr/bech32_address.rs
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,10 @@ impl Bech32Address {
self.bech32.to_owned()
}

pub fn to_hex(&self) -> String {
hex::encode(&self.address)
}

pub fn as_address(&self) -> &Address {
&self.address
}
Expand Down
2 changes: 2 additions & 0 deletions framework/scenario/src/facade/result_handlers.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
mod expect_error;
mod expect_message;
mod expect_status;
mod expect_value;
mod returns_message;
mod returns_new_bech32_address;
mod returns_new_token_identifier;
Expand All @@ -10,6 +11,7 @@ mod with_tx_raw_response;
pub use expect_error::ExpectError;
pub use expect_message::ExpectMessage;
pub use expect_status::ExpectStatus;
pub use expect_value::ExpectValue;
pub use returns_message::ReturnsMessage;
pub use returns_new_bech32_address::ReturnsNewBech32Address;
pub use returns_new_token_identifier::ReturnsNewTokenIdentifier;
Expand Down
40 changes: 40 additions & 0 deletions framework/scenario/src/facade/result_handlers/expect_value.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
use multiversx_sc::{
codec::{CodecFrom, TopEncodeMulti},
types::{RHListItem, RHListItemExec, TxEnv},
};

use crate::scenario_model::{BytesValue, CheckValue, TxExpect, TxResponse};

/// Verifies that transaction result matches the given value.
///
/// Can only be used in tests and interactors, not available in contracts.
pub struct ExpectValue<T>(pub T);

impl<Env, Original, T> RHListItem<Env, Original> for ExpectValue<T>
where
Env: TxEnv,
T: TopEncodeMulti,
Original: CodecFrom<T>,
{
type Returns = ();
}

impl<Env, Original, T> RHListItemExec<TxResponse, Env, Original> for ExpectValue<T>
where
Env: TxEnv<RHExpect = TxExpect>,
T: TopEncodeMulti,
Original: CodecFrom<T>,
{
fn item_tx_expect(&self, mut prev: TxExpect) -> TxExpect {
let mut encoded = Vec::<Vec<u8>>::new();
self.0.multi_encode(&mut encoded).expect("encoding error");
let out_values = encoded
.into_iter()
.map(|value| CheckValue::Equal(BytesValue::from(value)))
.collect();
prev.out = CheckValue::Equal(out_values);
prev
}

fn item_process_result(self, _: &TxResponse) -> Self::Returns {}
}
20 changes: 19 additions & 1 deletion framework/scenario/src/scenario/model/value/address_value.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use std::fmt;

use crate::multiversx_sc::types::Address;
use crate::multiversx_sc::types::{Address, AddressExpr, ScExpr};

use crate::{
facade::expr::Bech32Address,
Expand Down Expand Up @@ -133,3 +133,21 @@ impl From<&str> for AddressValue {
AddressValue::interpret_from(from, &InterpreterContext::default())
}
}

impl From<AddressExpr> for AddressValue {
fn from(from: AddressExpr) -> Self {
AddressValue {
value: from.eval_to_array().into(),
original: ValueSubTree::Str(from.eval_to_expr()),
}
}
}

impl From<ScExpr<'_>> for AddressValue {
fn from(from: ScExpr) -> Self {
AddressValue {
value: from.eval_to_array().into(),
original: ValueSubTree::Str(from.eval_to_expr()),
}
}
}

0 comments on commit 0330c4f

Please sign in to comment.