Skip to content

Commit

Permalink
fix: code generation for mut self methods (#616)
Browse files Browse the repository at this point in the history
  • Loading branch information
austinabell authored Nov 3, 2021
1 parent 35b46ab commit 8521e89
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 1 deletion.
37 changes: 37 additions & 0 deletions near-sdk-macros/src/core_impl/code_generator/item_impl_info.rs
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,43 @@ mod tests {
assert_eq!(expected.to_string(), actual.to_string());
}

#[test]
fn owned_no_args_no_return_no_mut() {
let impl_type: Type = syn::parse_str("Hello").unwrap();
let mut method: ImplItemMethod = syn::parse_str("pub fn method(self) { }").unwrap();
let method_info = ImplItemMethodInfo::new(&mut method, impl_type).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());
}


#[test]
fn mut_owned_no_args_no_return() {
let impl_type: Type = syn::parse_str("Hello").unwrap();
let mut method: ImplItemMethod = syn::parse_str("pub fn method(mut self) { }").unwrap();
let method_info = ImplItemMethodInfo::new(&mut method, impl_type).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());
}

#[test]
fn no_args_no_return_mut() {
let impl_type: Type = syn::parse_str("Hello").unwrap();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ impl AttrSigInfo {

if let Some(ref receiver) = receiver {
if matches!(method_type, MethodType::Regular) {
if receiver.mutability.is_none() {
if receiver.mutability.is_none() || receiver.reference.is_none() {
method_type = MethodType::View;
}
} else {
Expand Down

0 comments on commit 8521e89

Please sign in to comment.