From f65f2307f69a85cc7857b88409fe89c2bd2f79b7 Mon Sep 17 00:00:00 2001 From: Eric Seppanen Date: Sun, 6 Jun 2021 15:10:34 -0700 Subject: add test for add_derives This test derives PartialEq for the Test struct, and then attempts to use that by calling assert_ne! on two Test instances. If the derive callback doesn't work, no PartialEq will be present and the test will fail to compile. --- bindgen-integration/build.rs | 11 +++++++++++ bindgen-integration/src/lib.rs | 9 +++++++++ 2 files changed, 20 insertions(+) diff --git a/bindgen-integration/build.rs b/bindgen-integration/build.rs index b28dcee5..fa0246c4 100644 --- a/bindgen-integration/build.rs +++ b/bindgen-integration/build.rs @@ -119,6 +119,17 @@ impl ParseCallbacks for MacroCallback { } } } + + // Test the "custom derives" capability by adding `PartialEq` to the `Test` struct. + fn add_derives(&self, name: &str) -> Vec { + if name == "Test" { + vec![ + "PartialEq".into(), + ] + } else { + vec![] + } + } } impl Drop for MacroCallback { diff --git a/bindgen-integration/src/lib.rs b/bindgen-integration/src/lib.rs index 4b288afd..f56e7259 100755 --- a/bindgen-integration/src/lib.rs +++ b/bindgen-integration/src/lib.rs @@ -267,3 +267,12 @@ fn test_homogeneous_aggregate_float_union() { assert_eq!([1., 2., 3., 4.], coord.v) } } + +#[test] +fn test_custom_derive() { + // The `add_derives` callback should have added `#[derive(PartialEq)]` + // to the `Test` struct. If it didn't, this will fail to compile. + let test1 = unsafe { bindings::Test::new(5) }; + let test2 = unsafe { bindings::Test::new(6) }; + assert_ne!(test1, test2); +} -- cgit v1.2.3