From 1d623c544457b43f3f98a6363f90015277fdcb69 Mon Sep 17 00:00:00 2001 From: Yan Chen <48968912+chenyan-dfinity@users.noreply.github.com> Date: Wed, 1 May 2024 16:32:03 -0700 Subject: [PATCH] add test --- rust/candid_parser/tests/assets/example.toml | 11 ++ rust/candid_parser/tests/assets/ok/example.rs | 126 ++++++++++-------- rust/candid_parser/tests/parse_type.rs | 16 ++- 3 files changed, 97 insertions(+), 56 deletions(-) create mode 100644 rust/candid_parser/tests/assets/example.toml diff --git a/rust/candid_parser/tests/assets/example.toml b/rust/candid_parser/tests/assets/example.toml new file mode 100644 index 00000000..90d9f14a --- /dev/null +++ b/rust/candid_parser/tests/assets/example.toml @@ -0,0 +1,11 @@ +[rust] +attributes = "#[derive(CandidType, Deserialize, Debug)]" +visibility = "pub(crate)" +List.name = "MyList" +Nested41.variant.A = { name = "AAA", attributes = "#[serde(skip_deserializing)]" } +ListInner.attributes = "#derive[CandidType, Deserialize, Clone]" +ListInner.record = { visibility = "", head.name = "HEAD", attributes = "#[serde(skip_deserializing)]", tail.use_type = "Arc" } +my_type = { visibility = "", name = "CanisterId" } +nat.use_type = "u128" +BrokerFindRet = { name = "BrokerReturn", visibility = "pub" } +g1 = { name = "G11", arg0.name = "id", arg1.name = "list", arg2.name = "is_okay", ret0.use_type = "i128" } diff --git a/rust/candid_parser/tests/assets/ok/example.rs b/rust/candid_parser/tests/assets/ok/example.rs index 6dec7032..18891eb8 100644 --- a/rust/candid_parser/tests/assets/ok/example.rs +++ b/rust/candid_parser/tests/assets/ok/example.rs @@ -4,68 +4,88 @@ use candid::{self, CandidType, Deserialize, Principal, Encode, Decode}; use ic_cdk::api::call::CallResult as Result; -#[derive(CandidType, Deserialize)] -pub struct B (pub candid::Int,pub candid::Nat,); -#[derive(CandidType, Deserialize)] -pub struct Node { pub head: candid::Nat, pub tail: Box } -#[derive(CandidType, Deserialize)] -pub struct List(Option); -pub type A = Box; -#[derive(CandidType, Deserialize)] -pub struct B(Option); -#[derive(CandidType, Deserialize)] -pub enum Tree { +#[derive(CandidType, Deserialize, Debug)] +pub(crate) struct B (pub(crate) candid::Int,pub(crate) u128,); +#[derive(CandidType, Deserialize, Debug)] +pub(crate) struct Node { pub(crate) head: u128, pub(crate) tail: Box } +#[derive(CandidType, Deserialize, Debug)] +pub(crate) struct List(Option); +pub(crate) type A = Box; +#[derive(CandidType, Deserialize, Debug)] +pub(crate) struct B(Option); +#[derive(CandidType, Deserialize, Debug)] +pub(crate) enum Tree { #[serde(rename="branch")] Branch{ val: candid::Int, left: Box, right: Box }, #[serde(rename="leaf")] Leaf(candid::Int), } -candid::define_function!(pub StreamInnerNext : () -> (Stream) query); -#[derive(CandidType, Deserialize)] -pub struct StreamInner { pub head: candid::Nat, pub next: StreamInnerNext } -#[derive(CandidType, Deserialize)] -pub struct Stream(Option); -candid::define_service!(pub S : { +candid::define_function!(pub(crate) StreamInnerNext : () -> (Stream) query); +#[derive(CandidType, Deserialize, Debug)] +pub(crate) struct StreamInner { + pub(crate) head: u128, + pub(crate) next: StreamInnerNext, +} +#[derive(CandidType, Deserialize, Debug)] +pub(crate) struct Stream(Option); +candid::define_service!(pub(crate) S : { "f" : T::ty(); "g" : candid::func!((List) -> (B, Tree, Stream)); }); -candid::define_function!(pub T : (S) -> ()); -pub type MyType = Principal; -#[derive(CandidType, Deserialize)] -pub struct ListInner { pub head: candid::Int, pub tail: Box } -#[derive(CandidType, Deserialize)] -pub struct List(Option); -#[derive(CandidType, Deserialize)] -pub struct Nested3 { pub _0_: candid::Nat, pub _42_: candid::Nat, pub _43_: u8 } -#[derive(CandidType, Deserialize)] -pub enum Nested41 { _42_, A, B, C } -#[derive(CandidType, Deserialize)] -pub struct Nested { - pub _0_: candid::Nat, - pub _1_: candid::Nat, - pub _2_: (candid::Nat,candid::Int,), - pub _3_: Nested3, - pub _40_: candid::Nat, - pub _41_: Nested41, - pub _42_: candid::Nat, +candid::define_function!(pub(crate) T : (S) -> ()); +type CanisterId = Principal; +#derive[CandidType, Deserialize, Clone] +pub(crate) struct ListInner { + #[serde(skip_deserializing)] + #[serde(rename="head")] + HEAD: candid::Int, + #[serde(skip_deserializing)] + tail: Arc, +} +#[derive(CandidType, Deserialize, Debug)] +pub(crate) struct MyList(Option); +#[derive(CandidType, Deserialize, Debug)] +pub(crate) struct Nested3 { + pub(crate) _0_: u128, + pub(crate) _42_: u128, + pub(crate) _43_: u8, +} +#[derive(CandidType, Deserialize, Debug)] +pub(crate) enum Nested41 { + _42_, + #[serde(skip_deserializing)] + #[serde(rename="A")] + AAA, + B, + C, +} +#[derive(CandidType, Deserialize, Debug)] +pub(crate) struct Nested { + pub(crate) _0_: u128, + pub(crate) _1_: u128, + pub(crate) _2_: (u128,candid::Int,), + pub(crate) _3_: Nested3, + pub(crate) _40_: u128, + pub(crate) _41_: Nested41, + pub(crate) _42_: u128, } -candid::define_service!(pub BrokerFindRet : { +candid::define_service!(pub BrokerReturn : { "current" : candid::func!(() -> (u32)); "up" : candid::func!(() -> ()); }); -candid::define_service!(pub Broker : { - "find" : candid::func!((String) -> (BrokerFindRet)); +candid::define_service!(pub(crate) Broker : { + "find" : candid::func!((String) -> (BrokerReturn)); }); -#[derive(CandidType, Deserialize)] -pub enum HArg1 { A(candid::Nat), B(Option) } -#[derive(CandidType, Deserialize)] -pub struct HRet42 {} -#[derive(CandidType, Deserialize)] -pub struct HRet { pub _42_: HRet42, pub id: candid::Nat } -candid::define_function!(pub FArg1 : (i32) -> (i64)); -candid::define_function!(pub F : (List, FArg1) -> (Option)); -#[derive(CandidType, Deserialize)] -pub enum A { #[serde(rename="a")] A, #[serde(rename="b")] B(B) } +#[derive(CandidType, Deserialize, Debug)] +pub(crate) enum HArg1 { A(u128), B(Option) } +#[derive(CandidType, Deserialize, Debug)] +pub(crate) struct HRet42 {} +#[derive(CandidType, Deserialize, Debug)] +pub(crate) struct HRet { pub(crate) _42_: HRet42, pub(crate) id: u128 } +candid::define_function!(pub(crate) FArg1 : (i32) -> (i64)); +candid::define_function!(pub(crate) F : (MyList, FArg1) -> (Option)); +#[derive(CandidType, Deserialize, Debug)] +pub(crate) enum A { #[serde(rename="a")] A, #[serde(rename="b")] B(B) } pub struct Service(pub Principal); impl Service { @@ -81,13 +101,13 @@ impl Service { pub async fn g(&self, arg0: List) -> Result<(B,Tree,Stream,)> { ic_cdk::call(self.0, "g", (arg0,)).await } - pub async fn g_1(&self, arg0: MyType, arg1: List, arg2: Option, arg3: Nested) -> Result<(candid::Int,Broker,)> { - ic_cdk::call(self.0, "g1", (arg0,arg1,arg2,arg3,)).await + pub async fn G11(&self, id: CanisterId, list: MyList, is_okay: Option, arg3: Nested) -> Result<(i128,Broker,)> { + ic_cdk::call(self.0, "g1", (id,list,is_okay,arg3,)).await } - pub async fn h(&self, arg0: Vec>, arg1: HArg1, arg2: Option) -> Result<(HRet,)> { + pub async fn h(&self, arg0: Vec>, arg1: HArg1, arg2: Option) -> Result<(HRet,)> { ic_cdk::call(self.0, "h", (arg0,arg1,arg2,)).await } - pub async fn i(&self, arg0: List, arg1: FArg1) -> Result<(Option,)> { + pub async fn i(&self, arg0: MyList, arg1: FArg1) -> Result<(Option,)> { ic_cdk::call(self.0, "i", (arg0,arg1,)).await } pub async fn x(&self, arg0: A, arg1: B) -> Result<(Option,Option,)> { diff --git a/rust/candid_parser/tests/parse_type.rs b/rust/candid_parser/tests/parse_type.rs index 5f645c1f..2d7bb219 100644 --- a/rust/candid_parser/tests/parse_type.rs +++ b/rust/candid_parser/tests/parse_type.rs @@ -65,13 +65,23 @@ fn compiler_test(resource: &str) { { use rust::{Config, ExternalConfig}; use std::str::FromStr; - let config = Config::new(Configs::from_str("").unwrap()); + let mut config = Config::new(Configs::from_str("").unwrap()); let mut external = ExternalConfig::default(); external .0 .insert("canister_id".to_string(), "aaaaa-aa".to_string()); - if filename.file_name().unwrap().to_str().unwrap() == "management.did" { - external.0.insert("target".to_string(), "agent".to_string()); + match filename.file_name().unwrap().to_str().unwrap() { + "management.did" => { + drop(external.0.insert("target".to_string(), "agent".to_string())) + } + "example.did" => { + let configs = std::fs::read_to_string(base_path.join("example.toml")) + .unwrap() + .parse::() + .unwrap(); + config = Config::new(configs); + } + _ => (), } let mut output = mint.new_goldenfile(filename.with_extension("rs")).unwrap(); let content = rust::compile(&config, &env, &actor, external);