Skip to content

Commit

Permalink
Ues parse_quote in tests
Browse files Browse the repository at this point in the history
  • Loading branch information
konstin committed Dec 1, 2022
1 parent 23df95b commit d846ccd
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 22 deletions.
13 changes: 6 additions & 7 deletions pyo3-macros-backend/src/konst.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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());
Expand Down
16 changes: 7 additions & 9 deletions pyo3-macros-backend/src/pyfunction.rs
Original file line number Diff line number Diff line change
Expand Up @@ -484,15 +484,14 @@ fn type_is_pymodule(ty: &syn::Type) -> bool {
#[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");
Expand All @@ -502,10 +501,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());
Expand Down
11 changes: 5 additions & 6 deletions pyo3-macros-backend/src/pymethod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1337,17 +1337,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<ImplItemMethod> = 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));
Expand Down

0 comments on commit d846ccd

Please sign in to comment.