Skip to content

Commit

Permalink
add test
Browse files Browse the repository at this point in the history
  • Loading branch information
chenyan-dfinity committed May 1, 2024
1 parent d9a7519 commit 1d623c5
Show file tree
Hide file tree
Showing 3 changed files with 97 additions and 56 deletions.
11 changes: 11 additions & 0 deletions rust/candid_parser/tests/assets/example.toml
Original file line number Diff line number Diff line change
@@ -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<MyList>" }
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" }
126 changes: 73 additions & 53 deletions rust/candid_parser/tests/assets/ok/example.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<List> }
#[derive(CandidType, Deserialize)]
pub struct List(Option<Node>);
pub type A = Box<B>;
#[derive(CandidType, Deserialize)]
pub struct B(Option<A>);
#[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<List> }
#[derive(CandidType, Deserialize, Debug)]
pub(crate) struct List(Option<Node>);
pub(crate) type A = Box<B>;
#[derive(CandidType, Deserialize, Debug)]
pub(crate) struct B(Option<A>);
#[derive(CandidType, Deserialize, Debug)]
pub(crate) enum Tree {
#[serde(rename="branch")]
Branch{ val: candid::Int, left: Box<Tree>, right: Box<Tree> },
#[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<StreamInner>);
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<StreamInner>);
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<List> }
#[derive(CandidType, Deserialize)]
pub struct List(Option<ListInner>);
#[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<MyList>,
}
#[derive(CandidType, Deserialize, Debug)]
pub(crate) struct MyList(Option<ListInner>);
#[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<String>) }
#[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<List>));
#[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<String>) }
#[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<MyList>));
#[derive(CandidType, Deserialize, Debug)]
pub(crate) enum A { #[serde(rename="a")] A, #[serde(rename="b")] B(B) }

pub struct Service(pub Principal);
impl Service {
Expand All @@ -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<List>, 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<MyList>, arg3: Nested) -> Result<(i128,Broker,)> {
ic_cdk::call(self.0, "g1", (id,list,is_okay,arg3,)).await
}
pub async fn h(&self, arg0: Vec<Option<String>>, arg1: HArg1, arg2: Option<List>) -> Result<(HRet,)> {
pub async fn h(&self, arg0: Vec<Option<String>>, arg1: HArg1, arg2: Option<MyList>) -> Result<(HRet,)> {
ic_cdk::call(self.0, "h", (arg0,arg1,arg2,)).await
}
pub async fn i(&self, arg0: List, arg1: FArg1) -> Result<(Option<List>,)> {
pub async fn i(&self, arg0: MyList, arg1: FArg1) -> Result<(Option<MyList>,)> {
ic_cdk::call(self.0, "i", (arg0,arg1,)).await
}
pub async fn x(&self, arg0: A, arg1: B) -> Result<(Option<A>,Option<B>,)> {
Expand Down
16 changes: 13 additions & 3 deletions rust/candid_parser/tests/parse_type.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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::<Configs>()
.unwrap();
config = Config::new(configs);
}
_ => (),
}
let mut output = mint.new_goldenfile(filename.with_extension("rs")).unwrap();
let content = rust::compile(&config, &env, &actor, external);
Expand Down

0 comments on commit 1d623c5

Please sign in to comment.