Oregami
Repositories/oxedyne/fe2o3

oxedyne/fe2o3/fe2o3_data/tests/path.rs

4.5 KiB, 24 runs

created by r1870400018:285, 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

1use oxedyne_fe2o3_core::{
2 prelude::*,
3 path::NormalPath,
4 test::test_it,
5};
6
7use std::path::Path;
8
9pub fn test_path(filter: &'static str) -> Outcome<()> {
10
11 res!(test_it(filter, &["Normalise path 000", "all", "path"], || {
12 let inputs = [
13 "./",
14 "../../",
15 "/a/b/../c",
16 "a/../../..",
17 "./folder/../test.txt",
18 "./../parent/folder",
19 "./././file",
20 ".././../file",
21 "folder/../../another",
22 "./folder/./file",
23 "folder/subfolder/../../file",
24 "folder/../",
25 "folder/./subfolder/../file.txt",
26 ];
27 let expected = [
28 (".", false),
29 ("../..", true),
30 ("./a/c", false),
31 ("../..", true),
32 ("./test.txt", false),
33 ("../parent/folder", true),
34 ("./file", false),
35 ("../../file", true),
36 ("../another", true),
37 ("./folder/file", false),
38 ("./file", false),
39 (".", false),
40 ("./folder/file.txt", false),
41 ];
42 if inputs.len() != expected.len() {
43 return Err(err!(
44 "The number of inputs is {} which doesn't match the number of expected \
45 outputs {}.", inputs.len(), expected.len();
46 Test, Mismatch));
47 }
48 for (i, input) in inputs.iter().enumerate() {
49 debug!("input: {}", input);
50 let path = Path::new(input).normalise();
51 debug!(" normalised: {:?}", path);
52 req!(expected[i].1, path.escapes());
53 let result = path.into_inner().into_os_string().into_string().ok();
54 debug!(" string: {:?}", result);
55 match result {
56 Some(pstr) => req!(expected[i].0, &pstr),
57 None => return Err(err!(
58 "Normalising the path '{}' produced {:?}.", input, result;
59 Test, Mismatch)),
60 }
61 }
62 Ok(())
63 }));
64
65 res!(test_it(filter, &["Remove relative components of normalised path 000", "all", "path"], || {
66 let inputs = [
67 "./a",
68 "./a/b",
69 "../a",
70 "../a/b/",
71 "../../../a",
72 ];
73 let expected = [
74 "a",
75 "a/b",
76 "a",
77 "a/b",
78 "a",
79 ];
80 if inputs.len() != expected.len() {
81 return Err(err!(
82 "The number of inputs is {} which doesn't match the number of expected \
83 outputs {}.", inputs.len(), expected.len();
84 Test, Mismatch));
85 }
86 for (i, input) in inputs.iter().enumerate() {
87 debug!("input: {}", input);
88 let path = Path::new(input).normalise().remove_relative();
89 match path.into_inner().into_os_string().into_string() {
90 Ok(s) => {
91 debug!("result: {}", s);
92 req!(expected[i], s);
93 },
94 Err(oss) => return Err(err!(
95 "Could not convert '{:?}' to string.", oss;
96 Test, String, Conversion)),
97 }
98 }
99 Ok(())
100 }));
101
102 res!(test_it(filter, &["Absolute normalised path 000", "all", "path"], || {
103 let inputs = [
104 "./a",
105 "./a/b",
106 "../a",
107 "../a/b/",
108 "../../../a",
109 ];
110 let expected = [
111 "/a",
112 "/a/b",
113 "/a",
114 "/a/b",
115 "/a",
116 ];
117 if inputs.len() != expected.len() {
118 return Err(err!(
119 "The number of inputs is {} which doesn't match the number of expected \
120 outputs {}.", inputs.len(), expected.len();
121 Test, Mismatch));
122 }
123 for (i, input) in inputs.iter().enumerate() {
124 debug!("input: {}", input);
125 let path = Path::new(input).normalise().absolute();
126 match path.into_inner().into_os_string().into_string() {
127 Ok(s) => {
128 debug!("result: {}", s);
129 req!(expected[i], s);
130 },
131 Err(oss) => return Err(err!(
132 "Could not convert '{:?}' to string.", oss;
133 Test, String, Conversion)),
134 }
135 }
136 Ok(())
137 }));
138
139 Ok(())
140}