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(tests): refactor table driven tests #881

Open
rnbguy opened this issue Sep 26, 2023 · 2 comments
Open

imp(tests): refactor table driven tests #881

rnbguy opened this issue Sep 26, 2023 · 2 comments
Assignees
Labels
A: blocked Admin: blocked by another (internal/external) issue or PR O: code-hygiene Objective: aims to improve code hygiene O: maintainability Objective: cause to ease modification, fault corrections and improve code understanding O: testing Objective: aims to improve testing coverage

Comments

@rnbguy
Copy link
Collaborator

rnbguy commented Sep 26, 2023

Improvement Summary

Currently, some of the rust tests contain a group of table/data-driven tests. These tests will fail if one in the group fails.

fn test_parse_raw_coin() -> Result<(), TokenTransferError> {
{
let coin = RawCoin::from_str("123stake")?;
assert_eq!(coin.denom, "stake");
assert_eq!(coin.amount, 123u64.into());
}
{
let coin = RawCoin::from_str("1a1")?;
assert_eq!(coin.denom, "a1");
assert_eq!(coin.amount, 1u64.into());
}
{
let coin = RawCoin::from_str("0x1/:.\\_-")?;
assert_eq!(coin.denom, "x1/:.\\_-");
assert_eq!(coin.amount, 0u64.into());
}
{
// `!` is not allowed
let res = RawCoin::from_str("0x!");
assert!(res.is_err());
}

We want to treat each testcases individually. If one fails, the others in the group may continue.

Proposal

The current Rust release doesn't support this feature. The common opinion in the Rust community is to keep each testcases in a separate test method, potentially using a macro.

We don't want to implement/maintain our macro. So we will use rstest as a solution. The following is a refactored code of the above example using rstest.

#[rstest]
#[case::nat("123stake", RawCoin::new(123, "stake"))]
#[case::zero("0stake", RawCoin::new(0, "stake"))]
#[case::u256_max(
"115792089237316195423570985008687907853269984665640564039457584007913129639935stake",
RawCoin::new(
[u64::MAX; 4],
"stake"
)
)]
#[case::digit_in_denom("1a1", RawCoin::new(1, "a1"))]
#[case::chars_in_denom("0x1/:._-", RawCoin::new(0, "x1/:._-"))]
#[case::ibc_denom("1234ibc/a0B1C", RawCoin::new(1234, "ibc/a0B1C"))]
fn test_parse_raw_coin(#[case] parsed: RawCoin, #[case] expected: RawCoin) {
assert_eq!(parsed, expected);
}

@rnbguy rnbguy added O: testing Objective: aims to improve testing coverage O: code-hygiene Objective: aims to improve code hygiene O: maintainability Objective: cause to ease modification, fault corrections and improve code understanding labels Sep 26, 2023
@rnbguy rnbguy self-assigned this Sep 26, 2023
@rnbguy rnbguy changed the title Refactor table driven tests imp(tests): refactor table driven tests Jan 18, 2024
@kien6034
Copy link

kien6034 commented Apr 1, 2024

hi, can i work on this

@rnbguy rnbguy added the A: blocked Admin: blocked by another (internal/external) issue or PR label Apr 2, 2024
@rnbguy
Copy link
Collaborator Author

rnbguy commented Apr 2, 2024

@kien6034 thanks for your interest! but this requires significant changes, that

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A: blocked Admin: blocked by another (internal/external) issue or PR O: code-hygiene Objective: aims to improve code hygiene O: maintainability Objective: cause to ease modification, fault corrections and improve code understanding O: testing Objective: aims to improve testing coverage
Projects
Status: 🏗️ In Progress
Development

No branches or pull requests

2 participants