From e4c924058e9159ff54a5bfc4adaa8b79ba04d876 Mon Sep 17 00:00:00 2001 From: iho Date: Wed, 4 Oct 2023 21:55:57 +0300 Subject: [PATCH] chore: use `insta` crate for testing macro generated code (#1090) --- near-sdk-macros/Cargo.toml | 4 + .../src/core_impl/abi/abi_generator.rs | 157 +---- .../snapshots/generate_abi_callback_args.snap | 26 + .../generate_abi_fallible_borsh.snap | 23 + .../snapshots/generate_abi_fallible_json.snap | 27 + .../generate_abi_init_ignore_state.snap | 17 + .../abi/snapshots/generate_abi_no_return.snap | 17 + .../generate_abi_private_callback_vec.snap | 21 + .../src/core_impl/code_generator/ext.rs | 137 +---- .../code_generator/item_impl_info.rs | 578 +----------------- .../code_generator/item_trait_info.rs | 136 +---- .../code_generator/snapshots/arg_mut_ref.snap | 22 + .../snapshots/arg_no_return_no_mut.snap | 22 + .../code_generator/snapshots/arg_ref.snap | 22 + .../snapshots/args_no_return_mut.snap | 27 + .../snapshots/args_return_mut.snap | 30 + .../snapshots/args_return_mut_borsh.snap | 29 + .../snapshots/args_return_ref.snap | 15 + .../snapshots/callback_args.snap | 38 ++ .../callback_args_mixed_serialization.snap | 37 ++ .../snapshots/callback_args_only.snap | 28 + .../snapshots/callback_args_results.snap | 40 ++ .../snapshots/callback_args_vec.snap | 47 ++ .../code_generator/snapshots/ext_basic.snap | 71 +++ .../snapshots/ext_basic_borsh.snap | 24 + .../snapshots/ext_basic_json.snap | 25 + .../snapshots/ext_fn_non_bindgen_attrs.snap | 17 + .../code_generator/snapshots/ext_gen.snap | 37 ++ .../snapshots/handle_no_self.snap | 11 + .../snapshots/handle_result_borsh.snap | 20 + .../snapshots/handle_result_init.snap | 23 + .../handle_result_init_ignore_state.snap | 20 + .../snapshots/handle_result_json.snap | 20 + .../snapshots/handle_result_mut.snap | 24 + .../snapshots/init_ignore_state.snap | 25 + .../snapshots/init_payable.snap | 25 + .../snapshots/module_ext_gen.snap | 35 ++ .../mut_owned_no_args_no_return.snap | 12 + .../snapshots/no_args_no_return_mut.snap | 16 + .../no_args_no_return_mut_payable.snap | 13 + .../snapshots/no_args_no_return_no_mut.snap | 12 + .../owned_no_args_no_return_no_mut.snap | 12 + .../snapshots/private_method.snap | 20 + .../snapshots/serialize_with_borsh.snap | 59 ++ .../code_generator/snapshots/simple_init.snap | 28 + .../code_generator/snapshots/trait_implt.snap | 12 + near-sdk-macros/src/core_impl/utils/mod.rs | 3 + .../src/core_impl/utils/test_helpers.rs | 29 + 48 files changed, 1149 insertions(+), 944 deletions(-) create mode 100644 near-sdk-macros/src/core_impl/abi/snapshots/generate_abi_callback_args.snap create mode 100644 near-sdk-macros/src/core_impl/abi/snapshots/generate_abi_fallible_borsh.snap create mode 100644 near-sdk-macros/src/core_impl/abi/snapshots/generate_abi_fallible_json.snap create mode 100644 near-sdk-macros/src/core_impl/abi/snapshots/generate_abi_init_ignore_state.snap create mode 100644 near-sdk-macros/src/core_impl/abi/snapshots/generate_abi_no_return.snap create mode 100644 near-sdk-macros/src/core_impl/abi/snapshots/generate_abi_private_callback_vec.snap create mode 100644 near-sdk-macros/src/core_impl/code_generator/snapshots/arg_mut_ref.snap create mode 100644 near-sdk-macros/src/core_impl/code_generator/snapshots/arg_no_return_no_mut.snap create mode 100644 near-sdk-macros/src/core_impl/code_generator/snapshots/arg_ref.snap create mode 100644 near-sdk-macros/src/core_impl/code_generator/snapshots/args_no_return_mut.snap create mode 100644 near-sdk-macros/src/core_impl/code_generator/snapshots/args_return_mut.snap create mode 100644 near-sdk-macros/src/core_impl/code_generator/snapshots/args_return_mut_borsh.snap create mode 100644 near-sdk-macros/src/core_impl/code_generator/snapshots/args_return_ref.snap create mode 100644 near-sdk-macros/src/core_impl/code_generator/snapshots/callback_args.snap create mode 100644 near-sdk-macros/src/core_impl/code_generator/snapshots/callback_args_mixed_serialization.snap create mode 100644 near-sdk-macros/src/core_impl/code_generator/snapshots/callback_args_only.snap create mode 100644 near-sdk-macros/src/core_impl/code_generator/snapshots/callback_args_results.snap create mode 100644 near-sdk-macros/src/core_impl/code_generator/snapshots/callback_args_vec.snap create mode 100644 near-sdk-macros/src/core_impl/code_generator/snapshots/ext_basic.snap create mode 100644 near-sdk-macros/src/core_impl/code_generator/snapshots/ext_basic_borsh.snap create mode 100644 near-sdk-macros/src/core_impl/code_generator/snapshots/ext_basic_json.snap create mode 100644 near-sdk-macros/src/core_impl/code_generator/snapshots/ext_fn_non_bindgen_attrs.snap create mode 100644 near-sdk-macros/src/core_impl/code_generator/snapshots/ext_gen.snap create mode 100644 near-sdk-macros/src/core_impl/code_generator/snapshots/handle_no_self.snap create mode 100644 near-sdk-macros/src/core_impl/code_generator/snapshots/handle_result_borsh.snap create mode 100644 near-sdk-macros/src/core_impl/code_generator/snapshots/handle_result_init.snap create mode 100644 near-sdk-macros/src/core_impl/code_generator/snapshots/handle_result_init_ignore_state.snap create mode 100644 near-sdk-macros/src/core_impl/code_generator/snapshots/handle_result_json.snap create mode 100644 near-sdk-macros/src/core_impl/code_generator/snapshots/handle_result_mut.snap create mode 100644 near-sdk-macros/src/core_impl/code_generator/snapshots/init_ignore_state.snap create mode 100644 near-sdk-macros/src/core_impl/code_generator/snapshots/init_payable.snap create mode 100644 near-sdk-macros/src/core_impl/code_generator/snapshots/module_ext_gen.snap create mode 100644 near-sdk-macros/src/core_impl/code_generator/snapshots/mut_owned_no_args_no_return.snap create mode 100644 near-sdk-macros/src/core_impl/code_generator/snapshots/no_args_no_return_mut.snap create mode 100644 near-sdk-macros/src/core_impl/code_generator/snapshots/no_args_no_return_mut_payable.snap create mode 100644 near-sdk-macros/src/core_impl/code_generator/snapshots/no_args_no_return_no_mut.snap create mode 100644 near-sdk-macros/src/core_impl/code_generator/snapshots/owned_no_args_no_return_no_mut.snap create mode 100644 near-sdk-macros/src/core_impl/code_generator/snapshots/private_method.snap create mode 100644 near-sdk-macros/src/core_impl/code_generator/snapshots/serialize_with_borsh.snap create mode 100644 near-sdk-macros/src/core_impl/code_generator/snapshots/simple_init.snap create mode 100644 near-sdk-macros/src/core_impl/code_generator/snapshots/trait_implt.snap create mode 100644 near-sdk-macros/src/core_impl/utils/test_helpers.rs diff --git a/near-sdk-macros/Cargo.toml b/near-sdk-macros/Cargo.toml index 3edfba0fc..ba1944d55 100644 --- a/near-sdk-macros/Cargo.toml +++ b/near-sdk-macros/Cargo.toml @@ -23,6 +23,10 @@ quote = "1.0" Inflector = { version = "0.11.4", default-features = false, features = [] } darling = { version = "0.20.3" } +[dev-dependencies] +insta = { version = "1.31.0", features = ["yaml"] } +prettyplease = { version = "0.2.15" } + [features] abi = [] __abi-embed = ["abi"] diff --git a/near-sdk-macros/src/core_impl/abi/abi_generator.rs b/near-sdk-macros/src/core_impl/abi/abi_generator.rs index 9eac0634c..f39a301b9 100644 --- a/near-sdk-macros/src/core_impl/abi/abi_generator.rs +++ b/near-sdk-macros/src/core_impl/abi/abi_generator.rs @@ -295,10 +295,23 @@ pub fn parse_rustdoc(attrs: &[Attribute]) -> Option { #[rustfmt::skip] #[cfg(test)] mod tests { - use quote::quote; + use proc_macro2::TokenStream; use syn::{parse_quote, Type}; use crate::core_impl::ImplItemMethodInfo; + use crate::core_impl::utils::test_helpers::{local_insta_assert_snapshot, pretty_print_syn_str}; + use quote::quote; + + fn pretty_print_fn_body_syn_str(input: TokenStream) -> String { + let input = quote!( + fn main() { + #input + } + ); + let res = pretty_print_syn_str(&input).unwrap(); + res.strip_prefix("fn main() {\n").unwrap().strip_suffix("}\n").unwrap().to_string() + } + #[test] fn test_generate_abi_fallible_json() { let impl_type: Type = syn::parse_str("Test").unwrap(); @@ -309,34 +322,8 @@ mod tests { }; let method_info = ImplItemMethodInfo::new(&mut method, false, impl_type).unwrap().unwrap(); let actual = method_info.abi_struct(); - - let expected = quote! { - ::near_sdk::__private::AbiFunction { - name: ::std::string::String::from("f3"), - doc: ::std::option::Option::Some(::std::string::String::from(" I am a function.")), - kind: ::near_sdk::__private::AbiFunctionKind::Call, - modifiers: ::std::vec![], - params: ::near_sdk::__private::AbiParameters::Json { - args: ::std::vec![ - ::near_sdk::__private::AbiJsonParameter { - name: ::std::string::String::from("arg0"), - type_schema: gen.subschema_for::(), - }, - ::near_sdk::__private::AbiJsonParameter { - name: ::std::string::String::from("arg1"), - type_schema: gen.subschema_for::(), - } - ] - }, - callbacks: ::std::vec![], - callbacks_vec: ::std::option::Option::None, - result: ::std::option::Option::Some(::near_sdk::__private::AbiType::Json { - type_schema: gen.subschema_for::(), - }) - } - }; - - assert_eq!(actual.to_string(), expected.to_string()); + + local_insta_assert_snapshot!(pretty_print_fn_body_syn_str(actual)); } #[test] @@ -351,29 +338,7 @@ mod tests { let method_info = ImplItemMethodInfo::new(&mut method, false, impl_type).unwrap().unwrap(); let actual = method_info.abi_struct(); - let expected = quote! { - ::near_sdk::__private::AbiFunction { - name: ::std::string::String::from("f3"), - doc: ::std::option::Option::None, - kind: ::near_sdk::__private::AbiFunctionKind::Call, - modifiers: ::std::vec![::near_sdk::__private::AbiFunctionModifier::Payable], - params: ::near_sdk::__private::AbiParameters::Borsh { - args: ::std::vec![ - ::near_sdk::__private::AbiBorshParameter { - name: ::std::string::String::from("arg0"), - type_schema: ::schema_container(), - } - ] - }, - callbacks: ::std::vec![], - callbacks_vec: ::std::option::Option::None, - result: ::std::option::Option::Some(::near_sdk::__private::AbiType::Borsh { - type_schema: ::schema_container(), - }) - } - }; - - assert_eq!(actual.to_string(), expected.to_string()); + local_insta_assert_snapshot!(pretty_print_fn_body_syn_str(actual)); } #[test] @@ -388,27 +353,8 @@ mod tests { }; let method_info = ImplItemMethodInfo::new(&mut method, false, impl_type).unwrap().unwrap(); let actual = method_info.abi_struct(); - - let expected = quote! { - ::near_sdk::__private::AbiFunction { - name: ::std::string::String::from("method"), - doc: ::std::option::Option::None, - kind: ::near_sdk::__private::AbiFunctionKind::View , - modifiers: ::std::vec! [::near_sdk::__private::AbiFunctionModifier::Private], - params: ::near_sdk::__private::AbiParameters::Json { - args: ::std::vec![] - }, - callbacks: ::std::vec! [], - callbacks_vec: ::std::option::Option::Some(::near_sdk::__private::AbiType::Json { - type_schema: gen.subschema_for::< String >() , - }), - result: ::std::option::Option::Some(::near_sdk::__private::AbiType::Json { - type_schema: gen.subschema_for::< bool >() , - }) - } - }; - - assert_eq!(actual.to_string(), expected.to_string()); + + local_insta_assert_snapshot!(pretty_print_fn_body_syn_str(actual)); } #[test] @@ -420,34 +366,7 @@ mod tests { let method_info = ImplItemMethodInfo::new(&mut method, false, impl_type).unwrap().unwrap(); let actual = method_info.abi_struct(); - let expected = quote! { - ::near_sdk::__private::AbiFunction { - name: ::std::string::String::from("method"), - doc: ::std::option::Option::None, - kind: ::near_sdk::__private::AbiFunctionKind::View , - modifiers: ::std::vec! [], - params: ::near_sdk::__private::AbiParameters::Borsh { - args: ::std::vec! [ - ::near_sdk::__private::AbiBorshParameter { - name: ::std::string::String::from("y"), - type_schema: < String as ::near_sdk::borsh::BorshSchema >::schema_container(), - } - ] - }, - callbacks: ::std::vec! [ - ::near_sdk::__private::AbiType::Borsh { - type_schema: ::schema_container(), - }, - ::near_sdk::__private::AbiType::Json { - type_schema: gen.subschema_for::< Vec >(), - } - ], - callbacks_vec: ::std::option::Option::None, - result: ::std::option::Option::None - } - }; - - assert_eq!(actual.to_string(), expected.to_string()); + local_insta_assert_snapshot!(pretty_print_fn_body_syn_str(actual)); } #[test] @@ -460,24 +379,7 @@ mod tests { let method_info = ImplItemMethodInfo::new(&mut method, false, impl_type).unwrap().unwrap(); let actual = method_info.abi_struct(); - let expected = quote! { - ::near_sdk::__private::AbiFunction { - name: ::std::string::String::from("new"), - doc: ::std::option::Option::None, - kind: ::near_sdk::__private::AbiFunctionKind::Call, - modifiers: ::std::vec![ - ::near_sdk::__private::AbiFunctionModifier::Init - ], - params: ::near_sdk::__private::AbiParameters::Json { - args: ::std::vec![] - }, - callbacks: ::std::vec![], - callbacks_vec: ::std::option::Option::None, - result: ::std::option::Option::None - } - }; - - assert_eq!(actual.to_string(), expected.to_string()); + local_insta_assert_snapshot!(pretty_print_fn_body_syn_str(actual)); } #[test] @@ -489,21 +391,6 @@ mod tests { let method_info = ImplItemMethodInfo::new(&mut method, false, impl_type).unwrap().unwrap(); let actual = method_info.abi_struct(); - let expected = quote! { - ::near_sdk::__private::AbiFunction { - name: ::std::string::String::from("method"), - doc: ::std::option::Option::None, - kind: ::near_sdk::__private::AbiFunctionKind::View, - modifiers: ::std::vec![], - params: ::near_sdk::__private::AbiParameters::Json { - args: ::std::vec![] - }, - callbacks: ::std::vec![], - callbacks_vec: ::std::option::Option::None, - result: ::std::option::Option::None - } - }; - - assert_eq!(actual.to_string(), expected.to_string()); + local_insta_assert_snapshot!(pretty_print_fn_body_syn_str(actual)); } } diff --git a/near-sdk-macros/src/core_impl/abi/snapshots/generate_abi_callback_args.snap b/near-sdk-macros/src/core_impl/abi/snapshots/generate_abi_callback_args.snap new file mode 100644 index 000000000..8902a2d90 --- /dev/null +++ b/near-sdk-macros/src/core_impl/abi/snapshots/generate_abi_callback_args.snap @@ -0,0 +1,26 @@ +--- +source: near-sdk-macros/src/core_impl/abi/abi_generator.rs +expression: pretty_print_fn_body_syn_str(actual) +--- + ::near_sdk::__private::AbiFunction { + name: ::std::string::String::from("method"), + doc: ::std::option::Option::None, + kind: ::near_sdk::__private::AbiFunctionKind::View, + modifiers: ::std::vec![], + params: ::near_sdk::__private::AbiParameters::Borsh { + args: ::std::vec![ + ::near_sdk::__private::AbiBorshParameter { name : + ::std::string::String::from("y"), type_schema : < String as + ::near_sdk::borsh::BorshSchema > ::schema_container(), } + ], + }, + callbacks: ::std::vec![ + ::near_sdk::__private::AbiType::Borsh { type_schema : < u64 as + ::near_sdk::borsh::BorshSchema > ::schema_container(), }, + ::near_sdk::__private::AbiType::Json { type_schema : gen.subschema_for:: < + Vec < u8 > > (), } + ], + callbacks_vec: ::std::option::Option::None, + result: ::std::option::Option::None, + } + diff --git a/near-sdk-macros/src/core_impl/abi/snapshots/generate_abi_fallible_borsh.snap b/near-sdk-macros/src/core_impl/abi/snapshots/generate_abi_fallible_borsh.snap new file mode 100644 index 000000000..4dc61c359 --- /dev/null +++ b/near-sdk-macros/src/core_impl/abi/snapshots/generate_abi_fallible_borsh.snap @@ -0,0 +1,23 @@ +--- +source: near-sdk-macros/src/core_impl/abi/abi_generator.rs +expression: pretty_print_fn_body_syn_str(actual) +--- + ::near_sdk::__private::AbiFunction { + name: ::std::string::String::from("f3"), + doc: ::std::option::Option::None, + kind: ::near_sdk::__private::AbiFunctionKind::Call, + modifiers: ::std::vec![::near_sdk::__private::AbiFunctionModifier::Payable], + params: ::near_sdk::__private::AbiParameters::Borsh { + args: ::std::vec![ + ::near_sdk::__private::AbiBorshParameter { name : + ::std::string::String::from("arg0"), type_schema : < FancyStruct as + ::near_sdk::borsh::BorshSchema > ::schema_container(), } + ], + }, + callbacks: ::std::vec![], + callbacks_vec: ::std::option::Option::None, + result: ::std::option::Option::Some(::near_sdk::__private::AbiType::Borsh { + type_schema: ::schema_container(), + }), + } + diff --git a/near-sdk-macros/src/core_impl/abi/snapshots/generate_abi_fallible_json.snap b/near-sdk-macros/src/core_impl/abi/snapshots/generate_abi_fallible_json.snap new file mode 100644 index 000000000..a4826e567 --- /dev/null +++ b/near-sdk-macros/src/core_impl/abi/snapshots/generate_abi_fallible_json.snap @@ -0,0 +1,27 @@ +--- +source: near-sdk-macros/src/core_impl/abi/abi_generator.rs +expression: pretty_print_fn_body_syn_str(actual) +--- + ::near_sdk::__private::AbiFunction { + name: ::std::string::String::from("f3"), + doc: ::std::option::Option::Some( + ::std::string::String::from(" I am a function."), + ), + kind: ::near_sdk::__private::AbiFunctionKind::Call, + modifiers: ::std::vec![], + params: ::near_sdk::__private::AbiParameters::Json { + args: ::std::vec![ + ::near_sdk::__private::AbiJsonParameter { name : + ::std::string::String::from("arg0"), type_schema : gen.subschema_for:: < + FancyStruct > (), }, ::near_sdk::__private::AbiJsonParameter { name : + ::std::string::String::from("arg1"), type_schema : gen.subschema_for:: < + u64 > (), } + ], + }, + callbacks: ::std::vec![], + callbacks_vec: ::std::option::Option::None, + result: ::std::option::Option::Some(::near_sdk::__private::AbiType::Json { + type_schema: gen.subschema_for::(), + }), + } + diff --git a/near-sdk-macros/src/core_impl/abi/snapshots/generate_abi_init_ignore_state.snap b/near-sdk-macros/src/core_impl/abi/snapshots/generate_abi_init_ignore_state.snap new file mode 100644 index 000000000..18dc57fe9 --- /dev/null +++ b/near-sdk-macros/src/core_impl/abi/snapshots/generate_abi_init_ignore_state.snap @@ -0,0 +1,17 @@ +--- +source: near-sdk-macros/src/core_impl/abi/abi_generator.rs +expression: pretty_print_fn_body_syn_str(actual) +--- + ::near_sdk::__private::AbiFunction { + name: ::std::string::String::from("new"), + doc: ::std::option::Option::None, + kind: ::near_sdk::__private::AbiFunctionKind::Call, + modifiers: ::std::vec![::near_sdk::__private::AbiFunctionModifier::Init], + params: ::near_sdk::__private::AbiParameters::Json { + args: ::std::vec![], + }, + callbacks: ::std::vec![], + callbacks_vec: ::std::option::Option::None, + result: ::std::option::Option::None, + } + diff --git a/near-sdk-macros/src/core_impl/abi/snapshots/generate_abi_no_return.snap b/near-sdk-macros/src/core_impl/abi/snapshots/generate_abi_no_return.snap new file mode 100644 index 000000000..b0c0c688f --- /dev/null +++ b/near-sdk-macros/src/core_impl/abi/snapshots/generate_abi_no_return.snap @@ -0,0 +1,17 @@ +--- +source: near-sdk-macros/src/core_impl/abi/abi_generator.rs +expression: pretty_print_fn_body_syn_str(actual) +--- + ::near_sdk::__private::AbiFunction { + name: ::std::string::String::from("method"), + doc: ::std::option::Option::None, + kind: ::near_sdk::__private::AbiFunctionKind::View, + modifiers: ::std::vec![], + params: ::near_sdk::__private::AbiParameters::Json { + args: ::std::vec![], + }, + callbacks: ::std::vec![], + callbacks_vec: ::std::option::Option::None, + result: ::std::option::Option::None, + } + diff --git a/near-sdk-macros/src/core_impl/abi/snapshots/generate_abi_private_callback_vec.snap b/near-sdk-macros/src/core_impl/abi/snapshots/generate_abi_private_callback_vec.snap new file mode 100644 index 000000000..b378a92b7 --- /dev/null +++ b/near-sdk-macros/src/core_impl/abi/snapshots/generate_abi_private_callback_vec.snap @@ -0,0 +1,21 @@ +--- +source: near-sdk-macros/src/core_impl/abi/abi_generator.rs +expression: pretty_print_fn_body_syn_str(actual) +--- + ::near_sdk::__private::AbiFunction { + name: ::std::string::String::from("method"), + doc: ::std::option::Option::None, + kind: ::near_sdk::__private::AbiFunctionKind::View, + modifiers: ::std::vec![::near_sdk::__private::AbiFunctionModifier::Private], + params: ::near_sdk::__private::AbiParameters::Json { + args: ::std::vec![], + }, + callbacks: ::std::vec![], + callbacks_vec: ::std::option::Option::Some(::near_sdk::__private::AbiType::Json { + type_schema: gen.subschema_for::(), + }), + result: ::std::option::Option::Some(::near_sdk::__private::AbiType::Json { + type_schema: gen.subschema_for::(), + }), + } + diff --git a/near-sdk-macros/src/core_impl/code_generator/ext.rs b/near-sdk-macros/src/core_impl/code_generator/ext.rs index 592824703..c27e6a365 100644 --- a/near-sdk-macros/src/core_impl/code_generator/ext.rs +++ b/near-sdk-macros/src/core_impl/code_generator/ext.rs @@ -147,87 +147,23 @@ mod tests { use crate::core_impl::ImplItemMethodInfo; use super::*; - use quote::quote; use syn::{parse_quote, ImplItemFn, ItemStruct, Type}; + use crate::core_impl::utils::test_helpers::{local_insta_assert_snapshot, pretty_print_syn_str}; #[test] fn ext_gen() { let st: ItemStruct = parse_quote! { struct Test { a: u8 } }; let actual = generate_ext_structs(&st.ident, Some(&st.generics)); - let expected = quote!( - #[must_use] - pub struct TestExt { - pub(crate) account_id: ::near_sdk::AccountId, - pub(crate) deposit: ::near_sdk::Balance, - pub(crate) static_gas: ::near_sdk::Gas, - pub(crate) gas_weight: ::near_sdk::GasWeight, - } - impl TestExt { - pub fn with_attached_deposit(mut self, amount: ::near_sdk::Balance) -> Self { - self.deposit = amount; - self - } - pub fn with_static_gas(mut self, static_gas: ::near_sdk::Gas) -> Self { - self.static_gas = static_gas; - self - } - pub fn with_unused_gas_weight(mut self, gas_weight: u64) -> Self { - self.gas_weight = ::near_sdk::GasWeight(gas_weight); - self - } - } - impl Test { - /// API for calling this contract's functions in a subsequent execution. - pub fn ext(account_id: ::near_sdk::AccountId) -> TestExt { - TestExt { - account_id, - deposit: 0, - static_gas: ::near_sdk::Gas::from_gas(0), - gas_weight: ::near_sdk::GasWeight::default(), - } - } - } - ); - assert_eq!(expected.to_string(), actual.to_string()); + + local_insta_assert_snapshot!(pretty_print_syn_str(&actual).unwrap()); } #[test] fn module_ext_gen() { let ident: Ident = parse_quote! { Test }; let actual = generate_ext_structs(&ident, None); - let expected = quote!( - #[must_use] - pub struct TestExt { - pub(crate) account_id: ::near_sdk::AccountId, - pub(crate) deposit: ::near_sdk::Balance, - pub(crate) static_gas: ::near_sdk::Gas, - pub(crate) gas_weight: ::near_sdk::GasWeight, - } - impl TestExt { - pub fn with_attached_deposit(mut self, amount: ::near_sdk::Balance) -> Self { - self.deposit = amount; - self - } - pub fn with_static_gas(mut self, static_gas: ::near_sdk::Gas) -> Self { - self.static_gas = static_gas; - self - } - pub fn with_unused_gas_weight(mut self, gas_weight: u64) -> Self { - self.gas_weight = ::near_sdk::GasWeight(gas_weight); - self - } - } - /// API for calling this contract's functions in a subsequent execution. - pub fn ext(account_id: ::near_sdk::AccountId) -> TestExt { - TestExt { - account_id, - deposit: 0, - static_gas: ::near_sdk::Gas::from_gas(0), - gas_weight: ::near_sdk::GasWeight::default(), - } - } - ); - assert_eq!(expected.to_string(), actual.to_string()); + + local_insta_assert_snapshot!(pretty_print_syn_str(&actual).unwrap()); } /// Verifies that only whitelisted attributes are forwarded to `_Ext` @@ -244,21 +180,7 @@ mod tests { let method_info = ImplItemMethodInfo::new(&mut method, false, impl_type).unwrap().unwrap(); let actual = generate_ext_function(&method_info.attr_signature_info); - // Note: only whitelisted non-bindgen attributes are forwarded. - let expected = quote! { - #[cfg(target_os = "linux")] - pub fn method (self,) -> ::near_sdk::Promise { - let __args = ::std::vec![]; - ::near_sdk::Promise::new(self.account_id).function_call_weight( - ::std::string::String::from("method"), - __args, - self.deposit, - self.static_gas, - self.gas_weight, - ) - } - }; - assert_eq!(expected.to_string(), actual.to_string()); + local_insta_assert_snapshot!(pretty_print_syn_str(&actual).unwrap()); } #[test] @@ -269,27 +191,8 @@ mod tests { }; let method_info = ImplItemMethodInfo::new(&mut method, false, impl_type).unwrap().unwrap(); let actual = generate_ext_function(&method_info.attr_signature_info); - let expected = quote!( - pub fn method(self, k: &String,) -> ::near_sdk::Promise { - let __args = {#[derive(::near_sdk :: serde :: Serialize)] - #[serde(crate = "::near_sdk::serde")] - struct Input<'nearinput> { - k: &'nearinput String, - } - let __args = Input { k: &k, }; - ::near_sdk::serde_json::to_vec(&__args) - .expect("Failed to serialize the cross contract args using JSON.") - }; - ::near_sdk::Promise::new(self.account_id).function_call_weight( - ::std::string::String::from("method"), - __args, - self.deposit, - self.static_gas, - self.gas_weight, - ) - } - ); - assert_eq!(expected.to_string(), actual.to_string()); + + local_insta_assert_snapshot!(pretty_print_syn_str(&actual).unwrap()); } #[test] @@ -300,27 +203,7 @@ mod tests { }; let method_info = ImplItemMethodInfo::new(&mut method, false, impl_type).unwrap().unwrap(); let actual = generate_ext_function(&method_info.attr_signature_info); - let expected = quote!( - pub fn borsh_test(self, a: String,) -> ::near_sdk::Promise { - let __args = { - #[derive(::near_sdk :: borsh :: BorshSerialize)] - struct Input<'nearinput> { - a: &'nearinput String, - } - let __args = Input { a: &a, }; - ::near_sdk::borsh::BorshSerialize::try_to_vec(&__args) - .expect("Failed to serialize the cross contract args using Borsh.") - }; - ::near_sdk::Promise::new(self.account_id) - .function_call_weight( - ::std::string::String::from("borsh_test"), - __args, - self.deposit, - self.static_gas, - self.gas_weight, - ) - } - ); - assert_eq!(expected.to_string(), actual.to_string()); + + local_insta_assert_snapshot!(pretty_print_syn_str(&actual).unwrap()); } } diff --git a/near-sdk-macros/src/core_impl/code_generator/item_impl_info.rs b/near-sdk-macros/src/core_impl/code_generator/item_impl_info.rs index 6855ff46b..1dd3c3605 100644 --- a/near-sdk-macros/src/core_impl/code_generator/item_impl_info.rs +++ b/near-sdk-macros/src/core_impl/code_generator/item_impl_info.rs @@ -29,8 +29,8 @@ impl ItemImplInfo { #[cfg(test)] mod tests { use syn::{Type, ImplItemFn, parse_quote}; - use quote::quote; use crate::core_impl::info_extractor::ImplItemMethodInfo; + use crate::core_impl::utils::test_helpers::{local_insta_assert_snapshot, pretty_print_syn_str}; #[test] @@ -39,16 +39,7 @@ mod tests { let mut method: ImplItemFn = syn::parse_str("fn method(&self) { }").unwrap(); let method_info = ImplItemMethodInfo::new(&mut method, true, impl_type).unwrap().unwrap(); let actual = method_info.method_wrapper(); - let expected = quote!( - #[cfg(target_arch = "wasm32")] - #[no_mangle] - pub extern "C" fn method() { - ::near_sdk::env::setup_panic_hook(); - let contract: Hello = ::near_sdk::env::state_read().unwrap_or_default(); - contract.method(); - } - ); - assert_eq!(expected.to_string(), actual.to_string()); + local_insta_assert_snapshot!(pretty_print_syn_str(&actual).unwrap()); } #[test] @@ -57,16 +48,7 @@ mod tests { let mut method: ImplItemFn = syn::parse_str("pub fn method(&self) { }").unwrap(); let method_info = ImplItemMethodInfo::new(&mut method, false, impl_type).unwrap().unwrap(); let actual = method_info.method_wrapper(); - let expected = quote!( - #[cfg(target_arch = "wasm32")] - #[no_mangle] - pub extern "C" fn method() { - ::near_sdk::env::setup_panic_hook(); - let contract: Hello = ::near_sdk::env::state_read().unwrap_or_default(); - contract.method(); - } - ); - assert_eq!(expected.to_string(), actual.to_string()); + local_insta_assert_snapshot!(pretty_print_syn_str(&actual).unwrap()); } #[test] @@ -75,16 +57,7 @@ mod tests { let mut method: ImplItemFn = syn::parse_str("pub fn method(self) { }").unwrap(); let method_info = ImplItemMethodInfo::new(&mut method, false, impl_type).unwrap().unwrap(); let actual = method_info.method_wrapper(); - let expected = quote!( - #[cfg(target_arch = "wasm32")] - #[no_mangle] - pub extern "C" fn method() { - ::near_sdk::env::setup_panic_hook(); - let contract: Hello = ::near_sdk::env::state_read().unwrap_or_default(); - contract.method(); - } - ); - assert_eq!(expected.to_string(), actual.to_string()); + local_insta_assert_snapshot!(pretty_print_syn_str(&actual).unwrap()); } @@ -94,16 +67,7 @@ mod tests { let mut method: ImplItemFn = syn::parse_str("pub fn method(mut self) { }").unwrap(); let method_info = ImplItemMethodInfo::new(&mut method, false, impl_type).unwrap().unwrap(); let actual = method_info.method_wrapper(); - let expected = quote!( - #[cfg(target_arch = "wasm32")] - #[no_mangle] - pub extern "C" fn method() { - ::near_sdk::env::setup_panic_hook(); - let mut contract: Hello = ::near_sdk::env::state_read().unwrap_or_default(); - contract.method(); - } - ); - assert_eq!(expected.to_string(), actual.to_string()); + local_insta_assert_snapshot!(pretty_print_syn_str(&actual).unwrap()); } #[test] @@ -112,20 +76,7 @@ mod tests { let mut method: ImplItemFn = syn::parse_str("pub fn method(&mut self) { }").unwrap(); let method_info = ImplItemMethodInfo::new(&mut method, false, impl_type).unwrap().unwrap(); let actual = method_info.method_wrapper(); - let expected = quote!( - #[cfg(target_arch = "wasm32")] - #[no_mangle] - pub extern "C" fn method() { - ::near_sdk::env::setup_panic_hook(); - if ::near_sdk::env::attached_deposit() != 0 { - ::near_sdk::env::panic_str("Method method doesn't accept deposit"); - } - let mut contract: Hello = ::near_sdk::env::state_read().unwrap_or_default(); - contract.method(); - ::near_sdk::env::state_write(&contract); - } - ); - assert_eq!(expected.to_string(), actual.to_string()); + local_insta_assert_snapshot!(pretty_print_syn_str(&actual).unwrap()); } #[test] @@ -134,25 +85,7 @@ mod tests { let mut method: ImplItemFn = syn::parse_str("pub fn method(&self, k: u64) { }").unwrap(); let method_info = ImplItemMethodInfo::new(&mut method, false, impl_type).unwrap().unwrap(); let actual = method_info.method_wrapper(); - let expected = quote!( - #[cfg(target_arch = "wasm32")] - #[no_mangle] - pub extern "C" fn method() { - ::near_sdk::env::setup_panic_hook(); - #[derive(::near_sdk :: serde :: Deserialize)] - #[serde(crate = "::near_sdk::serde")] - struct Input { - k: u64, - } - let Input { k, }: Input = ::near_sdk::serde_json::from_slice( - &::near_sdk::env::input().expect("Expected input since method has arguments.") - ) - .expect("Failed to deserialize input from JSON."); - let contract: Hello = ::near_sdk::env::state_read().unwrap_or_default(); - contract.method(k, ); - } - ); - assert_eq!(expected.to_string(), actual.to_string()); + local_insta_assert_snapshot!(pretty_print_syn_str(&actual).unwrap()); } #[test] @@ -162,30 +95,7 @@ mod tests { syn::parse_str("pub fn method(&mut self, k: u64, m: Bar) { }").unwrap(); let method_info = ImplItemMethodInfo::new(&mut method, false, impl_type).unwrap().unwrap(); let actual = method_info.method_wrapper(); - let expected = quote!( - #[cfg(target_arch = "wasm32")] - #[no_mangle] - pub extern "C" fn method() { - ::near_sdk::env::setup_panic_hook(); - if ::near_sdk::env::attached_deposit() != 0 { - ::near_sdk::env::panic_str("Method method doesn't accept deposit"); - } - #[derive(::near_sdk :: serde :: Deserialize)] - #[serde(crate = "::near_sdk::serde")] - struct Input { - k: u64, - m: Bar, - } - let Input { k, m, }: Input = ::near_sdk::serde_json::from_slice( - &::near_sdk::env::input().expect("Expected input since method has arguments.") - ) - .expect("Failed to deserialize input from JSON."); - let mut contract: Hello = ::near_sdk::env::state_read().unwrap_or_default(); - contract.method(k, m, ); - ::near_sdk::env::state_write(&contract); - } - ); - assert_eq!(expected.to_string(), actual.to_string()); + local_insta_assert_snapshot!(pretty_print_syn_str(&actual).unwrap()); } #[test] @@ -195,33 +105,7 @@ mod tests { syn::parse_str("pub fn method(&mut self, k: u64, m: Bar) -> Option { }").unwrap(); let method_info = ImplItemMethodInfo::new(&mut method, false, impl_type).unwrap().unwrap(); let actual = method_info.method_wrapper(); - let expected = quote!( - #[cfg(target_arch = "wasm32")] - #[no_mangle] - pub extern "C" fn method() { - ::near_sdk::env::setup_panic_hook(); - if ::near_sdk::env::attached_deposit() != 0 { - ::near_sdk::env::panic_str("Method method doesn't accept deposit"); - } - #[derive(::near_sdk :: serde :: Deserialize)] - #[serde(crate = "::near_sdk::serde")] - struct Input { - k: u64, - m: Bar, - } - let Input { k, m, }: Input = ::near_sdk::serde_json::from_slice( - &::near_sdk::env::input().expect("Expected input since method has arguments.") - ) - .expect("Failed to deserialize input from JSON."); - let mut contract: Hello = ::near_sdk::env::state_read().unwrap_or_default(); - let result = contract.method(k, m, ); - let result = - ::near_sdk::serde_json::to_vec(&result).expect("Failed to serialize the return value using JSON."); - ::near_sdk::env::value_return(&result); - ::near_sdk::env::state_write(&contract); - } - ); - assert_eq!(expected.to_string(), actual.to_string()); + local_insta_assert_snapshot!(pretty_print_syn_str(&actual).unwrap()); } #[test] @@ -231,19 +115,7 @@ mod tests { syn::parse_str("pub fn method(&self) -> &Option { }").unwrap(); let method_info = ImplItemMethodInfo::new(&mut method, false, impl_type).unwrap().unwrap(); let actual = method_info.method_wrapper(); - let expected = quote!( - #[cfg(target_arch = "wasm32")] - #[no_mangle] - pub extern "C" fn method() { - ::near_sdk::env::setup_panic_hook(); - let contract: Hello = ::near_sdk::env::state_read().unwrap_or_default(); - let result = contract.method(); - let result = - ::near_sdk::serde_json::to_vec(&result).expect("Failed to serialize the return value using JSON."); - ::near_sdk::env::value_return(&result); - } - ); - assert_eq!(expected.to_string(), actual.to_string()); + local_insta_assert_snapshot!(pretty_print_syn_str(&actual).unwrap()); } #[test] @@ -252,25 +124,7 @@ mod tests { let mut method: ImplItemFn = syn::parse_str("pub fn method(&self, k: &u64) { }").unwrap(); let method_info = ImplItemMethodInfo::new(&mut method, false, impl_type).unwrap().unwrap(); let actual = method_info.method_wrapper(); - let expected = quote!( - #[cfg(target_arch = "wasm32")] - #[no_mangle] - pub extern "C" fn method() { - ::near_sdk::env::setup_panic_hook(); - #[derive(::near_sdk :: serde :: Deserialize)] - #[serde(crate = "::near_sdk::serde")] - struct Input { - k: u64, - } - let Input { k, }: Input = ::near_sdk::serde_json::from_slice( - &::near_sdk::env::input().expect("Expected input since method has arguments.") - ) - .expect("Failed to deserialize input from JSON."); - let contract: Hello = ::near_sdk::env::state_read().unwrap_or_default(); - contract.method(&k, ); - } - ); - assert_eq!(expected.to_string(), actual.to_string()); + local_insta_assert_snapshot!(pretty_print_syn_str(&actual).unwrap()); } #[test] @@ -280,25 +134,7 @@ mod tests { syn::parse_str("pub fn method(&self, k: &mut u64) { }").unwrap(); let method_info = ImplItemMethodInfo::new(&mut method, false, impl_type).unwrap().unwrap(); let actual = method_info.method_wrapper(); - let expected = quote!( - #[cfg(target_arch = "wasm32")] - #[no_mangle] - pub extern "C" fn method() { - ::near_sdk::env::setup_panic_hook(); - #[derive(::near_sdk :: serde :: Deserialize)] - #[serde(crate = "::near_sdk::serde")] - struct Input { - k: u64, - } - let Input { mut k, }: Input = ::near_sdk::serde_json::from_slice( - &::near_sdk::env::input().expect("Expected input since method has arguments.") - ) - .expect("Failed to deserialize input from JSON."); - let contract: Hello = ::near_sdk::env::state_read().unwrap_or_default(); - contract.method(&mut k, ); - } - ); - assert_eq!(expected.to_string(), actual.to_string()); + local_insta_assert_snapshot!(pretty_print_syn_str(&actual).unwrap()); } #[test] @@ -309,40 +145,7 @@ mod tests { }; let method_info = ImplItemMethodInfo::new(&mut method, false, impl_type).unwrap().unwrap(); let actual = method_info.method_wrapper(); - let expected = quote!( - #[cfg(target_arch = "wasm32")] - #[no_mangle] - pub extern "C" fn method() { - ::near_sdk::env::setup_panic_hook(); - if ::near_sdk::env::current_account_id() != ::near_sdk::env::predecessor_account_id() { - ::near_sdk::env::panic_str("Method method is private"); - } - #[derive(::near_sdk :: serde :: Deserialize)] - #[serde(crate = "::near_sdk::serde")] - struct Input { - y: ::std::string::String, - } - let Input { y, }: Input = ::near_sdk::serde_json::from_slice( - &::near_sdk::env::input().expect("Expected input since method has arguments.") - ) - .expect("Failed to deserialize input from JSON."); - let data: ::std::vec::Vec = match ::near_sdk::env::promise_result(0u64) { - ::near_sdk::PromiseResult::Successful(x) => x, - _ => ::near_sdk::env::panic_str("Callback computation 0 was not successful") - }; - let mut x: u64 = - ::near_sdk::serde_json::from_slice(&data).expect("Failed to deserialize callback using JSON"); - let data: ::std::vec::Vec = match ::near_sdk::env::promise_result(1u64) { - ::near_sdk::PromiseResult::Successful(x) => x, - _ => ::near_sdk::env::panic_str("Callback computation 1 was not successful") - }; - let z: ::std::vec::Vec = - ::near_sdk::serde_json::from_slice(&data).expect("Failed to deserialize callback using JSON"); - let contract: Hello = ::near_sdk::env::state_read().unwrap_or_default(); - contract.method(&mut x, y, z, ); - } - ); - assert_eq!(expected.to_string(), actual.to_string()); + local_insta_assert_snapshot!(pretty_print_syn_str(&actual).unwrap()); } #[test] @@ -353,32 +156,7 @@ mod tests { }; let method_info = ImplItemMethodInfo::new(&mut method, false, impl_type).unwrap().unwrap(); let actual = method_info.method_wrapper(); - let expected = quote!( - #[cfg(target_arch = "wasm32")] - #[no_mangle] - pub extern "C" fn method() { - ::near_sdk::env::setup_panic_hook(); - if ::near_sdk::env::current_account_id() != ::near_sdk::env::predecessor_account_id() { - ::near_sdk::env::panic_str("Method method is private"); - } - let data: ::std::vec::Vec = match ::near_sdk::env::promise_result(0u64) { - ::near_sdk::PromiseResult::Successful(x) => x, - _ => ::near_sdk::env::panic_str("Callback computation 0 was not successful") - }; - let mut x: u64 = - ::near_sdk::serde_json::from_slice(&data).expect("Failed to deserialize callback using JSON"); - let data: ::std::vec::Vec = match ::near_sdk::env::promise_result(1u64) { - ::near_sdk::PromiseResult::Successful(x) => x, - _ => ::near_sdk::env::panic_str("Callback computation 1 was not successful") - }; - let y: ::std::string::String = - ::near_sdk::serde_json::from_slice(&data).expect("Failed to deserialize callback using JSON"); - let contract: Hello = ::near_sdk::env::state_read().unwrap_or_default(); - contract.method(&mut x, y, ); - } - ); - - assert_eq!(expected.to_string(), actual.to_string()); + local_insta_assert_snapshot!(pretty_print_syn_str(&actual).unwrap()); } #[test] @@ -389,28 +167,7 @@ mod tests { }; let method_info = ImplItemMethodInfo::new(&mut method, false, impl_type).unwrap().unwrap(); let actual = method_info.method_wrapper(); - let expected = quote!( - #[cfg(target_arch = "wasm32")] - #[no_mangle] - pub extern "C" fn method() { - ::near_sdk::env::setup_panic_hook(); - if ::near_sdk::env::current_account_id() != ::near_sdk::env::predecessor_account_id() { - ::near_sdk::env::panic_str("Method method is private"); - } - let mut x: Result = match ::near_sdk::env::promise_result(0u64) { - ::near_sdk::PromiseResult::Successful(data) => ::std::result::Result::Ok(::near_sdk::serde_json::from_slice(&data).expect("Failed to deserialize callback using JSON")), - ::near_sdk::PromiseResult::Failed => ::std::result::Result::Err(::near_sdk::PromiseError::Failed), - }; - let y: Result<::std::string::String, PromiseError> = match ::near_sdk::env::promise_result(1u64) { - ::near_sdk::PromiseResult::Successful(data) => ::std::result::Result::Ok(::near_sdk::serde_json::from_slice(&data).expect("Failed to deserialize callback using JSON")), - ::near_sdk::PromiseResult::Failed => ::std::result::Result::Err(::near_sdk::PromiseError::Failed), - }; - let contract: Hello = ::near_sdk::env::state_read().unwrap_or_default(); - contract.method(&mut x, y, ); - } - ); - - assert_eq!(expected.to_string(), actual.to_string()); + local_insta_assert_snapshot!(pretty_print_syn_str(&actual).unwrap()); } #[test] @@ -421,35 +178,7 @@ mod tests { }; let method_info = ImplItemMethodInfo::new(&mut method, false, impl_type).unwrap().unwrap(); let actual = method_info.method_wrapper(); - let expected = quote!( - #[cfg(target_arch = "wasm32")] - #[no_mangle] - pub extern "C" fn method() { - ::near_sdk::env::setup_panic_hook(); - if ::near_sdk::env::current_account_id() != ::near_sdk::env::predecessor_account_id() { - ::near_sdk::env::panic_str("Method method is private"); - } - #[derive(::near_sdk :: serde :: Deserialize)] - #[serde(crate = "::near_sdk::serde")] - struct Input { - y: String, - } - let Input { y, }: Input = ::near_sdk::serde_json::from_slice( - &::near_sdk::env::input().expect("Expected input since method has arguments.") - ) - .expect("Failed to deserialize input from JSON."); - let x: Vec = ::std::iter::Iterator::collect(::std::iter::Iterator::map(0..::near_sdk::env::promise_results_count(), |i| { - let data: ::std::vec::Vec = match ::near_sdk::env::promise_result(i) { - ::near_sdk::PromiseResult::Successful(x) => x, - _ => ::near_sdk::env::panic_str(&::std::format!("Callback computation {} was not successful", i)), - }; - ::near_sdk::serde_json::from_slice(&data).expect("Failed to deserialize callback using JSON") - })); - let contract: Hello = ::near_sdk::env::state_read().unwrap_or_default(); - contract.method(x, y, ); - } - ); - assert_eq!(expected.to_string(), actual.to_string()); + local_insta_assert_snapshot!(pretty_print_syn_str(&actual).unwrap()); } #[test] @@ -461,31 +190,7 @@ mod tests { }; let method_info = ImplItemMethodInfo::new(&mut method, false, impl_type).unwrap().unwrap(); let actual = method_info.method_wrapper(); - let expected = quote!( - #[cfg(target_arch = "wasm32")] - #[no_mangle] - pub extern "C" fn method() { - ::near_sdk::env::setup_panic_hook(); - if ::near_sdk::env::attached_deposit() != 0 { - ::near_sdk::env::panic_str("Method method doesn't accept deposit"); - } - #[derive(::near_sdk :: serde :: Deserialize)] - #[serde(crate = "::near_sdk::serde")] - struct Input { - k: u64, - } - let Input { mut k, }: Input = ::near_sdk::serde_json::from_slice( - &::near_sdk::env::input().expect("Expected input since method has arguments.") - ) - .expect("Failed to deserialize input from JSON."); - if ::near_sdk::env::state_exists() { - ::near_sdk::env::panic_str("The contract has already been initialized"); - } - let contract = Hello::method(&mut k,); - ::near_sdk::env::state_write(&contract); - } - ); - assert_eq!(expected.to_string(), actual.to_string()); + local_insta_assert_snapshot!(pretty_print_syn_str(&actual).unwrap()); } #[test] @@ -509,28 +214,7 @@ mod tests { }; let method_info = ImplItemMethodInfo::new(&mut method, false, impl_type).unwrap().unwrap(); let actual = method_info.method_wrapper(); - let expected = quote!( - #[cfg(target_arch = "wasm32")] - #[no_mangle] - pub extern "C" fn method() { - ::near_sdk::env::setup_panic_hook(); - if ::near_sdk::env::attached_deposit() != 0 { - ::near_sdk::env::panic_str("Method method doesn't accept deposit"); - } - #[derive(::near_sdk :: serde :: Deserialize)] - #[serde(crate = "::near_sdk::serde")] - struct Input { - k: u64, - } - let Input { mut k, }: Input = ::near_sdk::serde_json::from_slice( - &::near_sdk::env::input().expect("Expected input since method has arguments.") - ) - .expect("Failed to deserialize input from JSON."); - let contract = Hello::method(&mut k,); - ::near_sdk::env::state_write(&contract); - } - ); - assert_eq!(expected.to_string(), actual.to_string()); + local_insta_assert_snapshot!(pretty_print_syn_str(&actual).unwrap()); } #[test] @@ -543,28 +227,7 @@ mod tests { }; let method_info = ImplItemMethodInfo::new(&mut method, false, impl_type).unwrap().unwrap(); let actual = method_info.method_wrapper(); - let expected = quote!( - #[cfg(target_arch = "wasm32")] - #[no_mangle] - pub extern "C" fn method() { - ::near_sdk::env::setup_panic_hook(); - #[derive(::near_sdk :: serde :: Deserialize)] - #[serde(crate = "::near_sdk::serde")] - struct Input { - k: u64, - } - let Input { mut k, }: Input = ::near_sdk::serde_json::from_slice( - &::near_sdk::env::input().expect("Expected input since method has arguments.") - ) - .expect("Failed to deserialize input from JSON."); - if ::near_sdk::env::state_exists() { - ::near_sdk::env::panic_str("The contract has already been initialized"); - } - let contract = Hello::method(&mut k,); - ::near_sdk::env::state_write(&contract); - } - ); - assert_eq!(expected.to_string(), actual.to_string()); + local_insta_assert_snapshot!(pretty_print_syn_str(&actual).unwrap()); } #[test] @@ -576,32 +239,7 @@ mod tests { }; let method_info = ImplItemMethodInfo::new(&mut method, false, impl_type).unwrap().unwrap(); let actual = method_info.method_wrapper(); - let expected = quote!( - #[cfg(target_arch = "wasm32")] - #[no_mangle] - pub extern "C" fn method() { - ::near_sdk::env::setup_panic_hook(); - if ::near_sdk::env::attached_deposit() != 0 { - ::near_sdk::env::panic_str("Method method doesn't accept deposit"); - } - #[derive(::near_sdk :: borsh :: BorshDeserialize)] - struct Input { - k: u64, - m: Bar, - } - let Input { k, m, }: Input = ::near_sdk::borsh::BorshDeserialize::try_from_slice( - &::near_sdk::env::input().expect("Expected input since method has arguments.") - ) - .expect("Failed to deserialize input from Borsh."); - let mut contract: Hello = ::near_sdk::env::state_read().unwrap_or_default(); - let result = contract.method(k, m, ); - let result = ::near_sdk::borsh::BorshSerialize::try_to_vec(&result) - .expect("Failed to serialize the return value using Borsh."); - ::near_sdk::env::value_return(&result); - ::near_sdk::env::state_write(&contract); - } - ); - assert_eq!(expected.to_string(), actual.to_string()); + local_insta_assert_snapshot!(pretty_print_syn_str(&actual).unwrap()); } #[test] @@ -612,39 +250,7 @@ mod tests { }; let method_info = ImplItemMethodInfo::new(&mut method, false, impl_type).unwrap().unwrap(); let actual = method_info.method_wrapper(); - let expected = quote!( - #[cfg(target_arch = "wasm32")] - #[no_mangle] - pub extern "C" fn method() { - ::near_sdk::env::setup_panic_hook(); - if ::near_sdk::env::current_account_id() != ::near_sdk::env::predecessor_account_id() { - ::near_sdk::env::panic_str("Method method is private"); - } - #[derive(::near_sdk :: borsh :: BorshDeserialize)] - struct Input { - y: ::std::string::String, - } - let Input { y, }: Input = ::near_sdk::borsh::BorshDeserialize::try_from_slice( - &::near_sdk::env::input().expect("Expected input since method has arguments.") - ) - .expect("Failed to deserialize input from Borsh."); - let data: ::std::vec::Vec = match ::near_sdk::env::promise_result(0u64) { - ::near_sdk::PromiseResult::Successful(x) => x, - _ => ::near_sdk::env::panic_str("Callback computation 0 was not successful") - }; - let mut x: u64 = ::near_sdk::borsh::BorshDeserialize::try_from_slice(&data) - .expect("Failed to deserialize callback using Borsh"); - let data: ::std::vec::Vec = match ::near_sdk::env::promise_result(1u64) { - ::near_sdk::PromiseResult::Successful(x) => x, - _ => ::near_sdk::env::panic_str("Callback computation 1 was not successful") - }; - let z: ::std::vec::Vec = - ::near_sdk::serde_json::from_slice(&data).expect("Failed to deserialize callback using JSON"); - let contract: Hello = ::near_sdk::env::state_read().unwrap_or_default(); - contract.method(&mut x, y, z, ); - } - ); - assert_eq!(expected.to_string(), actual.to_string()); + local_insta_assert_snapshot!(pretty_print_syn_str(&actual).unwrap()); } #[test] @@ -653,17 +259,7 @@ mod tests { let mut method: ImplItemFn = syn::parse_str("#[payable] pub fn method(&mut self) { }").unwrap(); let method_info = ImplItemMethodInfo::new(&mut method, false, impl_type).unwrap().unwrap(); let actual = method_info.method_wrapper(); - let expected = quote!( - #[cfg(target_arch = "wasm32")] - #[no_mangle] - pub extern "C" fn method() { - ::near_sdk::env::setup_panic_hook(); - let mut contract: Hello = ::near_sdk::env::state_read().unwrap_or_default(); - contract.method(); - ::near_sdk::env::state_write(&contract); - } - ); - assert_eq!(expected.to_string(), actual.to_string()); + local_insta_assert_snapshot!(pretty_print_syn_str(&actual).unwrap()); } #[test] @@ -672,23 +268,7 @@ mod tests { let mut method: ImplItemFn = syn::parse_str("#[private] pub fn private_method(&mut self) { }").unwrap(); let method_info = ImplItemMethodInfo::new(&mut method, false, impl_type).unwrap().unwrap(); let actual = method_info.method_wrapper(); - let expected = quote!( - #[cfg(target_arch = "wasm32")] - #[no_mangle] - pub extern "C" fn private_method() { - ::near_sdk::env::setup_panic_hook(); - if ::near_sdk::env::current_account_id() != ::near_sdk::env::predecessor_account_id() { - ::near_sdk::env::panic_str("Method private_method is private"); - } - if ::near_sdk::env::attached_deposit() != 0 { - ::near_sdk::env::panic_str("Method private_method doesn't accept deposit"); - } - let mut contract: Hello = ::near_sdk::env::state_read().unwrap_or_default(); - contract.private_method(); - ::near_sdk::env::state_write(&contract); - } - ); - assert_eq!(expected.to_string(), actual.to_string()); + local_insta_assert_snapshot!(pretty_print_syn_str(&actual).unwrap()); } #[test] @@ -700,24 +280,7 @@ mod tests { }; let method_info = ImplItemMethodInfo::new(&mut method, false, impl_type).unwrap().unwrap(); let actual = method_info.method_wrapper(); - let expected = quote!( - #[cfg(target_arch = "wasm32")] - #[no_mangle] - pub extern "C" fn method() { - ::near_sdk::env::setup_panic_hook(); - let contract: Hello = ::near_sdk::env::state_read().unwrap_or_default(); - let result = contract.method(); - match result { - ::std::result::Result::Ok(result) => { - let result = - ::near_sdk::serde_json::to_vec(&result).expect("Failed to serialize the return value using JSON."); - ::near_sdk::env::value_return(&result); - } - ::std::result::Result::Err(err) => ::near_sdk::FunctionError::panic(&err) - } - } - ); - assert_eq!(expected.to_string(), actual.to_string()); + local_insta_assert_snapshot!(pretty_print_syn_str(&actual).unwrap()); } #[test] @@ -729,28 +292,7 @@ mod tests { }; let method_info = ImplItemMethodInfo::new(&mut method, false, impl_type).unwrap().unwrap(); let actual = method_info.method_wrapper(); - let expected = quote!( - #[cfg(target_arch = "wasm32")] - #[no_mangle] - pub extern "C" fn method() { - ::near_sdk::env::setup_panic_hook(); - if ::near_sdk::env::attached_deposit() != 0 { - ::near_sdk::env::panic_str("Method method doesn't accept deposit"); - } - let mut contract: Hello = ::near_sdk::env::state_read().unwrap_or_default(); - let result = contract.method(); - match result { - ::std::result::Result::Ok(result) => { - let result = - ::near_sdk::serde_json::to_vec(&result).expect("Failed to serialize the return value using JSON."); - ::near_sdk::env::value_return(&result); - ::near_sdk::env::state_write(&contract); - } - ::std::result::Result::Err(err) => ::near_sdk::FunctionError::panic(&err) - } - } - ); - assert_eq!(expected.to_string(), actual.to_string()); + local_insta_assert_snapshot!(pretty_print_syn_str(&actual).unwrap()); } #[test] @@ -763,24 +305,7 @@ mod tests { }; let method_info = ImplItemMethodInfo::new(&mut method, false, impl_type).unwrap().unwrap(); let actual = method_info.method_wrapper(); - let expected = quote!( - #[cfg(target_arch = "wasm32")] - #[no_mangle] - pub extern "C" fn method() { - ::near_sdk::env::setup_panic_hook(); - let contract: Hello = ::near_sdk::env::state_read().unwrap_or_default(); - let result = contract.method(); - match result { - ::std::result::Result::Ok(result) => { - let result = - ::near_sdk::borsh::BorshSerialize::try_to_vec(&result).expect("Failed to serialize the return value using Borsh."); - ::near_sdk::env::value_return(&result); - } - ::std::result::Result::Err(err) => ::near_sdk::FunctionError::panic(&err) - } - } - ); - assert_eq!(expected.to_string(), actual.to_string()); + local_insta_assert_snapshot!(pretty_print_syn_str(&actual).unwrap()); } #[test] @@ -793,27 +318,7 @@ mod tests { }; let method_info = ImplItemMethodInfo::new(&mut method, false, impl_type).unwrap().unwrap(); let actual = method_info.method_wrapper(); - let expected = quote!( - #[cfg(target_arch = "wasm32")] - #[no_mangle] - pub extern "C" fn new() { - ::near_sdk::env::setup_panic_hook(); - if ::near_sdk::env::attached_deposit() != 0 { - ::near_sdk::env::panic_str("Method new doesn't accept deposit"); - } - if ::near_sdk::env::state_exists() { - ::near_sdk::env::panic_str("The contract has already been initialized"); - } - let contract = Hello::new(); - match contract { - ::std::result::Result::Ok(contract) => { - ::near_sdk::env::state_write(&contract); - } - ::std::result::Result::Err(err) => ::near_sdk::FunctionError::panic(&err) - } - } - ); - assert_eq!(expected.to_string(), actual.to_string()); + local_insta_assert_snapshot!(pretty_print_syn_str(&actual).unwrap()); } #[test] @@ -826,24 +331,7 @@ mod tests { }; let method_info = ImplItemMethodInfo::new(&mut method, false, impl_type).unwrap().unwrap(); let actual = method_info.method_wrapper(); - let expected = quote!( - #[cfg(target_arch = "wasm32")] - #[no_mangle] - pub extern "C" fn new() { - ::near_sdk::env::setup_panic_hook(); - if ::near_sdk::env::attached_deposit() != 0 { - ::near_sdk::env::panic_str("Method new doesn't accept deposit"); - } - let contract = Hello::new(); - match contract { - ::std::result::Result::Ok(contract) => { - ::near_sdk::env::state_write(&contract); - } - ::std::result::Result::Err(err) => ::near_sdk::FunctionError::panic(&err) - } - } - ); - assert_eq!(expected.to_string(), actual.to_string()); + local_insta_assert_snapshot!(pretty_print_syn_str(&actual).unwrap()); } #[test] @@ -852,14 +340,6 @@ mod tests { let mut method: ImplItemFn = syn::parse_str("pub fn method() { }").unwrap(); let method_info = ImplItemMethodInfo::new(&mut method, false, impl_type).unwrap().unwrap(); let actual = method_info.method_wrapper(); - let expected = quote!( - #[cfg(target_arch = "wasm32")] - #[no_mangle] - pub extern "C" fn method() { - ::near_sdk::env::setup_panic_hook(); - Hello::method(); - } - ); - assert_eq!(expected.to_string(), actual.to_string()); + local_insta_assert_snapshot!(pretty_print_syn_str(&actual).unwrap()); } } diff --git a/near-sdk-macros/src/core_impl/code_generator/item_trait_info.rs b/near-sdk-macros/src/core_impl/code_generator/item_trait_info.rs index 6b6f6405e..152346a9b 100644 --- a/near-sdk-macros/src/core_impl/code_generator/item_trait_info.rs +++ b/near-sdk-macros/src/core_impl/code_generator/item_trait_info.rs @@ -31,6 +31,7 @@ mod tests { use syn::ItemTrait; use quote::quote; use crate::core_impl::info_extractor::ItemTraitInfo; + use crate::core_impl::utils::test_helpers::{local_insta_assert_snapshot, pretty_print_syn_str}; #[test] fn ext_basic() { @@ -52,79 +53,7 @@ mod tests { ).unwrap(); let info = ItemTraitInfo::new(&mut t, None).unwrap(); let actual = info.wrap_trait_ext(); - - let expected = quote! { - pub mod external_cross_contract { - use super::*; - #[must_use] - pub struct ExternalCrossContractExt { - pub(crate) account_id: ::near_sdk::AccountId, - pub(crate) deposit: ::near_sdk::Balance, - pub(crate) static_gas: ::near_sdk::Gas, - pub(crate) gas_weight: ::near_sdk::GasWeight, - } - impl ExternalCrossContractExt { - pub fn with_attached_deposit(mut self, amount: ::near_sdk::Balance) -> Self { - self.deposit = amount; - self - } - pub fn with_static_gas(mut self, static_gas: ::near_sdk::Gas) -> Self { - self.static_gas = static_gas; - self - } - pub fn with_unused_gas_weight(mut self, gas_weight: u64) -> Self { - self.gas_weight = ::near_sdk::GasWeight(gas_weight); - self - } - } - /// API for calling this contract's functions in a subsequent execution. - pub fn ext(account_id: ::near_sdk::AccountId) -> ExternalCrossContractExt { - ExternalCrossContractExt { - account_id, - deposit: 0, - static_gas: ::near_sdk::Gas::from_gas(0), - gas_weight: ::near_sdk::GasWeight::default(), - } - } - impl ExternalCrossContractExt { - pub fn merge_sort( - self, - arr: Vec, - ) -> ::near_sdk::Promise { - let __args = { - #[derive(::near_sdk :: serde :: Serialize)] - #[serde(crate = "::near_sdk::serde")] - struct Input<'nearinput> { - arr: &'nearinput Vec, - } - let __args = Input { arr: &arr, }; - ::near_sdk::serde_json::to_vec(&__args) - .expect("Failed to serialize the cross contract args using JSON.") - }; - ::near_sdk::Promise::new(self.account_id) - .function_call_weight( - ::std::string::String::from("merge_sort"), - __args, - self.deposit, - self.static_gas, - self.gas_weight, - ) - } - pub fn merge(self,) -> ::near_sdk::Promise { - let __args = ::std::vec![]; - ::near_sdk::Promise::new(self.account_id) - .function_call_weight( - ::std::string::String::from("merge"), - __args, - self.deposit, - self.static_gas, - self.gas_weight, - ) - } - } - } - }; - assert_eq!(actual.to_string(), expected.to_string()); + local_insta_assert_snapshot!(pretty_print_syn_str(&actual).unwrap()); } #[test] @@ -140,65 +69,6 @@ mod tests { let info = ItemTraitInfo::new(&mut t, None).unwrap(); let actual = info.wrap_trait_ext(); - let expected = quote! { - pub mod test { - use super::*; - #[must_use] - pub struct TestExt { - pub(crate) account_id: ::near_sdk::AccountId, - pub(crate) deposit: ::near_sdk::Balance, - pub(crate) static_gas: ::near_sdk::Gas, - pub(crate) gas_weight: ::near_sdk::GasWeight, - } - impl TestExt { - pub fn with_attached_deposit(mut self, amount: ::near_sdk::Balance) -> Self { - self.deposit = amount; - self - } - pub fn with_static_gas(mut self, static_gas: ::near_sdk::Gas) -> Self { - self.static_gas = static_gas; - self - } - pub fn with_unused_gas_weight(mut self, gas_weight: u64) -> Self { - self.gas_weight = ::near_sdk::GasWeight(gas_weight); - self - } - } - /// API for calling this contract's functions in a subsequent execution. - pub fn ext(account_id: ::near_sdk::AccountId) -> TestExt { - TestExt { - account_id, - deposit: 0, - static_gas: ::near_sdk::Gas::from_gas(0), - gas_weight: ::near_sdk::GasWeight::default(), - } - } - impl TestExt { - pub fn test( - self, - v: Vec, - ) -> ::near_sdk::Promise { - let __args = { - #[derive(::near_sdk :: borsh :: BorshSerialize)] - struct Input<'nearinput> { - v: &'nearinput Vec, - } - let __args = Input { v: &v, }; - ::near_sdk::borsh::BorshSerialize::try_to_vec(&__args) - .expect("Failed to serialize the cross contract args using Borsh.") - }; - ::near_sdk::Promise::new(self.account_id) - .function_call_weight( - ::std::string::String::from("test"), - __args, - self.deposit, - self.static_gas, - self.gas_weight, - ) - } - } - } - }; - assert_eq!(actual.to_string(), expected.to_string()); + local_insta_assert_snapshot!(pretty_print_syn_str(&actual).unwrap()); } } diff --git a/near-sdk-macros/src/core_impl/code_generator/snapshots/arg_mut_ref.snap b/near-sdk-macros/src/core_impl/code_generator/snapshots/arg_mut_ref.snap new file mode 100644 index 000000000..3266525d8 --- /dev/null +++ b/near-sdk-macros/src/core_impl/code_generator/snapshots/arg_mut_ref.snap @@ -0,0 +1,22 @@ +--- +source: near-sdk-macros/src/core_impl/code_generator/item_impl_info.rs +expression: pretty_print_syn_str(&actual).unwrap() +--- +#[cfg(target_arch = "wasm32")] +#[no_mangle] +pub extern "C" fn method() { + ::near_sdk::env::setup_panic_hook(); + #[derive(::near_sdk::serde::Deserialize)] + #[serde(crate = "::near_sdk::serde")] + struct Input { + k: u64, + } + let Input { mut k }: Input = ::near_sdk::serde_json::from_slice( + &::near_sdk::env::input() + .expect("Expected input since method has arguments."), + ) + .expect("Failed to deserialize input from JSON."); + let contract: Hello = ::near_sdk::env::state_read().unwrap_or_default(); + contract.method(&mut k); +} + diff --git a/near-sdk-macros/src/core_impl/code_generator/snapshots/arg_no_return_no_mut.snap b/near-sdk-macros/src/core_impl/code_generator/snapshots/arg_no_return_no_mut.snap new file mode 100644 index 000000000..fc220c701 --- /dev/null +++ b/near-sdk-macros/src/core_impl/code_generator/snapshots/arg_no_return_no_mut.snap @@ -0,0 +1,22 @@ +--- +source: near-sdk-macros/src/core_impl/code_generator/item_impl_info.rs +expression: pretty_print_syn_str(&actual).unwrap() +--- +#[cfg(target_arch = "wasm32")] +#[no_mangle] +pub extern "C" fn method() { + ::near_sdk::env::setup_panic_hook(); + #[derive(::near_sdk::serde::Deserialize)] + #[serde(crate = "::near_sdk::serde")] + struct Input { + k: u64, + } + let Input { k }: Input = ::near_sdk::serde_json::from_slice( + &::near_sdk::env::input() + .expect("Expected input since method has arguments."), + ) + .expect("Failed to deserialize input from JSON."); + let contract: Hello = ::near_sdk::env::state_read().unwrap_or_default(); + contract.method(k); +} + diff --git a/near-sdk-macros/src/core_impl/code_generator/snapshots/arg_ref.snap b/near-sdk-macros/src/core_impl/code_generator/snapshots/arg_ref.snap new file mode 100644 index 000000000..8b275827f --- /dev/null +++ b/near-sdk-macros/src/core_impl/code_generator/snapshots/arg_ref.snap @@ -0,0 +1,22 @@ +--- +source: near-sdk-macros/src/core_impl/code_generator/item_impl_info.rs +expression: pretty_print_syn_str(&actual).unwrap() +--- +#[cfg(target_arch = "wasm32")] +#[no_mangle] +pub extern "C" fn method() { + ::near_sdk::env::setup_panic_hook(); + #[derive(::near_sdk::serde::Deserialize)] + #[serde(crate = "::near_sdk::serde")] + struct Input { + k: u64, + } + let Input { k }: Input = ::near_sdk::serde_json::from_slice( + &::near_sdk::env::input() + .expect("Expected input since method has arguments."), + ) + .expect("Failed to deserialize input from JSON."); + let contract: Hello = ::near_sdk::env::state_read().unwrap_or_default(); + contract.method(&k); +} + diff --git a/near-sdk-macros/src/core_impl/code_generator/snapshots/args_no_return_mut.snap b/near-sdk-macros/src/core_impl/code_generator/snapshots/args_no_return_mut.snap new file mode 100644 index 000000000..322b4d762 --- /dev/null +++ b/near-sdk-macros/src/core_impl/code_generator/snapshots/args_no_return_mut.snap @@ -0,0 +1,27 @@ +--- +source: near-sdk-macros/src/core_impl/code_generator/item_impl_info.rs +expression: pretty_print_syn_str(&actual).unwrap() +--- +#[cfg(target_arch = "wasm32")] +#[no_mangle] +pub extern "C" fn method() { + ::near_sdk::env::setup_panic_hook(); + if ::near_sdk::env::attached_deposit() != 0 { + ::near_sdk::env::panic_str("Method method doesn't accept deposit"); + } + #[derive(::near_sdk::serde::Deserialize)] + #[serde(crate = "::near_sdk::serde")] + struct Input { + k: u64, + m: Bar, + } + let Input { k, m }: Input = ::near_sdk::serde_json::from_slice( + &::near_sdk::env::input() + .expect("Expected input since method has arguments."), + ) + .expect("Failed to deserialize input from JSON."); + let mut contract: Hello = ::near_sdk::env::state_read().unwrap_or_default(); + contract.method(k, m); + ::near_sdk::env::state_write(&contract); +} + diff --git a/near-sdk-macros/src/core_impl/code_generator/snapshots/args_return_mut.snap b/near-sdk-macros/src/core_impl/code_generator/snapshots/args_return_mut.snap new file mode 100644 index 000000000..3e1c3f0c1 --- /dev/null +++ b/near-sdk-macros/src/core_impl/code_generator/snapshots/args_return_mut.snap @@ -0,0 +1,30 @@ +--- +source: near-sdk-macros/src/core_impl/code_generator/item_impl_info.rs +expression: pretty_print_syn_str(&actual).unwrap() +--- +#[cfg(target_arch = "wasm32")] +#[no_mangle] +pub extern "C" fn method() { + ::near_sdk::env::setup_panic_hook(); + if ::near_sdk::env::attached_deposit() != 0 { + ::near_sdk::env::panic_str("Method method doesn't accept deposit"); + } + #[derive(::near_sdk::serde::Deserialize)] + #[serde(crate = "::near_sdk::serde")] + struct Input { + k: u64, + m: Bar, + } + let Input { k, m }: Input = ::near_sdk::serde_json::from_slice( + &::near_sdk::env::input() + .expect("Expected input since method has arguments."), + ) + .expect("Failed to deserialize input from JSON."); + let mut contract: Hello = ::near_sdk::env::state_read().unwrap_or_default(); + let result = contract.method(k, m); + let result = ::near_sdk::serde_json::to_vec(&result) + .expect("Failed to serialize the return value using JSON."); + ::near_sdk::env::value_return(&result); + ::near_sdk::env::state_write(&contract); +} + diff --git a/near-sdk-macros/src/core_impl/code_generator/snapshots/args_return_mut_borsh.snap b/near-sdk-macros/src/core_impl/code_generator/snapshots/args_return_mut_borsh.snap new file mode 100644 index 000000000..86b102534 --- /dev/null +++ b/near-sdk-macros/src/core_impl/code_generator/snapshots/args_return_mut_borsh.snap @@ -0,0 +1,29 @@ +--- +source: near-sdk-macros/src/core_impl/code_generator/item_impl_info.rs +expression: pretty_print_syn_str(&actual).unwrap() +--- +#[cfg(target_arch = "wasm32")] +#[no_mangle] +pub extern "C" fn method() { + ::near_sdk::env::setup_panic_hook(); + if ::near_sdk::env::attached_deposit() != 0 { + ::near_sdk::env::panic_str("Method method doesn't accept deposit"); + } + #[derive(::near_sdk::borsh::BorshDeserialize)] + struct Input { + k: u64, + m: Bar, + } + let Input { k, m }: Input = ::near_sdk::borsh::BorshDeserialize::try_from_slice( + &::near_sdk::env::input() + .expect("Expected input since method has arguments."), + ) + .expect("Failed to deserialize input from Borsh."); + let mut contract: Hello = ::near_sdk::env::state_read().unwrap_or_default(); + let result = contract.method(k, m); + let result = ::near_sdk::borsh::BorshSerialize::try_to_vec(&result) + .expect("Failed to serialize the return value using Borsh."); + ::near_sdk::env::value_return(&result); + ::near_sdk::env::state_write(&contract); +} + diff --git a/near-sdk-macros/src/core_impl/code_generator/snapshots/args_return_ref.snap b/near-sdk-macros/src/core_impl/code_generator/snapshots/args_return_ref.snap new file mode 100644 index 000000000..4d990e343 --- /dev/null +++ b/near-sdk-macros/src/core_impl/code_generator/snapshots/args_return_ref.snap @@ -0,0 +1,15 @@ +--- +source: near-sdk-macros/src/core_impl/code_generator/item_impl_info.rs +expression: pretty_print_syn_str(&actual).unwrap() +--- +#[cfg(target_arch = "wasm32")] +#[no_mangle] +pub extern "C" fn method() { + ::near_sdk::env::setup_panic_hook(); + let contract: Hello = ::near_sdk::env::state_read().unwrap_or_default(); + let result = contract.method(); + let result = ::near_sdk::serde_json::to_vec(&result) + .expect("Failed to serialize the return value using JSON."); + ::near_sdk::env::value_return(&result); +} + diff --git a/near-sdk-macros/src/core_impl/code_generator/snapshots/callback_args.snap b/near-sdk-macros/src/core_impl/code_generator/snapshots/callback_args.snap new file mode 100644 index 000000000..2a38c958e --- /dev/null +++ b/near-sdk-macros/src/core_impl/code_generator/snapshots/callback_args.snap @@ -0,0 +1,38 @@ +--- +source: near-sdk-macros/src/core_impl/code_generator/item_impl_info.rs +expression: pretty_print_syn_str(&actual).unwrap() +--- +#[cfg(target_arch = "wasm32")] +#[no_mangle] +pub extern "C" fn method() { + ::near_sdk::env::setup_panic_hook(); + if ::near_sdk::env::current_account_id() != ::near_sdk::env::predecessor_account_id() + { + ::near_sdk::env::panic_str("Method method is private"); + } + #[derive(::near_sdk::serde::Deserialize)] + #[serde(crate = "::near_sdk::serde")] + struct Input { + y: ::std::string::String, + } + let Input { y }: Input = ::near_sdk::serde_json::from_slice( + &::near_sdk::env::input() + .expect("Expected input since method has arguments."), + ) + .expect("Failed to deserialize input from JSON."); + let data: ::std::vec::Vec = match ::near_sdk::env::promise_result(0u64) { + ::near_sdk::PromiseResult::Successful(x) => x, + _ => ::near_sdk::env::panic_str("Callback computation 0 was not successful"), + }; + let mut x: u64 = ::near_sdk::serde_json::from_slice(&data) + .expect("Failed to deserialize callback using JSON"); + let data: ::std::vec::Vec = match ::near_sdk::env::promise_result(1u64) { + ::near_sdk::PromiseResult::Successful(x) => x, + _ => ::near_sdk::env::panic_str("Callback computation 1 was not successful"), + }; + let z: ::std::vec::Vec = ::near_sdk::serde_json::from_slice(&data) + .expect("Failed to deserialize callback using JSON"); + let contract: Hello = ::near_sdk::env::state_read().unwrap_or_default(); + contract.method(&mut x, y, z); +} + diff --git a/near-sdk-macros/src/core_impl/code_generator/snapshots/callback_args_mixed_serialization.snap b/near-sdk-macros/src/core_impl/code_generator/snapshots/callback_args_mixed_serialization.snap new file mode 100644 index 000000000..bece80d27 --- /dev/null +++ b/near-sdk-macros/src/core_impl/code_generator/snapshots/callback_args_mixed_serialization.snap @@ -0,0 +1,37 @@ +--- +source: near-sdk-macros/src/core_impl/code_generator/item_impl_info.rs +expression: pretty_print_syn_str(&actual).unwrap() +--- +#[cfg(target_arch = "wasm32")] +#[no_mangle] +pub extern "C" fn method() { + ::near_sdk::env::setup_panic_hook(); + if ::near_sdk::env::current_account_id() != ::near_sdk::env::predecessor_account_id() + { + ::near_sdk::env::panic_str("Method method is private"); + } + #[derive(::near_sdk::borsh::BorshDeserialize)] + struct Input { + y: ::std::string::String, + } + let Input { y }: Input = ::near_sdk::borsh::BorshDeserialize::try_from_slice( + &::near_sdk::env::input() + .expect("Expected input since method has arguments."), + ) + .expect("Failed to deserialize input from Borsh."); + let data: ::std::vec::Vec = match ::near_sdk::env::promise_result(0u64) { + ::near_sdk::PromiseResult::Successful(x) => x, + _ => ::near_sdk::env::panic_str("Callback computation 0 was not successful"), + }; + let mut x: u64 = ::near_sdk::borsh::BorshDeserialize::try_from_slice(&data) + .expect("Failed to deserialize callback using Borsh"); + let data: ::std::vec::Vec = match ::near_sdk::env::promise_result(1u64) { + ::near_sdk::PromiseResult::Successful(x) => x, + _ => ::near_sdk::env::panic_str("Callback computation 1 was not successful"), + }; + let z: ::std::vec::Vec = ::near_sdk::serde_json::from_slice(&data) + .expect("Failed to deserialize callback using JSON"); + let contract: Hello = ::near_sdk::env::state_read().unwrap_or_default(); + contract.method(&mut x, y, z); +} + diff --git a/near-sdk-macros/src/core_impl/code_generator/snapshots/callback_args_only.snap b/near-sdk-macros/src/core_impl/code_generator/snapshots/callback_args_only.snap new file mode 100644 index 000000000..6be15f320 --- /dev/null +++ b/near-sdk-macros/src/core_impl/code_generator/snapshots/callback_args_only.snap @@ -0,0 +1,28 @@ +--- +source: near-sdk-macros/src/core_impl/code_generator/item_impl_info.rs +expression: pretty_print_syn_str(&actual).unwrap() +--- +#[cfg(target_arch = "wasm32")] +#[no_mangle] +pub extern "C" fn method() { + ::near_sdk::env::setup_panic_hook(); + if ::near_sdk::env::current_account_id() != ::near_sdk::env::predecessor_account_id() + { + ::near_sdk::env::panic_str("Method method is private"); + } + let data: ::std::vec::Vec = match ::near_sdk::env::promise_result(0u64) { + ::near_sdk::PromiseResult::Successful(x) => x, + _ => ::near_sdk::env::panic_str("Callback computation 0 was not successful"), + }; + let mut x: u64 = ::near_sdk::serde_json::from_slice(&data) + .expect("Failed to deserialize callback using JSON"); + let data: ::std::vec::Vec = match ::near_sdk::env::promise_result(1u64) { + ::near_sdk::PromiseResult::Successful(x) => x, + _ => ::near_sdk::env::panic_str("Callback computation 1 was not successful"), + }; + let y: ::std::string::String = ::near_sdk::serde_json::from_slice(&data) + .expect("Failed to deserialize callback using JSON"); + let contract: Hello = ::near_sdk::env::state_read().unwrap_or_default(); + contract.method(&mut x, y); +} + diff --git a/near-sdk-macros/src/core_impl/code_generator/snapshots/callback_args_results.snap b/near-sdk-macros/src/core_impl/code_generator/snapshots/callback_args_results.snap new file mode 100644 index 000000000..f38a0e0f4 --- /dev/null +++ b/near-sdk-macros/src/core_impl/code_generator/snapshots/callback_args_results.snap @@ -0,0 +1,40 @@ +--- +source: near-sdk-macros/src/core_impl/code_generator/item_impl_info.rs +expression: pretty_print_syn_str(&actual).unwrap() +--- +#[cfg(target_arch = "wasm32")] +#[no_mangle] +pub extern "C" fn method() { + ::near_sdk::env::setup_panic_hook(); + if ::near_sdk::env::current_account_id() != ::near_sdk::env::predecessor_account_id() + { + ::near_sdk::env::panic_str("Method method is private"); + } + let mut x: Result = match ::near_sdk::env::promise_result(0u64) { + ::near_sdk::PromiseResult::Successful(data) => { + ::std::result::Result::Ok( + ::near_sdk::serde_json::from_slice(&data) + .expect("Failed to deserialize callback using JSON"), + ) + } + ::near_sdk::PromiseResult::Failed => { + ::std::result::Result::Err(::near_sdk::PromiseError::Failed) + } + }; + let y: Result<::std::string::String, PromiseError> = match ::near_sdk::env::promise_result( + 1u64, + ) { + ::near_sdk::PromiseResult::Successful(data) => { + ::std::result::Result::Ok( + ::near_sdk::serde_json::from_slice(&data) + .expect("Failed to deserialize callback using JSON"), + ) + } + ::near_sdk::PromiseResult::Failed => { + ::std::result::Result::Err(::near_sdk::PromiseError::Failed) + } + }; + let contract: Hello = ::near_sdk::env::state_read().unwrap_or_default(); + contract.method(&mut x, y); +} + diff --git a/near-sdk-macros/src/core_impl/code_generator/snapshots/callback_args_vec.snap b/near-sdk-macros/src/core_impl/code_generator/snapshots/callback_args_vec.snap new file mode 100644 index 000000000..e9e5deffb --- /dev/null +++ b/near-sdk-macros/src/core_impl/code_generator/snapshots/callback_args_vec.snap @@ -0,0 +1,47 @@ +--- +source: near-sdk-macros/src/core_impl/code_generator/item_impl_info.rs +expression: pretty_print_syn_str(&actual).unwrap() +--- +#[cfg(target_arch = "wasm32")] +#[no_mangle] +pub extern "C" fn method() { + ::near_sdk::env::setup_panic_hook(); + if ::near_sdk::env::current_account_id() != ::near_sdk::env::predecessor_account_id() + { + ::near_sdk::env::panic_str("Method method is private"); + } + #[derive(::near_sdk::serde::Deserialize)] + #[serde(crate = "::near_sdk::serde")] + struct Input { + y: String, + } + let Input { y }: Input = ::near_sdk::serde_json::from_slice( + &::near_sdk::env::input() + .expect("Expected input since method has arguments."), + ) + .expect("Failed to deserialize input from JSON."); + let x: Vec = ::std::iter::Iterator::collect( + ::std::iter::Iterator::map( + 0..::near_sdk::env::promise_results_count(), + |i| { + let data: ::std::vec::Vec = match ::near_sdk::env::promise_result( + i, + ) { + ::near_sdk::PromiseResult::Successful(x) => x, + _ => { + ::near_sdk::env::panic_str( + &::std::format!( + "Callback computation {} was not successful", i + ), + ) + } + }; + ::near_sdk::serde_json::from_slice(&data) + .expect("Failed to deserialize callback using JSON") + }, + ), + ); + let contract: Hello = ::near_sdk::env::state_read().unwrap_or_default(); + contract.method(x, y); +} + diff --git a/near-sdk-macros/src/core_impl/code_generator/snapshots/ext_basic.snap b/near-sdk-macros/src/core_impl/code_generator/snapshots/ext_basic.snap new file mode 100644 index 000000000..d0feb1498 --- /dev/null +++ b/near-sdk-macros/src/core_impl/code_generator/snapshots/ext_basic.snap @@ -0,0 +1,71 @@ +--- +source: near-sdk-macros/src/core_impl/code_generator/item_trait_info.rs +expression: pretty_print_syn_str(&actual).unwrap() +--- +pub mod external_cross_contract { + use super::*; + #[must_use] + pub struct ExternalCrossContractExt { + pub(crate) account_id: ::near_sdk::AccountId, + pub(crate) deposit: ::near_sdk::Balance, + pub(crate) static_gas: ::near_sdk::Gas, + pub(crate) gas_weight: ::near_sdk::GasWeight, + } + impl ExternalCrossContractExt { + pub fn with_attached_deposit(mut self, amount: ::near_sdk::Balance) -> Self { + self.deposit = amount; + self + } + pub fn with_static_gas(mut self, static_gas: ::near_sdk::Gas) -> Self { + self.static_gas = static_gas; + self + } + pub fn with_unused_gas_weight(mut self, gas_weight: u64) -> Self { + self.gas_weight = ::near_sdk::GasWeight(gas_weight); + self + } + } + /// API for calling this contract's functions in a subsequent execution. + pub fn ext(account_id: ::near_sdk::AccountId) -> ExternalCrossContractExt { + ExternalCrossContractExt { + account_id, + deposit: 0, + static_gas: ::near_sdk::Gas::from_gas(0), + gas_weight: ::near_sdk::GasWeight::default(), + } + } + impl ExternalCrossContractExt { + pub fn merge_sort(self, arr: Vec) -> ::near_sdk::Promise { + let __args = { + #[derive(::near_sdk::serde::Serialize)] + #[serde(crate = "::near_sdk::serde")] + struct Input<'nearinput> { + arr: &'nearinput Vec, + } + let __args = Input { arr: &arr }; + ::near_sdk::serde_json::to_vec(&__args) + .expect("Failed to serialize the cross contract args using JSON.") + }; + ::near_sdk::Promise::new(self.account_id) + .function_call_weight( + ::std::string::String::from("merge_sort"), + __args, + self.deposit, + self.static_gas, + self.gas_weight, + ) + } + pub fn merge(self) -> ::near_sdk::Promise { + let __args = ::std::vec![]; + ::near_sdk::Promise::new(self.account_id) + .function_call_weight( + ::std::string::String::from("merge"), + __args, + self.deposit, + self.static_gas, + self.gas_weight, + ) + } + } +} + diff --git a/near-sdk-macros/src/core_impl/code_generator/snapshots/ext_basic_borsh.snap b/near-sdk-macros/src/core_impl/code_generator/snapshots/ext_basic_borsh.snap new file mode 100644 index 000000000..49065f4ff --- /dev/null +++ b/near-sdk-macros/src/core_impl/code_generator/snapshots/ext_basic_borsh.snap @@ -0,0 +1,24 @@ +--- +source: near-sdk-macros/src/core_impl/code_generator/ext.rs +expression: pretty_print_syn_str(&actual).unwrap() +--- +pub fn borsh_test(self, a: String) -> ::near_sdk::Promise { + let __args = { + #[derive(::near_sdk::borsh::BorshSerialize)] + struct Input<'nearinput> { + a: &'nearinput String, + } + let __args = Input { a: &a }; + ::near_sdk::borsh::BorshSerialize::try_to_vec(&__args) + .expect("Failed to serialize the cross contract args using Borsh.") + }; + ::near_sdk::Promise::new(self.account_id) + .function_call_weight( + ::std::string::String::from("borsh_test"), + __args, + self.deposit, + self.static_gas, + self.gas_weight, + ) +} + diff --git a/near-sdk-macros/src/core_impl/code_generator/snapshots/ext_basic_json.snap b/near-sdk-macros/src/core_impl/code_generator/snapshots/ext_basic_json.snap new file mode 100644 index 000000000..dd053c66a --- /dev/null +++ b/near-sdk-macros/src/core_impl/code_generator/snapshots/ext_basic_json.snap @@ -0,0 +1,25 @@ +--- +source: near-sdk-macros/src/core_impl/code_generator/ext.rs +expression: pretty_print_syn_str(&actual).unwrap() +--- +pub fn method(self, k: &String) -> ::near_sdk::Promise { + let __args = { + #[derive(::near_sdk::serde::Serialize)] + #[serde(crate = "::near_sdk::serde")] + struct Input<'nearinput> { + k: &'nearinput String, + } + let __args = Input { k: &k }; + ::near_sdk::serde_json::to_vec(&__args) + .expect("Failed to serialize the cross contract args using JSON.") + }; + ::near_sdk::Promise::new(self.account_id) + .function_call_weight( + ::std::string::String::from("method"), + __args, + self.deposit, + self.static_gas, + self.gas_weight, + ) +} + diff --git a/near-sdk-macros/src/core_impl/code_generator/snapshots/ext_fn_non_bindgen_attrs.snap b/near-sdk-macros/src/core_impl/code_generator/snapshots/ext_fn_non_bindgen_attrs.snap new file mode 100644 index 000000000..bec5cfc11 --- /dev/null +++ b/near-sdk-macros/src/core_impl/code_generator/snapshots/ext_fn_non_bindgen_attrs.snap @@ -0,0 +1,17 @@ +--- +source: near-sdk-macros/src/core_impl/code_generator/ext.rs +expression: pretty_print_syn_str(&actual).unwrap() +--- +#[cfg(target_os = "linux")] +pub fn method(self) -> ::near_sdk::Promise { + let __args = ::std::vec![]; + ::near_sdk::Promise::new(self.account_id) + .function_call_weight( + ::std::string::String::from("method"), + __args, + self.deposit, + self.static_gas, + self.gas_weight, + ) +} + diff --git a/near-sdk-macros/src/core_impl/code_generator/snapshots/ext_gen.snap b/near-sdk-macros/src/core_impl/code_generator/snapshots/ext_gen.snap new file mode 100644 index 000000000..8fd5e891b --- /dev/null +++ b/near-sdk-macros/src/core_impl/code_generator/snapshots/ext_gen.snap @@ -0,0 +1,37 @@ +--- +source: near-sdk-macros/src/core_impl/code_generator/ext.rs +expression: pretty_print_syn_str(&actual).unwrap() +--- +#[must_use] +pub struct TestExt { + pub(crate) account_id: ::near_sdk::AccountId, + pub(crate) deposit: ::near_sdk::Balance, + pub(crate) static_gas: ::near_sdk::Gas, + pub(crate) gas_weight: ::near_sdk::GasWeight, +} +impl TestExt { + pub fn with_attached_deposit(mut self, amount: ::near_sdk::Balance) -> Self { + self.deposit = amount; + self + } + pub fn with_static_gas(mut self, static_gas: ::near_sdk::Gas) -> Self { + self.static_gas = static_gas; + self + } + pub fn with_unused_gas_weight(mut self, gas_weight: u64) -> Self { + self.gas_weight = ::near_sdk::GasWeight(gas_weight); + self + } +} +impl Test { + /// API for calling this contract's functions in a subsequent execution. + pub fn ext(account_id: ::near_sdk::AccountId) -> TestExt { + TestExt { + account_id, + deposit: 0, + static_gas: ::near_sdk::Gas::from_gas(0), + gas_weight: ::near_sdk::GasWeight::default(), + } + } +} + diff --git a/near-sdk-macros/src/core_impl/code_generator/snapshots/handle_no_self.snap b/near-sdk-macros/src/core_impl/code_generator/snapshots/handle_no_self.snap new file mode 100644 index 000000000..2ac7d6934 --- /dev/null +++ b/near-sdk-macros/src/core_impl/code_generator/snapshots/handle_no_self.snap @@ -0,0 +1,11 @@ +--- +source: near-sdk-macros/src/core_impl/code_generator/item_impl_info.rs +expression: pretty_print_syn_str(&actual).unwrap() +--- +#[cfg(target_arch = "wasm32")] +#[no_mangle] +pub extern "C" fn method() { + ::near_sdk::env::setup_panic_hook(); + Hello::method(); +} + diff --git a/near-sdk-macros/src/core_impl/code_generator/snapshots/handle_result_borsh.snap b/near-sdk-macros/src/core_impl/code_generator/snapshots/handle_result_borsh.snap new file mode 100644 index 000000000..8524bfed4 --- /dev/null +++ b/near-sdk-macros/src/core_impl/code_generator/snapshots/handle_result_borsh.snap @@ -0,0 +1,20 @@ +--- +source: near-sdk-macros/src/core_impl/code_generator/item_impl_info.rs +expression: pretty_print_syn_str(&actual).unwrap() +--- +#[cfg(target_arch = "wasm32")] +#[no_mangle] +pub extern "C" fn method() { + ::near_sdk::env::setup_panic_hook(); + let contract: Hello = ::near_sdk::env::state_read().unwrap_or_default(); + let result = contract.method(); + match result { + ::std::result::Result::Ok(result) => { + let result = ::near_sdk::borsh::BorshSerialize::try_to_vec(&result) + .expect("Failed to serialize the return value using Borsh."); + ::near_sdk::env::value_return(&result); + } + ::std::result::Result::Err(err) => ::near_sdk::FunctionError::panic(&err), + } +} + diff --git a/near-sdk-macros/src/core_impl/code_generator/snapshots/handle_result_init.snap b/near-sdk-macros/src/core_impl/code_generator/snapshots/handle_result_init.snap new file mode 100644 index 000000000..173e5d1e8 --- /dev/null +++ b/near-sdk-macros/src/core_impl/code_generator/snapshots/handle_result_init.snap @@ -0,0 +1,23 @@ +--- +source: near-sdk-macros/src/core_impl/code_generator/item_impl_info.rs +expression: pretty_print_syn_str(&actual).unwrap() +--- +#[cfg(target_arch = "wasm32")] +#[no_mangle] +pub extern "C" fn new() { + ::near_sdk::env::setup_panic_hook(); + if ::near_sdk::env::attached_deposit() != 0 { + ::near_sdk::env::panic_str("Method new doesn't accept deposit"); + } + if ::near_sdk::env::state_exists() { + ::near_sdk::env::panic_str("The contract has already been initialized"); + } + let contract = Hello::new(); + match contract { + ::std::result::Result::Ok(contract) => { + ::near_sdk::env::state_write(&contract); + } + ::std::result::Result::Err(err) => ::near_sdk::FunctionError::panic(&err), + } +} + diff --git a/near-sdk-macros/src/core_impl/code_generator/snapshots/handle_result_init_ignore_state.snap b/near-sdk-macros/src/core_impl/code_generator/snapshots/handle_result_init_ignore_state.snap new file mode 100644 index 000000000..612e3a934 --- /dev/null +++ b/near-sdk-macros/src/core_impl/code_generator/snapshots/handle_result_init_ignore_state.snap @@ -0,0 +1,20 @@ +--- +source: near-sdk-macros/src/core_impl/code_generator/item_impl_info.rs +expression: pretty_print_syn_str(&actual).unwrap() +--- +#[cfg(target_arch = "wasm32")] +#[no_mangle] +pub extern "C" fn new() { + ::near_sdk::env::setup_panic_hook(); + if ::near_sdk::env::attached_deposit() != 0 { + ::near_sdk::env::panic_str("Method new doesn't accept deposit"); + } + let contract = Hello::new(); + match contract { + ::std::result::Result::Ok(contract) => { + ::near_sdk::env::state_write(&contract); + } + ::std::result::Result::Err(err) => ::near_sdk::FunctionError::panic(&err), + } +} + diff --git a/near-sdk-macros/src/core_impl/code_generator/snapshots/handle_result_json.snap b/near-sdk-macros/src/core_impl/code_generator/snapshots/handle_result_json.snap new file mode 100644 index 000000000..c613a8c21 --- /dev/null +++ b/near-sdk-macros/src/core_impl/code_generator/snapshots/handle_result_json.snap @@ -0,0 +1,20 @@ +--- +source: near-sdk-macros/src/core_impl/code_generator/item_impl_info.rs +expression: pretty_print_syn_str(&actual).unwrap() +--- +#[cfg(target_arch = "wasm32")] +#[no_mangle] +pub extern "C" fn method() { + ::near_sdk::env::setup_panic_hook(); + let contract: Hello = ::near_sdk::env::state_read().unwrap_or_default(); + let result = contract.method(); + match result { + ::std::result::Result::Ok(result) => { + let result = ::near_sdk::serde_json::to_vec(&result) + .expect("Failed to serialize the return value using JSON."); + ::near_sdk::env::value_return(&result); + } + ::std::result::Result::Err(err) => ::near_sdk::FunctionError::panic(&err), + } +} + diff --git a/near-sdk-macros/src/core_impl/code_generator/snapshots/handle_result_mut.snap b/near-sdk-macros/src/core_impl/code_generator/snapshots/handle_result_mut.snap new file mode 100644 index 000000000..8beb619c1 --- /dev/null +++ b/near-sdk-macros/src/core_impl/code_generator/snapshots/handle_result_mut.snap @@ -0,0 +1,24 @@ +--- +source: near-sdk-macros/src/core_impl/code_generator/item_impl_info.rs +expression: pretty_print_syn_str(&actual).unwrap() +--- +#[cfg(target_arch = "wasm32")] +#[no_mangle] +pub extern "C" fn method() { + ::near_sdk::env::setup_panic_hook(); + if ::near_sdk::env::attached_deposit() != 0 { + ::near_sdk::env::panic_str("Method method doesn't accept deposit"); + } + let mut contract: Hello = ::near_sdk::env::state_read().unwrap_or_default(); + let result = contract.method(); + match result { + ::std::result::Result::Ok(result) => { + let result = ::near_sdk::serde_json::to_vec(&result) + .expect("Failed to serialize the return value using JSON."); + ::near_sdk::env::value_return(&result); + ::near_sdk::env::state_write(&contract); + } + ::std::result::Result::Err(err) => ::near_sdk::FunctionError::panic(&err), + } +} + diff --git a/near-sdk-macros/src/core_impl/code_generator/snapshots/init_ignore_state.snap b/near-sdk-macros/src/core_impl/code_generator/snapshots/init_ignore_state.snap new file mode 100644 index 000000000..31436940f --- /dev/null +++ b/near-sdk-macros/src/core_impl/code_generator/snapshots/init_ignore_state.snap @@ -0,0 +1,25 @@ +--- +source: near-sdk-macros/src/core_impl/code_generator/item_impl_info.rs +expression: pretty_print_syn_str(&actual).unwrap() +--- +#[cfg(target_arch = "wasm32")] +#[no_mangle] +pub extern "C" fn method() { + ::near_sdk::env::setup_panic_hook(); + if ::near_sdk::env::attached_deposit() != 0 { + ::near_sdk::env::panic_str("Method method doesn't accept deposit"); + } + #[derive(::near_sdk::serde::Deserialize)] + #[serde(crate = "::near_sdk::serde")] + struct Input { + k: u64, + } + let Input { mut k }: Input = ::near_sdk::serde_json::from_slice( + &::near_sdk::env::input() + .expect("Expected input since method has arguments."), + ) + .expect("Failed to deserialize input from JSON."); + let contract = Hello::method(&mut k); + ::near_sdk::env::state_write(&contract); +} + diff --git a/near-sdk-macros/src/core_impl/code_generator/snapshots/init_payable.snap b/near-sdk-macros/src/core_impl/code_generator/snapshots/init_payable.snap new file mode 100644 index 000000000..e68d08b88 --- /dev/null +++ b/near-sdk-macros/src/core_impl/code_generator/snapshots/init_payable.snap @@ -0,0 +1,25 @@ +--- +source: near-sdk-macros/src/core_impl/code_generator/item_impl_info.rs +expression: pretty_print_syn_str(&actual).unwrap() +--- +#[cfg(target_arch = "wasm32")] +#[no_mangle] +pub extern "C" fn method() { + ::near_sdk::env::setup_panic_hook(); + #[derive(::near_sdk::serde::Deserialize)] + #[serde(crate = "::near_sdk::serde")] + struct Input { + k: u64, + } + let Input { mut k }: Input = ::near_sdk::serde_json::from_slice( + &::near_sdk::env::input() + .expect("Expected input since method has arguments."), + ) + .expect("Failed to deserialize input from JSON."); + if ::near_sdk::env::state_exists() { + ::near_sdk::env::panic_str("The contract has already been initialized"); + } + let contract = Hello::method(&mut k); + ::near_sdk::env::state_write(&contract); +} + diff --git a/near-sdk-macros/src/core_impl/code_generator/snapshots/module_ext_gen.snap b/near-sdk-macros/src/core_impl/code_generator/snapshots/module_ext_gen.snap new file mode 100644 index 000000000..09c99787a --- /dev/null +++ b/near-sdk-macros/src/core_impl/code_generator/snapshots/module_ext_gen.snap @@ -0,0 +1,35 @@ +--- +source: near-sdk-macros/src/core_impl/code_generator/ext.rs +expression: pretty_print_syn_str(&actual).unwrap() +--- +#[must_use] +pub struct TestExt { + pub(crate) account_id: ::near_sdk::AccountId, + pub(crate) deposit: ::near_sdk::Balance, + pub(crate) static_gas: ::near_sdk::Gas, + pub(crate) gas_weight: ::near_sdk::GasWeight, +} +impl TestExt { + pub fn with_attached_deposit(mut self, amount: ::near_sdk::Balance) -> Self { + self.deposit = amount; + self + } + pub fn with_static_gas(mut self, static_gas: ::near_sdk::Gas) -> Self { + self.static_gas = static_gas; + self + } + pub fn with_unused_gas_weight(mut self, gas_weight: u64) -> Self { + self.gas_weight = ::near_sdk::GasWeight(gas_weight); + self + } +} +/// API for calling this contract's functions in a subsequent execution. +pub fn ext(account_id: ::near_sdk::AccountId) -> TestExt { + TestExt { + account_id, + deposit: 0, + static_gas: ::near_sdk::Gas::from_gas(0), + gas_weight: ::near_sdk::GasWeight::default(), + } +} + diff --git a/near-sdk-macros/src/core_impl/code_generator/snapshots/mut_owned_no_args_no_return.snap b/near-sdk-macros/src/core_impl/code_generator/snapshots/mut_owned_no_args_no_return.snap new file mode 100644 index 000000000..d5ef4602b --- /dev/null +++ b/near-sdk-macros/src/core_impl/code_generator/snapshots/mut_owned_no_args_no_return.snap @@ -0,0 +1,12 @@ +--- +source: near-sdk-macros/src/core_impl/code_generator/item_impl_info.rs +expression: pretty_print_syn_str(&actual).unwrap() +--- +#[cfg(target_arch = "wasm32")] +#[no_mangle] +pub extern "C" fn method() { + ::near_sdk::env::setup_panic_hook(); + let mut contract: Hello = ::near_sdk::env::state_read().unwrap_or_default(); + contract.method(); +} + diff --git a/near-sdk-macros/src/core_impl/code_generator/snapshots/no_args_no_return_mut.snap b/near-sdk-macros/src/core_impl/code_generator/snapshots/no_args_no_return_mut.snap new file mode 100644 index 000000000..71e181103 --- /dev/null +++ b/near-sdk-macros/src/core_impl/code_generator/snapshots/no_args_no_return_mut.snap @@ -0,0 +1,16 @@ +--- +source: near-sdk-macros/src/core_impl/code_generator/item_impl_info.rs +expression: pretty_print_syn_str(&actual).unwrap() +--- +#[cfg(target_arch = "wasm32")] +#[no_mangle] +pub extern "C" fn method() { + ::near_sdk::env::setup_panic_hook(); + if ::near_sdk::env::attached_deposit() != 0 { + ::near_sdk::env::panic_str("Method method doesn't accept deposit"); + } + let mut contract: Hello = ::near_sdk::env::state_read().unwrap_or_default(); + contract.method(); + ::near_sdk::env::state_write(&contract); +} + diff --git a/near-sdk-macros/src/core_impl/code_generator/snapshots/no_args_no_return_mut_payable.snap b/near-sdk-macros/src/core_impl/code_generator/snapshots/no_args_no_return_mut_payable.snap new file mode 100644 index 000000000..b5affc0eb --- /dev/null +++ b/near-sdk-macros/src/core_impl/code_generator/snapshots/no_args_no_return_mut_payable.snap @@ -0,0 +1,13 @@ +--- +source: near-sdk-macros/src/core_impl/code_generator/item_impl_info.rs +expression: pretty_print_syn_str(&actual).unwrap() +--- +#[cfg(target_arch = "wasm32")] +#[no_mangle] +pub extern "C" fn method() { + ::near_sdk::env::setup_panic_hook(); + let mut contract: Hello = ::near_sdk::env::state_read().unwrap_or_default(); + contract.method(); + ::near_sdk::env::state_write(&contract); +} + diff --git a/near-sdk-macros/src/core_impl/code_generator/snapshots/no_args_no_return_no_mut.snap b/near-sdk-macros/src/core_impl/code_generator/snapshots/no_args_no_return_no_mut.snap new file mode 100644 index 000000000..278f88e72 --- /dev/null +++ b/near-sdk-macros/src/core_impl/code_generator/snapshots/no_args_no_return_no_mut.snap @@ -0,0 +1,12 @@ +--- +source: near-sdk-macros/src/core_impl/code_generator/item_impl_info.rs +expression: pretty_print_syn_str(&actual).unwrap() +--- +#[cfg(target_arch = "wasm32")] +#[no_mangle] +pub extern "C" fn method() { + ::near_sdk::env::setup_panic_hook(); + let contract: Hello = ::near_sdk::env::state_read().unwrap_or_default(); + contract.method(); +} + diff --git a/near-sdk-macros/src/core_impl/code_generator/snapshots/owned_no_args_no_return_no_mut.snap b/near-sdk-macros/src/core_impl/code_generator/snapshots/owned_no_args_no_return_no_mut.snap new file mode 100644 index 000000000..278f88e72 --- /dev/null +++ b/near-sdk-macros/src/core_impl/code_generator/snapshots/owned_no_args_no_return_no_mut.snap @@ -0,0 +1,12 @@ +--- +source: near-sdk-macros/src/core_impl/code_generator/item_impl_info.rs +expression: pretty_print_syn_str(&actual).unwrap() +--- +#[cfg(target_arch = "wasm32")] +#[no_mangle] +pub extern "C" fn method() { + ::near_sdk::env::setup_panic_hook(); + let contract: Hello = ::near_sdk::env::state_read().unwrap_or_default(); + contract.method(); +} + diff --git a/near-sdk-macros/src/core_impl/code_generator/snapshots/private_method.snap b/near-sdk-macros/src/core_impl/code_generator/snapshots/private_method.snap new file mode 100644 index 000000000..34545e4fa --- /dev/null +++ b/near-sdk-macros/src/core_impl/code_generator/snapshots/private_method.snap @@ -0,0 +1,20 @@ +--- +source: near-sdk-macros/src/core_impl/code_generator/item_impl_info.rs +expression: pretty_print_syn_str(&actual).unwrap() +--- +#[cfg(target_arch = "wasm32")] +#[no_mangle] +pub extern "C" fn private_method() { + ::near_sdk::env::setup_panic_hook(); + if ::near_sdk::env::current_account_id() != ::near_sdk::env::predecessor_account_id() + { + ::near_sdk::env::panic_str("Method private_method is private"); + } + if ::near_sdk::env::attached_deposit() != 0 { + ::near_sdk::env::panic_str("Method private_method doesn't accept deposit"); + } + let mut contract: Hello = ::near_sdk::env::state_read().unwrap_or_default(); + contract.private_method(); + ::near_sdk::env::state_write(&contract); +} + diff --git a/near-sdk-macros/src/core_impl/code_generator/snapshots/serialize_with_borsh.snap b/near-sdk-macros/src/core_impl/code_generator/snapshots/serialize_with_borsh.snap new file mode 100644 index 000000000..721de724f --- /dev/null +++ b/near-sdk-macros/src/core_impl/code_generator/snapshots/serialize_with_borsh.snap @@ -0,0 +1,59 @@ +--- +source: near-sdk-macros/src/core_impl/code_generator/item_trait_info.rs +expression: pretty_print_syn_str(&actual).unwrap() +--- +pub mod test { + use super::*; + #[must_use] + pub struct TestExt { + pub(crate) account_id: ::near_sdk::AccountId, + pub(crate) deposit: ::near_sdk::Balance, + pub(crate) static_gas: ::near_sdk::Gas, + pub(crate) gas_weight: ::near_sdk::GasWeight, + } + impl TestExt { + pub fn with_attached_deposit(mut self, amount: ::near_sdk::Balance) -> Self { + self.deposit = amount; + self + } + pub fn with_static_gas(mut self, static_gas: ::near_sdk::Gas) -> Self { + self.static_gas = static_gas; + self + } + pub fn with_unused_gas_weight(mut self, gas_weight: u64) -> Self { + self.gas_weight = ::near_sdk::GasWeight(gas_weight); + self + } + } + /// API for calling this contract's functions in a subsequent execution. + pub fn ext(account_id: ::near_sdk::AccountId) -> TestExt { + TestExt { + account_id, + deposit: 0, + static_gas: ::near_sdk::Gas::from_gas(0), + gas_weight: ::near_sdk::GasWeight::default(), + } + } + impl TestExt { + pub fn test(self, v: Vec) -> ::near_sdk::Promise { + let __args = { + #[derive(::near_sdk::borsh::BorshSerialize)] + struct Input<'nearinput> { + v: &'nearinput Vec, + } + let __args = Input { v: &v }; + ::near_sdk::borsh::BorshSerialize::try_to_vec(&__args) + .expect("Failed to serialize the cross contract args using Borsh.") + }; + ::near_sdk::Promise::new(self.account_id) + .function_call_weight( + ::std::string::String::from("test"), + __args, + self.deposit, + self.static_gas, + self.gas_weight, + ) + } + } +} + diff --git a/near-sdk-macros/src/core_impl/code_generator/snapshots/simple_init.snap b/near-sdk-macros/src/core_impl/code_generator/snapshots/simple_init.snap new file mode 100644 index 000000000..e78ee5b3c --- /dev/null +++ b/near-sdk-macros/src/core_impl/code_generator/snapshots/simple_init.snap @@ -0,0 +1,28 @@ +--- +source: near-sdk-macros/src/core_impl/code_generator/item_impl_info.rs +expression: pretty_print_syn_str(&actual).unwrap() +--- +#[cfg(target_arch = "wasm32")] +#[no_mangle] +pub extern "C" fn method() { + ::near_sdk::env::setup_panic_hook(); + if ::near_sdk::env::attached_deposit() != 0 { + ::near_sdk::env::panic_str("Method method doesn't accept deposit"); + } + #[derive(::near_sdk::serde::Deserialize)] + #[serde(crate = "::near_sdk::serde")] + struct Input { + k: u64, + } + let Input { mut k }: Input = ::near_sdk::serde_json::from_slice( + &::near_sdk::env::input() + .expect("Expected input since method has arguments."), + ) + .expect("Failed to deserialize input from JSON."); + if ::near_sdk::env::state_exists() { + ::near_sdk::env::panic_str("The contract has already been initialized"); + } + let contract = Hello::method(&mut k); + ::near_sdk::env::state_write(&contract); +} + diff --git a/near-sdk-macros/src/core_impl/code_generator/snapshots/trait_implt.snap b/near-sdk-macros/src/core_impl/code_generator/snapshots/trait_implt.snap new file mode 100644 index 000000000..278f88e72 --- /dev/null +++ b/near-sdk-macros/src/core_impl/code_generator/snapshots/trait_implt.snap @@ -0,0 +1,12 @@ +--- +source: near-sdk-macros/src/core_impl/code_generator/item_impl_info.rs +expression: pretty_print_syn_str(&actual).unwrap() +--- +#[cfg(target_arch = "wasm32")] +#[no_mangle] +pub extern "C" fn method() { + ::near_sdk::env::setup_panic_hook(); + let contract: Hello = ::near_sdk::env::state_read().unwrap_or_default(); + contract.method(); +} + diff --git a/near-sdk-macros/src/core_impl/utils/mod.rs b/near-sdk-macros/src/core_impl/utils/mod.rs index 257d7251e..c51aa60bb 100644 --- a/near-sdk-macros/src/core_impl/utils/mod.rs +++ b/near-sdk-macros/src/core_impl/utils/mod.rs @@ -4,6 +4,9 @@ use syn::spanned::Spanned; use syn::token::{And, Mut}; use syn::{GenericArgument, Path, PathArguments, Signature, Type}; +#[cfg(test)] +pub mod test_helpers; + /// Checks whether the given path is literally "Result". /// Note that it won't match a fully qualified name `core::result::Result` or a type alias like /// `type StringResult = Result`. diff --git a/near-sdk-macros/src/core_impl/utils/test_helpers.rs b/near-sdk-macros/src/core_impl/utils/test_helpers.rs new file mode 100644 index 000000000..267daab66 --- /dev/null +++ b/near-sdk-macros/src/core_impl/utils/test_helpers.rs @@ -0,0 +1,29 @@ +use prettyplease; +use proc_macro2::TokenStream; +use quote::quote; + +pub fn pretty_print_syn_str(input: &TokenStream) -> syn::Result { + let input = format!("{}", quote!(#input)); + let syn_file = syn::parse_str::(&input)?; + + Ok(prettyplease::unparse(&syn_file)) +} + +// macro_rules! local_insta_assert_debug_snapshot { +// ($value:expr) => {{ + +// insta::with_settings!({prepend_module_to_snapshot => false}, { +// insta::assert_debug_snapshot!($value); +// }); +// }}; +// } + +macro_rules! local_insta_assert_snapshot { + ($value:expr) => {{ + + insta::with_settings!({prepend_module_to_snapshot => false}, { + insta::assert_snapshot!($value); + }); + }}; +} +pub(crate) use local_insta_assert_snapshot;