Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

imp(test): refactor table driven tests #1308

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions ibc-apps/ics20-transfer/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ serde-json = { workspace = true, optional = true }
# ibc dependencies
ibc-core = { workspace = true }
ibc-app-transfer-types = { workspace = true }
rstest.workspace = true

[dev-dependencies]
subtle-encoding = { workspace = true }
Expand Down
91 changes: 39 additions & 52 deletions ibc-apps/ics20-transfer/src/module.rs
Original file line number Diff line number Diff line change
Expand Up @@ -310,66 +310,53 @@ pub fn on_timeout_packet_execute(

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

#[test]
fn test_ack_ser() {
fn ser_json_assert_eq(ack: AcknowledgementStatus, json_str: &str) {
let ser = serde_json::to_string(&ack).unwrap();
assert_eq!(ser, json_str)
}
use rstest::*;

ser_json_assert_eq(
AcknowledgementStatus::success(ack_success_b64()),
r#"{"result":"AQ=="}"#,
);
ser_json_assert_eq(
AcknowledgementStatus::error(TokenTransferError::PacketDataDeserialization.into()),
r#"{"error":"failed to deserialize packet data"}"#,
);
}

#[test]
fn test_ack_success_to_vec() {
let ack_success: Vec<u8> = AcknowledgementStatus::success(ack_success_b64()).into();

// Check that it's the same output as ibc-go
// Note: this also implicitly checks that the ack bytes are non-empty,
// which would make the conversion to `Acknowledgement` panic
assert_eq!(ack_success, br#"{"result":"AQ=="}"#);
use super::*;
#[rstest]
#[case::success(
AcknowledgementStatus::success(ack_success_b64()),
r#"{"result":"AQ=="}"#
)]
#[case::error(
AcknowledgementStatus::error(TokenTransferError::PacketDataDeserialization.into()),
r#"{"error":"failed to deserialize packet data"}"#
)]
fn test_ack_ser(#[case] ack: AcknowledgementStatus, #[case] json_str: &str) {
let ser = serde_json::to_string(&ack).unwrap();
assert_eq!(ser, json_str);
}

#[test]
fn test_ack_error_to_vec() {
let ack_error: Vec<u8> =
AcknowledgementStatus::error(TokenTransferError::PacketDataDeserialization.into())
.into();

#[rstest]
#[case::success(
AcknowledgementStatus::success(ack_success_b64()),
br#"{"result":"AQ=="}"#
)]
#[case::error(
AcknowledgementStatus::error(TokenTransferError::PacketDataDeserialization.into())
, br#"{"error":"failed to deserialize packet data"}"#
)]
fn test_ack_to_vec(#[case] ack: AcknowledgementStatus, #[case] output: &[u8]) {
// Check that it's the same output as ibc-go
// Note: this also implicitly checks that the ack bytes are non-empty,
// which would make the conversion to `Acknowledgement` panic
assert_eq!(
ack_error,
br#"{"error":"failed to deserialize packet data"}"#
);
let ack_vec: Vec<u8> = ack.into();
assert_eq!(ack_vec, output);
}

#[rstest]
#[rstest]
#[case::success(
AcknowledgementStatus::success(ack_success_b64()),
r#"{"result":"AQ=="}"#
)]
#[case::error(
AcknowledgementStatus::error(TokenTransferError::PacketDataDeserialization.into())
, r#"{"error":"failed to deserialize packet data"}"#
)]
#[test]
fn test_ack_de() {
fn de_json_assert_eq(json_str: &str, ack: AcknowledgementStatus) {
let de = serde_json::from_str::<AcknowledgementStatus>(json_str).unwrap();
assert_eq!(de, ack)
}

de_json_assert_eq(
r#"{"result":"AQ=="}"#,
AcknowledgementStatus::success(ack_success_b64()),
);
de_json_assert_eq(
r#"{"error":"failed to deserialize packet data"}"#,
AcknowledgementStatus::error(TokenTransferError::PacketDataDeserialization.into()),
);

assert!(serde_json::from_str::<AcknowledgementStatus>(r#"{"success":"AQ=="}"#).is_err());
fn test_ack_de(#[case] ack: AcknowledgementStatus, #[case] json_str: &str) {
let de = serde_json::from_str::<AcknowledgementStatus>(json_str).unwrap();
assert_eq!(de, ack)
}
}
1 change: 1 addition & 0 deletions ibc-apps/ics721-nft-transfer/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ serde-json = { workspace = true, optional = true }
# ibc dependencies
ibc-app-nft-transfer-types = { workspace = true }
ibc-core = { workspace = true }
rstest.workspace = true

[features]
default = [ "std" ]
Expand Down
103 changes: 53 additions & 50 deletions ibc-apps/ics721-nft-transfer/src/module.rs
Original file line number Diff line number Diff line change
Expand Up @@ -307,65 +307,68 @@ pub fn on_timeout_packet_execute(

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

#[test]
fn test_ack_ser() {
fn ser_json_assert_eq(ack: AcknowledgementStatus, json_str: &str) {
let ser = serde_json::to_string(&ack).unwrap();
assert_eq!(ser, json_str)
}
use super::*;

ser_json_assert_eq(
AcknowledgementStatus::success(ack_success_b64()),
r#"{"result":"AQ=="}"#,
);
ser_json_assert_eq(
AcknowledgementStatus::error(NftTransferError::PacketDataDeserialization.into()),
r#"{"error":"failed to deserialize packet data"}"#,
);
#[rstest]
#[case::success(
AcknowledgementStatus::success(ack_success_b64()),
r#"{"result":"AQ=="}"#
)]
#[case::error(
AcknowledgementStatus::error(NftTransferError::PacketDataDeserialization.into()),
r#"{"error":"failed to deserialize packet data"}"#
)]
fn test_ack_ser(#[case] ack: AcknowledgementStatus, #[case] json_str: &str) {
let ser = serde_json::to_string(&ack).unwrap();
assert_eq!(ser, json_str);
}

#[test]
fn test_ack_success_to_vec() {
let ack_success: Vec<u8> = AcknowledgementStatus::success(ack_success_b64()).into();

#[rstest]
#[case::success(
AcknowledgementStatus::success(ack_success_b64()),
br#"{"result":"AQ=="}"#
)]
#[case::error(
AcknowledgementStatus::error(NftTransferError::PacketDataDeserialization.into()),
br#"{"error":"failed to deserialize packet data"}"#
)]
fn test_ack_to_vec(#[case] ack: AcknowledgementStatus, #[case] output: &[u8]) {
// Check that it's the same output as ibc-go
// Note: this also implicitly checks that the ack bytes are non-empty,
// which would make the conversion to `Acknowledgement` panic
assert_eq!(ack_success, br#"{"result":"AQ=="}"#);
let ack_vec: Vec<u8> = ack.into();
assert_eq!(ack_vec, output);
}

#[test]
fn test_ack_error_to_vec() {
let ack_error: Vec<u8> =
AcknowledgementStatus::error(NftTransferError::PacketDataDeserialization.into()).into();

// Check that it's the same output as ibc-go
// Note: this also implicitly checks that the ack bytes are non-empty,
// which would make the conversion to `Acknowledgement` panic
assert_eq!(
ack_error,
br#"{"error":"failed to deserialize packet data"}"#
);
}

#[test]
fn test_ack_de() {
fn de_json_assert_eq(json_str: &str, ack: AcknowledgementStatus) {
let de = serde_json::from_str::<AcknowledgementStatus>(json_str).unwrap();
assert_eq!(de, ack)
#[rstest]
#[case::success(
AcknowledgementStatus::success(ack_success_b64()),
r#"{"result":"AQ=="}"#,
false
)]
#[case::error(
AcknowledgementStatus::error(NftTransferError::PacketDataDeserialization.into()),
r#"{"error":"failed to deserialize packet data"}"#,
false
)]
#[case::invalid(
AcknowledgementStatus::success(ack_success_b64()), // This value won't be used for invalid case
r#"{"success":"AQ=="}"#,
true
)]
fn test_ack_de(
#[case] expected_ack: AcknowledgementStatus,
#[case] json_str: &str,
#[case] should_err: bool,
) {
let result = serde_json::from_str::<AcknowledgementStatus>(json_str);
if should_err {
assert!(result.is_err());
} else {
let de = result.unwrap();
assert_eq!(de, expected_ack);
}

de_json_assert_eq(
r#"{"result":"AQ=="}"#,
AcknowledgementStatus::success(ack_success_b64()),
);
de_json_assert_eq(
r#"{"error":"failed to deserialize packet data"}"#,
AcknowledgementStatus::error(NftTransferError::PacketDataDeserialization.into()),
);

assert!(serde_json::from_str::<AcknowledgementStatus>(r#"{"success":"AQ=="}"#).is_err());
}
}
Loading