diff --git a/crates/blueprint/metadata/tests/assets/job_enum_param.rs b/crates/blueprint/metadata/tests/assets/job_enum_param.rs new file mode 100644 index 00000000..37697220 --- /dev/null +++ b/crates/blueprint/metadata/tests/assets/job_enum_param.rs @@ -0,0 +1,33 @@ +use std::convert::Infallible; +use blueprint_sdk::event_listeners::tangle::events::TangleEventListener; +use blueprint_sdk::event_listeners::tangle::services::{services_post_processor, services_pre_processor}; +use blueprint_sdk::macros::contexts::{ServicesContext, TangleClientContext}; +use blueprint_sdk::macros::ext::tangle::tangle_subxt::tangle_testnet_runtime::api::services::events::JobCalled; +use blueprint_sdk::config::GadgetConfiguration; + +#[derive(Clone, TangleClientContext, ServicesContext)] +pub struct MyContext { + #[config] + pub env: GadgetConfiguration, + #[call_id] + pub call_id: Option, +} + +#[derive(PartialEq)] +pub enum Status { + Failed, + Success, +} + +#[blueprint_sdk::job( + id = 0, + params(status), + event_listener( + listener = TangleEventListener, + pre_processor = services_pre_processor, + post_processor = services_post_processor, + ), +)] +pub fn check_status(status: Status, _context: MyContext) -> Result { + Ok(status == Status::Success) +} diff --git a/crates/blueprint/metadata/tests/assets/job_primitive_nested_struct_param.rs b/crates/blueprint/metadata/tests/assets/job_primitive_nested_struct_param.rs new file mode 100644 index 00000000..49287a78 --- /dev/null +++ b/crates/blueprint/metadata/tests/assets/job_primitive_nested_struct_param.rs @@ -0,0 +1,47 @@ +use std::convert::Infallible; +use blueprint_sdk::event_listeners::tangle::events::TangleEventListener; +use blueprint_sdk::event_listeners::tangle::services::{services_post_processor, services_pre_processor}; +use blueprint_sdk::macros::contexts::{ServicesContext, TangleClientContext}; +use blueprint_sdk::macros::ext::tangle::tangle_subxt::tangle_testnet_runtime::api::services::events::JobCalled; +use blueprint_sdk::config::GadgetConfiguration; + +#[derive(Clone, TangleClientContext, ServicesContext)] +pub struct MyContext { + #[config] + pub env: GadgetConfiguration, + #[call_id] + pub call_id: Option, +} + +pub struct SomeParam { + pub a: u8, + pub nested: SomeNestedParam, +} + +pub struct SomeNestedParam { + pub b: u16, + pub c: u32, + pub d: u64, + pub e: u128, + pub f: i8, + pub g: i16, + pub h: i32, + pub i: i64, + pub j: i128, + pub k: f32, + pub l: f64, + pub m: bool, +} + +#[blueprint_sdk::job( + id = 0, + params(x), + event_listener( + listener = TangleEventListener, + pre_processor = services_pre_processor, + post_processor = services_post_processor, + ), +)] +pub fn xsquare(x: SomeParam, _context: MyContext) -> Result { + Ok(x.nested.d.saturating_pow(2)) +} diff --git a/crates/blueprint/metadata/tests/generate.rs b/crates/blueprint/metadata/tests/generate.rs index 7aed613f..1eafa424 100644 --- a/crates/blueprint/metadata/tests/generate.rs +++ b/crates/blueprint/metadata/tests/generate.rs @@ -159,3 +159,60 @@ fn generate_job_with_primitive_struct_param() { ) ); } + +#[test] +fn generate_job_with_primitive_nested_struct_param() { + let blueprint = do_test("job_primitive_nested_struct_param"); + assert_eq!(blueprint.jobs.len(), 1); + + let job = &blueprint.jobs[0]; + assert_eq!(job.job_id, 0); + assert_eq!(job.metadata.name, "xsquare"); + + assert_eq!(job.params.len(), 1); + assert_eq!(job.params[0].as_rust_type(), "SomeParam"); + + let nested_struct = FieldType::Struct( + String::from("SomeNestedParam"), + vec![ + (String::from("b"), Box::new(FieldType::Uint16)), + (String::from("c"), Box::new(FieldType::Uint32)), + (String::from("d"), Box::new(FieldType::Uint64)), + (String::from("e"), Box::new(FieldType::Uint128)), + (String::from("f"), Box::new(FieldType::Int8)), + (String::from("g"), Box::new(FieldType::Int16)), + (String::from("h"), Box::new(FieldType::Int32)), + (String::from("i"), Box::new(FieldType::Int64)), + (String::from("j"), Box::new(FieldType::Int128)), + (String::from("k"), Box::new(FieldType::Float64)), + (String::from("l"), Box::new(FieldType::Float64)), + (String::from("m"), Box::new(FieldType::Bool)), + ], + ); + + assert_eq!( + job.params[0], + FieldType::Struct( + "SomeParam".to_string(), + vec![ + (String::from("a"), Box::new(FieldType::Uint8),), + (String::from("nested"), Box::new(nested_struct)) + ] + ) + ); +} + +#[test] +fn generate_job_with_enum_param() { + let blueprint = do_test("job_enum_param"); + assert_eq!(blueprint.jobs.len(), 1); + + let job = &blueprint.jobs[0]; + assert_eq!(job.job_id, 0); + assert_eq!(job.metadata.name, "check_status"); + + assert_eq!(job.params.len(), 1); + + // Enums are represented as strings, since we only support unit variants + assert_eq!(job.params[0].as_rust_type(), "String"); +}