From 8fdf7fb6d3d9b84574c1538aa4962bf822c26cf0 Mon Sep 17 00:00:00 2001 From: konstin Date: Thu, 1 Dec 2022 21:18:57 +0100 Subject: [PATCH] Ues parse_quote in tests --- pyo3-macros-backend/src/konst.rs | 13 ++++++------- pyo3-macros-backend/src/pyfunction.rs | 16 +++++++--------- pyo3-macros-backend/src/pymethod.rs | 11 +++++------ 3 files changed, 18 insertions(+), 22 deletions(-) diff --git a/pyo3-macros-backend/src/konst.rs b/pyo3-macros-backend/src/konst.rs index ee95eb7f7c0..83be903d00b 100644 --- a/pyo3-macros-backend/src/konst.rs +++ b/pyo3-macros-backend/src/konst.rs @@ -135,23 +135,22 @@ fn parse_attribute(mut attributes: &mut ConstAttributes, meta: &Meta) -> Result< mod test { use crate::konst::ConstAttributes; use quote::ToTokens; - use syn::ItemConst; + use syn::{parse_quote, ItemConst}; #[test] fn test_const_attributes() { - let inputs = [ - ("#[classattr] const MAX: u16 = 65535;", 0), + let inputs: Vec<(ItemConst, usize)> = vec![ + (parse_quote! { #[classattr] const MAX: u16 = 65535; }, 0), ( - r#"#[cfg_attr(feature = "pyo3", classattr)] const MAX: u16 = 65535;"#, + parse_quote! { #[cfg_attr(feature = "pyo3", classattr)] const MAX: u16 = 65535; }, 0, ), ( - r#"#[cfg_attr(feature = "pyo3", other, classattr, still_other)] const MAX: u16 = 65535;"#, + parse_quote! { #[cfg_attr(feature = "pyo3", other, classattr, still_other)] const MAX: u16 = 65535; }, 1, ), ]; - for (code, attrs_remaining) in inputs { - let mut konst: ItemConst = syn::parse_str(code).unwrap(); + for (mut konst, attrs_remaining) in inputs { let actual = ConstAttributes::from_attrs(&mut konst.attrs).unwrap(); assert!(actual.is_class_attr); assert!(actual.name.is_none()); diff --git a/pyo3-macros-backend/src/pyfunction.rs b/pyo3-macros-backend/src/pyfunction.rs index 6b1e61d6146..297bbad77cb 100644 --- a/pyo3-macros-backend/src/pyfunction.rs +++ b/pyo3-macros-backend/src/pyfunction.rs @@ -507,15 +507,14 @@ pub(crate) fn text_signature_or_auto( #[cfg(test)] mod test { use crate::PyFunctionOptions; - use syn::ImplItemMethod; + use syn::{parse_quote, ImplItemMethod}; /// Ensure it leaves the other attr be #[test] fn test_py_function_options() { - let mut meth: ImplItemMethod = syn::parse_str( - r#"#[cfg_attr(feature = "pyo3", classattr)] #[pyo3(name = "bar")] fn foo() -> i32 { 5 }"#, - ) - .unwrap(); + let mut meth: ImplItemMethod = parse_quote! { + #[cfg_attr(feature = "pyo3", classattr)] #[pyo3(name = "bar")] fn foo() -> i32 { 5 } + }; let expected_attrs = vec![meth.attrs[0].clone()]; let options = PyFunctionOptions::from_attrs(&mut meth.attrs).unwrap(); assert_eq!(options.name.unwrap().value.0.to_string(), "bar"); @@ -525,10 +524,9 @@ mod test { /// Ensure the nested parsing works #[test] fn test_py_function_options_pyo3_in_cfg_attr() { - let mut meth: ImplItemMethod = syn::parse_str( - r#"#[cfg_attr(feature = "pyo3", pyo3(name = "bar"))] fn foo() -> i32 { 5 }"#, - ) - .unwrap(); + let mut meth: ImplItemMethod = parse_quote! { + #[cfg_attr(feature = "pyo3", pyo3(name = "bar"))] fn foo() -> i32 { 5 } + }; let options = PyFunctionOptions::from_attrs(&mut meth.attrs).unwrap(); assert_eq!(options.name.unwrap().value.0.to_string(), "bar"); assert_eq!(meth.attrs, Vec::new()); diff --git a/pyo3-macros-backend/src/pymethod.rs b/pyo3-macros-backend/src/pymethod.rs index c42be734749..f5b0797caf7 100644 --- a/pyo3-macros-backend/src/pymethod.rs +++ b/pyo3-macros-backend/src/pymethod.rs @@ -1383,17 +1383,16 @@ impl ToTokens for TokenGenerator { mod test { use crate::method::FnType; use crate::pymethod::PyMethod; - use syn::ImplItemMethod; + use syn::{parse_quote, ImplItemMethod}; /// Ensure it parses identical whether wrapped or not #[test] fn test_method_attributes() { - let inputs = [ - r#"#[cfg_attr(feature = "pyo3", classattr)] fn foo() -> i32 { 5 }"#, - r#"#[classattr] fn foo() -> i32 { 5 }"#, + let inputs: Vec = vec![ + parse_quote! {#[cfg_attr(feature = "pyo3", classattr)] fn foo() -> i32 { 5 } }, + parse_quote! {#[classattr] fn foo() -> i32 { 5 } }, ]; - for code in inputs { - let mut method: ImplItemMethod = syn::parse_str(code).unwrap(); + for mut method in inputs { let parsed = PyMethod::parse(&mut method.sig, &mut method.attrs, Default::default()).unwrap(); assert!(matches!(parsed.spec.tp, FnType::ClassAttribute));