use ic_host_text::table::Alignment;
use crate::presentation::token_amount::e8s_decimal_text;
use crate::presentation::{
text_value::{field, optional_number_text, optional_text, table, truncate_text, yes_no},
token_amount::base_units_decimal_text,
};
#[test]
fn base_unit_adapter_preserves_input_policy_and_decimal_scale() {
for (value, decimals, expected) in [
("0", 8, "0.00"),
("123", 0, "123.00"),
("123456789", 8, "1.23"),
(" 000_123_500_000 ", 8, "1.24"),
("___", 8, "0.00"),
("5", u8::MAX, "0.00"),
(
"340282366920938463463374607431768211456",
2,
"3402823669209384634633746074317682114.56",
),
] {
assert_eq!(base_units_decimal_text(value, decimals), expected);
}
assert_eq!(
base_units_decimal_text(&format!("5{}", "0".repeat(252)), u8::MAX),
"0.01"
);
assert_eq!(base_units_decimal_text(&"9".repeat(255), u8::MAX), "1.00");
assert_eq!(
base_units_decimal_text(&format!("{}500000", "0".repeat(4096)), 8),
"0.01"
);
for invalid in [
"",
" ",
"-123",
"12.3",
"12é",
"not-a-number",
" invalid ",
] {
assert_eq!(base_units_decimal_text(invalid, 8), invalid);
}
}
#[test]
fn e8s_render_as_two_decimal_token_amounts() {
assert_eq!(e8s_decimal_text(0), "0.00");
assert_eq!(e8s_decimal_text(100_000_000), "1.00");
assert_eq!(e8s_decimal_text(123_456_789), "1.23");
assert_eq!(e8s_decimal_text(123_500_000), "1.24");
}
#[test]
fn report_tables_keep_query_column_gap_separator_and_alignment() {
assert_eq!(
table(
&["ID", "NAME"],
&[["1", "Dragginz"]],
&[Alignment::Right, Alignment::Left],
),
"ID NAME\n-- --------\n 1 Dragginz"
);
}
#[test]
fn scalar_text_helpers_escape_controls_without_changing_raw_models() {
let optional = "line one\nline two\u{1b}[31m".to_string();
assert_eq!(
optional_text(Some(&optional)),
"line one\\nline two\\u{1b}[31m"
);
assert_eq!(truncate_text("abcdef", 3), "ab…");
assert_eq!(optional_number_text(None::<u64>), "-");
assert_eq!(optional_number_text(Some(42_u64)), "42");
assert_eq!(yes_no(true), "yes");
assert_eq!(yes_no(false), "no");
assert!(optional.contains('\n'));
assert!(optional.contains('\u{1b}'));
}
#[test]
fn labelled_fields_escape_values_without_changing_printable_unicode_or_numbers() {
assert_eq!(
field("title", &"界\n\u{1b}[31m\u{202e}x"),
"title: 界\\n\\u{1b}[31m\\u{202e}x"
);
assert_eq!(field("count", &42), "count: 42");
assert_eq!(field("path", &r"literal\n"), r"path: literal\n");
let title = "界\n\u{1b}[31m\u{202e}x";
assert_eq!(
crate::presentation::text_value::report_fields!(title; "nested.title" = title),
["nested.title: 界\\n\\u{1b}[31m\\u{202e}x"]
);
}