Skip to content

Commit 13e4c1e

Browse files
committed
transaction/fee: add unit tests for fee formatting
So we can change its implementation knowing it remains correct.
1 parent 7ab7233 commit 13e4c1e

File tree

1 file changed

+23
-1
lines changed

1 file changed

+23
-1
lines changed

src/rust/bitbox02-rust/src/workflow/transaction.rs

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ use crate::bb02_async::option_no_screensaver;
1616
use core::cell::RefCell;
1717

1818
use alloc::boxed::Box;
19+
use alloc::string::String;
1920

2021
pub struct UserAbort;
2122

@@ -33,6 +34,10 @@ pub async fn verify_recipient(recipient: &str, amount: &str) -> Result<(), UserA
3334
option_no_screensaver(&result).await
3435
}
3536

37+
fn format_percentage(p: f64) -> String {
38+
format!("{:.1}", p)
39+
}
40+
3641
pub async fn verify_total_fee(
3742
total: &str,
3843
fee: &str,
@@ -57,7 +62,10 @@ pub async fn verify_total_fee(
5762
if let Some(fee_percentage) = fee_percentage {
5863
match super::confirm::confirm(&super::confirm::Params {
5964
title: "High fee",
60-
body: &format!("The fee is {:.1}%\nthe send amount.\nProceed?", fee_percentage),
65+
body: &format!(
66+
"The fee is {}%\nthe send amount.\nProceed?",
67+
format_percentage(fee_percentage)
68+
),
6169
longtouch: true,
6270
..Default::default()
6371
})
@@ -69,3 +77,17 @@ pub async fn verify_total_fee(
6977
}
7078
Ok(())
7179
}
80+
81+
#[cfg(test)]
82+
mod tests {
83+
use super::*;
84+
85+
#[test]
86+
fn test_format_percentage() {
87+
assert_eq!(format_percentage(0.), "0.0");
88+
assert_eq!(format_percentage(10.0), "10.0");
89+
assert_eq!(format_percentage(10.1), "10.1");
90+
assert_eq!(format_percentage(10.14), "10.1");
91+
assert_eq!(format_percentage(10.15), "10.2");
92+
}
93+
}

0 commit comments

Comments
 (0)