use super :: *;
use the_module ::
{
parser ::Parser,
VerifiedCommand,
executor ::Context,
Type,
grammar ::Dictionary,
verifier ::Verifier,
Executor,
};
tests_impls! {
fn basic()
{
let parser = Parser;
let dictionary = &Dictionary ::former()
.command
(
wca ::grammar ::Command ::former()
.hint( "hint" )
.long_hint( "long_hint" )
.phrase( "command" )
.routine( || println!( "hello" ) )
.form()
)
.form();
let verifier = Verifier;
let raw_command = parser.parse( [ ".command" ] ).unwrap().commands.remove( 0 );
let grammar_command = verifier.to_command( dictionary, raw_command ).unwrap();
let executor = Executor ::former().form();
a_true!( executor.command( dictionary, grammar_command ).is_ok() );
}
fn with_subject()
{
let parser = Parser;
let dictionary = &Dictionary ::former()
.command
(
wca ::grammar ::Command ::former()
.hint( "hint" )
.long_hint( "long_hint" )
.phrase( "command" )
.subject().hint( "hint" ).kind( Type ::String ).optional( false ).end()
.routine( | o: VerifiedCommand | o.args.get( 0 ).map( | a | println!( "{a:?}" ) ).ok_or_else( || "Subject not found" ) )
.form()
)
.form();
let verifier = Verifier;
let executor = Executor ::former().form();
let raw_command = parser.parse( [ ".command", "subject" ] ).unwrap().commands.remove( 0 );
let grammar_command = verifier.to_command( dictionary, raw_command ).unwrap();
a_true!( executor.command( dictionary, grammar_command ).is_ok() );
let raw_command = parser.parse( [ ".command" ] ).unwrap().commands.remove( 0 );
let grammar_command = verifier.to_command( dictionary, raw_command );
a_true!( grammar_command.is_err() );
}
fn with_property()
{
let parser = Parser;
let dictionary = &Dictionary ::former()
.command
(
the_module ::grammar ::Command ::former()
.hint( "hint" )
.long_hint( "long_hint" )
.phrase( "command" )
.subject().hint( "Any string." ).kind( Type ::String ).optional( true ).end()
.routine( | o: VerifiedCommand | println!( "command executed" ) )
.form()
)
.form();
let verifier = Verifier;
let executor = Executor ::former().form();
let raw_command = parser.parse( [ ".command", "prop: value" ] ).unwrap().commands.remove( 0 );
let grammar_command = verifier.to_command( dictionary, raw_command ).unwrap();
a_true!( executor.command( dictionary, grammar_command ).is_ok() );
let raw_command = parser.parse( [ ".command", "subject" ] ).unwrap().commands.remove( 0 );
let grammar_command = verifier.to_command( dictionary, raw_command );
a_true!( grammar_command.is_ok() );
let raw_command = parser.parse( [ ".command" ] ).unwrap().commands.remove( 0 );
let grammar_command = verifier.to_command( dictionary, raw_command );
a_true!( grammar_command.is_ok() );
}
fn with_context()
{
use std ::sync :: { Arc, Mutex };
let parser = Parser;
let dictionary = &Dictionary ::former()
.command
(
wca ::grammar ::Command ::former()
.hint( "hint" )
.long_hint( "long_hint" )
.phrase( "check" )
.routine
(
| ctx: Context |
ctx
.get()
.ok_or_else( || "Have no value" )
.and_then( | x: Arc< Mutex< i32 > > | if *x.lock().unwrap() != 1 { Err( "x not eq 1" ) } else { Ok( () ) } )
)
.form()
)
.form();
let verifier = Verifier;
let mut ctx = wca ::executor ::Context ::new( Mutex ::new( 1 ) );
let executor = Executor ::former()
.context( ctx )
.form();
let raw_command = parser.parse( [ ".check" ] ).unwrap().commands.remove( 0 );
let grammar_command = verifier.to_command( dictionary, raw_command ).unwrap();
a_true!( executor.command( dictionary, grammar_command ).is_ok() );
}
fn without_routine()
{
let parser = Parser;
let dictionary = &Dictionary ::former()
.command
(
wca ::grammar ::Command ::former()
.hint( "hint" )
.long_hint( "long_hint" )
.phrase( "command" )
.form()
)
.form();
let verifier = Verifier;
let executor = Executor ::former().form();
let raw_command = parser.parse( [ ".command" ] ).unwrap().commands.remove( 0 );
let grammar_command = verifier.to_command( dictionary, raw_command ).unwrap();
a_true!( executor.command( dictionary, grammar_command ).is_err() );
}
}
tests_index! {
basic,
with_subject,
with_property,
with_context,
without_routine,
}