Commit 0aada672 authored by Ryan Hunt's avatar Ryan Hunt
Browse files

Add a test harness

Fixes #3
parent 38db2e29
......@@ -6,3 +6,11 @@ rust:
matrix:
allow_failures:
- rust: nightly
addons:
apt:
packages:
- g++
script:
- cargo build --verbose
- cargo test --verbose
- python run-tests.py
\ No newline at end of file
[root]
name = "cbindgen"
version = "0.1.7"
version = "0.1.8"
dependencies = [
"clap 2.23.2 (registry+https://github.com/rust-lang/crates.io-index)",
"log 0.3.7 (registry+https://github.com/rust-lang/crates.io-index)",
......
......@@ -47,7 +47,7 @@ fn main() {
## Examples
See `samples/` for some examples of rust source that can be handled.
See `tests/` for some examples of rust source that can be handled.
## How it works
......
#!/bin/python
import os
import glob
import subprocess
import sys
def cbindgen(rust_src, out):
subprocess.check_output(["cargo", "run", "--", rust_src, "-o", out])
def gxx(src):
subprocess.check_output(["g++", "-c", src, "-o", "tests/tmp.o"])
os.remove("tests/tmp.o")
def run_compile_test(rust_src, leave_output):
out = rust_src.replace(".rs", ".cpp")
try:
cbindgen(rust_src, out)
gxx(out)
if not leave_output:
os.remove(out)
except subprocess.CalledProcessError:
if not leave_output and os.path.exists(out):
os.remove(out)
return False
return True
tests = glob.glob("tests/*.rs")
num_pass = 0
num_fail = 0
leave_output = False
if len(sys.argv) == 2 and sys.argv[1] == "-l":
leave_output = True
for test in tests:
if run_compile_test(test, leave_output):
num_pass += 1
print("Pass - %s" % test)
else:
num_fail += 1
print("Fail - %s" % test)
print("Tests complete. %i passed, %i failed." % (num_pass, num_fail))
[package]
name = "build-script"
version = "0.1.0"
authors = ["Ryan Hunt <rhunt@eqrion.net>"]
build = "build.rs"
[build-dependencies]
cbindgen = { path = "../../" }
extern crate cbindgen;
use std::env;
use cbindgen::{Config, Library};
fn main() {
let root = env::var("CARGO_MANIFEST_DIR").unwrap();
let config = Config::from_root_or_default(&root);
Library::load(&root, &config)
.generate().unwrap()
.write_to_file("bindings.h");
}
include_guard = "build_script_h"
include_version = true
braces = "NextLine"
tab_width = 4
language = "C++"
[enum]
rename_variants = "SnakeCase"
[fn]
rename_args = "UpperCase"
args = "Vertical"
[struct]
rename_fields = "GeckoCase"
derive_eq = true
derive_lt = true
derive_lte = true
#![allow(dead_code)]
#![allow(unused_variables)]
#[repr(u32)]
pub enum Options {
First,
Second,
LastOne,
}
pub struct Opaque {
x: i32,
y: f32,
}
/// cbindgen:rename-all=UpperCase
#[repr(C)]
pub struct Normal {
x: i32,
y: f32,
}
#[repr(C)]
pub struct Comparable {
x: i32,
}
#[repr(C)]
pub struct TupleStruct(i32, f32);
#[no_mangle]
pub extern "C" fn root(x: *mut Opaque,
y: Normal,
z: Comparable,
w: Options,
a: TupleStruct)
{
}
#[repr(C)]
struct Dep
{
struct Dep {
a: i32,
b: f32,
}
#[repr(C)]
struct Foo<X>
{
struct Foo<X> {
a: X,
b: X,
c: Dep,
}
#[repr(u32)]
enum Status
{
enum Status {
Ok,
Err,
}
......@@ -27,7 +24,8 @@ type Unit = i32;
type SpecialStatus = Status;
#[no_mangle]
extern "C" fn root(x: IntFoo, y: DoubleFoo, z: Unit, w: SpecialStatus)
{
}
extern "C" fn root(x: IntFoo,
y: DoubleFoo,
z: Unit,
w: SpecialStatus)
{ }
/// cbindgen:struct-gen-op-lt=true
/// cbindgen:struct-gen-op-lte=true
/// cbindgen:derive-lt=true
/// cbindgen:derive-lte=true
/// cbindgen:rename-all=GeckoCase
#[repr(C)]
struct A(i32);
......@@ -7,15 +8,15 @@ struct A(i32);
#[repr(C)]
struct B(i32, f32);
/// cbindgen:enum-trailing-values=[Z, W]
/// cbindgen:trailing-values=[Z, W]
#[repr(u32)]
enum C {
X = 2,
Y,
}
/// cbindgen:function-prefix=PREFIX
/// cbindgen:function-postfix=PREFIX
#[no_mangle]
extern "C" fn root(x: A, y: B, z: C) {
}
extern "C" fn root(x: A,
y: B,
z: C)
{ }
......@@ -28,7 +28,8 @@ enum C {
}
#[no_mangle]
extern "C" fn root(x: *mut Opaque, y: A, z: B, w: C)
{
}
extern "C" fn root(x: *mut Opaque,
y: A,
z: B,
w: C)
{ }
type Callback = fn (i32, i32) -> bool;
#[no_mangle]
extern "C" fn root(x: fn(), y: Callback)
{
}
extern "C" fn root(x: fn(),
y: Callback)
{ }
......@@ -9,15 +9,17 @@ struct Normal {
y: f32,
}
/// cbindgen:rename-all=GeckoCase
#[repr(C)]
struct Tuple(i32, f32);
struct TupleRenamed(i32, f32);
/// cbindgen:field-names=[x, y]
#[repr(C)]
struct TupleNamed(i32, f32);
#[no_mangle]
extern "C" fn root(x: *mut Opaque, y: Normal, z: Tuple, w: TupleNamed)
{
}
extern "C" fn root(x: *mut Opaque,
y: Normal,
z: TupleRenamed,
w: TupleNamed)
{ }
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment