Skip to content

Commit

Permalink
Merge pull request #150 from CosmWasm/refactoring
Browse files Browse the repository at this point in the history
Refactoring
  • Loading branch information
DariuszDepta authored Mar 20, 2024
2 parents 14ee82f + 32b9244 commit 80b272b
Show file tree
Hide file tree
Showing 6 changed files with 111 additions and 5 deletions.
4 changes: 2 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@
name = "cw-multi-test"
version = "1.0.0-rc.2"
authors = ["Ethan Frey <[email protected]>"]
edition = "2021"
description = "Testing tools for multi-contract interactions"
license = "Apache-2.0"
repository = "https://github.com/CosmWasm/cw-multi-test"
homepage = "https://cosmwasm.com"
license = "Apache-2.0"
edition = "2021"

[features]
default = []
Expand Down
8 changes: 7 additions & 1 deletion src/addresses.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
//! # Implementation of address generators
//! # Implementation of address conversions and generators
use crate::error::AnyResult;
use crate::{MockApiBech32, MockApiBech32m};
Expand All @@ -21,10 +21,12 @@ pub trait IntoAddr {
}

impl IntoAddr for &str {
/// Converts [&str] into [Addr].
fn into_addr(self) -> Addr {
MockApiBech32::new(DEFAULT_PREFIX).addr_make(self)
}

/// Converts [&str] into [Addr] with custom prefix.
fn into_addr_with_prefix(self, prefix: &'static str) -> Addr {
MockApiBech32::new(prefix).addr_make(self)
}
Expand All @@ -40,10 +42,12 @@ pub trait IntoBech32 {
}

impl IntoBech32 for &str {
/// Converts [&str] into [Addr] containing a string compatible with `Bech32` format with default prefix.
fn into_bech32(self) -> Addr {
MockApiBech32::new(DEFAULT_PREFIX).addr_make(self)
}

/// Converts [&str] into [Addr] containing a string compatible with `Bech32` format with custom prefix.
fn into_bech32_with_prefix(self, prefix: &'static str) -> Addr {
MockApiBech32::new(prefix).addr_make(self)
}
Expand All @@ -58,10 +62,12 @@ pub trait IntoBech32m {
}

impl IntoBech32m for &str {
/// Converts [&str] into [Addr] containing a string compatible with `Bech32m` format with default prefix.
fn into_bech32m(self) -> Addr {
MockApiBech32m::new(DEFAULT_PREFIX).addr_make(self)
}

/// Converts [&str] into [Addr] containing a string compatible with `Bech32m` format with custom prefix.
fn into_bech32m_with_prefix(self, prefix: &'static str) -> Addr {
MockApiBech32m::new(prefix).addr_make(self)
}
Expand Down
2 changes: 1 addition & 1 deletion src/bank.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ const BALANCES: Map<&Addr, NativeBalance> = Map::new("balances");
#[cfg(feature = "cosmwasm_1_3")]
const DENOM_METADATA: Map<String, DenomMetadata> = Map::new("metadata");

/// Default namespace for bank module.
/// Default storage namespace for bank module.
pub const NAMESPACE_BANK: &[u8] = b"bank";

/// A message representing privileged actions in bank module.
Expand Down
2 changes: 1 addition & 1 deletion src/checksums.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
//! # Implementation of the interface to custom checksum generator
//! # Implementation of checksum generator
use cosmwasm_std::{Addr, HexBinary};
use sha2::{Digest, Sha256};
Expand Down
1 change: 1 addition & 0 deletions tests/test_api/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ use hex_literal::hex;
mod test_addr;
mod test_bech32;
mod test_bech32m;
mod test_prefixed;

const SECP256K1_MSG_HASH: [u8; 32] =
hex!("5ae8317d34d1e595e3fa7247db80c0af4320cce1116de187f8f7e2e099c0d8d0");
Expand Down
99 changes: 99 additions & 0 deletions tests/test_api/test_prefixed.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
use super::*;
use cosmwasm_std::CanonicalAddr;
use cw_multi_test::MockApiBech32;

const HUMAN_ADDRESS: &str = "juno1h34lmpywh4upnjdg90cjf4j70aee6z8qqfspugamjp42e4q28kqsksmtyp";

fn api_prefix(prefix: &'static str) -> MockApiBech32 {
MockApiBech32::new(prefix)
}

fn api_juno() -> MockApiBech32 {
api_prefix("juno")
}

fn api_osmo() -> MockApiBech32 {
api_prefix("osmo")
}

#[test]
fn new_api_bech32_should_work() {
let addr = api_juno().addr_make("creator");
assert_eq!(HUMAN_ADDRESS, addr.as_str(),);
}

#[test]
fn address_validate_should_work() {
assert_eq!(
api_juno().addr_validate(HUMAN_ADDRESS).unwrap().as_str(),
HUMAN_ADDRESS
)
}

#[test]
fn address_validate_invalid_address() {
api_juno().addr_validate("creator").unwrap_err();
}

#[test]
fn addr_validate_invalid_prefix() {
api_juno()
.addr_validate(api_osmo().addr_make("creator").as_str())
.unwrap_err();
}

#[test]
fn address_canonicalize_humanize_should_work() {
let api = api_juno();
assert_eq!(
api.addr_humanize(&api.addr_canonicalize(HUMAN_ADDRESS).unwrap())
.unwrap()
.as_str(),
HUMAN_ADDRESS
);
}

#[test]
fn address_humanize_prefix_too_long() {
api_prefix(
"juno_juno_juno_juno_juno_juno_juno_juno_juno_juno_juno_juno_juno_juno_juno_juno_juno_",
)
.addr_humanize(&CanonicalAddr::from([1, 2, 3, 4, 5]))
.unwrap_err();
}

#[test]
fn debug_should_not_panic() {
assert_debug_does_not_panic(&api_juno());
}

#[test]
#[should_panic(
expected = "Generating address failed with reason: hrp is too long, found 85 characters, must be <= 126"
)]
fn address_make_prefix_too_long() {
api_prefix(
"juno_juno_juno_juno_juno_juno_juno_juno_juno_juno_juno_juno_juno_juno_juno_juno_juno_",
)
.addr_make("creator");
}

#[test]
fn secp256k1_verify_works() {
assert_secp256k1_verify_works(&api_juno());
}

#[test]
fn secp256k1_recover_pubkey_works() {
assert_secp256k1_recover_pubkey_works(&api_juno());
}

#[test]
fn ed25519_verify_works() {
assert_ed25519_verify_works(&api_juno());
}

#[test]
fn ed25519_batch_verify_works() {
assert_ed25519_batch_verify_works(&api_juno());
}

0 comments on commit 80b272b

Please sign in to comment.