Skip to content

Commit

Permalink
fix(tests): use visitor to traverse syntax tree
Browse files Browse the repository at this point in the history
  • Loading branch information
oberrich committed Jan 16, 2025
1 parent a2a5359 commit 5268ae4
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 15 deletions.
2 changes: 1 addition & 1 deletion bindgen-integration/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ rust-version.workspace = true
edition.workspace = true

[dependencies]
syn = { workspace = true, features = ["full"] }
syn = { workspace = true, features = ["full", "visit"] }

[build-dependencies]
bindgen = { workspace = true, default-features = true, features = ["experimental"] }
Expand Down
44 changes: 30 additions & 14 deletions bindgen-integration/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -302,7 +302,8 @@ fn test_custom_fn_attribute() {
use std::env;
use std::fs;
use std::path::Path;
use syn::{parse_file, File, Item, ItemFn};
use syn::visit::Visit;
use syn::{parse_file, File, ForeignItem, Item, ItemForeignMod};

let out_dir =
std::env::var("OUT_DIR").expect("OUT_DIR environment variable not set");
Expand All @@ -312,27 +313,42 @@ fn test_custom_fn_attribute() {
let syntax_tree: File =
parse_file(&file_content).expect("Failed to parse test.rs");

let mut found_coord = false;
let mut has_must_use = false;

for item in syntax_tree.items {
if let Item::Fn(item_fn) = item {
if item_fn.sig.ident == "coord" {
found_coord = true;
has_must_use = item_fn
.attrs
.iter()
.any(|attr| attr.path().is_ident("must_use"));
struct FunctionVisitor {
found_coord: bool,
has_must_use: bool,
}

impl<'ast> Visit<'ast> for FunctionVisitor {
fn visit_item_foreign_mod(
&mut self,
foreign_mod: &'ast ItemForeignMod,
) {
for foreign_item in &foreign_mod.items {
if let ForeignItem::Fn(item_fn) = foreign_item {
if item_fn.sig.ident == "coord" {
self.found_coord = true;
self.has_must_use = item_fn
.attrs
.iter()
.any(|attr| attr.path().is_ident("must_use"));
}
}
}
}
}

let mut visitor = FunctionVisitor {
found_coord: false,
has_must_use: false,
};
visitor.visit_file(&syntax_tree);

assert!(
found_coord,
visitor.found_coord,
"The function 'coord' was not found in the source."
);
assert!(
has_must_use,
visitor.has_must_use,
"The function 'coord' does not have the #[must_use] attribute."
);
}
Expand Down

0 comments on commit 5268ae4

Please sign in to comment.