Skip to content

Commit dc369f8

Browse files
committed
stty: Add comprehensive unit and integration tests for error handling
- Add unit tests for parse_rows_cols() with edge cases and wraparound - Add unit tests for string_to_baud() with platform-specific handling - Add unit tests for string_to_combo() with all combo modes - Add 17 integration tests for missing arguments and invalid inputs - Enhance test_invalid_arg() with better error message assertions - Update coverage script for improved reporting Coverage improved from 22.26% to 23.14% regions.
1 parent faf10db commit dc369f8

File tree

3 files changed

+424
-4
lines changed

3 files changed

+424
-4
lines changed

src/uu/stty/src/stty.rs

Lines changed: 207 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1176,4 +1176,211 @@ mod tests {
11761176
assert_eq!(get_sane_control_char(S::VKILL), 21); // ^U
11771177
assert_eq!(get_sane_control_char(S::VEOF), 4); // ^D
11781178
}
1179+
1180+
// Additional tests for parse_rows_cols
1181+
#[test]
1182+
fn test_parse_rows_cols_valid() {
1183+
assert_eq!(parse_rows_cols("80"), Some(80));
1184+
assert_eq!(parse_rows_cols("65535"), Some(65535));
1185+
assert_eq!(parse_rows_cols("0"), Some(0));
1186+
assert_eq!(parse_rows_cols("1"), Some(1));
1187+
}
1188+
1189+
#[test]
1190+
fn test_parse_rows_cols_wraparound() {
1191+
// Test u16 wraparound: (u16::MAX + 1) % (u16::MAX + 1) = 0
1192+
assert_eq!(parse_rows_cols("131071"), Some(65535)); // (2*65536 - 1) % 65536 = 65535
1193+
assert_eq!(parse_rows_cols("131072"), Some(0)); // (2*65536) % 65536 = 0
1194+
}
1195+
1196+
#[test]
1197+
fn test_parse_rows_cols_invalid() {
1198+
assert_eq!(parse_rows_cols(""), None);
1199+
assert_eq!(parse_rows_cols("abc"), None);
1200+
assert_eq!(parse_rows_cols("-1"), None);
1201+
assert_eq!(parse_rows_cols("12.5"), None);
1202+
assert_eq!(parse_rows_cols("not_a_number"), None);
1203+
}
1204+
1205+
// Tests for string_to_baud
1206+
#[test]
1207+
fn test_string_to_baud_valid() {
1208+
#[cfg(not(any(
1209+
target_os = "freebsd",
1210+
target_os = "dragonfly",
1211+
target_os = "ios",
1212+
target_os = "macos",
1213+
target_os = "netbsd",
1214+
target_os = "openbsd"
1215+
)))]
1216+
{
1217+
assert!(string_to_baud("9600").is_some());
1218+
assert!(string_to_baud("115200").is_some());
1219+
assert!(string_to_baud("38400").is_some());
1220+
assert!(string_to_baud("19200").is_some());
1221+
}
1222+
1223+
#[cfg(any(
1224+
target_os = "freebsd",
1225+
target_os = "dragonfly",
1226+
target_os = "ios",
1227+
target_os = "macos",
1228+
target_os = "netbsd",
1229+
target_os = "openbsd"
1230+
))]
1231+
{
1232+
assert!(string_to_baud("9600").is_some());
1233+
assert!(string_to_baud("115200").is_some());
1234+
assert!(string_to_baud("1000000").is_some());
1235+
assert!(string_to_baud("0").is_some());
1236+
}
1237+
}
1238+
1239+
#[test]
1240+
fn test_string_to_baud_invalid() {
1241+
#[cfg(not(any(
1242+
target_os = "freebsd",
1243+
target_os = "dragonfly",
1244+
target_os = "ios",
1245+
target_os = "macos",
1246+
target_os = "netbsd",
1247+
target_os = "openbsd"
1248+
)))]
1249+
{
1250+
assert_eq!(string_to_baud("995"), None);
1251+
assert_eq!(string_to_baud("invalid"), None);
1252+
assert_eq!(string_to_baud(""), None);
1253+
assert_eq!(string_to_baud("abc"), None);
1254+
}
1255+
}
1256+
1257+
// Tests for string_to_combo
1258+
#[test]
1259+
fn test_string_to_combo_valid() {
1260+
assert_eq!(string_to_combo("sane"), Some("sane"));
1261+
assert_eq!(string_to_combo("raw"), Some("raw"));
1262+
assert_eq!(string_to_combo("cooked"), Some("cooked"));
1263+
assert_eq!(string_to_combo("-raw"), Some("-raw"));
1264+
assert_eq!(string_to_combo("-cooked"), Some("-cooked"));
1265+
assert_eq!(string_to_combo("cbreak"), Some("cbreak"));
1266+
assert_eq!(string_to_combo("-cbreak"), Some("-cbreak"));
1267+
assert_eq!(string_to_combo("nl"), Some("nl"));
1268+
assert_eq!(string_to_combo("-nl"), Some("-nl"));
1269+
assert_eq!(string_to_combo("ek"), Some("ek"));
1270+
assert_eq!(string_to_combo("evenp"), Some("evenp"));
1271+
assert_eq!(string_to_combo("-evenp"), Some("-evenp"));
1272+
assert_eq!(string_to_combo("parity"), Some("parity"));
1273+
assert_eq!(string_to_combo("-parity"), Some("-parity"));
1274+
assert_eq!(string_to_combo("oddp"), Some("oddp"));
1275+
assert_eq!(string_to_combo("-oddp"), Some("-oddp"));
1276+
assert_eq!(string_to_combo("pass8"), Some("pass8"));
1277+
assert_eq!(string_to_combo("-pass8"), Some("-pass8"));
1278+
assert_eq!(string_to_combo("litout"), Some("litout"));
1279+
assert_eq!(string_to_combo("-litout"), Some("-litout"));
1280+
assert_eq!(string_to_combo("crt"), Some("crt"));
1281+
assert_eq!(string_to_combo("dec"), Some("dec"));
1282+
assert_eq!(string_to_combo("decctlq"), Some("decctlq"));
1283+
assert_eq!(string_to_combo("-decctlq"), Some("-decctlq"));
1284+
}
1285+
1286+
#[test]
1287+
fn test_string_to_combo_invalid() {
1288+
assert_eq!(string_to_combo("notacombo"), None);
1289+
assert_eq!(string_to_combo(""), None);
1290+
assert_eq!(string_to_combo("invalid"), None);
1291+
// Test non-negatable combos with negation
1292+
assert_eq!(string_to_combo("-sane"), None);
1293+
assert_eq!(string_to_combo("-ek"), None);
1294+
assert_eq!(string_to_combo("-crt"), None);
1295+
assert_eq!(string_to_combo("-dec"), None);
1296+
}
1297+
1298+
// Tests for cc_to_index
1299+
#[test]
1300+
fn test_cc_to_index_valid() {
1301+
assert_eq!(cc_to_index("intr"), Some(S::VINTR));
1302+
assert_eq!(cc_to_index("quit"), Some(S::VQUIT));
1303+
assert_eq!(cc_to_index("erase"), Some(S::VERASE));
1304+
assert_eq!(cc_to_index("kill"), Some(S::VKILL));
1305+
assert_eq!(cc_to_index("eof"), Some(S::VEOF));
1306+
assert_eq!(cc_to_index("start"), Some(S::VSTART));
1307+
assert_eq!(cc_to_index("stop"), Some(S::VSTOP));
1308+
assert_eq!(cc_to_index("susp"), Some(S::VSUSP));
1309+
assert_eq!(cc_to_index("rprnt"), Some(S::VREPRINT));
1310+
assert_eq!(cc_to_index("werase"), Some(S::VWERASE));
1311+
assert_eq!(cc_to_index("lnext"), Some(S::VLNEXT));
1312+
assert_eq!(cc_to_index("discard"), Some(S::VDISCARD));
1313+
}
1314+
1315+
#[test]
1316+
fn test_cc_to_index_invalid() {
1317+
assert_eq!(cc_to_index("notachar"), None);
1318+
assert_eq!(cc_to_index(""), None);
1319+
assert_eq!(cc_to_index("INTR"), None); // case sensitive
1320+
assert_eq!(cc_to_index("invalid"), None);
1321+
}
1322+
1323+
// Tests for check_flag_group
1324+
#[test]
1325+
fn test_check_flag_group() {
1326+
let flag_with_group = Flag::new_grouped("cs5", ControlFlags::CS5, ControlFlags::CSIZE);
1327+
let flag_without_group = Flag::new("parenb", ControlFlags::PARENB);
1328+
1329+
assert_eq!(check_flag_group(&flag_with_group, true), true);
1330+
assert_eq!(check_flag_group(&flag_with_group, false), false);
1331+
assert_eq!(check_flag_group(&flag_without_group, true), false);
1332+
assert_eq!(check_flag_group(&flag_without_group, false), false);
1333+
}
1334+
1335+
// Additional tests for get_sane_control_char
1336+
#[test]
1337+
fn test_get_sane_control_char_all_defined() {
1338+
assert_eq!(get_sane_control_char(S::VSTART), 17); // ^Q
1339+
assert_eq!(get_sane_control_char(S::VSTOP), 19); // ^S
1340+
assert_eq!(get_sane_control_char(S::VSUSP), 26); // ^Z
1341+
assert_eq!(get_sane_control_char(S::VREPRINT), 18); // ^R
1342+
assert_eq!(get_sane_control_char(S::VWERASE), 23); // ^W
1343+
assert_eq!(get_sane_control_char(S::VLNEXT), 22); // ^V
1344+
assert_eq!(get_sane_control_char(S::VDISCARD), 15); // ^O
1345+
}
1346+
1347+
// Tests for parse_u8_or_err
1348+
#[test]
1349+
fn test_parse_u8_or_err_valid() {
1350+
assert_eq!(parse_u8_or_err("0").unwrap(), 0);
1351+
assert_eq!(parse_u8_or_err("255").unwrap(), 255);
1352+
assert_eq!(parse_u8_or_err("128").unwrap(), 128);
1353+
assert_eq!(parse_u8_or_err("1").unwrap(), 1);
1354+
}
1355+
1356+
#[test]
1357+
fn test_parse_u8_or_err_overflow() {
1358+
// Test that overflow values return an error
1359+
// Note: In test environment, translate!() returns the key, not the translated string
1360+
let err = parse_u8_or_err("256").unwrap_err();
1361+
assert!(
1362+
err.contains("value-too-large") || err.contains("Value too large") || err.contains("Valeur trop grande"),
1363+
"Expected overflow error, got: {}",
1364+
err
1365+
);
1366+
1367+
assert!(parse_u8_or_err("1000").is_err());
1368+
assert!(parse_u8_or_err("65536").is_err());
1369+
}
1370+
1371+
#[test]
1372+
fn test_parse_u8_or_err_invalid() {
1373+
// Test that invalid values return an error
1374+
// Note: In test environment, translate!() returns the key, not the translated string
1375+
let err = parse_u8_or_err("-1").unwrap_err();
1376+
assert!(
1377+
err.contains("invalid-integer-argument") || err.contains("invalid integer argument") || err.contains("argument entier invalide"),
1378+
"Expected invalid argument error, got: {}",
1379+
err
1380+
);
1381+
1382+
assert!(parse_u8_or_err("abc").is_err());
1383+
assert!(parse_u8_or_err("").is_err());
1384+
assert!(parse_u8_or_err("12.5").is_err());
1385+
}
11791386
}

0 commit comments

Comments
 (0)