Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit ba4c71a

Browse files
committedJul 5, 2024
add unit tests for extra extension feature
Signed-off-by: tison <wander4096@gmail.com>
1 parent 55fc20b commit ba4c71a

File tree

1 file changed

+74
-0
lines changed

1 file changed

+74
-0
lines changed
 

‎std/src/path/tests.rs

+74
Original file line numberDiff line numberDiff line change
@@ -1401,6 +1401,37 @@ pub fn test_set_extension() {
14011401
tfe!("/", "foo", "/", false);
14021402
}
14031403

1404+
#[test]
1405+
pub fn test_add_extension() {
1406+
macro_rules! tfe (
1407+
($path:expr, $ext:expr, $expected:expr, $output:expr) => ({
1408+
let mut p = PathBuf::from($path);
1409+
let output = p.add_extension($ext);
1410+
assert!(p.to_str() == Some($expected) && output == $output,
1411+
"adding extension of {:?} to {:?}: Expected {:?}/{:?}, got {:?}/{:?}",
1412+
$path, $ext, $expected, $output,
1413+
p.to_str().unwrap(), output);
1414+
});
1415+
);
1416+
1417+
tfe!("foo", "txt", "foo.txt", true);
1418+
tfe!("foo.bar", "txt", "foo.bar.txt", true);
1419+
tfe!("foo.bar.baz", "txt", "foo.bar.baz.txt", true);
1420+
tfe!(".test", "txt", ".test.txt", true);
1421+
tfe!("foo.txt", "", "foo.txt", true);
1422+
tfe!("foo", "", "foo", true);
1423+
tfe!("", "foo", "", false);
1424+
tfe!(".", "foo", ".", false);
1425+
tfe!("foo/", "bar", "foo.bar", true);
1426+
tfe!("foo/.", "bar", "foo.bar", true);
1427+
tfe!("..", "foo", "..", false);
1428+
tfe!("foo/..", "bar", "foo/..", false);
1429+
tfe!("/", "foo", "/", false);
1430+
1431+
// edge cases
1432+
tfe!("/foo.ext////", "bar", "/foo.ext.bar", true);
1433+
}
1434+
14041435
#[test]
14051436
pub fn test_with_extension() {
14061437
macro_rules! twe (
@@ -1441,6 +1472,49 @@ pub fn test_with_extension() {
14411472
twe!("ccc.bbb_bbb", "aaa_aaa_aaa", "ccc.aaa_aaa_aaa");
14421473
}
14431474

1475+
#[test]
1476+
pub fn test_with_added_extension() {
1477+
macro_rules! twe (
1478+
($input:expr, $extension:expr, $expected:expr) => ({
1479+
let input = Path::new($input);
1480+
let output = input.with_added_extension($extension);
1481+
1482+
assert!(
1483+
output.to_str() == Some($expected),
1484+
"calling Path::new({:?}).with_added_extension({:?}): Expected {:?}, got {:?}",
1485+
$input, $extension, $expected, output,
1486+
);
1487+
});
1488+
);
1489+
1490+
twe!("foo", "txt", "foo.txt");
1491+
twe!("foo.bar", "txt", "foo.bar.txt");
1492+
twe!("foo.bar.baz", "txt", "foo.bar.baz.txt");
1493+
twe!(".test", "txt", ".test.txt");
1494+
twe!("foo.txt", "", "foo.txt");
1495+
twe!("foo", "", "foo");
1496+
twe!("", "foo", "");
1497+
twe!(".", "foo", ".");
1498+
twe!("foo/", "bar", "foo.bar");
1499+
twe!("foo/.", "bar", "foo.bar");
1500+
twe!("..", "foo", "..");
1501+
twe!("foo/..", "bar", "foo/..");
1502+
twe!("/", "foo", "/");
1503+
1504+
// edge cases
1505+
twe!("/foo.ext////", "bar", "/foo.ext.bar");
1506+
1507+
// New extension is smaller than file name
1508+
twe!("aaa_aaa_aaa", "bbb_bbb", "aaa_aaa_aaa.bbb_bbb");
1509+
// New extension is greater than file name
1510+
twe!("bbb_bbb", "aaa_aaa_aaa", "bbb_bbb.aaa_aaa_aaa");
1511+
1512+
// New extension is smaller than previous extension
1513+
twe!("ccc.aaa_aaa_aaa", "bbb_bbb", "ccc.aaa_aaa_aaa.bbb_bbb");
1514+
// New extension is greater than previous extension
1515+
twe!("ccc.bbb_bbb", "aaa_aaa_aaa", "ccc.bbb_bbb.aaa_aaa_aaa");
1516+
}
1517+
14441518
#[test]
14451519
fn test_eq_receivers() {
14461520
use crate::borrow::Cow;

0 commit comments

Comments
 (0)
Failed to load comments.