|
| 1 | +#[cfg(test)] |
| 2 | +mod tests { |
| 3 | + use bindgen::callbacks::{DeriveInfo, ParseCallbacks}; |
| 4 | + use bindgen::{Bindings, Builder}; |
| 5 | + use std::path::PathBuf; |
| 6 | + |
| 7 | + #[derive(Debug)] |
| 8 | + struct AddDerivesCallback(Vec<String>); |
| 9 | + |
| 10 | + impl ParseCallbacks for AddDerivesCallback { |
| 11 | + fn add_derives(&self, _info: &DeriveInfo<'_>) -> Vec<String> { |
| 12 | + self.0.clone() |
| 13 | + } |
| 14 | + } |
| 15 | + |
| 16 | + struct WriteAdapter<'a>(&'a mut Vec<u8>); |
| 17 | + |
| 18 | + impl std::io::Write for WriteAdapter<'_> { |
| 19 | + fn write(&mut self, buf: &[u8]) -> std::io::Result<usize> { |
| 20 | + self.0.extend_from_slice(buf); |
| 21 | + Ok(buf.len()) |
| 22 | + } |
| 23 | + fn flush(&mut self) -> std::io::Result<()> { |
| 24 | + Ok(()) |
| 25 | + } |
| 26 | + } |
| 27 | + |
| 28 | + fn write_bindings_to_string(bindings: &Bindings) -> String { |
| 29 | + let mut output = Vec::<u8>::new(); |
| 30 | + bindings |
| 31 | + .write(Box::new(WriteAdapter(&mut output))) |
| 32 | + .expect("Failed to write generated bindings"); |
| 33 | + String::from_utf8(output) |
| 34 | + .expect("Failed to convert generated bindings to string") |
| 35 | + } |
| 36 | + |
| 37 | + /// Tests that adding a derive trait that's already derived automatically |
| 38 | + /// does not result in a duplicate derive trait (which would not compile). |
| 39 | + #[test] |
| 40 | + fn test_add_derives_callback_dedupe() { |
| 41 | + let crate_dir = |
| 42 | + PathBuf::from(std::env::var("CARGO_MANIFEST_DIR").unwrap()); |
| 43 | + let header_path = crate_dir.join( |
| 44 | + "tests/parse_callbacks/add_derives_callback/header_add_derives.h", |
| 45 | + ); |
| 46 | + |
| 47 | + let builder = Builder::default() |
| 48 | + .header(header_path.display().to_string()) |
| 49 | + .derive_debug(true) |
| 50 | + .derive_copy(false) |
| 51 | + .derive_default(false) |
| 52 | + .derive_partialeq(false) |
| 53 | + .derive_eq(false) |
| 54 | + .derive_partialord(false) |
| 55 | + .derive_ord(false) |
| 56 | + .derive_hash(false) |
| 57 | + .parse_callbacks(Box::new(AddDerivesCallback(vec![ |
| 58 | + "Debug".to_string() |
| 59 | + ]))); |
| 60 | + let bindings = builder.generate().expect("Failed to generate bindings"); |
| 61 | + let output = write_bindings_to_string(&bindings); |
| 62 | + assert!( |
| 63 | + output.contains("#[derive(Debug)]") && |
| 64 | + !output.contains("#[derive(Debug, Debug)]"), |
| 65 | + "Unexpected bindgen output:\n{}", |
| 66 | + output.as_str() |
| 67 | + ); |
| 68 | + } |
| 69 | + |
| 70 | + /// Tests that adding a derive trait that's not already derived automatically |
| 71 | + /// adds it to the end of the derive list. |
| 72 | + #[test] |
| 73 | + fn test_add_derives_callback() { |
| 74 | + let crate_dir = |
| 75 | + PathBuf::from(std::env::var("CARGO_MANIFEST_DIR").unwrap()); |
| 76 | + let header_path = crate_dir.join( |
| 77 | + "tests/parse_callbacks/add_derives_callback/header_add_derives.h", |
| 78 | + ); |
| 79 | + |
| 80 | + let builder = Builder::default() |
| 81 | + .header(header_path.display().to_string()) |
| 82 | + .derive_debug(true) |
| 83 | + .derive_copy(false) |
| 84 | + .derive_default(false) |
| 85 | + .derive_partialeq(false) |
| 86 | + .derive_eq(false) |
| 87 | + .derive_partialord(false) |
| 88 | + .derive_ord(false) |
| 89 | + .derive_hash(false) |
| 90 | + .parse_callbacks(Box::new(AddDerivesCallback(vec![ |
| 91 | + "Default".to_string() |
| 92 | + ]))); |
| 93 | + let bindings = builder.generate().expect("Failed to generate bindings"); |
| 94 | + let output = write_bindings_to_string(&bindings); |
| 95 | + assert!( |
| 96 | + output.contains("#[derive(Debug, Default)]"), |
| 97 | + "Unexpected bindgen output:\n{}", |
| 98 | + output.as_str() |
| 99 | + ); |
| 100 | + } |
| 101 | +} |
0 commit comments