Skip to content

Commit 284aaee

Browse files
Rollup merge of #141105 - GrantBirki:grantbirki/path-tests, r=jhpratt
additional edge cases tests for `path.rs` 🧪 This pull request adds a few new edge case tests to the `std::path` module. The new tests cover scenarios such as paths with only separators, non-ASCII and Unicode characters, embedded new lines, etc. Each new test is documented with some helpful in-line comments as well.
2 parents 07157b7 + 4358a1c commit 284aaee

File tree

1 file changed

+31
-0
lines changed

1 file changed

+31
-0
lines changed

library/std/tests/path.rs

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1976,3 +1976,34 @@ fn clone_to_uninit() {
19761976
unsafe { a.clone_to_uninit(ptr::from_mut::<Path>(&mut b).cast()) };
19771977
assert_eq!(a, &*b);
19781978
}
1979+
1980+
// Test: Only separators (e.g., "/" or "\\")
1981+
// This test checks how Path handles a string that consists only of path separators.
1982+
// It should recognize the root and not treat it as a normal component.
1983+
#[test]
1984+
fn test_only_separators() {
1985+
let path = Path::new("/////");
1986+
assert!(path.has_root());
1987+
assert_eq!(path.iter().count(), 1);
1988+
assert_eq!(path.parent(), None);
1989+
}
1990+
1991+
// Test: Non-ASCII/Unicode
1992+
// This test verifies that Path can handle Unicode and non-ASCII characters in the path.
1993+
// It ensures that such paths are not rejected or misinterpreted.
1994+
#[test]
1995+
fn test_non_ascii_unicode() {
1996+
let path = Path::new("/tmp/❤/🚀/file.txt");
1997+
assert!(path.to_str().is_some());
1998+
assert_eq!(path.file_name(), Some(OsStr::new("file.txt")));
1999+
}
2000+
2001+
// Test: Embedded newlines
2002+
// This test verifies that newlines within path components are preserved and do not break path parsing.
2003+
// It ensures that Path treats newlines as normal characters.
2004+
#[test]
2005+
fn test_embedded_newline() {
2006+
let path = Path::new("foo\nbar");
2007+
assert_eq!(path.file_name(), Some(OsStr::new("foo\nbar")));
2008+
assert_eq!(path.to_str(), Some("foo\nbar"));
2009+
}

0 commit comments

Comments
 (0)