Skip to content

Commit

Permalink
test fix
Browse files Browse the repository at this point in the history
  • Loading branch information
SRetip committed May 28, 2024
1 parent e6ab345 commit 22f9eac
Show file tree
Hide file tree
Showing 4 changed files with 95 additions and 164 deletions.
16 changes: 0 additions & 16 deletions module/move/wca/tests/assets/wca_hello_test/src/main.rs

This file was deleted.

This file was deleted.

This file was deleted.

186 changes: 95 additions & 91 deletions module/move/wca/tests/inc/commands_aggregator/help.rs
Original file line number Diff line number Diff line change
@@ -1,23 +1,8 @@
use std::fs::File;
use std::fs::{DirBuilder, File};
use std::io::Write;
use std::path::Path;
use std::process::{Command, Stdio};
use assert_fs::fixture::PathCopy;

const ASSET_PATH : &str = concat!( env!("CARGO_MANIFEST_DIR"), "/tests/assets/" );


fn arrange( source: &str ) -> assert_fs::TempDir
{
let root_path = Path::new( env!( "CARGO_MANIFEST_DIR" ) );
let assets_relative_path = Path::new( ASSET_PATH );
let assets_path = root_path.join( assets_relative_path );

let temp = assert_fs::TempDir::new().unwrap();
temp.copy_from( assets_path.join( source ), &[ "**" ] ).unwrap();

temp
}
pub fn start_sync< AP, Args, Arg, P >
(
application : AP,
Expand All @@ -29,13 +14,15 @@ pub fn start_sync< AP, Args, Arg, P >
let args = args.into_iter().map( | a | a.as_ref().into() ).collect::< Vec< std::ffi::OsString > >();
let child = Command::new( application ).args( &args ).stdout( Stdio::piped() ).stderr( Stdio::piped() ).current_dir( path ).spawn().unwrap();
let output = child.wait_with_output().unwrap();

String::from_utf8( output.stdout ).unwrap()
}

#[ test ]
fn help_command_with_optional_params()
{
let temp = assert_fs::TempDir::new().unwrap();

let toml = format!
(
r#"[package]
Expand All @@ -46,53 +33,38 @@ edition = "2021"
wca = {{path = "{}"}}"#,
env!( "CARGO_MANIFEST_DIR" ).replace( "\\", "/" )
) ;

let temp = arrange( "wca_hello_test" );
let mut file = File::create( temp.path().join( "Cargo.toml" ) ).unwrap();
file.write_all( toml.as_bytes() ).unwrap();

let main = r#"use wca::{ Type, VerifiedCommand };
fn main(){
let ca = wca::CommandsAggregator::former()
.command( "echo" )
.hint( "prints all subjects and properties" )
.subject().hint( "Subject" ).kind( Type::String ).optional( true ).end()
.property( "property" ).hint( "simple property" ).kind( Type::String ).optional( true ).end()
.routine( | o : VerifiedCommand | { println!( "= Args\n{:?}\n\n= Properties\n{:?}\n", o.args, o.props ) } )
.end()
.perform();
let args = std::env::args().skip( 1 ).collect::< Vec< String > >();
ca.perform( args ).unwrap();
}
"#;
File::create( temp.path().join( "Cargo.toml" ) ).unwrap().write_all( toml.as_bytes() ).unwrap();
DirBuilder::new().create( temp.join( "src" ) ).unwrap();
File::create( temp.path().join( "src" ).join( "main.rs" ) ).unwrap().write_all( main.as_bytes() ).unwrap();
let result = start_sync( "cargo", [ "r", ".help", "echo" ], temp.path() );

assert_eq!
(
"Help command\n\n.echo < subjects > < properties > - prints all subjects and properties\n\nSubjects:\n\t- Subject [?String]\nProperties:\n\tproperty - simple property [?String]\n",
result
);
}

/// `wca_help_test_nature_order/src/main.rs` :
/// ```rust
/// fn main()
/// {
/// use wca::{ Type, VerifiedCommand };
///
/// let ca = wca::CommandsAggregator::former()
/// .command( "c" )
/// .hint( "c" )
/// .property( "c-property" ).kind( Type::String ).optional( true ).end()
/// .property( "b-property" ).kind( Type::String ).optional( true ).end()
/// .property( "a-property" ).kind( Type::String ).optional( true ).end()
/// .routine( | o : VerifiedCommand | { println!("c") } )
/// .end()
/// .command( "b" )
/// .hint( "b" )
/// .property( "b-property" ).kind( Type::String ).optional( true ).end()
/// .routine( | o : VerifiedCommand | { println!("b") } )
/// .end()
/// .command( "a" )
/// .hint( "a" )
/// .property( "a-property" ).kind( Type::String ).optional( true ).end()
/// .routine( | o : VerifiedCommand | { println!("a") } )
/// .end()
/// .with_nature_sort( true )
/// .perform();
///
/// let args = std::env::args().skip( 1 ).collect::< Vec< String > >();
/// ca.perform( args ).unwrap();
/// }
/// ```
#[ test ]
fn help_command_with_nature_order()
{
let temp = assert_fs::TempDir::new().unwrap();

let toml = format!
(
r#"[package]
Expand All @@ -104,9 +76,40 @@ wca = {{path = "{}"}}"#,
env!( "CARGO_MANIFEST_DIR" ).replace( "\\", "/" )
) ;

let temp = arrange( "wca_help_test_nature_order" );
let mut file = File::create( temp.path().join( "Cargo.toml" ) ).unwrap();
file.write_all( toml.as_bytes() ).unwrap();
let main = r#"fn main()
{
use wca::{ Type, VerifiedCommand, Order };
let ca = wca::CommandsAggregator::former()
.command( "c" )
.hint( "c" )
.property( "c-property" ).kind( Type::String ).optional( true ).end()
.property( "b-property" ).kind( Type::String ).optional( true ).end()
.property( "a-property" ).kind( Type::String ).optional( true ).end()
.routine( | o : VerifiedCommand | { println!("c") } )
.end()
.command( "b" )
.hint( "b" )
.property( "b-property" ).kind( Type::String ).optional( true ).end()
.routine( | o : VerifiedCommand | { println!("b") } )
.end()
.command( "a" )
.hint( "a" )
.property( "a-property" ).kind( Type::String ).optional( true ).end()
.routine( | o : VerifiedCommand | { println!("a") } )
.end()
.order( Order::Nature )
.perform();
let args = std::env::args().skip( 1 ).collect::< Vec< String > >();
ca.perform( args ).unwrap();
}"#;

File::create( temp.path().join( "Cargo.toml" ) ).unwrap().write_all( toml.as_bytes() ).unwrap();
DirBuilder::new().create( temp.join( "src" ) ).unwrap();
File::create( temp.path().join( "src" ).join( "main.rs" ) ).unwrap().write_all( main.as_bytes() ).unwrap();

let result = start_sync( "cargo", [ "r", ".help" ], temp.path() );

assert_eq!
Expand All @@ -117,7 +120,7 @@ wca = {{path = "{}"}}"#,

let result = start_sync( "cargo", [ "r", ".help", "c" ], temp.path() );

println!("{result}");
println!( "{result}" );

assert_eq!
(
Expand All @@ -126,40 +129,11 @@ wca = {{path = "{}"}}"#,
);
}

/// `wca_help_test_lexicography_order/src/main.rs` :
/// ```rust
/// fn main()
/// {
/// use wca::{ Type, VerifiedCommand };
///
/// let ca = wca::CommandsAggregator::former()
/// .command( "c" )
/// .hint( "c" )
/// .property( "c-property" ).kind( Type::String ).optional( true ).end()
/// .property( "b-property" ).kind( Type::String ).optional( true ).end()
/// .property( "a-property" ).kind( Type::String ).optional( true ).end()
/// .routine( | o : VerifiedCommand | { println!("c") } )
/// .end()
/// .command( "b" )
/// .hint( "b" )
/// .property( "b-property" ).kind( Type::String ).optional( true ).end()
/// .routine( | o : VerifiedCommand | { println!("b") } )
/// .end()
/// .command( "a" )
/// .hint( "a" )
/// .property( "a-property" ).kind( Type::String ).optional( true ).end()
/// .routine( | o : VerifiedCommand | { println!("a") } )
/// .end()
/// .with_nature_sort( false )
/// .perform();
///
/// let args = std::env::args().skip( 1 ).collect::< Vec< String > >();
/// ca.perform( args ).unwrap();
/// }
/// ```
#[ test ]
fn help_command_with_lexicography_order()
{
let temp = assert_fs::TempDir::new().unwrap();

let toml = format!
(
r#"[package]
Expand All @@ -171,9 +145,39 @@ wca = {{path = "{}"}}"#,
env!( "CARGO_MANIFEST_DIR" ).replace( "\\", "/" )
) ;

let temp = arrange( "wca_help_test_lexicography_order" );
let mut file = File::create( temp.path().join( "Cargo.toml" ) ).unwrap();
file.write_all( toml.as_bytes() ).unwrap();
let main = r#"fn main()
{
use wca::{ Type, VerifiedCommand, Order };
let ca = wca::CommandsAggregator::former()
.command( "c" )
.hint( "c" )
.property( "c-property" ).kind( Type::String ).optional( true ).end()
.property( "b-property" ).kind( Type::String ).optional( true ).end()
.property( "a-property" ).kind( Type::String ).optional( true ).end()
.routine( | o : VerifiedCommand | { println!("c") } )
.end()
.command( "b" )
.hint( "b" )
.property( "b-property" ).kind( Type::String ).optional( true ).end()
.routine( | o : VerifiedCommand | { println!("b") } )
.end()
.command( "a" )
.hint( "a" )
.property( "a-property" ).kind( Type::String ).optional( true ).end()
.routine( | o : VerifiedCommand | { println!("a") } )
.end()
.order( Order::Lexicography )
.perform();
let args = std::env::args().skip( 1 ).collect::< Vec< String > >();
ca.perform( args ).unwrap();
}"#;

File::create( temp.path().join( "Cargo.toml" ) ).unwrap().write_all( toml.as_bytes() ).unwrap();
DirBuilder::new().create( temp.join( "src" ) ).unwrap();
File::create( temp.path().join( "src" ).join( "main.rs" ) ).unwrap().write_all( main.as_bytes() ).unwrap();

let result = start_sync( "cargo", [ "r", ".help" ], temp.path() );

assert_eq!
Expand Down

0 comments on commit 22f9eac

Please sign in to comment.