Skip to content

Commit 0f0ba65

Browse files
Add a failed test for the impl_attrs feature.
1 parent 6b49520 commit 0f0ba65

File tree

5 files changed

+100
-1
lines changed

5 files changed

+100
-1
lines changed

Cargo.toml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,3 +26,6 @@ quote = "1"
2626
syn = "2"
2727
proc-macro2 = { version = "1", default-features = false }
2828
proc-macro-error2 = "2"
29+
30+
[dev-dependencies]
31+
fixture = { path = "tests/fixture" }

tests/clone_getters.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ mod submodule {
5151

5252
#[derive(CloneGetters, Default)]
5353
#[get_clone]
54-
pub struct Generic<T: Clone + Default> {
54+
pub struct Generic<T: Clone + Default> {
5555
/// A doc comment.
5656
/// Multiple lines, even.
5757
private_accessible: T,

tests/fixture/Cargo.toml

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
[package]
2+
name = "fixture"
3+
edition = "2018"
4+
5+
[lib]
6+
proc-macro = true
7+
8+
[dependencies]
9+
proc-macro-error2 = "2.0.1"
10+
quote = "1.0.40"
11+
syn = { version = "2.0.100", features = ["full"] }

tests/fixture/src/lib.rs

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
use proc_macro::TokenStream;
2+
use proc_macro_error2::{abort, proc_macro_error};
3+
use quote::quote;
4+
use syn::{parse_macro_input, spanned::Spanned as _, ImplItem, ItemImpl};
5+
6+
#[proc_macro_attribute]
7+
#[proc_macro_error]
8+
pub fn add_1_to_implementation(_attr: TokenStream, item_impl: TokenStream) -> TokenStream {
9+
let input = parse_macro_input!(item_impl as ItemImpl);
10+
let attrs = &input.attrs;
11+
let name = &input.self_ty;
12+
let (generics, ty_generics, where_clause) = &input.generics.split_for_impl();
13+
let statements = &input
14+
.items
15+
.iter()
16+
.map(|item| match item {
17+
ImplItem::Fn(function) => {
18+
let func_attrs = &function.attrs;
19+
let sig = &function.sig;
20+
let body = &function.block.stmts[0];
21+
quote! {
22+
#(#func_attrs)*
23+
#sig {
24+
#body + 1
25+
}
26+
}
27+
}
28+
_ => abort!(item.span(), "Expected a method."),
29+
})
30+
.collect::<Vec<_>>();
31+
32+
quote! {
33+
#(#attrs)*
34+
impl #generics #name #ty_generics #where_clause {
35+
#(#statements)*
36+
}
37+
}
38+
.into()
39+
}

tests/impl_attrs.rs

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
use getset::{CloneGetters, CopyGetters};
2+
3+
#[derive(CopyGetters, CloneGetters)]
4+
#[getset(
5+
get_clone = "with_prefix",
6+
get_copy,
7+
impl_attrs = r#"
8+
#[add_1_to_implementation]
9+
#[cfg(target_os = "linux")]
10+
#[add_1_to_implementation]
11+
#[allow(unused)]
12+
#[add_1_to_implementation]
13+
"#
14+
)]
15+
struct Wardrobe {
16+
shirts: u8,
17+
pants: u8,
18+
}
19+
20+
#[test]
21+
fn basic() {
22+
let wardrobe = Wardrobe {
23+
shirts: 2,
24+
pants: 1,
25+
};
26+
assert_eq!(
27+
wardrobe.shirts(),
28+
5,
29+
"Function attribute not applied correctly."
30+
);
31+
assert_eq!(
32+
wardrobe.pants(),
33+
4,
34+
"Function attribute not applied correctly."
35+
);
36+
assert_eq!(
37+
wardrobe.get_shirts(),
38+
5,
39+
"Function attribute not applied correctly."
40+
);
41+
assert_eq!(
42+
wardrobe.get_pants(),
43+
4,
44+
"Function attribute not applied correctly."
45+
);
46+
}

0 commit comments

Comments
 (0)