housekeeping

This commit is contained in:
vms 2021-04-01 12:11:31 +03:00
parent 6fca2485eb
commit 8e2318dc77
6 changed files with 65 additions and 74 deletions

View File

@ -22,6 +22,7 @@ use fce_wit_parser::interface::FCEModuleInterface;
use std::path::PathBuf;
#[derive(Debug, Clone, PartialEq, Eq)]
pub(super) struct Module<'m> {
pub name: &'m str,
pub interface: FCEModuleInterface,
@ -33,6 +34,7 @@ impl<'m> Module<'m> {
}
}
/// Returns all modules the provided config consist of.
pub(super) fn collect_modules(
config: &TomlAppServiceConfig,
modules_dir: PathBuf,
@ -67,7 +69,11 @@ fn collect_module_paths(
.collect::<Vec<_>>()
}
pub(super) fn determine_modules_dir(
/// Tries to determine a dir with compiled Wasm modules according to the following
/// - if the modules_dir attribute is specified (by user) it will be chosen,
/// - otherwise if modules_dir is specified in AppService config it will be chosen,
/// - otherwise None will be returned.
pub(super) fn resolve_modules_dir(
config: &TomlAppServiceConfig,
modules_dir: Option<String>,
) -> Option<PathBuf> {

View File

@ -18,6 +18,7 @@ use crate::attributes::FCETestAttributes;
use crate::TResult;
use crate::TestGeneratorError;
use crate::fce_test;
use crate::fce_test::config_utils;
use fluence_app_service::TomlAppServiceConfig;
use proc_macro2::TokenStream;
@ -89,7 +90,7 @@ use std::path::PathBuf;
///```
///
/// Here [(0), (1)] - module_definitions
/// [(1), (2)] - fce ctor
/// [(1), (2)] - AppService ctor
/// [(2), (3)] - ctors of all modules of the tested service
/// [(3), (4)] - original test function
pub(super) fn generate_test_glue_code(
@ -97,28 +98,29 @@ pub(super) fn generate_test_glue_code(
attrs: FCETestAttributes,
) -> TResult<TokenStream> {
let fce_config = TomlAppServiceConfig::load(&attrs.config_path)?;
let modules_dir =
match fce_test::config_worker::determine_modules_dir(&fce_config, attrs.modules_dir) {
Some(modules_dir) => modules_dir,
None => return Err(TestGeneratorError::ModulesDirUnspecified),
};
let modules_dir = match config_utils::resolve_modules_dir(&fce_config, attrs.modules_dir) {
Some(modules_dir) => modules_dir,
None => return Err(TestGeneratorError::ModulesDirUnspecified),
};
let fce_ctor = generate_fce_ctor(&attrs.config_path, &modules_dir);
let module_interfaces = fce_test::config_worker::collect_modules(&fce_config, modules_dir)?;
let app_service_ctor = generate_app_service_ctor(&attrs.config_path, &modules_dir);
let module_interfaces = fce_test::config_utils::collect_modules(&fce_config, modules_dir)?;
let module_definitions =
fce_test::module_generator::generate_module_definitions(module_interfaces.iter())?;
let module_iter = module_interfaces.iter().map(|module| module.name);
let module_ctors = generate_module_ctors(module_iter)?;
let original_block = func_item.block;
let signature = func_item.sig;
let glue_code = quote! {
#[test]
#signature {
#module_definitions
#(#module_definitions)*
#fce_ctor
#app_service_ctor
#module_ctors
@ -129,7 +131,7 @@ pub(super) fn generate_test_glue_code(
Ok(glue_code)
}
fn generate_fce_ctor(config_path: &str, modules_dir: &PathBuf) -> TokenStream {
fn generate_app_service_ctor(config_path: &str, modules_dir: &PathBuf) -> TokenStream {
let config_path = config_path.to_token_stream();
let modules_dir = modules_dir.to_string_lossy().to_string();

View File

@ -14,7 +14,7 @@
* limitations under the License.
*/
mod config_worker;
mod config_utils;
mod fce_test_impl;
mod glue_code_generator;
mod module_generator;

View File

@ -18,11 +18,9 @@ mod methods_generator;
mod record_type_generator;
use crate::fce_test::utils;
use crate::fce_test::config_worker::Module;
use crate::fce_test::config_utils::Module;
use crate::TResult;
use fce_wit_parser::interface::FCEModuleInterface;
use proc_macro2::TokenStream;
use quote::quote;
@ -57,25 +55,19 @@ use quote::quote;
///```
pub(super) fn generate_module_definitions<'i>(
modules: impl ExactSizeIterator<Item = &'i Module<'i>>,
) -> TResult<TokenStream> {
let mut module_definitions = Vec::with_capacity(modules.len());
for module in modules {
let module_definition = generate_module_definition(&module.name, &module.interface)?;
module_definitions.push(module_definition);
}
let module_definitions = quote! { #(#module_definitions),*};
Ok(module_definitions)
) -> TResult<Vec<TokenStream>> {
modules
.into_iter()
.map(generate_module_definition)
.collect::<Result<Vec<_>, _>>()
}
fn generate_module_definition(
module_name: &str,
module_interface: &FCEModuleInterface,
) -> TResult<TokenStream> {
fn generate_module_definition(module: &Module<'_>) -> TResult<TokenStream> {
let module_name = module.name;
let module_name_ident = utils::generate_module_name(module_name)?;
let struct_name_ident = utils::generate_struct_name(module_name)?;
let module_interface = &module.interface;
let module_records = record_type_generator::generate_records(&module_interface.record_types)?;
let module_functions = methods_generator::generate_module_methods(
module_name,
@ -85,7 +77,7 @@ fn generate_module_definition(
let module_definition = quote! {
pub mod #module_name_ident {
#module_records
#(#module_records)*
pub struct #struct_name_ident {
fce: std::rc::Rc<std::cell::RefCell<fluence_test::internal::AppService>>,

View File

@ -30,24 +30,22 @@ pub(super) fn generate_module_methods<'m, 'r>(
method_signatures: impl ExactSizeIterator<Item = &'m FCEFunctionSignature>,
records: &'r FCERecordTypes,
) -> TResult<Vec<TokenStream>> {
let mut result = Vec::with_capacity(method_signatures.len());
method_signatures
.map(|signature| -> TResult<TokenStream> {
let func_name = utils::new_ident(&signature.name)?;
let arguments = generate_arguments(signature.arguments.iter(), records)?;
let output_type = generate_output_type(&signature.outputs, records)?;
let fce_call = generate_fce_call(module_name, &signature, records)?;
for signature in method_signatures {
let func_name = utils::new_ident(&signature.name)?;
let arguments = generate_arguments(signature.arguments.iter(), records)?;
let output_type = generate_output_type(&signature.outputs, records)?;
let fce_call = generate_fce_call(module_name, &signature, records)?;
let module_method = quote! {
pub fn #func_name(&mut self, #(#arguments),*) #output_type {
#fce_call
}
};
let module_method = quote! {
pub fn #func_name(&mut self, #(#arguments),*) #output_type {
#fce_call
}
};
result.push(module_method);
}
Ok(result)
Ok(module_method)
})
.collect::<Result<Vec<_>, _>>()
}
fn generate_fce_call(
@ -79,20 +77,16 @@ fn generate_fce_call(
Ok(function_call)
}
/// Generates type convertor to json because of AppService receives them in json.
fn generate_arguments_converter<'a>(
args: impl ExactSizeIterator<Item = &'a str>,
) -> TResult<TokenStream> {
let mut arguments = Vec::with_capacity(args.len());
let arg_idents: Vec<syn::Ident> = args.map(utils::new_ident).collect::<Result<_, _>>()?;
for arg in args {
let arg_ident = utils::new_ident(arg)?;
arguments.push(arg_ident);
}
let args_converter =
quote! { let arguments = fluence_test::internal::json!([#(#arg_idents),*]); };
let arguments_serializer =
quote! { let arguments = fluence_test::internal::json!([#(#arguments),*]); };
Ok(arguments_serializer)
Ok(args_converter)
}
fn generate_function_call(module_name: &str, method_name: &str) -> TokenStream {

View File

@ -23,40 +23,37 @@ use fce_wit_parser::interface::FCERecordTypes;
use proc_macro2::TokenStream;
use quote::quote;
pub(super) fn generate_records(records: &FCERecordTypes) -> TResult<TokenStream> {
pub(super) fn generate_records(records: &FCERecordTypes) -> TResult<Vec<TokenStream>> {
use std::ops::Deref;
let mut result = TokenStream::new();
for (_, record) in records.iter() {
records.iter().map(|(_, record)| -> TResult<TokenStream> {
let record_name_ident = utils::generate_record_name(&record.name)?;
let fields = prepare_field(record.fields.deref().iter(), records)?;
let record = quote! {
let generated_record = quote! {
#[derive(Clone, fluence_test::internal::Serialize, fluence_test::internal::Deserialize)]
pub struct #record_name_ident {
#(#fields),*
}
};
result.extend(record);
}
Ok(result)
Ok(generated_record)
}
).collect::<Result<Vec<_>, _>>()
}
fn prepare_field<'f>(
fields: impl ExactSizeIterator<Item = &'f IRecordFieldType>,
records: &FCERecordTypes,
) -> TResult<Vec<TokenStream>> {
let mut result = Vec::with_capacity(fields.len());
fields
.map(|field| -> TResult<TokenStream> {
let field_name = utils::new_ident(&field.name)?;
let field_type = utils::itype_to_tokens(&field.ty, records)?;
for field in fields {
let field_name = utils::new_ident(&field.name)?;
let field_type = utils::itype_to_tokens(&field.ty, records)?;
let generated_field = quote! { #field_name: #field_type };
let field = quote! { #field_name: #field_type };
result.push(field);
}
Ok(result)
Ok(generated_field)
})
.collect::<Result<Vec<_>, _>>()
}