Skip to content

Commit

Permalink
Merge pull request Wandalen#924 from Barsik-sus/willbe_integration_tests
Browse files Browse the repository at this point in the history
READY : willbe - wca
  • Loading branch information
dmvict authored Mar 20, 2023
2 parents 399b30b + 8bacbaf commit b2ec3b0
Show file tree
Hide file tree
Showing 19 changed files with 543 additions and 167 deletions.
6 changes: 3 additions & 3 deletions module/move/wca/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -58,9 +58,9 @@ path = "rust/test/ca/wca_tests.rs"
name = "wca_smoke_test"
path = "rust/test/_integration_test/smoke_test.rs"

[[example]]
name = "wca_trivial_sample"
path = "sample/rust/wca_trivial_sample/src/main.rs"
# [[example]]
# name = "wca_trivial_sample"
# path = "sample/rust/wca_trivial_sample/src/main.rs"

[dependencies]
wtools = { version = "~0.2", path = "../../rust/wtools" }
Expand Down
8 changes: 5 additions & 3 deletions module/move/willbe/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -45,9 +45,9 @@ path = "rust/impl/willbe/willbe_entry.rs"
name = "willbe_test"
path = "rust/test/willbe/willbe_test.rs"

# [[test]]
# name = "willbe_smoke_test"
# path = "rust/test/_integration_test/smoke_test.rs"
[[test]]
name = "willbe_smoke_test"
path = "rust/test/_integration_test/smoke_test.rs"

# [[example]]
# name = "willbe_trivial_sample"
Expand All @@ -66,3 +66,5 @@ rand = "0.8.4"
[dev-dependencies]
test_tools = { version = "~0.1", path = "../../rust/test_tools" }
tempfile = "3"
assert_cmd = "2.0"
predicates = "2.1"
115 changes: 90 additions & 25 deletions rust/impl/willbe/commands/each.rs
Original file line number Diff line number Diff line change
@@ -1,46 +1,110 @@
/// Internal namespace.
pub( crate ) mod private
{
use std::env;
use wtools::error::BasicError;
use std::{ env, rc::Rc, cell::RefCell, };
use wca::
{
Args,
NoProperties, Context,
Args, Props,
Context,
Result, BasicError
};

use crate::protected::*;
use wca::InstructionParser;
use crate::commands::{ StartPointStack, EndPointStack };

#[ derive( Clone ) ]
struct PackagesIterator
(
Rc< RefCell< dyn Iterator< Item = Package > > >
);

impl< I > From< I > for PackagesIterator
where
I : Iterator< Item = Package > + 'static
{
fn from( iter : I ) -> Self
{
Self( Rc::new( RefCell::new( iter ) ) )
}
}

impl PackagesIterator
{
fn next( &self ) -> Option< Package >
{
self.0.borrow_mut().next()
}
}

/// Each command declaration
pub fn each_command() -> wca::Command
{
wca::Command::former()
.hint( "Iterate over packages" )
.long_hint( "Iterates over all packages from current directory" )
.phrase( "each" )
.form()
}

///
/// Iterate over subject
/// Iterate over packages
///
pub fn each( args : Args< String, NoProperties >, ctx : Context ) -> Result< (), BasicError >
pub fn each( _ : ( Args, Props ), mut ctx : Context ) -> Result< () >
{
let ctx : &State = ctx.get_ref().unwrap();
println!( "[LOG] Called each command" );

let current_path = env::current_dir().unwrap();
// Already iterate
if let Some( iter ) = ctx.get_mut::< PackagesIterator >()
{
// It isn't end of iterator
let is_current_package_exists = ctx.get_ref::< Option< Package > >().and_then( | p | p.as_ref() ).is_some();
let next_package = iter.next();
if is_current_package_exists && next_package.is_some()
{
ctx.insert( next_package );
}
else
{
ctx.remove::< Option< Package > >();
ctx.remove::< PackagesIterator >();
// At the end of each - go to first endpoint
// remove self from startpoints
ctx.get_mut::< StartPointStack >().and_then( | sp | sp.pop() );
// go to endpoint
let prog_state = ctx.get_mut::< wca::RuntimeState >()
.ok_or_else( || BasicError::new( "Have no Program State" ) )?;

println!( "[LOG] Called each command" );
ctx.get_mut::< EndPointStack >()
.and_then( | ep | ep.pop() )
.map( | point | prog_state.pos = point )
//? What is better - panic or go to the end of the program when endpoints doesn't exists for any reason
.unwrap_or_else( || prog_state.pos = usize::MAX );
}
}
else
{
// Begin iteration
let current_path = env::current_dir().unwrap();
let mut packages_iter = packages_iterate( current_path );

println!( "context: {:#?}\nargs: {:?}", &ctx, &args );
let package = packages_iter.next();

let parser = wca::instruction::DefaultInstructionParser::former().form();
// But anyway program must found the end of `.each`
if package.is_none()
{
println!( "Any package was found at current directory" );
}

let routine = ctx[&args.subject].routine.clone();
packages_iterate(current_path)
.into_iter()
.for_each( | package |
{
env::set_current_dir( package.path() ).unwrap();
routine.perform
(
&parser.parse( &args.subject ).unwrap(),
Some( wca::Context::new( package.path().to_owned() ) )
)
.ok();
});
// Add current package and the iterator to context
ctx.insert( package );
ctx.insert::< PackagesIterator >( packages_iter.into() );

// Start point to previous instruction( back to current )
let prog_state = ctx.get_ref::< wca::RuntimeState >()
.ok_or_else( || BasicError::new( "Have no Program State" ) )?;
ctx.get_or_default::< StartPointStack >().push( prog_state.pos - 1 );
}

Ok( () )
}
Expand All @@ -50,5 +114,6 @@ pub( crate ) mod private

crate::mod_interface!
{
prelude use each_command;
prelude use each;
}
61 changes: 61 additions & 0 deletions rust/impl/willbe/commands/end.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
/// Internal namespace.
pub( crate ) mod private
{
use wca::
{
Args, Props,
Context,
Result, BasicError,
};

use crate::commands::{ StartPointStack, EndPointStack };

// ! TODO: Remove this command somehow

/// End command declaration
pub fn end_command() -> wca::Command
{
wca::Command::former()
.hint( "Command that is end of a block or a program" )
.phrase( "end" )
.form()
}

///
/// End of loop/program
///
pub fn end( _ : ( Args, Props ), ctx : Context ) -> Result< () >
{
println!( "[LOG] end called" );

if let Some( startpoints ) = ctx.get_ref::< StartPointStack >()
{
if let Some( point ) = startpoints.last()
{
let prog_state = ctx.get_mut::< wca::RuntimeState >()
.ok_or_else( || BasicError::new( "Have no Program State" ) )?;

let endpoints = ctx.get_or_default::< EndPointStack >();
// if has no point at current instruction - push it
if endpoints.last() != Some( &( prog_state.pos - 1 ) )
{
endpoints.push( prog_state.pos - 1 );
}

// Go to start point
prog_state.pos = *point;
}
}

Ok( () )
}
}

//

crate::mod_interface!
{
prelude use end_command;
prelude use end;
}
53 changes: 25 additions & 28 deletions rust/impl/willbe/commands/init.rs
Original file line number Diff line number Diff line change
@@ -1,39 +1,35 @@
/// Internal namespace.
pub( crate ) mod private
{
use crate::commands;

///
/// Form CA commands.
/// Form CA commands grammar.
///
pub fn commands_form() -> std::collections::HashMap< String, wca::command::Command >
pub fn grammar_form() -> Vec< wca::Command >
{
let info_command = wca::Command::former()
.hint( "Prints information about package" )
.long_hint( "Prints information about package at current directory" )
.phrase( "crate.info" )
.routine_with_ctx( &crate::commands::info::info )
.form();

let each_command = wca::Command::former()
.hint( "--- each ---" )
.long_hint( "--- each ---" )
.phrase( "each" )
.subject_hint( "What to iterate(?)" )
.routine_with_ctx( &crate::commands::each::each )
.form();
vec!
[
commands::each::each_command(),
commands::package::info::info_command(),
commands::package::publish::publish_command(),
commands::end::end_command(),
]
}

let publish_command = wca::Command::former()
.hint( "--- publish ---" )
.long_hint( "--- publish ---" )
.phrase( "each" )
.routine( &crate::commands::publish::publish )
.form();
///
/// Form CA commands executor.
///
std::collections::HashMap::from
([
( ".crate.info".to_string(), info_command ),
( ".each".to_string(), each_command ),
( ".crate.publish".to_string(), publish_command ),
pub fn executor_form() -> std::collections::HashMap< String, wca::Routine >
{
std::collections::HashMap::from(
[
( "each".to_owned(), wca::Routine::new_with_ctx( commands::each::each ) ),
( "crate.info".to_owned(), wca::Routine::new_with_ctx( commands::package::info::info ) ),
( "crate.publish".to_owned(), wca::Routine::new_with_ctx( commands::package::publish::publish ) ),
( "end".to_owned(), wca::Routine::new_with_ctx( commands::end::end ) ),
])
}
}
Expand All @@ -42,5 +38,6 @@ pub( crate ) mod private

crate::mod_interface!
{
prelude use commands_form;
prelude use grammar_form;
prelude use executor_form;
}
Loading

0 comments on commit b2ec3b0

Please sign in to comment.