std: add tests for Path::with_extension

This commit is contained in:
João M. Bezerra 2023-07-14 13:19:45 -03:00
parent 11f35d6016
commit 6ffca76e9b

View File

@ -1183,7 +1183,7 @@ pub fn test_prefix_ext() {
#[test] #[test]
pub fn test_push() { pub fn test_push() {
macro_rules! tp ( macro_rules! tp (
($path:expr, $push:expr, $expected:expr) => ( { ($path:expr, $push:expr, $expected:expr) => ({
let mut actual = PathBuf::from($path); let mut actual = PathBuf::from($path);
actual.push($push); actual.push($push);
assert!(actual.to_str() == Some($expected), assert!(actual.to_str() == Some($expected),
@ -1281,7 +1281,7 @@ macro_rules! tp (
#[test] #[test]
pub fn test_pop() { pub fn test_pop() {
macro_rules! tp ( macro_rules! tp (
($path:expr, $expected:expr, $output:expr) => ( { ($path:expr, $expected:expr, $output:expr) => ({
let mut actual = PathBuf::from($path); let mut actual = PathBuf::from($path);
let output = actual.pop(); let output = actual.pop();
assert!(actual.to_str() == Some($expected) && output == $output, assert!(actual.to_str() == Some($expected) && output == $output,
@ -1335,7 +1335,7 @@ macro_rules! tp (
#[test] #[test]
pub fn test_set_file_name() { pub fn test_set_file_name() {
macro_rules! tfn ( macro_rules! tfn (
($path:expr, $file:expr, $expected:expr) => ( { ($path:expr, $file:expr, $expected:expr) => ({
let mut p = PathBuf::from($path); let mut p = PathBuf::from($path);
p.set_file_name($file); p.set_file_name($file);
assert!(p.to_str() == Some($expected), assert!(p.to_str() == Some($expected),
@ -1369,7 +1369,7 @@ macro_rules! tfn (
#[test] #[test]
pub fn test_set_extension() { pub fn test_set_extension() {
macro_rules! tfe ( macro_rules! tfe (
($path:expr, $ext:expr, $expected:expr, $output:expr) => ( { ($path:expr, $ext:expr, $expected:expr, $output:expr) => ({
let mut p = PathBuf::from($path); let mut p = PathBuf::from($path);
let output = p.set_extension($ext); let output = p.set_extension($ext);
assert!(p.to_str() == Some($expected) && output == $output, assert!(p.to_str() == Some($expected) && output == $output,
@ -1394,6 +1394,46 @@ macro_rules! tfe (
tfe!("/", "foo", "/", false); tfe!("/", "foo", "/", false);
} }
#[test]
pub fn test_with_extension() {
macro_rules! twe (
($input:expr, $extension:expr, $expected:expr) => ({
let input = Path::new($input);
let output = input.with_extension($extension);
assert!(
output.to_str() == Some($expected),
"calling Path::new({:?}).with_extension({:?}): Expected {:?}, got {:?}",
$input, $extension, $expected, output,
);
});
);
twe!("foo", "txt", "foo.txt");
twe!("foo.bar", "txt", "foo.txt");
twe!("foo.bar.baz", "txt", "foo.bar.txt");
twe!(".test", "txt", ".test.txt");
twe!("foo.txt", "", "foo");
twe!("foo", "", "foo");
twe!("", "foo", "");
twe!(".", "foo", ".");
twe!("foo/", "bar", "foo.bar");
twe!("foo/.", "bar", "foo.bar");
twe!("..", "foo", "..");
twe!("foo/..", "bar", "foo/..");
twe!("/", "foo", "/");
// New extension is smaller than file name
twe!("aaa_aaa_aaa", "bbb_bbb", "aaa_aaa_aaa.bbb_bbb");
// New extension is greater than file name
twe!("bbb_bbb", "aaa_aaa_aaa", "bbb_bbb.aaa_aaa_aaa");
// New extension is smaller than previous extension
twe!("ccc.aaa_aaa_aaa", "bbb_bbb", "ccc.bbb_bbb");
// New extension is greater than previous extension
twe!("ccc.bbb_bbb", "aaa_aaa_aaa", "ccc.aaa_aaa_aaa");
}
#[test] #[test]
fn test_eq_receivers() { fn test_eq_receivers() {
use crate::borrow::Cow; use crate::borrow::Cow;
@ -1669,7 +1709,7 @@ fn into_rc() {
#[test] #[test]
fn test_ord() { fn test_ord() {
macro_rules! ord( macro_rules! ord(
($ord:ident, $left:expr, $right:expr) => ( { ($ord:ident, $left:expr, $right:expr) => ({
use core::cmp::Ordering; use core::cmp::Ordering;
let left = Path::new($left); let left = Path::new($left);