Skip to content

Commit e21fb3f

Browse files
committed
Ues parse_quote in tests
1 parent 6583da7 commit e21fb3f

File tree

3 files changed

+18
-22
lines changed

3 files changed

+18
-22
lines changed

pyo3-macros-backend/src/konst.rs

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -135,23 +135,22 @@ fn parse_attribute(mut attributes: &mut ConstAttributes, meta: &Meta) -> Result<
135135
mod test {
136136
use crate::konst::ConstAttributes;
137137
use quote::ToTokens;
138-
use syn::ItemConst;
138+
use syn::{parse_quote, ItemConst};
139139

140140
#[test]
141141
fn test_const_attributes() {
142-
let inputs = [
143-
("#[classattr] const MAX: u16 = 65535;", 0),
142+
let inputs: Vec<(ItemConst, usize)> = vec![
143+
(parse_quote! { #[classattr] const MAX: u16 = 65535; }, 0),
144144
(
145-
r#"#[cfg_attr(feature = "pyo3", classattr)] const MAX: u16 = 65535;"#,
145+
parse_quote! { #[cfg_attr(feature = "pyo3", classattr)] const MAX: u16 = 65535; },
146146
0,
147147
),
148148
(
149-
r#"#[cfg_attr(feature = "pyo3", other, classattr, still_other)] const MAX: u16 = 65535;"#,
149+
parse_quote! { #[cfg_attr(feature = "pyo3", other, classattr, still_other)] const MAX: u16 = 65535; },
150150
1,
151151
),
152152
];
153-
for (code, attrs_remaining) in inputs {
154-
let mut konst: ItemConst = syn::parse_str(code).unwrap();
153+
for (mut konst, attrs_remaining) in inputs {
155154
let actual = ConstAttributes::from_attrs(&mut konst.attrs).unwrap();
156155
assert!(actual.is_class_attr);
157156
assert!(actual.name.is_none());

pyo3-macros-backend/src/pyfunction.rs

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -507,15 +507,14 @@ pub(crate) fn text_signature_or_auto(
507507
#[cfg(test)]
508508
mod test {
509509
use crate::PyFunctionOptions;
510-
use syn::ImplItemMethod;
510+
use syn::{parse_quote, ImplItemMethod};
511511

512512
/// Ensure it leaves the other attr be
513513
#[test]
514514
fn test_py_function_options() {
515-
let mut meth: ImplItemMethod = syn::parse_str(
516-
r#"#[cfg_attr(feature = "pyo3", classattr)] #[pyo3(name = "bar")] fn foo() -> i32 { 5 }"#,
517-
)
518-
.unwrap();
515+
let mut meth: ImplItemMethod = parse_quote! {
516+
#[cfg_attr(feature = "pyo3", classattr)] #[pyo3(name = "bar")] fn foo() -> i32 { 5 }
517+
};
519518
let expected_attrs = vec![meth.attrs[0].clone()];
520519
let options = PyFunctionOptions::from_attrs(&mut meth.attrs).unwrap();
521520
assert_eq!(options.name.unwrap().value.0.to_string(), "bar");
@@ -525,10 +524,9 @@ mod test {
525524
/// Ensure the nested parsing works
526525
#[test]
527526
fn test_py_function_options_pyo3_in_cfg_attr() {
528-
let mut meth: ImplItemMethod = syn::parse_str(
529-
r#"#[cfg_attr(feature = "pyo3", pyo3(name = "bar"))] fn foo() -> i32 { 5 }"#,
530-
)
531-
.unwrap();
527+
let mut meth: ImplItemMethod = parse_quote! {
528+
#[cfg_attr(feature = "pyo3", pyo3(name = "bar"))] fn foo() -> i32 { 5 }
529+
};
532530
let options = PyFunctionOptions::from_attrs(&mut meth.attrs).unwrap();
533531
assert_eq!(options.name.unwrap().value.0.to_string(), "bar");
534532
assert_eq!(meth.attrs, Vec::new());

pyo3-macros-backend/src/pymethod.rs

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1353,17 +1353,16 @@ impl ToTokens for TokenGenerator {
13531353
mod test {
13541354
use crate::method::FnType;
13551355
use crate::pymethod::PyMethod;
1356-
use syn::ImplItemMethod;
1356+
use syn::{parse_quote, ImplItemMethod};
13571357

13581358
/// Ensure it parses identical whether wrapped or not
13591359
#[test]
13601360
fn test_method_attributes() {
1361-
let inputs = [
1362-
r#"#[cfg_attr(feature = "pyo3", classattr)] fn foo() -> i32 { 5 }"#,
1363-
r#"#[classattr] fn foo() -> i32 { 5 }"#,
1361+
let inputs: Vec<ImplItemMethod> = vec![
1362+
parse_quote! {#[cfg_attr(feature = "pyo3", classattr)] fn foo() -> i32 { 5 } },
1363+
parse_quote! {#[classattr] fn foo() -> i32 { 5 } },
13641364
];
1365-
for code in inputs {
1366-
let mut method: ImplItemMethod = syn::parse_str(code).unwrap();
1365+
for mut method in inputs {
13671366
let parsed =
13681367
PyMethod::parse(&mut method.sig, &mut method.attrs, Default::default()).unwrap();
13691368
assert!(matches!(parsed.spec.tp, FnType::ClassAttribute));

0 commit comments

Comments
 (0)