Skip to content

Commit af98fd4

Browse files
Jethro Beekmanemilio
Jethro Beekman
authored andcommitted
Add test infrastructure for ParseCallbacks
1 parent dedbea5 commit af98fd4

File tree

2 files changed

+55
-12
lines changed

2 files changed

+55
-12
lines changed

tests/parse_callbacks/mod.rs

Lines changed: 7 additions & 0 deletions
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

Lines changed: 48 additions & 12 deletions
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,7 @@ 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+
let (builder, roundtrip_builder) = builder.into_builder(check_roundtrip)?;
200196

201197
// We skip the generate() error here so we get a full diff below
202198
let (actual, rustfmt_stderr) = match builder.generate() {
@@ -262,8 +258,7 @@ fn compare_generated_header(
262258
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."));
263259
}
264260

265-
if check_roundtrip {
266-
let roundtrip_builder = builder_from_flags(flags.into_iter())?.0;
261+
if let Some(roundtrip_builder) = roundtrip_builder {
267262
if let Err(e) =
268263
compare_generated_header(&header, roundtrip_builder, false)
269264
{
@@ -281,7 +276,36 @@ fn builder() -> Builder {
281276
bindgen::builder().disable_header_comment()
282277
}
283278

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

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

291315
// Scoop up bindgen-flags from test header
292316
let mut flags = Vec::with_capacity(2);
317+
let mut parse_callbacks = None;
293318

294319
for line in reader.lines() {
295320
let line = line?;
@@ -311,6 +336,10 @@ fn create_bindgen_builder(header: &PathBuf) -> Result<Builder, Error> {
311336
.map(ToString::to_string)
312337
.chain(flags)
313338
.collect();
339+
} else if line.contains("bindgen-parse-callbacks: ") {
340+
let parse_cb =
341+
line.split("bindgen-parse-callbacks: ").last().unwrap();
342+
parse_callbacks = Some(parse_cb.to_owned());
314343
}
315344
}
316345

@@ -353,7 +382,14 @@ fn create_bindgen_builder(header: &PathBuf) -> Result<Builder, Error> {
353382
.map(ToString::to_string)
354383
.chain(flags.into_iter());
355384

356-
builder_from_flags(args).map(|(builder, _, _)| builder)
385+
let mut builder = builder_from_flags(args)?.0;
386+
if let Some(ref parse_cb) = parse_callbacks {
387+
builder = builder.parse_callbacks(parse_callbacks::lookup(&parse_cb));
388+
}
389+
Ok(BuilderState {
390+
builder,
391+
parse_callbacks,
392+
})
357393
}
358394

359395
macro_rules! test_header {

0 commit comments

Comments
 (0)