Skip to content

Commit a5add12

Browse files
committed
feat(bindgen-integration): add test for custom fn attributes
1 parent 8ce3503 commit a5add12

File tree

4 files changed

+58
-4
lines changed

4 files changed

+58
-4
lines changed

Cargo.lock

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

bindgen-integration/Cargo.toml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,9 @@ build = "build.rs"
1010
rust-version.workspace = true
1111
edition.workspace = true
1212

13+
[dependencies]
14+
syn = { workspace = true, features = ["full"] }
15+
1316
[build-dependencies]
1417
bindgen = { workspace = true, default-features = true, features = ["experimental"] }
1518
cc.workspace = true

bindgen-integration/build.rs

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
extern crate bindgen;
22

33
use bindgen::callbacks::{
4-
DeriveInfo, IntKind, MacroParsingBehavior, ParseCallbacks,
4+
AttributeItemKind, DeriveInfo, FunctionKind, IntKind, MacroParsingBehavior,
5+
ParseCallbacks,
56
};
67
use bindgen::{Builder, EnumVariation, Formatter};
78
use std::collections::HashSet;
@@ -140,10 +141,16 @@ impl ParseCallbacks for MacroCallback {
140141
info: &bindgen::callbacks::AttributeInfo<'_>,
141142
) -> Vec<String> {
142143
if info.name == "Test" {
143-
vec!["#[cfg_attr(test, derive(PartialOrd))]".into()]
144-
} else {
145-
vec![]
144+
assert!(info.kind == AttributeItemKind::Struct);
145+
return vec!["#[cfg_attr(test, derive(PartialOrd))]".into()];
146+
} else if info.name == "coord" {
147+
assert!(
148+
info.kind ==
149+
AttributeItemKind::Function(FunctionKind::Function)
150+
);
151+
return vec!["#[must_use]".into()];
146152
}
153+
return vec![];
147154
}
148155
}
149156

bindgen-integration/src/lib.rs

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -297,6 +297,49 @@ fn test_custom_derive() {
297297
assert!(!(test1 > test2));
298298
}
299299

300+
#[test]
301+
fn test_custom_fn_attribute() {
302+
use std::env;
303+
use std::fs;
304+
use std::path::Path;
305+
use syn::{parse_file, File, Item, ItemFn};
306+
307+
let out_dir =
308+
std::env::var("OUT_DIR").expect("OUT_DIR environment variable not set");
309+
let test_file_path = Path::new(&out_dir).join("test.rs");
310+
let file_content = fs::read_to_string(&test_file_path)
311+
.expect("Failed to read test.rs file");
312+
let syntax_tree: File =
313+
parse_file(&file_content).expect("Failed to parse test.rs");
314+
315+
let mut found_coord = false;
316+
let mut has_must_use = false;
317+
318+
for item in syntax_tree.items {
319+
if let Item::Fn(item_fn) = item {
320+
if item_fn.sig.ident == "coord" {
321+
found_coord = true;
322+
has_must_use = item_fn.attrs.iter().any(|attr| {
323+
if let Ok(meta) = attr.parse_meta() {
324+
meta.path().is_ident("must_use")
325+
} else {
326+
false
327+
}
328+
});
329+
}
330+
}
331+
}
332+
333+
assert!(
334+
found_coord,
335+
"The function 'coord' was not found in the source."
336+
);
337+
assert!(
338+
has_must_use,
339+
"The function 'coord' does not have the #[must_use] attribute."
340+
);
341+
}
342+
300343
#[test]
301344
fn test_custom_attributes() {
302345
// The `add_attributes` callback should have added `#[cfg_attr(test, derive(PartialOrd))])`

0 commit comments

Comments
 (0)