Skip to content

Commit 2d6c87d

Browse files
committed
rust: str: take advantage of the -> Result support in KUnit #[test]'s
Since now we have support for returning `-> Result`s, we can convert some of these tests to use the feature, and serve as a first user for it too. Thus convert them, which allows us to remove some `unwrap()`s. We keep the actual assertions we want to make as explicit ones with `assert*!`s. Reviewed-by: David Gow <davidgow@google.com> Acked-by: Danilo Krummrich <dakr@kernel.org> Link: https://lore.kernel.org/r/20250502215133.1923676-6-ojeda@kernel.org [ Split the `CString` simplification into a new commit. - Miguel ] Signed-off-by: Miguel Ojeda <ojeda@kernel.org>
1 parent 1486554 commit 2d6c87d

File tree

1 file changed

+30
-22
lines changed

1 file changed

+30
-22
lines changed

rust/kernel/str.rs

Lines changed: 30 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -578,7 +578,7 @@ mod tests {
578578

579579
macro_rules! format {
580580
($($f:tt)*) => ({
581-
CString::try_from_fmt(::kernel::fmt!($($f)*)).unwrap().to_str().unwrap()
581+
CString::try_from_fmt(::kernel::fmt!($($f)*))?.to_str()?
582582
})
583583
}
584584

@@ -597,66 +597,72 @@ mod tests {
597597
\\xf0\\xf1\\xf2\\xf3\\xf4\\xf5\\xf6\\xf7\\xf8\\xf9\\xfa\\xfb\\xfc\\xfd\\xfe\\xff";
598598

599599
#[test]
600-
fn test_cstr_to_str() {
600+
fn test_cstr_to_str() -> Result {
601601
let good_bytes = b"\xf0\x9f\xa6\x80\0";
602-
let checked_cstr = CStr::from_bytes_with_nul(good_bytes).unwrap();
603-
let checked_str = checked_cstr.to_str().unwrap();
602+
let checked_cstr = CStr::from_bytes_with_nul(good_bytes)?;
603+
let checked_str = checked_cstr.to_str()?;
604604
assert_eq!(checked_str, "🦀");
605+
Ok(())
605606
}
606607

607608
#[test]
608-
fn test_cstr_to_str_invalid_utf8() {
609+
fn test_cstr_to_str_invalid_utf8() -> Result {
609610
let bad_bytes = b"\xc3\x28\0";
610-
let checked_cstr = CStr::from_bytes_with_nul(bad_bytes).unwrap();
611+
let checked_cstr = CStr::from_bytes_with_nul(bad_bytes)?;
611612
assert!(checked_cstr.to_str().is_err());
613+
Ok(())
612614
}
613615

614616
#[test]
615-
fn test_cstr_as_str_unchecked() {
617+
fn test_cstr_as_str_unchecked() -> Result {
616618
let good_bytes = b"\xf0\x9f\x90\xA7\0";
617-
let checked_cstr = CStr::from_bytes_with_nul(good_bytes).unwrap();
619+
let checked_cstr = CStr::from_bytes_with_nul(good_bytes)?;
618620
// SAFETY: The contents come from a string literal which contains valid UTF-8.
619621
let unchecked_str = unsafe { checked_cstr.as_str_unchecked() };
620622
assert_eq!(unchecked_str, "🐧");
623+
Ok(())
621624
}
622625

623626
#[test]
624-
fn test_cstr_display() {
625-
let hello_world = CStr::from_bytes_with_nul(b"hello, world!\0").unwrap();
627+
fn test_cstr_display() -> Result {
628+
let hello_world = CStr::from_bytes_with_nul(b"hello, world!\0")?;
626629
assert_eq!(format!("{hello_world}"), "hello, world!");
627-
let non_printables = CStr::from_bytes_with_nul(b"\x01\x09\x0a\0").unwrap();
630+
let non_printables = CStr::from_bytes_with_nul(b"\x01\x09\x0a\0")?;
628631
assert_eq!(format!("{non_printables}"), "\\x01\\x09\\x0a");
629-
let non_ascii = CStr::from_bytes_with_nul(b"d\xe9j\xe0 vu\0").unwrap();
632+
let non_ascii = CStr::from_bytes_with_nul(b"d\xe9j\xe0 vu\0")?;
630633
assert_eq!(format!("{non_ascii}"), "d\\xe9j\\xe0 vu");
631-
let good_bytes = CStr::from_bytes_with_nul(b"\xf0\x9f\xa6\x80\0").unwrap();
634+
let good_bytes = CStr::from_bytes_with_nul(b"\xf0\x9f\xa6\x80\0")?;
632635
assert_eq!(format!("{good_bytes}"), "\\xf0\\x9f\\xa6\\x80");
636+
Ok(())
633637
}
634638

635639
#[test]
636-
fn test_cstr_display_all_bytes() {
640+
fn test_cstr_display_all_bytes() -> Result {
637641
let mut bytes: [u8; 256] = [0; 256];
638642
// fill `bytes` with [1..=255] + [0]
639643
for i in u8::MIN..=u8::MAX {
640644
bytes[i as usize] = i.wrapping_add(1);
641645
}
642-
let cstr = CStr::from_bytes_with_nul(&bytes).unwrap();
646+
let cstr = CStr::from_bytes_with_nul(&bytes)?;
643647
assert_eq!(format!("{cstr}"), ALL_ASCII_CHARS);
648+
Ok(())
644649
}
645650

646651
#[test]
647-
fn test_cstr_debug() {
648-
let hello_world = CStr::from_bytes_with_nul(b"hello, world!\0").unwrap();
652+
fn test_cstr_debug() -> Result {
653+
let hello_world = CStr::from_bytes_with_nul(b"hello, world!\0")?;
649654
assert_eq!(format!("{hello_world:?}"), "\"hello, world!\"");
650-
let non_printables = CStr::from_bytes_with_nul(b"\x01\x09\x0a\0").unwrap();
655+
let non_printables = CStr::from_bytes_with_nul(b"\x01\x09\x0a\0")?;
651656
assert_eq!(format!("{non_printables:?}"), "\"\\x01\\x09\\x0a\"");
652-
let non_ascii = CStr::from_bytes_with_nul(b"d\xe9j\xe0 vu\0").unwrap();
657+
let non_ascii = CStr::from_bytes_with_nul(b"d\xe9j\xe0 vu\0")?;
653658
assert_eq!(format!("{non_ascii:?}"), "\"d\\xe9j\\xe0 vu\"");
654-
let good_bytes = CStr::from_bytes_with_nul(b"\xf0\x9f\xa6\x80\0").unwrap();
659+
let good_bytes = CStr::from_bytes_with_nul(b"\xf0\x9f\xa6\x80\0")?;
655660
assert_eq!(format!("{good_bytes:?}"), "\"\\xf0\\x9f\\xa6\\x80\"");
661+
Ok(())
656662
}
657663

658664
#[test]
659-
fn test_bstr_display() {
665+
fn test_bstr_display() -> Result {
660666
let hello_world = BStr::from_bytes(b"hello, world!");
661667
assert_eq!(format!("{hello_world}"), "hello, world!");
662668
let escapes = BStr::from_bytes(b"_\t_\n_\r_\\_\'_\"_");
@@ -667,10 +673,11 @@ mod tests {
667673
assert_eq!(format!("{non_ascii}"), "d\\xe9j\\xe0 vu");
668674
let good_bytes = BStr::from_bytes(b"\xf0\x9f\xa6\x80");
669675
assert_eq!(format!("{good_bytes}"), "\\xf0\\x9f\\xa6\\x80");
676+
Ok(())
670677
}
671678

672679
#[test]
673-
fn test_bstr_debug() {
680+
fn test_bstr_debug() -> Result {
674681
let hello_world = BStr::from_bytes(b"hello, world!");
675682
assert_eq!(format!("{hello_world:?}"), "\"hello, world!\"");
676683
let escapes = BStr::from_bytes(b"_\t_\n_\r_\\_\'_\"_");
@@ -681,6 +688,7 @@ mod tests {
681688
assert_eq!(format!("{non_ascii:?}"), "\"d\\xe9j\\xe0 vu\"");
682689
let good_bytes = BStr::from_bytes(b"\xf0\x9f\xa6\x80");
683690
assert_eq!(format!("{good_bytes:?}"), "\"\\xf0\\x9f\\xa6\\x80\"");
691+
Ok(())
684692
}
685693
}
686694

0 commit comments

Comments
 (0)