Skip to content

Commit 77e2134

Browse files
committed
Add tests
1 parent 259f83d commit 77e2134

File tree

4 files changed

+157
-11
lines changed

4 files changed

+157
-11
lines changed

packages/schema-derive/src/cw_serde.rs

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,42 @@ mod tests {
7373
use super::*;
7474
use syn::parse_quote;
7575

76+
#[test]
77+
fn crate_rename() {
78+
let expanded = cw_serde_impl(
79+
Options {
80+
crate_path: parse_quote!(::my_crate::cw_schema),
81+
},
82+
parse_quote! {
83+
pub struct InstantiateMsg {
84+
pub verifier: String,
85+
pub beneficiary: String,
86+
}
87+
},
88+
)
89+
.unwrap();
90+
91+
let expected = parse_quote! {
92+
#[derive(
93+
::my_crate::cw_schema::serde::Serialize,
94+
::my_crate::cw_schema::serde::Deserialize,
95+
::std::clone::Clone,
96+
::std::fmt::Debug,
97+
::std::cmp::PartialEq,
98+
::my_crate::cw_schema::schemars::JsonSchema
99+
)]
100+
#[allow(clippy::derive_partial_eq_without_eq)]
101+
#[serde(deny_unknown_fields, crate = ":: my_crate :: cw_schema::serde")]
102+
#[schemars(crate = ":: my_crate :: cw_schema::schemars")]
103+
pub struct InstantiateMsg {
104+
pub verifier: String,
105+
pub beneficiary: String,
106+
}
107+
};
108+
109+
assert_eq!(expanded, expected);
110+
}
111+
76112
#[test]
77113
fn structs() {
78114
let expanded = cw_serde_impl(

packages/schema-derive/src/generate_api.rs

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -233,6 +233,32 @@ impl Parse for Options {
233233
mod tests {
234234
use super::*;
235235

236+
#[test]
237+
fn crate_rename() {
238+
assert_eq!(
239+
generate_api_impl(&parse_quote! {
240+
crate_name: ::my_crate::cw_schema,
241+
instantiate: InstantiateMsg,
242+
execute: ExecuteMsg,
243+
query: QueryMsg,
244+
migrate: MigrateMsg,
245+
sudo: SudoMsg,
246+
}),
247+
parse_quote! {
248+
::my_crate::cw_schema::Api {
249+
contract_name: ::std::env!("CARGO_PKG_NAME").to_string(),
250+
contract_version: ::std::env!("CARGO_PKG_VERSION").to_string(),
251+
instantiate: Some(::my_crate::cw_schema::schema_for!(InstantiateMsg)),
252+
execute: Some(::my_crate::cw_schema::schema_for!(ExecuteMsg)),
253+
query: Some(::my_crate::cw_schema::schema_for!(QueryMsg)),
254+
migrate: Some(::my_crate::cw_schema::schema_for!(MigrateMsg)),
255+
sudo: Some(::my_crate::cw_schema::schema_for!(SudoMsg)),
256+
responses: Some(<QueryMsg as ::my_crate::cw_schema::QueryResponses>::response_schemas().unwrap()),
257+
}
258+
}
259+
);
260+
}
261+
236262
#[test]
237263
fn api_object_minimal() {
238264
assert_eq!(

packages/schema-derive/src/query_responses.rs

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -162,6 +162,69 @@ mod tests {
162162
}
163163
}
164164

165+
#[test]
166+
fn crate_rename() {
167+
let input: ItemEnum = parse_quote! {
168+
#[derive(Serialize, Deserialize, Clone, Debug, PartialEq, JsonSchema, QueryResponses)]
169+
#[serde(rename_all = "snake_case")]
170+
#[query_responses(crate = "::my_crate::cw_schema")]
171+
pub enum QueryMsg {
172+
#[returns(some_crate::AnotherType)]
173+
Supply {},
174+
#[returns(SomeType)]
175+
Balance {},
176+
}
177+
};
178+
179+
assert_eq!(
180+
query_responses_derive_impl(input).unwrap(),
181+
parse_quote! {
182+
#[automatically_derived]
183+
#[cfg(not(target_arch = "wasm32"))]
184+
impl ::my_crate::cw_schema::QueryResponses for QueryMsg {
185+
fn response_schemas_impl() -> ::std::collections::BTreeMap<String, ::my_crate::cw_schema::schemars::schema::RootSchema> {
186+
::std::collections::BTreeMap::from([
187+
("supply".to_string(), ::my_crate::cw_schema::schema_for!(some_crate::AnotherType)),
188+
("balance".to_string(), ::my_crate::cw_schema::schema_for!(SomeType)),
189+
])
190+
}
191+
}
192+
}
193+
);
194+
}
195+
196+
#[test]
197+
fn crate_rename_nested() {
198+
let input: ItemEnum = parse_quote! {
199+
#[derive(Serialize, Deserialize, Clone, Debug, PartialEq, JsonSchema, QueryResponses)]
200+
#[serde(untagged)]
201+
#[query_responses(crate = "::my_crate::cw_schema", nested)]
202+
pub enum ContractQueryMsg {
203+
Cw1(QueryMsg1),
204+
Whitelist(whitelist::QueryMsg),
205+
Cw1WhitelistContract(QueryMsg),
206+
}
207+
};
208+
let result = query_responses_derive_impl(input).unwrap();
209+
assert_eq!(
210+
result,
211+
parse_quote! {
212+
#[automatically_derived]
213+
#[cfg(not(target_arch = "wasm32"))]
214+
impl ::my_crate::cw_schema::QueryResponses for ContractQueryMsg {
215+
fn response_schemas_impl() -> ::std::collections::BTreeMap<String, ::my_crate::cw_schema::schemars::schema::RootSchema> {
216+
let subqueries = [
217+
<QueryMsg1 as ::my_crate::cw_schema::QueryResponses>::response_schemas_impl(),
218+
<whitelist::QueryMsg as ::my_crate::cw_schema::QueryResponses>::response_schemas_impl(),
219+
<QueryMsg as ::my_crate::cw_schema::QueryResponses>::response_schemas_impl(),
220+
];
221+
::my_crate::cw_schema::combine_subqueries::<3usize, ContractQueryMsg>(subqueries)
222+
}
223+
}
224+
}
225+
);
226+
}
227+
165228
#[test]
166229
fn happy_path() {
167230
let input: ItemEnum = parse_quote! {

packages/schema-derive/src/query_responses/context.rs

Lines changed: 32 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -31,23 +31,16 @@ pub fn get_context(input: &ItemEnum) -> syn::Result<Context> {
3131
let meta_list = attr.meta.require_list()?;
3232
meta_list.parse_nested_meta(|param| {
3333
if param.path.is_ident("no_bounds_for") {
34-
let meta_list: syn::MetaList = param.input.parse()?;
35-
meta_list.parse_nested_meta(|item| {
36-
let syn::Meta::Path(p) = item.input.parse()? else {
37-
bail!(
38-
item.input.span(),
39-
"`no_bounds_for` only accepts a list of type params"
40-
);
41-
};
42-
43-
ctx.no_bounds_for.insert(p.get_ident().unwrap().clone());
34+
param.parse_nested_meta(|item| {
35+
ctx.no_bounds_for
36+
.insert(item.path.get_ident().unwrap().clone());
4437

4538
Ok(())
4639
})?;
4740
} else if param.path.is_ident("nested") {
4841
ctx.is_nested = true;
4942
} else if param.path.is_ident("crate") {
50-
let crate_name_str: LitStr = param.input.parse()?;
43+
let crate_name_str: LitStr = param.value()?.parse()?;
5144
ctx.crate_name = crate_name_str.parse()?;
5245
} else {
5346
bail!(param.path, "unrecognized QueryResponses param");
@@ -59,3 +52,31 @@ pub fn get_context(input: &ItemEnum) -> syn::Result<Context> {
5952

6053
Ok(ctx)
6154
}
55+
56+
#[cfg(test)]
57+
mod test {
58+
use std::collections::HashSet;
59+
60+
use quote::format_ident;
61+
use syn::parse_quote;
62+
63+
use super::get_context;
64+
65+
#[test]
66+
fn parse_context() {
67+
let input = parse_quote! {
68+
#[query_responses(crate = "::my_crate::cw_schema")]
69+
#[query_responses(nested)]
70+
#[query_responses(no_bounds_for(Item1, Item2))]
71+
enum Test {}
72+
};
73+
let context = get_context(&input).unwrap();
74+
75+
assert_eq!(context.crate_name, parse_quote!(::my_crate::cw_schema));
76+
assert!(context.is_nested);
77+
assert_eq!(
78+
context.no_bounds_for,
79+
HashSet::from([format_ident!("Item1"), format_ident!("Item2")])
80+
);
81+
}
82+
}

0 commit comments

Comments
 (0)