about summary refs log tree commit diff
diff options
context:
space:
mode:
authorGuillaume Gomez <guillaume1.gomez@gmail.com>2025-05-24 21:23:48 +0200
committerGitHub <noreply@github.com>2025-05-24 21:23:48 +0200
commit284aaee0c16c9741cdd6c19ef944d63c2559138a (patch)
tree2155245f23d9fd6a41d1478bb5d5aa3620ef6dd3
parent07157b78b1acc47100bebfc0070d322cee2d9fa5 (diff)
parent4358a1c05e8a8878edf22c8cb1f3eae07e09efc0 (diff)
downloadrust-284aaee0c16c9741cdd6c19ef944d63c2559138a.tar.gz
rust-284aaee0c16c9741cdd6c19ef944d63c2559138a.zip
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.
-rw-r--r--library/std/tests/path.rs31
1 files changed, 31 insertions, 0 deletions
diff --git a/library/std/tests/path.rs b/library/std/tests/path.rs
index 978402b6fda..87e0d226cbd 100644
--- a/library/std/tests/path.rs
+++ b/library/std/tests/path.rs
@@ -1976,3 +1976,34 @@ fn clone_to_uninit() {
     unsafe { a.clone_to_uninit(ptr::from_mut::<Path>(&mut b).cast()) };
     assert_eq!(a, &*b);
 }
+
+// Test: Only separators (e.g., "/" or "\\")
+// This test checks how Path handles a string that consists only of path separators.
+// It should recognize the root and not treat it as a normal component.
+#[test]
+fn test_only_separators() {
+    let path = Path::new("/////");
+    assert!(path.has_root());
+    assert_eq!(path.iter().count(), 1);
+    assert_eq!(path.parent(), None);
+}
+
+// Test: Non-ASCII/Unicode
+// This test verifies that Path can handle Unicode and non-ASCII characters in the path.
+// It ensures that such paths are not rejected or misinterpreted.
+#[test]
+fn test_non_ascii_unicode() {
+    let path = Path::new("/tmp/โค/๐Ÿš€/file.txt");
+    assert!(path.to_str().is_some());
+    assert_eq!(path.file_name(), Some(OsStr::new("file.txt")));
+}
+
+// Test: Embedded newlines
+// This test verifies that newlines within path components are preserved and do not break path parsing.
+// It ensures that Path treats newlines as normal characters.
+#[test]
+fn test_embedded_newline() {
+    let path = Path::new("foo\nbar");
+    assert_eq!(path.file_name(), Some(OsStr::new("foo\nbar")));
+    assert_eq!(path.to_str(), Some("foo\nbar"));
+}