Skip to content

Commit

Permalink
Added tests.
Browse files Browse the repository at this point in the history
  • Loading branch information
Dariusz Depta committed Sep 8, 2023
1 parent 2ba0ef4 commit 9dc633c
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 4 deletions.
9 changes: 7 additions & 2 deletions contracts/cw20-ics20/src/contract.rs
Original file line number Diff line number Diff line change
Expand Up @@ -234,7 +234,7 @@ pub fn migrate(mut deps: DepsMut, env: Env, msg: MigrateMsg) -> Result<Response,
previous_version: stored.version,
});
}
// run the v1->v2 converstion if we are v1 style
// run the v1->v2 conversion if we are v1 style
if storage_version <= MIGRATE_VERSION_2.parse().map_err(from_semver)? {
let old_config = v1::CONFIG.load(deps.storage)?;
ADMIN.set(deps.branch(), Some(old_config.gov_contract))?;
Expand All @@ -244,7 +244,7 @@ pub fn migrate(mut deps: DepsMut, env: Env, msg: MigrateMsg) -> Result<Response,
};
CONFIG.save(deps.storage, &config)?;
}
// run the v2->v3 converstion if we are v2 style
// run the v2->v3 conversion if we are v2 style
if storage_version <= MIGRATE_VERSION_3.parse().map_err(from_semver)? {
v2::update_balances(deps.branch(), &env)?;
}
Expand Down Expand Up @@ -702,4 +702,9 @@ mod test {
panic!("Unexpected return message: {:?}", res.messages[0]);
}
}

#[test]
fn invalid_contract_version_should_fail() {
assert!("A.1.0".parse::<Version>().map_err(from_semver).is_err());
}
}
45 changes: 43 additions & 2 deletions packages/cw20/src/balance.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use cosmwasm_schema::cw_serde;
use cosmwasm_std::Coin;

use std::fmt;
use std::{fmt, fmt::Display};

use cw_utils::NativeBalance;

Expand All @@ -20,7 +20,7 @@ impl Default for Balance {
}
}

impl fmt::Display for Balance {
impl Display for Balance {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
match self {
Balance::Native(native) => write!(f, "{native}"),
Expand Down Expand Up @@ -58,3 +58,44 @@ impl From<Cw20CoinVerified> for Balance {
Balance::Cw20(cw20_coin)
}
}

#[cfg(test)]
mod tests {
use super::*;
use cosmwasm_std::{Addr, Uint128};

#[test]
fn default_balance_is_native() {
let balance: Balance = Default::default();
assert!(matches!(balance, Balance::Native(_)));
}

#[test]
fn displaying_native_balance_works() {
let balance: Balance = Default::default();
assert_eq!("", format!("{balance}",));
}

#[test]
fn displaying_cw20_balance_works() {
let balance = Balance::Cw20(Cw20CoinVerified {
address: Addr::unchecked("sender"),
amount: Uint128::zero(),
});
assert_eq!("address: sender, amount: 0", format!("{balance}",));
}

#[test]
fn default_native_balance_is_empty() {
assert!(Balance::default().is_empty());
}

#[test]
fn cw20_balance_with_zero_amount_is_empty() {
assert!(Balance::Cw20(Cw20CoinVerified {
address: Addr::unchecked("sender"),
amount: Uint128::zero(),
})
.is_empty());
}
}

0 comments on commit 9dc633c

Please sign in to comment.