| 1 |
#![allow(clippy::too_many_lines)] |
| 2 |
|
| 3 |
use super::*; |
| 4 |
|
| 5 |
use std::path::Path; |
| 6 |
use std::rc::Rc; |
| 7 |
use std::sync::Arc; |
| 8 |
|
| 9 |
macro_rules! t( |
| 10 |
($path:expr, iter: $iter:expr) => ( |
| 11 |
{ |
| 12 |
let path = RelativePath::new($path); |
| 13 |
|
| 14 |
// Forward iteration |
| 15 |
let comps = path.iter().map(str::to_string).collect::<Vec<String>>(); |
| 16 |
let exp: &[&str] = &$iter; |
| 17 |
let exps = exp.iter().map(|s| s.to_string()).collect::<Vec<String>>(); |
| 18 |
assert!(comps == exps, "iter: Expected {:?}, found {:?}", |
| 19 |
exps, comps); |
| 20 |
|
| 21 |
// Reverse iteration |
| 22 |
let comps = RelativePath::new($path).iter().rev().map(str::to_string) |
| 23 |
.collect::<Vec<String>>(); |
| 24 |
let exps = exps.into_iter().rev().collect::<Vec<String>>(); |
| 25 |
assert!(comps == exps, "iter().rev(): Expected {:?}, found {:?}", |
| 26 |
exps, comps); |
| 27 |
} |
| 28 |
); |
| 29 |
|
| 30 |
($path:expr, parent: $parent:expr, file_name: $file:expr) => ( |
| 31 |
{ |
| 32 |
let path = RelativePath::new($path); |
| 33 |
|
| 34 |
let parent = path.parent().map(|p| p.as_str()); |
| 35 |
let exp_parent: Option<&str> = $parent; |
| 36 |
assert!(parent == exp_parent, "parent: Expected {:?}, found {:?}", |
| 37 |
exp_parent, parent); |
| 38 |
|
| 39 |
let file = path.file_name(); |
| 40 |
let exp_file: Option<&str> = $file; |
| 41 |
assert!(file == exp_file, "file_name: Expected {:?}, found {:?}", |
| 42 |
exp_file, file); |
| 43 |
} |
| 44 |
); |
| 45 |
|
| 46 |
($path:expr, file_stem: $file_stem:expr, extension: $extension:expr) => ( |
| 47 |
{ |
| 48 |
let path = RelativePath::new($path); |
| 49 |
|
| 50 |
let stem = path.file_stem(); |
| 51 |
let exp_stem: Option<&str> = $file_stem; |
| 52 |
assert!(stem == exp_stem, "file_stem: Expected {:?}, found {:?}", |
| 53 |
exp_stem, stem); |
| 54 |
|
| 55 |
let ext = path.extension(); |
| 56 |
let exp_ext: Option<&str> = $extension; |
| 57 |
assert!(ext == exp_ext, "extension: Expected {:?}, found {:?}", |
| 58 |
exp_ext, ext); |
| 59 |
} |
| 60 |
); |
| 61 |
|
| 62 |
($path:expr, iter: $iter:expr, |
| 63 |
parent: $parent:expr, file_name: $file:expr, |
| 64 |
file_stem: $file_stem:expr, extension: $extension:expr) => ( |
| 65 |
{ |
| 66 |
t!($path, iter: $iter); |
| 67 |
t!($path, parent: $parent, file_name: $file); |
| 68 |
t!($path, file_stem: $file_stem, extension: $extension); |
| 69 |
} |
| 70 |
); |
| 71 |
); |
| 72 |
|
| 73 |
fn assert_components(components: &[&str], path: &RelativePath) { |
| 74 |
let components = components |
| 75 |
.iter() |
| 76 |
.copied() |
| 77 |
.map(Component::Normal) |
| 78 |
.collect::<Vec<_>>(); |
| 79 |
let result: Vec<_> = path.components().collect(); |
| 80 |
assert_eq!(&components[..], &result[..]); |
| 81 |
} |
| 82 |
|
| 83 |
fn rp(input: &str) -> &RelativePath { |
| 84 |
RelativePath::new(input) |
| 85 |
} |
| 86 |
|
| 87 |
#[test] |
| 88 |
#[allow(clippy::cognitive_complexity)] |
| 89 |
pub fn test_decompositions() { |
| 90 |
t!("", |
| 91 |
iter: [], |
| 92 |
parent: None, |
| 93 |
file_name: None, |
| 94 |
file_stem: None, |
| 95 |
extension: None |
| 96 |
); |
| 97 |
|
| 98 |
t!("foo", |
| 99 |
iter: ["foo"], |
| 100 |
parent: Some(""), |
| 101 |
file_name: Some("foo"), |
| 102 |
file_stem: Some("foo"), |
| 103 |
extension: None |
| 104 |
); |
| 105 |
|
| 106 |
t!("/", |
| 107 |
iter: [], |
| 108 |
parent: Some(""), |
| 109 |
file_name: None, |
| 110 |
file_stem: None, |
| 111 |
extension: None |
| 112 |
); |
| 113 |
|
| 114 |
t!("/foo", |
| 115 |
iter: ["foo"], |
| 116 |
parent: Some(""), |
| 117 |
file_name: Some("foo"), |
| 118 |
file_stem: Some("foo"), |
| 119 |
extension: None |
| 120 |
); |
| 121 |
|
| 122 |
t!("foo/", |
| 123 |
iter: ["foo"], |
| 124 |
parent: Some(""), |
| 125 |
file_name: Some("foo"), |
| 126 |
file_stem: Some("foo"), |
| 127 |
extension: None |
| 128 |
); |
| 129 |
|
| 130 |
t!("/foo/", |
| 131 |
iter: ["foo"], |
| 132 |
parent: Some(""), |
| 133 |
file_name: Some("foo"), |
| 134 |
file_stem: Some("foo"), |
| 135 |
extension: None |
| 136 |
); |
| 137 |
|
| 138 |
t!("foo/bar", |
| 139 |
iter: ["foo", "bar"], |
| 140 |
parent: Some("foo"), |
| 141 |
file_name: Some("bar"), |
| 142 |
file_stem: Some("bar"), |
| 143 |
extension: None |
| 144 |
); |
| 145 |
|
| 146 |
t!("/foo/bar", |
| 147 |
iter: ["foo", "bar"], |
| 148 |
parent: Some("/foo"), |
| 149 |
file_name: Some("bar"), |
| 150 |
file_stem: Some("bar"), |
| 151 |
extension: None |
| 152 |
); |
| 153 |
|
| 154 |
t!("///foo///", |
| 155 |
iter: ["foo"], |
| 156 |
parent: Some(""), |
| 157 |
file_name: Some("foo"), |
| 158 |
file_stem: Some("foo"), |
| 159 |
extension: None |
| 160 |
); |
| 161 |
|
| 162 |
t!("///foo///bar", |
| 163 |
iter: ["foo", "bar"], |
| 164 |
parent: Some("///foo"), |
| 165 |
file_name: Some("bar"), |
| 166 |
file_stem: Some("bar"), |
| 167 |
extension: None |
| 168 |
); |
| 169 |
|
| 170 |
t!("./.", |
| 171 |
iter: [".", "."], |
| 172 |
parent: Some(""), |
| 173 |
file_name: None, |
| 174 |
file_stem: None, |
| 175 |
extension: None |
| 176 |
); |
| 177 |
|
| 178 |
t!("/..", |
| 179 |
iter: [".."], |
| 180 |
parent: Some(""), |
| 181 |
file_name: None, |
| 182 |
file_stem: None, |
| 183 |
extension: None |
| 184 |
); |
| 185 |
|
| 186 |
t!("../", |
| 187 |
iter: [".."], |
| 188 |
parent: Some(""), |
| 189 |
file_name: None, |
| 190 |
file_stem: None, |
| 191 |
extension: None |
| 192 |
); |
| 193 |
|
| 194 |
t!("foo/.", |
| 195 |
iter: ["foo", "."], |
| 196 |
parent: Some(""), |
| 197 |
file_name: Some("foo"), |
| 198 |
file_stem: Some("foo"), |
| 199 |
extension: None |
| 200 |
); |
| 201 |
|
| 202 |
t!("foo/..", |
| 203 |
iter: ["foo", ".."], |
| 204 |
parent: Some("foo"), |
| 205 |
file_name: None, |
| 206 |
file_stem: None, |
| 207 |
extension: None |
| 208 |
); |
| 209 |
|
| 210 |
t!("foo/./", |
| 211 |
iter: ["foo", "."], |
| 212 |
parent: Some(""), |
| 213 |
file_name: Some("foo"), |
| 214 |
file_stem: Some("foo"), |
| 215 |
extension: None |
| 216 |
); |
| 217 |
|
| 218 |
t!("foo/./bar", |
| 219 |
iter: ["foo", ".", "bar"], |
| 220 |
parent: Some("foo/."), |
| 221 |
file_name: Some("bar"), |
| 222 |
file_stem: Some("bar"), |
| 223 |
extension: None |
| 224 |
); |
| 225 |
|
| 226 |
t!("foo/../", |
| 227 |
iter: ["foo", ".."], |
| 228 |
parent: Some("foo"), |
| 229 |
file_name: None, |
| 230 |
file_stem: None, |
| 231 |
extension: None |
| 232 |
); |
| 233 |
|
| 234 |
t!("foo/../bar", |
| 235 |
iter: ["foo", "..", "bar"], |
| 236 |
parent: Some("foo/.."), |
| 237 |
file_name: Some("bar"), |
| 238 |
file_stem: Some("bar"), |
| 239 |
extension: None |
| 240 |
); |
| 241 |
|
| 242 |
t!("./a", |
| 243 |
iter: [".", "a"], |
| 244 |
parent: Some("."), |
| 245 |
file_name: Some("a"), |
| 246 |
file_stem: Some("a"), |
| 247 |
extension: None |
| 248 |
); |
| 249 |
|
| 250 |
t!(".", |
| 251 |
iter: ["."], |
| 252 |
parent: Some(""), |
| 253 |
file_name: None, |
| 254 |
file_stem: None, |
| 255 |
extension: None |
| 256 |
); |
| 257 |
|
| 258 |
t!("./", |
| 259 |
iter: ["."], |
| 260 |
parent: Some(""), |
| 261 |
file_name: None, |
| 262 |
file_stem: None, |
| 263 |
extension: None |
| 264 |
); |
| 265 |
|
| 266 |
t!("a/b", |
| 267 |
iter: ["a", "b"], |
| 268 |
parent: Some("a"), |
| 269 |
file_name: Some("b"), |
| 270 |
file_stem: Some("b"), |
| 271 |
extension: None |
| 272 |
); |
| 273 |
|
| 274 |
t!("a//b", |
| 275 |
iter: ["a", "b"], |
| 276 |
parent: Some("a"), |
| 277 |
file_name: Some("b"), |
| 278 |
file_stem: Some("b"), |
| 279 |
extension: None |
| 280 |
); |
| 281 |
|
| 282 |
t!("a/./b", |
| 283 |
iter: ["a", ".", "b"], |
| 284 |
parent: Some("a/."), |
| 285 |
file_name: Some("b"), |
| 286 |
file_stem: Some("b"), |
| 287 |
extension: None |
| 288 |
); |
| 289 |
|
| 290 |
t!("a/b/c", |
| 291 |
iter: ["a", "b", "c"], |
| 292 |
parent: Some("a/b"), |
| 293 |
file_name: Some("c"), |
| 294 |
file_stem: Some("c"), |
| 295 |
extension: None |
| 296 |
); |
| 297 |
|
| 298 |
t!(".foo", |
| 299 |
iter: [".foo"], |
| 300 |
parent: Some(""), |
| 301 |
file_name: Some(".foo"), |
| 302 |
file_stem: Some(".foo"), |
| 303 |
extension: None |
| 304 |
); |
| 305 |
} |
| 306 |
|
| 307 |
#[test] |
| 308 |
pub fn test_stem_ext() { |
| 309 |
t!("foo", |
| 310 |
file_stem: Some("foo"), |
| 311 |
extension: None |
| 312 |
); |
| 313 |
|
| 314 |
t!("foo.", |
| 315 |
file_stem: Some("foo"), |
| 316 |
extension: Some("") |
| 317 |
); |
| 318 |
|
| 319 |
t!(".foo", |
| 320 |
file_stem: Some(".foo"), |
| 321 |
extension: None |
| 322 |
); |
| 323 |
|
| 324 |
t!("foo.txt", |
| 325 |
file_stem: Some("foo"), |
| 326 |
extension: Some("txt") |
| 327 |
); |
| 328 |
|
| 329 |
t!("foo.bar.txt", |
| 330 |
file_stem: Some("foo.bar"), |
| 331 |
extension: Some("txt") |
| 332 |
); |
| 333 |
|
| 334 |
t!("foo.bar.", |
| 335 |
file_stem: Some("foo.bar"), |
| 336 |
extension: Some("") |
| 337 |
); |
| 338 |
|
| 339 |
t!(".", file_stem: None, extension: None); |
| 340 |
|
| 341 |
t!("..", file_stem: None, extension: None); |
| 342 |
|
| 343 |
t!("", file_stem: None, extension: None); |
| 344 |
} |
| 345 |
|
| 346 |
#[test] |
| 347 |
pub fn test_set_file_name() { |
| 348 |
macro_rules! tfn( |
| 349 |
($path:expr, $file:expr, $expected:expr) => ( { |
| 350 |
let mut p = RelativePathBuf::from($path); |
| 351 |
p.set_file_name($file); |
| 352 |
assert!(p.as_str() == $expected, |
| 353 |
"setting file name of {:?} to {:?}: Expected {:?}, got {:?}", |
| 354 |
$path, $file, $expected, |
| 355 |
p.as_str()); |
| 356 |
}); |
| 357 |
); |
| 358 |
|
| 359 |
tfn!("foo", "foo", "foo"); |
| 360 |
tfn!("foo", "bar", "bar"); |
| 361 |
tfn!("foo", "", ""); |
| 362 |
tfn!("", "foo", "foo"); |
| 363 |
|
| 364 |
tfn!(".", "foo", "./foo"); |
| 365 |
tfn!("foo/", "bar", "bar"); |
| 366 |
tfn!("foo/.", "bar", "bar"); |
| 367 |
tfn!("..", "foo", "../foo"); |
| 368 |
tfn!("foo/..", "bar", "foo/../bar"); |
| 369 |
tfn!("/", "foo", "/foo"); |
| 370 |
} |
| 371 |
|
| 372 |
#[test] |
| 373 |
pub fn test_set_extension() { |
| 374 |
macro_rules! tse( |
| 375 |
($path:expr, $ext:expr, $expected:expr, $output:expr) => ( { |
| 376 |
let mut p = RelativePathBuf::from($path); |
| 377 |
let output = p.set_extension($ext); |
| 378 |
assert!(p.as_str() == $expected && output == $output, |
| 379 |
"setting extension of {:?} to {:?}: Expected {:?}/{:?}, got {:?}/{:?}", |
| 380 |
$path, $ext, $expected, $output, |
| 381 |
p.as_str(), output); |
| 382 |
}); |
| 383 |
); |
| 384 |
|
| 385 |
tse!("foo", "txt", "foo.txt", true); |
| 386 |
tse!("foo.bar", "txt", "foo.txt", true); |
| 387 |
tse!("foo.bar.baz", "txt", "foo.bar.txt", true); |
| 388 |
tse!(".test", "txt", ".test.txt", true); |
| 389 |
tse!("foo.txt", "", "foo", true); |
| 390 |
tse!("foo", "", "foo", true); |
| 391 |
tse!("", "foo", "", false); |
| 392 |
tse!(".", "foo", ".", false); |
| 393 |
tse!("foo/", "bar", "foo.bar", true); |
| 394 |
tse!("foo/.", "bar", "foo.bar", true); |
| 395 |
tse!("..", "foo", "..", false); |
| 396 |
tse!("foo/..", "bar", "foo/..", false); |
| 397 |
tse!("/", "foo", "/", false); |
| 398 |
} |
| 399 |
|
| 400 |
#[test] |
| 401 |
fn test_eq_recievers() { |
| 402 |
use std::borrow::Cow; |
| 403 |
|
| 404 |
let borrowed: &RelativePath = RelativePath::new("foo/bar"); |
| 405 |
let mut owned: RelativePathBuf = RelativePathBuf::new(); |
| 406 |
owned.push("foo"); |
| 407 |
owned.push("bar"); |
| 408 |
let borrowed_cow: Cow<RelativePath> = borrowed.into(); |
| 409 |
let owned_cow: Cow<RelativePath> = owned.clone().into(); |
| 410 |
|
| 411 |
macro_rules! t { |
| 412 |
($($current:expr),+) => { |
| 413 |
$( |
| 414 |
assert_eq!($current, borrowed); |
| 415 |
assert_eq!($current, owned); |
| 416 |
assert_eq!($current, borrowed_cow); |
| 417 |
assert_eq!($current, owned_cow); |
| 418 |
)+ |
| 419 |
} |
| 420 |
} |
| 421 |
|
| 422 |
t!(borrowed, owned, borrowed_cow, owned_cow); |
| 423 |
} |
| 424 |
|
| 425 |
#[test] |
| 426 |
#[allow(clippy::cognitive_complexity)] |
| 427 |
pub fn test_compare() { |
| 428 |
use std::collections::hash_map::DefaultHasher; |
| 429 |
use std::hash::{Hash, Hasher}; |
| 430 |
|
| 431 |
fn hash<T: Hash>(t: T) -> u64 { |
| 432 |
let mut s = DefaultHasher::new(); |
| 433 |
t.hash(&mut s); |
| 434 |
s.finish() |
| 435 |
} |
| 436 |
|
| 437 |
macro_rules! tc( |
| 438 |
($path1:expr, $path2:expr, eq: $eq:expr, |
| 439 |
starts_with: $starts_with:expr, ends_with: $ends_with:expr, |
| 440 |
relative_from: $relative_from:expr) => ({ |
| 441 |
let path1 = RelativePath::new($path1); |
| 442 |
let path2 = RelativePath::new($path2); |
| 443 |
|
| 444 |
let eq = path1 == path2; |
| 445 |
assert!(eq == $eq, "{:?} == {:?}, expected {:?}, got {:?}", |
| 446 |
$path1, $path2, $eq, eq); |
| 447 |
assert!($eq == (hash(path1) == hash(path2)), |
| 448 |
"{:?} == {:?}, expected {:?}, got {} and {}", |
| 449 |
$path1, $path2, $eq, hash(path1), hash(path2)); |
| 450 |
|
| 451 |
let starts_with = path1.starts_with(path2); |
| 452 |
assert!(starts_with == $starts_with, |
| 453 |
"{:?}.starts_with({:?}), expected {:?}, got {:?}", $path1, $path2, |
| 454 |
$starts_with, starts_with); |
| 455 |
|
| 456 |
let ends_with = path1.ends_with(path2); |
| 457 |
assert!(ends_with == $ends_with, |
| 458 |
"{:?}.ends_with({:?}), expected {:?}, got {:?}", $path1, $path2, |
| 459 |
$ends_with, ends_with); |
| 460 |
|
| 461 |
let relative_from = path1.strip_prefix(path2) |
| 462 |
.map(|p| p.as_str()) |
| 463 |
.ok(); |
| 464 |
let exp: Option<&str> = $relative_from; |
| 465 |
assert!(relative_from == exp, |
| 466 |
"{:?}.strip_prefix({:?}), expected {:?}, got {:?}", |
| 467 |
$path1, $path2, exp, relative_from); |
| 468 |
}); |
| 469 |
); |
| 470 |
|
| 471 |
tc!("", "", |
| 472 |
eq: true, |
| 473 |
starts_with: true, |
| 474 |
ends_with: true, |
| 475 |
relative_from: Some("") |
| 476 |
); |
| 477 |
|
| 478 |
tc!("foo", "", |
| 479 |
eq: false, |
| 480 |
starts_with: true, |
| 481 |
ends_with: true, |
| 482 |
relative_from: Some("foo") |
| 483 |
); |
| 484 |
|
| 485 |
tc!("", "foo", |
| 486 |
eq: false, |
| 487 |
starts_with: false, |
| 488 |
ends_with: false, |
| 489 |
relative_from: None |
| 490 |
); |
| 491 |
|
| 492 |
tc!("foo", "foo", |
| 493 |
eq: true, |
| 494 |
starts_with: true, |
| 495 |
ends_with: true, |
| 496 |
relative_from: Some("") |
| 497 |
); |
| 498 |
|
| 499 |
tc!("foo/", "foo", |
| 500 |
eq: true, |
| 501 |
starts_with: true, |
| 502 |
ends_with: true, |
| 503 |
relative_from: Some("") |
| 504 |
); |
| 505 |
|
| 506 |
tc!("foo/bar", "foo", |
| 507 |
eq: false, |
| 508 |
starts_with: true, |
| 509 |
ends_with: false, |
| 510 |
relative_from: Some("bar") |
| 511 |
); |
| 512 |
|
| 513 |
tc!("foo/bar/baz", "foo/bar", |
| 514 |
eq: false, |
| 515 |
starts_with: true, |
| 516 |
ends_with: false, |
| 517 |
relative_from: Some("baz") |
| 518 |
); |
| 519 |
|
| 520 |
tc!("foo/bar", "foo/bar/baz", |
| 521 |
eq: false, |
| 522 |
starts_with: false, |
| 523 |
ends_with: false, |
| 524 |
relative_from: None |
| 525 |
); |
| 526 |
} |
| 527 |
|
| 528 |
#[test] |
| 529 |
fn test_join() { |
| 530 |
assert_components(&["foo", "bar", "baz"], &rp("foo/bar").join("baz///")); |
| 531 |
assert_components( |
| 532 |
&["hello", "world", "foo", "bar", "baz"], |
| 533 |
&rp("hello/world").join("///foo/bar/baz"), |
| 534 |
); |
| 535 |
assert_components(&["foo", "bar", "baz"], &rp("").join("foo/bar/baz")); |
| 536 |
} |
| 537 |
|
| 538 |
#[test] |
| 539 |
fn test_components_iterator() { |
| 540 |
use self::Component::*; |
| 541 |
|
| 542 |
assert_eq!( |
| 543 |
vec![Normal("hello"), Normal("world")], |
| 544 |
rp("/hello///world//").components().collect::<Vec<_>>() |
| 545 |
); |
| 546 |
} |
| 547 |
|
| 548 |
#[test] |
| 549 |
fn test_to_path_buf() { |
| 550 |
let path = rp("/hello///world//"); |
| 551 |
let path_buf = path.to_path("."); |
| 552 |
let expected = Path::new(".").join("hello").join("world"); |
| 553 |
assert_eq!(expected, path_buf); |
| 554 |
} |
| 555 |
|
| 556 |
#[test] |
| 557 |
fn test_eq() { |
| 558 |
assert_eq!(rp("//foo///bar"), rp("/foo/bar")); |
| 559 |
assert_eq!(rp("foo///bar"), rp("foo/bar")); |
| 560 |
assert_eq!(rp("foo"), rp("foo")); |
| 561 |
assert_eq!(rp("foo"), rp("foo").to_relative_path_buf()); |
| 562 |
} |
| 563 |
|
| 564 |
#[test] |
| 565 |
fn test_next_back() { |
| 566 |
use self::Component::*; |
| 567 |
|
| 568 |
let mut it = rp("baz/bar///foo").components(); |
| 569 |
assert_eq!(Some(Normal("foo")), it.next_back()); |
| 570 |
assert_eq!(Some(Normal("bar")), it.next_back()); |
| 571 |
assert_eq!(Some(Normal("baz")), it.next_back()); |
| 572 |
assert_eq!(None, it.next_back()); |
| 573 |
} |
| 574 |
|
| 575 |
#[test] |
| 576 |
fn test_parent() { |
| 577 |
let path = rp("baz/./bar/foo//./."); |
| 578 |
|
| 579 |
assert_eq!(Some(rp("baz/./bar")), path.parent()); |
| 580 |
assert_eq!( |
| 581 |
Some(rp("baz/.")), |
| 582 |
path.parent().and_then(RelativePath::parent) |
| 583 |
); |
| 584 |
assert_eq!( |
| 585 |
Some(rp("")), |
| 586 |
path.parent() |
| 587 |
.and_then(RelativePath::parent) |
| 588 |
.and_then(RelativePath::parent) |
| 589 |
); |
| 590 |
assert_eq!( |
| 591 |
None, |
| 592 |
path.parent() |
| 593 |
.and_then(RelativePath::parent) |
| 594 |
.and_then(RelativePath::parent) |
| 595 |
.and_then(RelativePath::parent) |
| 596 |
); |
| 597 |
} |
| 598 |
|
| 599 |
#[test] |
| 600 |
fn test_relative_path_buf() { |
| 601 |
assert_eq!( |
| 602 |
rp("hello/world/."), |
| 603 |
rp("/hello///world//").to_owned().join(".") |
| 604 |
); |
| 605 |
} |
| 606 |
|
| 607 |
#[test] |
| 608 |
fn test_normalize() { |
| 609 |
assert_eq!(rp("c/d"), rp("a/.././b/../c/d").normalize()); |
| 610 |
} |
| 611 |
|
| 612 |
#[test] |
| 613 |
fn test_relative_to() { |
| 614 |
assert_eq!( |
| 615 |
rp("foo/foo/bar"), |
| 616 |
rp("foo/bar").join_normalized("../foo/bar") |
| 617 |
); |
| 618 |
|
| 619 |
assert_eq!( |
| 620 |
rp("../c/e"), |
| 621 |
rp("x/y").join_normalized("../../a/b/../../../c/d/../e") |
| 622 |
); |
| 623 |
} |
| 624 |
|
| 625 |
#[test] |
| 626 |
fn test_from() { |
| 627 |
assert_eq!( |
| 628 |
rp("foo/bar").to_owned(), |
| 629 |
RelativePathBuf::from(String::from("foo/bar")), |
| 630 |
); |
| 631 |
|
| 632 |
assert_eq!( |
| 633 |
RelativePathBuf::from(rp("foo/bar")), |
| 634 |
RelativePathBuf::from("foo/bar"), |
| 635 |
); |
| 636 |
|
| 637 |
assert_eq!(rp("foo/bar").to_owned(), RelativePathBuf::from("foo/bar"),); |
| 638 |
|
| 639 |
assert_eq!(&*Box::<RelativePath>::from(rp("foo/bar")), rp("foo/bar")); |
| 640 |
assert_eq!( |
| 641 |
&*Box::<RelativePath>::from(RelativePathBuf::from("foo/bar")), |
| 642 |
rp("foo/bar") |
| 643 |
); |
| 644 |
|
| 645 |
assert_eq!(&*Arc::<RelativePath>::from(rp("foo/bar")), rp("foo/bar")); |
| 646 |
assert_eq!( |
| 647 |
&*Arc::<RelativePath>::from(RelativePathBuf::from("foo/bar")), |
| 648 |
rp("foo/bar") |
| 649 |
); |
| 650 |
|
| 651 |
assert_eq!(&*Rc::<RelativePath>::from(rp("foo/bar")), rp("foo/bar")); |
| 652 |
assert_eq!( |
| 653 |
&*Rc::<RelativePath>::from(RelativePathBuf::from("foo/bar")), |
| 654 |
rp("foo/bar") |
| 655 |
); |
| 656 |
} |
| 657 |
|
| 658 |
#[test] |
| 659 |
fn test_relative_path_asref_str() { |
| 660 |
assert_eq!( |
| 661 |
<RelativePath as AsRef<str>>::as_ref(rp("foo/bar")), |
| 662 |
"foo/bar" |
| 663 |
); |
| 664 |
} |
| 665 |
|
| 666 |
#[test] |
| 667 |
fn test_default() { |
| 668 |
assert_eq!(RelativePathBuf::new(), RelativePathBuf::default(),); |
| 669 |
} |
| 670 |
|
| 671 |
#[test] |
| 672 |
pub fn test_push() { |
| 673 |
macro_rules! tp( |
| 674 |
($path:expr, $push:expr, $expected:expr) => ( { |
| 675 |
let mut actual = RelativePathBuf::from($path); |
| 676 |
actual.push($push); |
| 677 |
assert!(actual.as_str() == $expected, |
| 678 |
"pushing {:?} onto {:?}: Expected {:?}, got {:?}", |
| 679 |
$push, $path, $expected, actual.as_str()); |
| 680 |
}); |
| 681 |
); |
| 682 |
|
| 683 |
tp!("", "foo", "foo"); |
| 684 |
tp!("foo", "bar", "foo/bar"); |
| 685 |
tp!("foo/", "bar", "foo/bar"); |
| 686 |
tp!("foo//", "bar", "foo//bar"); |
| 687 |
tp!("foo/.", "bar", "foo/./bar"); |
| 688 |
tp!("foo./.", "bar", "foo././bar"); |
| 689 |
tp!("foo", "", "foo/"); |
| 690 |
tp!("foo", ".", "foo/."); |
| 691 |
tp!("foo", "..", "foo/.."); |
| 692 |
} |
| 693 |
|
| 694 |
#[test] |
| 695 |
pub fn test_pop() { |
| 696 |
macro_rules! tp( |
| 697 |
($path:expr, $expected:expr, $output:expr) => ( { |
| 698 |
let mut actual = RelativePathBuf::from($path); |
| 699 |
let output = actual.pop(); |
| 700 |
assert!(actual.as_str() == $expected && output == $output, |
| 701 |
"popping from {:?}: Expected {:?}/{:?}, got {:?}/{:?}", |
| 702 |
$path, $expected, $output, |
| 703 |
actual.as_str(), output); |
| 704 |
}); |
| 705 |
); |
| 706 |
|
| 707 |
tp!("", "", false); |
| 708 |
tp!("/", "", true); |
| 709 |
tp!("foo", "", true); |
| 710 |
tp!(".", "", true); |
| 711 |
tp!("/foo", "", true); |
| 712 |
tp!("/foo/bar", "/foo", true); |
| 713 |
tp!("/foo/bar/.", "/foo", true); |
| 714 |
tp!("foo/bar", "foo", true); |
| 715 |
tp!("foo/.", "", true); |
| 716 |
tp!("foo//bar", "foo", true); |
| 717 |
} |
| 718 |
|
| 719 |
#[test] |
| 720 |
pub fn test_display() { |
| 721 |
// NB: display delegated to the underlying string. |
| 722 |
assert_eq!(RelativePathBuf::from("foo/bar").to_string(), "foo/bar"); |
| 723 |
assert_eq!(RelativePath::new("foo/bar").to_string(), "foo/bar"); |
| 724 |
|
| 725 |
assert_eq!(format!("{}", RelativePathBuf::from("foo/bar")), "foo/bar"); |
| 726 |
assert_eq!(format!("{}", RelativePath::new("foo/bar")), "foo/bar"); |
| 727 |
} |
| 728 |
|
| 729 |
#[cfg(unix)] |
| 730 |
#[test] |
| 731 |
pub fn test_unix_from_path() { |
| 732 |
use std::ffi::OsStr; |
| 733 |
use std::os::unix::ffi::OsStrExt; |
| 734 |
|
| 735 |
assert_eq!( |
| 736 |
Err(FromPathErrorKind::NonRelative.into()), |
| 737 |
RelativePath::from_path("/foo/bar") |
| 738 |
); |
| 739 |
|
| 740 |
// Continuation byte without continuation. |
| 741 |
let non_utf8 = OsStr::from_bytes(&[0x80u8]); |
| 742 |
|
| 743 |
assert_eq!( |
| 744 |
Err(FromPathErrorKind::NonUtf8.into()), |
| 745 |
RelativePath::from_path(non_utf8) |
| 746 |
); |
| 747 |
} |
| 748 |
|
| 749 |
#[cfg(windows)] |
| 750 |
#[test] |
| 751 |
pub fn test_windows_from_path() { |
| 752 |
assert_eq!( |
| 753 |
Err(FromPathErrorKind::NonRelative.into()), |
| 754 |
RelativePath::from_path("c:\\foo\\bar") |
| 755 |
); |
| 756 |
|
| 757 |
assert_eq!( |
| 758 |
Err(FromPathErrorKind::BadSeparator.into()), |
| 759 |
RelativePath::from_path("foo\\bar") |
| 760 |
); |
| 761 |
} |
| 762 |
|
| 763 |
#[cfg(unix)] |
| 764 |
#[test] |
| 765 |
pub fn test_unix_owned_from_path() { |
| 766 |
use std::ffi::OsStr; |
| 767 |
use std::os::unix::ffi::OsStrExt; |
| 768 |
|
| 769 |
assert_eq!( |
| 770 |
Err(FromPathErrorKind::NonRelative.into()), |
| 771 |
RelativePathBuf::from_path(Path::new("/foo/bar")) |
| 772 |
); |
| 773 |
|
| 774 |
// Continuation byte without continuation. |
| 775 |
let non_utf8 = OsStr::from_bytes(&[0x80u8]); |
| 776 |
|
| 777 |
assert_eq!( |
| 778 |
Err(FromPathErrorKind::NonUtf8.into()), |
| 779 |
RelativePathBuf::from_path(Path::new(non_utf8)) |
| 780 |
); |
| 781 |
} |
| 782 |
|
| 783 |
#[cfg(windows)] |
| 784 |
#[test] |
| 785 |
pub fn test_windows_owned_from_path() { |
| 786 |
assert_eq!( |
| 787 |
Err(FromPathErrorKind::NonRelative.into()), |
| 788 |
RelativePathBuf::from_path(Path::new("c:\\foo\\bar")) |
| 789 |
); |
| 790 |
} |