Oregami
Repositories/oxedyne/fe2o3

oxedyne/fe2o3/fe2o3_file/tests/relpath.rs

3.7 KiB, 1 run

created by r1870400018:74071, which is this file's identity for as long as the history lasts, whatever it is later renamed to

download · who wrote it · its history

1//! [Written with AI entirely](https://need2know.ai/entirely-ai/code)\
2//! Anthropic Claude
3
4use oxedyne_fe2o3_file::relpath::{
5 Fault,
6 RelPath,
7};
8
9use oxedyne_fe2o3_core::{
10 prelude::*,
11 test::test_it,
12};
13
14
15pub fn test_relpath(filter: &'static str) -> Outcome<()> {
16
17 res!(test_it(filter, &["Every non-canonical spelling is refused, never trimmed 000", "all", "relpath"], || {
18 let cases: &[(&str, Fault)] = &[
19 ("", Fault::Empty),
20 (".", Fault::Dot),
21 ("..", Fault::DotDot),
22 ("./a", Fault::Dot),
23 ("a/./b", Fault::Dot),
24 ("a/.", Fault::Dot),
25 ("../a", Fault::DotDot),
26 ("a/../b", Fault::DotDot),
27 ("a/..", Fault::DotDot),
28 ("/a", Fault::Absolute),
29 ("/", Fault::Absolute),
30 ("a\\b", Fault::Backslash),
31 ("..\\x", Fault::Backslash),
32 ("a//b", Fault::EmptySegment),
33 ("a/", Fault::EmptySegment),
34 ("a/b/", Fault::EmptySegment),
35 (" a", Fault::EdgeSpace),
36 ("a ", Fault::EdgeSpace),
37 ("a/ b", Fault::EdgeSpace),
38 ("a/b /c", Fault::EdgeSpace),
39 ("\u{a0}a", Fault::EdgeSpace), // no-break space
40 ("a\u{3000}", Fault::EdgeSpace), // ideographic space
41 (" ", Fault::EdgeSpace),
42 ("a\0b", Fault::Control),
43 ("\0", Fault::Control),
44 ("a\u{7}", Fault::Control),
45 ("a\nb", Fault::Control),
46 ("a\u{9f}b", Fault::Control),
47 // Daimond HIST-SAFE U2 post-deploy QA, F1 and F2, verbatim.
48 (" sp", Fault::EdgeSpace),
49 (" secret", Fault::EdgeSpace),
50 ("secret ", Fault::EdgeSpace),
51 ("\tsecret", Fault::Control),
52 ("chatc/../secret", Fault::DotDot),
53 ("/secret", Fault::Absolute),
54 ("secret/", Fault::EmptySegment),
55 ("./secret", Fault::Dot),
56 ("code/../secret", Fault::DotDot),
57 ("../secret", Fault::DotDot),
58 ("code/..", Fault::DotDot),
59 ("code//..//secret",Fault::EmptySegment),
60 // CAPTURE-FENCE QA B, F1, verbatim.
61 (" ../x.md", Fault::EdgeSpace),
62 (".. /x.md", Fault::EdgeSpace),
63 ("..\t/x.md", Fault::Control),
64 ];
65 for (s, want) in cases {
66 let got = RelPath::fault(s);
67 if got != Some(*want) {
68 return Err(err!("RelPath::fault({:?}) = {:?}, expected {:?}.", s, got, want; Test));
69 }
70 match RelPath::parse(s) {
71 Ok(p) => return Err(err!("RelPath::parse({:?}) accepted {:?}.", s, p; Test)),
72 Err(e) => {
73 let msg = fmt!("{}", e);
74 if !msg.contains(&fmt!("{:?}", s)) || !msg.contains(&fmt!("{}", want)) {
75 return Err(err!("The refusal of {:?} does not name the value and the reason: {}", s, msg; Test));
76 }
77 }
78 }
79 }
80 Ok(())
81 }));
82
83 res!(test_it(filter, &["Legal names are kept exactly as written 000", "all", "relpath"], || {
84 let ok = [
85 "a", "sp", "a/b/c.md", "a..b.md", "v1.2.3.md", ".../x.md", "..hidden.md", "x...md",
86 "..../e.md", ".hidden", "a b/c d.md", "café/naïve.txt", "%2e%2e/x", "\u{2024}\u{2024}/x",
87 "diamonds/0da1/shots/self.png",
88 ];
89 for s in ok {
90 let p = res!(RelPath::parse(s));
91 if p.as_str() != s {
92 return Err(err!("RelPath::parse({:?}) changed it to {:?}.", s, p; Test));
93 }
94 }
95 Ok(())
96 }));
97
98 res!(test_it(filter, &["Segments, name, parent, join and is_within 000", "all", "relpath"], || {
99 let p = res!(RelPath::parse("a/b/c.md"));
100 let segs: Vec<&str> = p.segments().collect();
101 assert_eq!(segs, vec!["a", "b", "c.md"]);
102 assert_eq!(p.name(), "c.md");
103 assert_eq!(p.parent().map(|q| q.into_string()), Some(fmt!("a/b")));
104 assert_eq!(res!(RelPath::parse("a")).parent(), None);
105 let a = res!(RelPath::parse("a"));
106 let ab = res!(RelPath::parse("ab"));
107 assert!(p.is_within(&a));
108 assert!(a.is_within(&a));
109 assert!(!ab.is_within(&a));
110 assert!(!a.is_within(&p));
111 let j = a.join(&res!(RelPath::parse("b/c.md")));
112 assert_eq!(j, p);
113 assert!(RelPath::is_canonical(j.as_str()));
114 Ok(())
115 }));
116
117 Ok(())
118}