Add sanity tests
This commit is contained in:
@@ -10,3 +10,6 @@ proc-macro = true
|
||||
[dependencies]
|
||||
syn = { version = "0.15.34", features = ["full"] }
|
||||
quote = "0.6.12"
|
||||
|
||||
[dev-dependencies]
|
||||
trybuild = "1.0.4"
|
||||
@@ -10,6 +10,9 @@ pub fn wasi_common_cbindgen(attr: TokenStream, function: TokenStream) -> TokenSt
|
||||
|
||||
let function = syn::parse_macro_input!(function as syn::ItemFn);
|
||||
|
||||
// capture visibility
|
||||
let vis = &function.vis;
|
||||
|
||||
// generate C fn name prefixed with __wasi_
|
||||
let fn_ident = &function.ident;
|
||||
let concatenated = format!("__wasi_{}", fn_ident);
|
||||
@@ -54,7 +57,7 @@ pub fn wasi_common_cbindgen(attr: TokenStream, function: TokenStream) -> TokenSt
|
||||
let result = quote! {
|
||||
#function
|
||||
|
||||
pub unsafe extern "C" fn #c_fn_ident(
|
||||
#vis unsafe extern "C" fn #c_fn_ident(
|
||||
#(
|
||||
#arg_ident: #arg_type,
|
||||
)*
|
||||
|
||||
20
wasi-common-cbindgen/tests/mut_args.rs
Normal file
20
wasi-common-cbindgen/tests/mut_args.rs
Normal file
@@ -0,0 +1,20 @@
|
||||
extern crate wasi_common_cbindgen;
|
||||
|
||||
pub use wasi_common_cbindgen::wasi_common_cbindgen;
|
||||
|
||||
#[wasi_common_cbindgen]
|
||||
fn mut_args(a: &mut usize) {
|
||||
*a = *a + 1
|
||||
}
|
||||
|
||||
fn main() {
|
||||
let mut expected = Box::new(2);
|
||||
mut_args(expected.as_mut());
|
||||
let given = unsafe {
|
||||
let given = Box::new(2);
|
||||
let raw = Box::into_raw(given);
|
||||
__wasi_mut_args(raw);
|
||||
Box::from_raw(raw)
|
||||
};
|
||||
assert_eq!(*given, *expected);
|
||||
}
|
||||
12
wasi-common-cbindgen/tests/no_args.rs
Normal file
12
wasi-common-cbindgen/tests/no_args.rs
Normal file
@@ -0,0 +1,12 @@
|
||||
extern crate wasi_common_cbindgen;
|
||||
|
||||
pub use wasi_common_cbindgen::wasi_common_cbindgen;
|
||||
|
||||
#[wasi_common_cbindgen]
|
||||
fn no_args() -> u32 {
|
||||
0
|
||||
}
|
||||
|
||||
fn main() {
|
||||
assert_eq!(unsafe { __wasi_no_args() }, no_args());
|
||||
}
|
||||
20
wasi-common-cbindgen/tests/ref_args.rs
Normal file
20
wasi-common-cbindgen/tests/ref_args.rs
Normal file
@@ -0,0 +1,20 @@
|
||||
extern crate wasi_common_cbindgen;
|
||||
|
||||
pub use wasi_common_cbindgen::wasi_common_cbindgen;
|
||||
|
||||
#[wasi_common_cbindgen]
|
||||
fn ref_args(a: &usize) -> usize {
|
||||
a + 1
|
||||
}
|
||||
|
||||
fn main() {
|
||||
let a = Box::new(2);
|
||||
let expected = ref_args(a.as_ref());
|
||||
let given = unsafe {
|
||||
let raw = Box::into_raw(a);
|
||||
let res = __wasi_ref_args(raw);
|
||||
Box::from_raw(raw);
|
||||
res
|
||||
};
|
||||
assert_eq!(given, expected);
|
||||
}
|
||||
8
wasi-common-cbindgen/tests/test.rs
Normal file
8
wasi-common-cbindgen/tests/test.rs
Normal file
@@ -0,0 +1,8 @@
|
||||
#[test]
|
||||
fn tests() {
|
||||
let t = trybuild::TestCases::new();
|
||||
t.pass("tests/no_args.rs");
|
||||
t.pass("tests/val_args.rs");
|
||||
t.pass("tests/ref_args.rs");
|
||||
t.pass("tests/mut_args.rs");
|
||||
}
|
||||
12
wasi-common-cbindgen/tests/val_args.rs
Normal file
12
wasi-common-cbindgen/tests/val_args.rs
Normal file
@@ -0,0 +1,12 @@
|
||||
extern crate wasi_common_cbindgen;
|
||||
|
||||
pub use wasi_common_cbindgen::wasi_common_cbindgen;
|
||||
|
||||
#[wasi_common_cbindgen]
|
||||
fn val_args(a: usize, b: usize) -> usize {
|
||||
a + b
|
||||
}
|
||||
|
||||
fn main() {
|
||||
assert_eq!(unsafe { __wasi_val_args(1, 2) }, val_args(1, 2));
|
||||
}
|
||||
Reference in New Issue
Block a user