Skip to content

Commit 91e69aa

Browse files
author
Jethro Beekman
committed
Add test infrastructure for ParseCallbacks
1 parent 84c7020 commit 91e69aa

File tree

2 files changed

+48
-12
lines changed

2 files changed

+48
-12
lines changed

tests/parse_callbacks/mod.rs

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
use bindgen::callbacks::ParseCallbacks;
2+
3+
pub fn lookup(cb: &str) -> Box<dyn ParseCallbacks> {
4+
match cb {
5+
_ => panic!("Couldn't find name ParseCallbacks: {}", cb),
6+
}
7+
}

tests/tests.rs

+41-12
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@ use std::sync::Once;
1717
mod options;
1818
use crate::options::builder_from_flags;
1919

20+
mod parse_callbacks;
21+
2022
// Run `rustfmt` on the given source string and return a tuple of the formatted
2123
// bindings, and rustfmt's stderr.
2224
fn rustfmt(source: String) -> (String, String) {
@@ -115,7 +117,7 @@ The latest `rustfmt` is required to run the `bindgen` test suite. Install
115117

116118
fn compare_generated_header(
117119
header: &PathBuf,
118-
builder: Builder,
120+
builder: BuilderState,
119121
check_roundtrip: bool,
120122
) -> Result<(), Error> {
121123
let file_name = header.file_name().ok_or(Error::new(
@@ -190,13 +192,8 @@ fn compare_generated_header(
190192
),
191193
};
192194

193-
let flags = if check_roundtrip {
194-
let mut flags = builder.command_line_flags();
195-
flags.insert(0, "bindgen".into());
196-
flags
197-
} else {
198-
vec![]
199-
};
195+
196+
let (builder, roundtrip_builder) = builder.into_builder(check_roundtrip)?;
200197

201198
// We skip the generate() error here so we get a full diff below
202199
let (actual, rustfmt_stderr) = match builder.generate() {
@@ -262,8 +259,7 @@ fn compare_generated_header(
262259
return Err(Error::new(ErrorKind::Other, "Header and binding differ! Run with BINDGEN_OVERWRITE_EXPECTED=1 in the environment to automatically overwrite the expectation or with BINDGEN_TESTS_DIFFTOOL=meld to do this manually."));
263260
}
264261

265-
if check_roundtrip {
266-
let roundtrip_builder = builder_from_flags(flags.into_iter())?.0;
262+
if let Some(roundtrip_builder) = roundtrip_builder {
267263
if let Err(e) =
268264
compare_generated_header(&header, roundtrip_builder, false)
269265
{
@@ -281,7 +277,29 @@ fn builder() -> Builder {
281277
bindgen::builder().disable_header_comment()
282278
}
283279

284-
fn create_bindgen_builder(header: &PathBuf) -> Result<Builder, Error> {
280+
struct BuilderState {
281+
builder: Builder,
282+
parse_callbacks: Vec<String>,
283+
}
284+
285+
impl BuilderState {
286+
fn into_builder(self, with_roundtrip_builder: bool) -> Result<(Builder, Option<BuilderState>), Error> {
287+
let roundtrip_builder = if with_roundtrip_builder {
288+
let mut flags = self.builder.command_line_flags();
289+
flags.insert(0, "bindgen".into());
290+
let mut builder = builder_from_flags(flags.into_iter())?.0;
291+
for parse_cb in &self.parse_callbacks {
292+
builder = builder.parse_callbacks(parse_callbacks::lookup(parse_cb));
293+
}
294+
Some(BuilderState { builder, parse_callbacks: self.parse_callbacks })
295+
} else {
296+
None
297+
};
298+
Ok((self.builder, roundtrip_builder))
299+
}
300+
}
301+
302+
fn create_bindgen_builder(header: &PathBuf) -> Result<BuilderState, Error> {
285303
#[cfg(feature = "logging")]
286304
let _ = env_logger::try_init();
287305

@@ -290,6 +308,7 @@ fn create_bindgen_builder(header: &PathBuf) -> Result<Builder, Error> {
290308

291309
// Scoop up bindgen-flags from test header
292310
let mut flags = Vec::with_capacity(2);
311+
let mut parse_callbacks = vec![];
293312

294313
for line in reader.lines() {
295314
let line = line?;
@@ -311,6 +330,12 @@ fn create_bindgen_builder(header: &PathBuf) -> Result<Builder, Error> {
311330
.map(ToString::to_string)
312331
.chain(flags)
313332
.collect();
333+
} else if line.contains("bindgen-parse-callbacks: ") {
334+
let parse_cb = line
335+
.split("bindgen-parse-callbacks: ")
336+
.last()
337+
.unwrap();
338+
parse_callbacks.push(parse_cb.to_owned());
314339
}
315340
}
316341

@@ -353,7 +378,11 @@ fn create_bindgen_builder(header: &PathBuf) -> Result<Builder, Error> {
353378
.map(ToString::to_string)
354379
.chain(flags.into_iter());
355380

356-
builder_from_flags(args).map(|(builder, _, _)| builder)
381+
let mut builder = builder_from_flags(args)?.0;
382+
for parse_cb in &parse_callbacks {
383+
builder = builder.parse_callbacks(parse_callbacks::lookup(parse_cb));
384+
}
385+
Ok(BuilderState { builder, parse_callbacks })
357386
}
358387

359388
macro_rules! test_header {

0 commit comments

Comments
 (0)