From a494b0ff24dcd01bd341fb4bcd7dba66811c31b8 Mon Sep 17 00:00:00 2001 From: Valery Antopol Date: Tue, 7 Sep 2021 11:41:23 +0300 Subject: [PATCH] wip code for sharing --- crates/marine-test-macro-impl/src/lib.rs | 1 + .../src/marine_test/glue_code_generator.rs | 44 ++++++++++++++++--- .../src/marine_test/marine_test_impl.rs | 13 ++++++ .../src/marine_test/mod.rs | 1 + 4 files changed, 54 insertions(+), 5 deletions(-) diff --git a/crates/marine-test-macro-impl/src/lib.rs b/crates/marine-test-macro-impl/src/lib.rs index ae3d902..2764bd8 100644 --- a/crates/marine-test-macro-impl/src/lib.rs +++ b/crates/marine-test-macro-impl/src/lib.rs @@ -31,6 +31,7 @@ mod errors; mod marine_test; pub use marine_test::marine_test_impl; +pub use marine_test::marine_test_impl2; pub use errors::TestGeneratorError; pub(crate) type TResult = std::result::Result; diff --git a/crates/marine-test-macro-impl/src/marine_test/glue_code_generator.rs b/crates/marine-test-macro-impl/src/marine_test/glue_code_generator.rs index c775e67..4d81fef 100644 --- a/crates/marine-test-macro-impl/src/marine_test/glue_code_generator.rs +++ b/crates/marine-test-macro-impl/src/marine_test/glue_code_generator.rs @@ -127,7 +127,7 @@ pub(super) fn generate_test_glue_code( }; let app_service_ctor = generate_app_service_ctor(&attrs.config_path, &modules_dir)?; - let modules_dir = file_path.join(modules_dir); + /*let modules_dir = file_path.join(modules_dir); let module_interfaces = marine_test::config_utils::collect_modules(&marine_config, modules_dir)?; let linked_modules = marine_test::modules_linker::link_modules(&module_interfaces)?; @@ -135,7 +135,7 @@ pub(super) fn generate_test_glue_code( let module_definitions = marine_test::module_generator::generate_module_definitions( module_interfaces.iter(), &linked_modules, - )?; + )?;*/ let original_block = func_item.block; let signature = func_item.sig; @@ -148,9 +148,7 @@ pub(super) fn generate_test_glue_code( #[test] fn #name() { // definitions for wasm modules specified in config - pub mod marine_test_env { - #(#module_definitions)* - } + // AppService constructor and instantiation to implicit `marine` variable #app_service_ctor @@ -170,6 +168,42 @@ pub(super) fn generate_test_glue_code( Ok(glue_code) } +pub(super) fn generate_test_glue_code2( + attrs: MTestAttributes, +) -> TResult { + let file_path = PathBuf::from("."); + let config_path = file_path.join(&attrs.config_path); + + let marine_config = TomlAppServiceConfig::load(&config_path)?; + let modules_dir = match config_utils::resolve_modules_dir(&marine_config, attrs.modules_dir) { + Some(modules_dir) => modules_dir, + None => return Err(TestGeneratorError::ModulesDirUnspecified), + }; + + let modules_dir = file_path.join(modules_dir); + let module_interfaces = + marine_test::config_utils::collect_modules(&marine_config, modules_dir)?; + let linked_modules = marine_test::modules_linker::link_modules(&module_interfaces)?; + + let module_definitions = marine_test::module_generator::generate_module_definitions( + module_interfaces.iter(), + &linked_modules, + )?; + + let glue_code = quote! { + use marine_rs_sdk_test; + pub mod marine_test_env { + #(#module_definitions)* + } + + pub fn message() -> String { + "hello".to_string() + } + }; + + Ok(glue_code) +} + fn generate_app_service_ctor(config_path: &str, modules_dir: &Path) -> TResult { let modules_dir = modules_dir .to_str() diff --git a/crates/marine-test-macro-impl/src/marine_test/marine_test_impl.rs b/crates/marine-test-macro-impl/src/marine_test/marine_test_impl.rs index 832fc5a..ad1fdda 100644 --- a/crates/marine-test-macro-impl/src/marine_test/marine_test_impl.rs +++ b/crates/marine-test-macro-impl/src/marine_test/marine_test_impl.rs @@ -17,6 +17,7 @@ use crate::attributes::MTestAttributes; use crate::TResult; use crate::marine_test::glue_code_generator::generate_test_glue_code; +use crate::marine_test::glue_code_generator::generate_test_glue_code2; use proc_macro2::TokenStream; use darling::FromMeta; @@ -38,3 +39,15 @@ pub fn marine_test_impl( generate_test_glue_code(func_item, attrs, file_path) } + +pub fn marine_test_impl2( + attrs: TokenStream, +) -> TResult { + // from https://github.com/dtolnay/syn/issues/788 + let parser = syn::punctuated::Punctuated::::parse_terminated; + let attrs = parser.parse2(attrs)?; + let attrs: Vec = attrs.into_iter().collect(); + let attrs = MTestAttributes::from_list(&attrs)?; + + generate_test_glue_code2(attrs) +} diff --git a/crates/marine-test-macro-impl/src/marine_test/mod.rs b/crates/marine-test-macro-impl/src/marine_test/mod.rs index 8b98009..6c3f069 100644 --- a/crates/marine-test-macro-impl/src/marine_test/mod.rs +++ b/crates/marine-test-macro-impl/src/marine_test/mod.rs @@ -22,3 +22,4 @@ mod utils; mod modules_linker; pub use marine_test_impl::marine_test_impl; +pub use marine_test_impl::marine_test_impl2; \ No newline at end of file