Skip to content

Commit f15f66e

Browse files
committed
numfmt: add unit tests for detailed_error_message, parse_number_part, and apply_grouping
1 parent 9ce8c7e commit f15f66e

1 file changed

Lines changed: 136 additions & 0 deletions

File tree

src/uu/numfmt/src/format.rs

Lines changed: 136 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -994,4 +994,140 @@ mod tests {
994994
assert_eq!(raw_suffix as i32, RawSuffix::Q as i32);
995995
assert_eq!(value, 5.0);
996996
}
997+
998+
#[test]
999+
fn test_detailed_error_message_empty() {
1000+
let result = detailed_error_message("", Unit::Auto, "");
1001+
assert!(result.is_some());
1002+
}
1003+
1004+
#[test]
1005+
fn test_detailed_error_message_valid_number() {
1006+
// A plain valid number should return None (no error)
1007+
assert!(detailed_error_message("123", Unit::Auto, "").is_none());
1008+
assert!(detailed_error_message("5K", Unit::Auto, "").is_none());
1009+
assert!(detailed_error_message("-3.5M", Unit::Auto, "").is_none());
1010+
}
1011+
1012+
#[test]
1013+
fn test_detailed_error_message_trailing_garbage() {
1014+
// Number with suffix followed by extra chars
1015+
let result = detailed_error_message("5Kx", Unit::Auto, "").unwrap();
1016+
assert!(
1017+
result.contains("numfmt-error-invalid-specific-suffix")
1018+
|| result.contains("invalid suffix")
1019+
);
1020+
}
1021+
1022+
#[test]
1023+
fn test_detailed_error_message_dot_only() {
1024+
let result = detailed_error_message(".", Unit::Auto, "").unwrap();
1025+
assert!(
1026+
result.contains("numfmt-error-invalid-suffix") || result.contains("invalid suffix")
1027+
);
1028+
}
1029+
1030+
#[test]
1031+
fn test_detailed_error_message_trailing_dot() {
1032+
let result = detailed_error_message("5.", Unit::Auto, "").unwrap();
1033+
assert!(
1034+
result.contains("numfmt-error-invalid-number") || result.contains("invalid number")
1035+
);
1036+
}
1037+
1038+
#[test]
1039+
fn test_detailed_error_message_unit_separator() {
1040+
// With unit separator, "5 K" is valid
1041+
assert!(detailed_error_message("5 K", Unit::Auto, " ").is_none());
1042+
1043+
// "5 Kx" should report trailing garbage after the suffix
1044+
let result = detailed_error_message("5 Kx", Unit::Auto, " ");
1045+
assert!(result.is_some());
1046+
}
1047+
1048+
#[test]
1049+
fn test_parse_number_part_valid() {
1050+
assert_eq!(parse_number_part("42", "42").unwrap(), 42.0);
1051+
assert_eq!(parse_number_part("-3.5", "-3.5").unwrap(), -3.5);
1052+
assert_eq!(parse_number_part("0", "0").unwrap(), 0.0);
1053+
}
1054+
1055+
#[test]
1056+
fn test_parse_number_part_trailing_dot() {
1057+
assert!(parse_number_part("5.", "5.").is_err());
1058+
}
1059+
1060+
#[test]
1061+
fn test_parse_number_part_non_numeric() {
1062+
assert!(parse_number_part("abc", "abc").is_err());
1063+
assert!(parse_number_part("", "").is_err());
1064+
}
1065+
1066+
#[test]
1067+
fn test_apply_grouping_short_numbers() {
1068+
// Numbers with fewer than 4 digits should be unchanged
1069+
assert_eq!(apply_grouping("0"), "0");
1070+
assert_eq!(apply_grouping("999"), "999");
1071+
assert_eq!(apply_grouping("-99"), "-99");
1072+
}
1073+
1074+
#[test]
1075+
fn test_apply_grouping_with_fraction() {
1076+
// Fraction part should not be grouped
1077+
let result = apply_grouping("1234.567");
1078+
// Depending on locale, separator may or may not be present
1079+
assert!(result.contains("567"));
1080+
assert!(result.contains('.'));
1081+
}
1082+
1083+
#[test]
1084+
fn test_apply_grouping_negative() {
1085+
let result = apply_grouping("-1234");
1086+
assert!(result.starts_with('-'));
1087+
}
1088+
1089+
#[test]
1090+
fn test_apply_grouping_large_numbers() {
1091+
// These tests verify grouping structure; actual separator depends on locale
1092+
let result = apply_grouping("1000000");
1093+
// Should have separators inserted (length grows if separator is non-empty)
1094+
assert!(result.len() >= 7);
1095+
1096+
let result = apply_grouping("1234567890");
1097+
assert!(result.len() >= 10);
1098+
1099+
let result = apply_grouping("-9999999999999");
1100+
assert!(result.starts_with('-'));
1101+
assert!(result.len() >= 13);
1102+
}
1103+
1104+
#[test]
1105+
fn test_apply_grouping_tiny_fraction() {
1106+
// Small decimal: integer part < 4 digits, so no grouping
1107+
assert_eq!(apply_grouping("0.000001"), "0.000001");
1108+
assert_eq!(apply_grouping("1.23456789"), "1.23456789");
1109+
}
1110+
1111+
#[test]
1112+
fn test_apply_grouping_exactly_four_digits() {
1113+
let result = apply_grouping("1000");
1114+
// Should be grouped (4 digits)
1115+
assert!(result.len() >= 4);
1116+
}
1117+
1118+
#[test]
1119+
fn test_parse_number_part_large_and_tiny() {
1120+
assert_eq!(
1121+
parse_number_part("999999999999", "999999999999").unwrap(),
1122+
999_999_999_999.0
1123+
);
1124+
assert_eq!(
1125+
parse_number_part("0.000000001", "0.000000001").unwrap(),
1126+
0.000_000_001
1127+
);
1128+
assert_eq!(
1129+
parse_number_part("-99999999", "-99999999").unwrap(),
1130+
-99_999_999.0
1131+
);
1132+
}
9971133
}

0 commit comments

Comments
 (0)