Skip to content

Commit

Permalink
Add tests to cw20 InstantiateMsg
Browse files Browse the repository at this point in the history
  • Loading branch information
etienne-napoleone committed Aug 10, 2022
1 parent eafc009 commit d0d73b8
Showing 1 changed file with 50 additions and 0 deletions.
50 changes: 50 additions & 0 deletions contracts/cw20-base/src/msg.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ pub struct InstantiateMarketingInfo {
}

#[derive(Serialize, Deserialize, JsonSchema, Debug, Clone, PartialEq)]
#[cfg_attr(test, derive(Default))]
pub struct InstantiateMsg {
pub name: String,
pub symbol: String,
Expand Down Expand Up @@ -122,3 +123,52 @@ pub enum QueryMsg {

#[derive(Serialize, Deserialize, JsonSchema)]
pub struct MigrateMsg {}

#[cfg(test)]
mod tests {
use super::*;

#[test]
fn validate_instantiatemsg_name() {
// Too short
let mut msg = InstantiateMsg {
name: str::repeat("a", 2),
..InstantiateMsg::default()
};
assert!(!msg.has_valid_name());

// In the correct length range
msg.name = str::repeat("a", 3);
assert!(msg.has_valid_name());

// Too long
msg.name = str::repeat("a", 51);
assert!(!msg.has_valid_name());
}

#[test]
fn validate_instantiatemsg_symbol() {
// Too short
let mut msg = InstantiateMsg {
symbol: str::repeat("a", 2),
..InstantiateMsg::default()
};
assert!(!msg.has_valid_symbol());

// In the correct length range
msg.symbol = str::repeat("a", 3);
assert!(msg.has_valid_symbol());

// Too long
msg.symbol = str::repeat("a", 13);
assert!(!msg.has_valid_symbol());

// Has illegal char
let illegal_chars = [[64u8], [91u8], [123u8]];
illegal_chars.iter().for_each(|c| {
let c = std::str::from_utf8(c).unwrap();
msg.symbol = str::repeat(c, 3);
assert!(!msg.has_valid_symbol());
});
}
}

0 comments on commit d0d73b8

Please sign in to comment.