oxedyne/fe2o3/fe2o3_core/tests/path.rs
4.8 KiB, 15 runs
created by r1870400018:130, 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 | use oxedyne_fe2o3_core::{ |
| 2 | prelude::*, |
| 3 | path::{ |
| 4 | self, |
| 5 | NormalPath, |
| 6 | }, |
| 7 | test::test_it, |
| 8 | }; |
| 9 | |
| 10 | use std::path::Path; |
| 11 | |
| 12 | pub fn test_path(filter: &'static str) -> Outcome<()> { |
| 13 | |
| 14 | res!(test_it(filter, &["Normalise path 000", "all", "path"], || { |
| 15 | let cases = [ |
| 16 | ("./", (".", false) ), |
| 17 | ("../../", ("../..", true) ), |
| 18 | ("/a/b/../c", ("./a/c", false) ), |
| 19 | ("/a/b/../../c", ("./c", false) ), |
| 20 | ("/a/b/../../../c", ("../c", true) ), |
| 21 | ("a/../../..", ("../..", true) ), |
| 22 | ("./folder/../test.txt", ("./test.txt", false) ), |
| 23 | ("./../parent/folder", ("../parent/folder", true) ), |
| 24 | ("./././file", ("./file", false) ), |
| 25 | (".././../file", ("../../file", true) ), |
| 26 | ("folder/../../another", ("../another", true) ), |
| 27 | ("./folder/./file", ("./folder/file", false) ), |
| 28 | ("folder/subfolder/../../file", ("./file", false) ), |
| 29 | ("folder/../", (".", false) ), |
| 30 | ("folder/./subfolder/../file.txt", ("./folder/file.txt", false) ), |
| 31 | ]; |
| 32 | for (i, (input, (expected, escapes))) in cases.iter().enumerate() { |
| 33 | test!("Path case {} input: '{}'", i+1 , input); |
| 34 | let path = Path::new(input).normalise(); |
| 35 | test!(" normalised: {:?}", path); |
| 36 | req!(*escapes, path.escapes(), "(L: expected)"); |
| 37 | let result = path.into_inner().into_os_string().into_string().ok(); |
| 38 | test!(" string: {:?}", result); |
| 39 | match result { |
| 40 | Some(pstr) => req!(expected, &pstr), |
| 41 | None => return Err(err!( |
| 42 | "Normalising the path '{}' produced {:?}.", input, result; |
| 43 | Test, Mismatch)), |
| 44 | } |
| 45 | } |
| 46 | Ok(()) |
| 47 | })); |
| 48 | |
| 49 | res!(test_it(filter, &["Remove relative components of normalised path 000", "all", "path"], || { |
| 50 | let cases = [ |
| 51 | ("./a", "a" ), |
| 52 | ("./a/b", "a/b" ), |
| 53 | ("../a", "a" ), |
| 54 | ("../a/b/", "a/b" ), |
| 55 | ("../../../a", "a" ), |
| 56 | ]; |
| 57 | for (i, (input, expected)) in cases.iter().enumerate() { |
| 58 | test!("Path case {} input: {}", i+1, input); |
| 59 | let path = Path::new(input).normalise().remove_relative(); |
| 60 | match path.into_inner().into_os_string().into_string() { |
| 61 | Ok(s) => { |
| 62 | test!("result: {}", s); |
| 63 | req!(expected, &s, "(L: expected)"); |
| 64 | }, |
| 65 | Err(oss) => return Err(err!( |
| 66 | "Could not convert '{:?}' to string.", oss; |
| 67 | Test, String, Conversion)), |
| 68 | } |
| 69 | } |
| 70 | Ok(()) |
| 71 | })); |
| 72 | |
| 73 | res!(test_it(filter, &["Absolute normalised path 000", "all", "path"], || { |
| 74 | let cases = [ |
| 75 | ("./a", "/a" ), |
| 76 | ("./a/b", "/a/b" ), |
| 77 | ("../a", "/a" ), |
| 78 | ("../a/b/", "/a/b" ), |
| 79 | ("../../../a", "/a" ), |
| 80 | ("/a/b/c/d/../../e", "/a/b/e" ), |
| 81 | ]; |
| 82 | for (i, (input, expected)) in cases.iter().enumerate() { |
| 83 | test!("Path case {} input: {}", i+1, input); |
| 84 | let path = Path::new(input).normalise().absolute(); |
| 85 | match path.into_inner().into_os_string().into_string() { |
| 86 | Ok(s) => { |
| 87 | test!("result: {}", s); |
| 88 | req!(expected, &s, "(L: expected)"); |
| 89 | }, |
| 90 | Err(oss) => return Err(err!( |
| 91 | "Could not convert '{:?}' to string.", oss; |
| 92 | Test, String, Conversion)), |
| 93 | } |
| 94 | } |
| 95 | Ok(()) |
| 96 | })); |
| 97 | |
| 98 | res!(test_it(filter, &["Filename identifier", "all", "path"], || { |
| 99 | let cases = [ |
| 100 | ("test.html", true, ), |
| 101 | ("./test.htm", false ), |
| 102 | ("test/", false ), |
| 103 | ("/test/", false ), |
| 104 | ("../test/", false ), |
| 105 | (".test.htm", true ), |
| 106 | ]; |
| 107 | for (i, (input, expected)) in cases.iter().enumerate() { |
| 108 | test!("Path case {} input: {}", i+1, input); |
| 109 | req!(*expected, path::is_filename(input), "(L: expected)"); |
| 110 | } |
| 111 | Ok(()) |
| 112 | })); |
| 113 | |
| 114 | Ok(()) |
| 115 | } |