-
Notifications
You must be signed in to change notification settings - Fork 90
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: macros that decoding arguments can set custom decoder using dec…
…ode_with (#544) * cleanup dfn_macro internal * no arg decoding if function sig has no args * name check * decode_with: set custom arg decoder
- Loading branch information
Showing
3 changed files
with
171 additions
and
72 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
use candid::utils::{decode_args, decode_one}; | ||
use ic_cdk::api::msg_arg_data; | ||
use ic_cdk::update; | ||
|
||
#[update(decode_with = "decode_u0")] | ||
fn u0() {} | ||
fn decode_u0() {} | ||
|
||
#[update(decode_with = "decode_u1")] | ||
fn u1(a: u32) { | ||
assert_eq!(a, 1) | ||
} | ||
fn decode_u1() -> u32 { | ||
let arg_bytes = msg_arg_data(); | ||
decode_one(&arg_bytes).unwrap() | ||
} | ||
|
||
#[update(decode_with = "decode_u2")] | ||
fn u2(a: u32, b: u32) { | ||
assert_eq!(a, 1); | ||
assert_eq!(b, 2); | ||
} | ||
fn decode_u2() -> (u32, u32) { | ||
let arg_bytes = msg_arg_data(); | ||
decode_args(&arg_bytes).unwrap() | ||
} | ||
|
||
fn main() {} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
use pocket_ic::call_candid; | ||
use pocket_ic::common::rest::RawEffectivePrincipal; | ||
|
||
mod test_utilities; | ||
use test_utilities::{cargo_build_canister, pocket_ic}; | ||
|
||
#[test] | ||
fn call_macros() { | ||
let pic = pocket_ic(); | ||
let wasm = cargo_build_canister("macros"); | ||
let canister_id = pic.create_canister(); | ||
pic.add_cycles(canister_id, 100_000_000_000_000); | ||
pic.install_canister(canister_id, wasm, vec![], None); | ||
let _: () = call_candid(&pic, canister_id, RawEffectivePrincipal::None, "u0", ()).unwrap(); | ||
let _: () = call_candid( | ||
&pic, | ||
canister_id, | ||
RawEffectivePrincipal::None, | ||
"u1", | ||
(1u32,), | ||
) | ||
.unwrap(); | ||
let _: () = call_candid( | ||
&pic, | ||
canister_id, | ||
RawEffectivePrincipal::None, | ||
"u2", | ||
(1u32, 2u32), | ||
) | ||
.unwrap(); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters