[move-cli] added unit tests for short_str_lossless
Closes:
@@ -47,7 +47,11 @@ impl AccountAddress {
pub fn short_str_lossless(&self) -> String {
let hex_str = hex::encode(&self.0).trim_start_matches('0').to_string();
- if hex_str.is_empty() { "0".to_string() } else { hex_str }
+ if hex_str.is_empty() {
+ "0".to_string()
+ } else {
+ hex_str
+ }
}
pub fn to_vec(&self) -> Vec<u8> {
@@ -257,3 +261,46 @@ impl Serialize for AccountAddress {
}
}
}
+
+#[cfg(test)]
+mod tests {
+ use super::*;
+ use hex::FromHex;
+
+ #[test]
+ fn test_short_str_lossless() {
+ let hex = Vec::from_hex("00c0f1f95c5b1c5f0eda533eff269000")
+ .expect("You must provide a valid Hex format");
+
+ let address: AccountAddress = AccountAddress::try_from(&hex[..]).unwrap_or_else(|_| {
+ panic!(
+ "The address {:?} is of invalid length. Addresses must be 16-bytes long",
+ &hex
+ )
+ });
+
+ let string_lossless = address.short_str_lossless();
+
+ assert_eq!(
+ "c0f1f95c5b1c5f0eda533eff269000".to_string(),
+ string_lossless
+ );
+ }
+
+ #[test]
+ fn test_short_str_lossless_zero() {
+ let hex = Vec::from_hex("00000000000000000000000000000000")
+ .expect("You must provide a valid Hex format");
+
+ let address: AccountAddress = AccountAddress::try_from(&hex[..]).unwrap_or_else(|_| {
+ panic!(
+ "The address {:?} is of invalid length. Addresses must be 16-bytes long",
+ &hex
+ )
+ });
+
+ let string_lossless = address.short_str_lossless();
+
+ assert_eq!("0".to_string(), string_lossless);
+ }
+}
