tests: Provide style definition to the C / C++ tests.

This will be useful to add static assertion to some tests like #463 wants to do.

For example, in the case of the test for #463, the test would need something
like:

    trailer = """
    #include <assert.h>

    #if defined(CBINDGEN_STYLE_TAG) && !defined(__cplusplus)
    static_assert(sizeof(struct P) == 4, "unexpected size for P");
    #else
    static_assert(sizeof(P) == 4, "unexpected size for P");
    #endif
    """

As more of these tests are added it may be worth just adding a helper header
like this to avoid some duplication:

    #include <assert.h>
    #if defined(CBINDGEN_STYLE_TAG) && !defined(__cplusplus)
    #define CBINDGEN_STRUCT(name) struct name
    #else
    #define CBINDGEN_STRUCT(name) name
    #endif

And so on, so the previous configuration would become just:

    trailer = """
    #include "testing-helpers.h" // Or whatever
    static_assert(sizeof(CBINDGEN_STRUCT(P)) == 4, "unexpected size for P");
    """

That may or may not be overkill to do as part of #463.
This commit is contained in:
Emilio Cobos Álvarez 2020-01-26 01:57:37 +01:00
parent ec18611e58
commit bd831ded19

View File

@ -5,6 +5,14 @@ use std::path::Path;
use std::process::Command;
use std::{env, fs, str};
fn style_str(style: Style) -> &'static str {
match style {
Style::Both => "both",
Style::Tag => "tag",
Style::Type => "type",
}
}
fn run_cbindgen(
cbindgen_path: &'static str,
path: &Path,
@ -27,12 +35,7 @@ fn run_cbindgen(
}
if let Some(style) = style {
command.arg("--style");
command.arg(match style {
Style::Both => "both",
Style::Tag => "tag",
Style::Type => "type",
});
command.arg("--style").arg(style_str(style));
}
command.arg("-o").arg(output);
@ -59,7 +62,7 @@ fn run_cbindgen(
);
}
fn compile(cbindgen_output: &Path, language: Language) {
fn compile(cbindgen_output: &Path, language: Language, style: Option<Style>) {
let cc = match language {
Language::Cxx => env::var("CXX").unwrap_or_else(|_| "g++".to_owned()),
Language::C => env::var("CC").unwrap_or_else(|_| "gcc".to_owned()),
@ -82,6 +85,14 @@ fn compile(cbindgen_output: &Path, language: Language) {
command.arg("-std=c++17");
}
if let Some(style) = style {
command.arg("-D");
command.arg(format!(
"CBINDGEN_STYLE_{}",
style_str(style).to_uppercase()
));
}
println!("Running: {:?}", command);
let out = command.output().expect("failed to compile");
assert!(out.status.success(), "Output failed to compile: {:?}", out);
@ -127,10 +138,10 @@ fn run_compile_test(
output.push(format!("{}.{}", name, ext));
run_cbindgen(cbindgen_path, path, &output, language, cpp_compat, style);
compile(&output, language);
compile(&output, language, style);
if language == Language::C && cpp_compat {
compile(&output, Language::Cxx)
compile(&output, Language::Cxx, style)
}
}