mirror of
https://github.com/cloud-hypervisor/cloud-hypervisor.git
synced 2026-08-05 02:19:16 +00:00
misc: Eliminate use of assert!((...).is_ok())
Asserting on .is_ok()/.is_err() leads to hard to debug failures (as if the test fails, it will only say "assertion failed: false". We replace these with `.unwrap()`, which also prints the exact error variant that was unexpectedly encountered (we can to this these days thanks to efforts to implement Display and Debug for our error types). If the assert!((...).is_ok()) was followed by an .unwrap() anyway, we just drop the assert. Inspired by and quoted from @roypat. Signed-off-by: Ruoqing He <heruoqing@iscas.ac.cn>
This commit is contained in:
@@ -146,16 +146,16 @@ mod tests {
|
||||
#[test]
|
||||
fn test_mac_addr() {
|
||||
// too long
|
||||
assert!(MacAddr::parse_str("aa:aa:aa:aa:aa:aa:aa").is_err());
|
||||
MacAddr::parse_str("aa:aa:aa:aa:aa:aa:aa").unwrap_err();
|
||||
|
||||
// invalid hex
|
||||
assert!(MacAddr::parse_str("aa:aa:aa:aa:aa:ax").is_err());
|
||||
MacAddr::parse_str("aa:aa:aa:aa:aa:ax").unwrap_err();
|
||||
|
||||
// single digit mac address component should be invalid
|
||||
assert!(MacAddr::parse_str("aa:aa:aa:aa:aa:b").is_err());
|
||||
MacAddr::parse_str("aa:aa:aa:aa:aa:b").unwrap_err();
|
||||
|
||||
// components with more than two digits should also be invalid
|
||||
assert!(MacAddr::parse_str("aa:aa:aa:aa:aa:bbb").is_err());
|
||||
MacAddr::parse_str("aa:aa:aa:aa:aa:bbb").unwrap_err();
|
||||
|
||||
let mac = MacAddr::parse_str("12:34:56:78:9a:BC").unwrap();
|
||||
|
||||
@@ -171,12 +171,12 @@ mod tests {
|
||||
let src2 = [0x01, 0x02, 0x03, 0x04, 0x05, 0x06];
|
||||
let src3 = [0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07];
|
||||
|
||||
assert!(MacAddr::from_bytes(&src1[..]).is_err());
|
||||
MacAddr::from_bytes(&src1[..]).unwrap_err();
|
||||
|
||||
let x = MacAddr::from_bytes(&src2[..]).unwrap();
|
||||
assert_eq!(x.to_string(), String::from("01:02:03:04:05:06"));
|
||||
|
||||
assert!(MacAddr::from_bytes(&src3[..]).is_err());
|
||||
MacAddr::from_bytes(&src3[..]).unwrap_err();
|
||||
}
|
||||
|
||||
#[test]
|
||||
|
||||
Reference in New Issue
Block a user