Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: use insta crate #1090

Merged
merged 2 commits into from
Oct 4, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions near-sdk-macros/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"]
Expand Down
157 changes: 22 additions & 135 deletions near-sdk-macros/src/core_impl/abi/abi_generator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -295,10 +295,23 @@ pub fn parse_rustdoc(attrs: &[Attribute]) -> Option<String> {
#[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();
Expand All @@ -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::<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::<IsOk>(),
})
}
};

assert_eq!(actual.to_string(), expected.to_string());

local_insta_assert_snapshot!(pretty_print_fn_body_syn_str(actual));
}

#[test]
Expand All @@ -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: <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: <IsOk as ::near_sdk::borsh::BorshSchema>::schema_container(),
})
}
};

assert_eq!(actual.to_string(), expected.to_string());
local_insta_assert_snapshot!(pretty_print_fn_body_syn_str(actual));
}

#[test]
Expand All @@ -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]
Expand All @@ -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: <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
}
};

assert_eq!(actual.to_string(), expected.to_string());
local_insta_assert_snapshot!(pretty_print_fn_body_syn_str(actual));
}

#[test]
Expand All @@ -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]
Expand All @@ -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));
}
}
Original file line number Diff line number Diff line change
@@ -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,
}

Original file line number Diff line number Diff line change
@@ -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: <IsOk as ::near_sdk::borsh::BorshSchema>::schema_container(),
}),
}

Original file line number Diff line number Diff line change
@@ -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::<IsOk>(),
}),
}

Original file line number Diff line number Diff line change
@@ -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,
}

Original file line number Diff line number Diff line change
@@ -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,
}

Original file line number Diff line number Diff line change
@@ -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::<String>(),
}),
result: ::std::option::Option::Some(::near_sdk::__private::AbiType::Json {
type_schema: gen.subschema_for::<bool>(),
}),
}

Loading
Loading