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 executor = Executor ::former().form();
let raw_program = parser.parse( [ ".command" ] ).unwrap();
let grammar_program = verifier.to_program( dictionary, raw_program ).unwrap();
a_true!( executor.program( dictionary, grammar_program ).is_ok() );
}
fn with_context()
{
use std ::sync :: { Arc, Mutex };
use ::error_tools ::untyped ::Error;
let parser = Parser;
let dictionary = &Dictionary ::former()
.command
(
wca ::grammar ::Command ::former()
.hint( "hint" )
.long_hint( "long_hint" )
.phrase( "inc" )
.routine
(
| ctx: Context |
ctx
.get()
.ok_or_else( || "Have no value" )
.and_then( | x: Arc< Mutex< i32 > > | { *x.lock().unwrap() += 1; Ok( () ) } )
)
.form()
)
.command
(
wca ::grammar ::Command ::former()
.hint( "hint" )
.long_hint( "long_hint" )
.phrase( "eq" )
.subject().hint( "number" ).kind( Type ::Number ).optional( true ).end()
.routine
(
| ctx: Context, o: VerifiedCommand |
ctx
.get()
.ok_or_else( || "Have no value".to_string() )
.and_then
(
| x: Arc< Mutex< i32 > > |
{
let x = x.lock().unwrap();
let y: i32 = o.args.get( 0 ).ok_or_else( || "Missing subject".to_string() ).unwrap().to_owned().into();
if dbg!( *x ) != y
{ Err( format!( "{} not eq {}", x, y ) ) } else { Ok( () ) }
}
)
)
.form()
)
.form();
let verifier = Verifier;
let ctx = wca ::executor ::Context ::new( Mutex ::new( 0 ) );
let executor = Executor ::former()
.context( ctx )
.form();
let raw_program = parser.parse( [ ".eq", "1" ] ).unwrap();
let grammar_program = verifier.to_program( dictionary, raw_program ).unwrap();
a_true!( executor.program( dictionary, grammar_program ).is_err() );
let raw_program = parser.parse( [ ".eq", "0", ".inc", ".inc", ".eq", "2" ] ).unwrap();
let grammar_program = verifier.to_program( dictionary, raw_program ).unwrap();
a_true!( executor.program( dictionary, grammar_program ).is_ok() );
}
}
tests_index! {
basic,
with_context,
}