use super::*;
use std::
{
borrow::Cow,
io,
path::{ Path, PathBuf },
};
#[ test ]
fn basic() -> Result< (), io::Error >
{
use the_module::PathJoined;
use std::path::PathBuf;
let path1 : &str = "/some";
let path2 : String = "path".into();
let path3 : PathBuf = "to/file".into();
let path4 : &str = "extra";
let path5 : String = "components".into();
let joined1 : PathBuf = ( path1, ).iter_join()?;
println!( "Joined PathBuf (1): {:?}", joined1 );
let joined2 : PathBuf = ( path1, path2.clone() ).iter_join()?;
println!( "Joined PathBuf (2): {:?}", joined2 );
let joined3 : PathBuf = ( path1, path2.clone(), path3.clone() ).iter_join()?;
println!( "Joined PathBuf (3): {:?}", joined3 );
let joined4 : PathBuf = ( path1, path2.clone(), path3.clone(), path4 ).iter_join()?;
println!( "Joined PathBuf (4): {:?}", joined4 );
let joined5 : PathBuf = ( path1, path2, path3, path4, path5 ).iter_join()?;
println!( "Joined PathBuf (5): {:?}", joined5 );
Ok( () )
}
#[ test ]
fn array_join_paths_test() -> Result< (), io::Error >
{
use the_module::{ PathJoined, TryIntoCowPath };
use std::path::PathBuf;
let path_components : [ &str; 3 ] = [ "/some", "path", "to/file" ];
let joined : PathBuf = path_components.iter_join()?;
println!( "Joined PathBuf from slice: {:?}", joined );
let expected = PathBuf::from( "/some/path/to/file" );
assert_eq!( joined, expected );
Ok( () )
}
#[ test ]
fn slice_join_paths_test() -> Result< (), io::Error >
{
use the_module::{ PathJoined, TryIntoCowPath };
use std::path::PathBuf;
let path_components : [ &str; 3 ] = [ "/some", "path", "to/file" ];
let slice : &[ &str ] = &path_components[ .. ];
let joined : PathBuf = slice.iter_join()?;
println!( "Joined PathBuf from slice: {:?}", joined );
let expected = PathBuf::from( "/some/path/to/file" );
assert_eq!( joined, expected );
Ok( () )
}
#[ test ]
fn all_types() -> Result< (), io::Error >
{
use std::path::Path;
use the_module::{ AbsolutePath, CanonicalPath, NativePath, CurrentPath };
use the_module::{ PathJoined, AsPath, TryIntoPath };
{
let absolute_path = AbsolutePath::try_from( "/absolute/path" ).unwrap();
let current_path = CurrentPath;
let joined = ( absolute_path.clone(), current_path ).iter_join()?;
let expected = current_path.try_into_path()?;
println!( "Joined PathBuf: {:?}", joined );
assert_eq!( joined, expected );
}
{
let absolute_path = AbsolutePath::try_from( "/absolute/path" ).unwrap();
let component = Path::new( "/component/path" ).components().next().unwrap();
println!( "component : {component:?}" );
let joined = ( absolute_path, component ).iter_join()?;
let expected = component.as_path();
println!( "Joined PathBuf: {:?}", joined );
assert_eq!( joined, expected );
}
{
let absolute_path = AbsolutePath::try_from( "/absolute/path" ).unwrap();
let path_str : &str = "additional/str";
let joined = ( absolute_path, path_str ).iter_join()?;
let expected = PathBuf::from( "/absolute/path/additional/str" );
println!( "Joined PathBuf: {:?}", joined );
assert_eq!( joined, expected );
}
{
let absolute_path = AbsolutePath::try_from( "/absolute/path" ).unwrap();
let native_path = NativePath::try_from( PathBuf::from( "/native/path" ) ).unwrap();
let joined = ( absolute_path, native_path ).iter_join()?;
let expected = PathBuf::from( "/native/path" );
println!( "Joined PathBuf: {:?}", joined );
assert_eq!( joined, expected );
}
{
let absolute_path = AbsolutePath::try_from( "/absolute/path" ).unwrap();
let canonical_path = CanonicalPath::try_from( "/canonical/path" ).unwrap();
let joined = ( absolute_path, canonical_path ).iter_join()?;
let expected = PathBuf::from( "/canonical/path" );
println!( "Joined PathBuf: {:?}", joined );
assert_eq!( joined, expected );
}
{
let native_path = NativePath::try_from( PathBuf::from( "/native/path" ) ).unwrap();
let current_path = CurrentPath;
let joined = ( native_path, current_path ).iter_join()?;
let expected = current_path.try_into_path()?;
println!( "Joined PathBuf: {:?}", joined );
assert_eq!( joined, expected );
}
{
let canonical_path = CanonicalPath::try_from( "/canonical/path" ).unwrap();
let component = Path::new( "/component/path" ).components().next().unwrap();
println!( "component : {component:?}" );
let joined = ( canonical_path, component ).iter_join()?;
let expected = component.as_path();
println!( "Joined PathBuf: {:?}", joined );
assert_eq!( joined, expected );
}
Ok( () )
}
#[ test ]
fn join_function_test() -> Result< (), io::Error >
{
use the_module::path;
use std::path::PathBuf;
let path1 : &str = "/some";
let path2 : String = "path".into();
let path3 : PathBuf = "to/file".into();
let joined : PathBuf = path::join( ( path1, path2.clone(), path3.clone() ) )?;
println!( "Joined PathBuf: {:?}", joined );
let expected = PathBuf::from( "/some/path/to/file" );
assert_eq!( joined, expected );
let joined : PathBuf = path::join( ( path1, path2.clone() ) )?;
println!( "Joined PathBuf (2 components): {:?}", joined );
let expected = PathBuf::from( "/some/path" );
assert_eq!( joined, expected );
let joined : PathBuf = path::join( ( path1, ) )?;
println!( "Joined PathBuf (1 component): {:?}", joined );
let expected = PathBuf::from( "/some" );
assert_eq!( joined, expected );
Ok( () )
}