Skip to content

Commit

Permalink
Merge pull request #1686 from multiversx/address-eq
Browse files Browse the repository at this point in the history
test address PartialEq implementation
  • Loading branch information
andrei-marinica authored Jun 21, 2024
2 parents 323919e + d8c479a commit 4fee541
Show file tree
Hide file tree
Showing 3 changed files with 118 additions and 4 deletions.
44 changes: 42 additions & 2 deletions framework/base/src/types/interaction/expr/test_address.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,13 @@ use crate::{
abi::TypeAbiFrom,
api::ManagedTypeApi,
types::{
AnnotatedValue, ManagedAddress, ManagedBuffer, TxEnv, TxFrom, TxFromSpecified, TxTo,
TxToSpecified,
heap::Address, AnnotatedValue, ManagedAddress, ManagedBuffer, TxEnv, TxFrom,
TxFromSpecified, TxTo, TxToSpecified,
},
};

use super::TestSCAddress;

const ADDRESS_PREFIX: &str = "address:";

/// Encodes a dummy address, to be used for tests.
Expand Down Expand Up @@ -40,12 +42,50 @@ impl<'a> TestAddress<'a> {
result
}

pub fn to_address(&self) -> Address {
self.eval_to_array().into()
}

pub fn to_managed_address<Api: ManagedTypeApi>(&self) -> ManagedAddress<Api> {
self.eval_to_array().into()
}

#[cfg(feature = "alloc")]
pub fn eval_to_expr(&self) -> alloc::string::String {
alloc::format!("{ADDRESS_PREFIX}{}", self.name)
}
}

impl<'a, 'b> PartialEq<TestSCAddress<'b>> for TestAddress<'a> {
fn eq(&self, other: &TestSCAddress) -> bool {
self.to_address() == other.to_address()
}
}

impl<'a> PartialEq<Address> for TestAddress<'a> {
fn eq(&self, other: &Address) -> bool {
&self.to_address() == other
}
}

impl<'a> PartialEq<TestAddress<'a>> for Address {
fn eq(&self, other: &TestAddress<'a>) -> bool {
self == &other.to_address()
}
}

impl<'a, Api: ManagedTypeApi> PartialEq<ManagedAddress<Api>> for TestAddress<'a> {
fn eq(&self, other: &ManagedAddress<Api>) -> bool {
self.to_address() == other.to_address()
}
}

impl<'a, Api: ManagedTypeApi> PartialEq<TestAddress<'a>> for ManagedAddress<Api> {
fn eq(&self, other: &TestAddress<'a>) -> bool {
self.to_address() == other.to_address()
}
}

impl<'a, Env> AnnotatedValue<Env, ManagedAddress<Env::Api>> for TestAddress<'a>
where
Env: TxEnv,
Expand Down
39 changes: 37 additions & 2 deletions framework/base/src/types/interaction/expr/test_sc_address.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ use crate::{
},
};

use super::TestAddress;

const SC_PREFIX: &str = "sc:";
const VM_TYPE_LEN: usize = 2;
const DEFAULT_VM_TYPE: &[u8] = &[5, 0];
Expand Down Expand Up @@ -48,8 +50,41 @@ where

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

pub fn to_managed_address<Api: ManagedTypeApi>(&self) -> ManagedAddress<Api> {
self.eval_to_array().into()
}
}

impl<'a, 'b> PartialEq<TestAddress<'b>> for TestSCAddress<'a> {
fn eq(&self, other: &TestAddress) -> bool {
self.to_address() == other.to_address()
}
}

impl<'a> PartialEq<Address> for TestSCAddress<'a> {
fn eq(&self, other: &Address) -> bool {
&self.to_address() == other
}
}

impl<'a> PartialEq<TestSCAddress<'a>> for Address {
fn eq(&self, other: &TestSCAddress<'a>) -> bool {
self == &other.to_address()
}
}

impl<'a, Api: ManagedTypeApi> PartialEq<ManagedAddress<Api>> for TestSCAddress<'a> {
fn eq(&self, other: &ManagedAddress<Api>) -> bool {
self.to_address() == other.to_address()
}
}

impl<'a, Api: ManagedTypeApi> PartialEq<TestSCAddress<'a>> for ManagedAddress<Api> {
fn eq(&self, other: &TestSCAddress<'a>) -> bool {
self.to_address() == other.to_address()
}
}

Expand Down
39 changes: 39 additions & 0 deletions framework/scenario/tests/address_eq_test.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
use multiversx_sc::types::TestAddress;
use multiversx_sc_scenario::api::StaticApi;

const ALICE: TestAddress = TestAddress::new("alice");
const SC_ADDR: TestAddress = TestAddress::new("sc");

#[test]
fn test_address_eq() {
let alice2 = TestAddress::new("alice");

assert_eq!(ALICE, alice2);
assert_eq!(ALICE, alice2.to_address());
assert_eq!(ALICE.to_address(), alice2);
assert_eq!(ALICE.to_address(), alice2.to_address());

assert_eq!(ALICE, alice2.to_managed_address::<StaticApi>());
assert_eq!(ALICE.to_managed_address::<StaticApi>(), alice2);
assert_eq!(
ALICE.to_managed_address::<StaticApi>(),
alice2.to_managed_address::<StaticApi>()
);
}

#[test]
fn test_sc_address_eq() {
let sc2 = TestAddress::new("sc");

assert_eq!(SC_ADDR, sc2);
assert_eq!(SC_ADDR, sc2.to_address());
assert_eq!(SC_ADDR.to_address(), sc2);
assert_eq!(SC_ADDR.to_address(), sc2.to_address());

assert_eq!(SC_ADDR, sc2.to_managed_address::<StaticApi>());
assert_eq!(SC_ADDR.to_managed_address::<StaticApi>(), sc2);
assert_eq!(
SC_ADDR.to_managed_address::<StaticApi>(),
sc2.to_managed_address::<StaticApi>()
);
}

0 comments on commit 4fee541

Please sign in to comment.