first pass at splitting out a test, making ctx/errno reusable
This commit is contained in:
34
tests/ctx.rs
Normal file
34
tests/ctx.rs
Normal file
@@ -0,0 +1,34 @@
|
||||
use wiggle_runtime::GuestError;
|
||||
|
||||
pub struct WasiCtx {
|
||||
pub guest_errors: Vec<GuestError>,
|
||||
}
|
||||
|
||||
impl WasiCtx {
|
||||
pub fn new() -> Self {
|
||||
Self {
|
||||
guest_errors: vec![],
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Errno is used as a first return value in the functions above, therefore
|
||||
// it must implement GuestErrorType with type Context = WasiCtx.
|
||||
// The context type should let you do logging or debugging or whatever you need
|
||||
// with these errors. We just push them to vecs.
|
||||
#[macro_export]
|
||||
macro_rules! impl_errno {
|
||||
( $errno:ty ) => {
|
||||
impl wiggle_runtime::GuestErrorType for $errno {
|
||||
type Context = WasiCtx;
|
||||
fn success() -> $errno {
|
||||
<$errno>::Ok
|
||||
}
|
||||
fn from_error(e: GuestError, ctx: &mut WasiCtx) -> $errno {
|
||||
eprintln!("GUEST ERROR: {:?}", e);
|
||||
ctx.guest_errors.push(e);
|
||||
types::Errno::InvalidArg
|
||||
}
|
||||
}
|
||||
};
|
||||
}
|
||||
8
tests/errno.witx
Normal file
8
tests/errno.witx
Normal file
@@ -0,0 +1,8 @@
|
||||
(typename $errno
|
||||
(enum u32
|
||||
$ok
|
||||
$invalid_arg
|
||||
$dont_want_to
|
||||
$physically_unable
|
||||
$picket_line))
|
||||
|
||||
@@ -11,24 +11,12 @@ wiggle_generate::from_witx!({
|
||||
ctx: WasiCtx,
|
||||
});
|
||||
|
||||
pub struct WasiCtx {
|
||||
guest_errors: Vec<GuestError>,
|
||||
}
|
||||
mod ctx;
|
||||
use ctx::WasiCtx;
|
||||
|
||||
impl WasiCtx {
|
||||
pub fn new() -> Self {
|
||||
Self {
|
||||
guest_errors: vec![],
|
||||
}
|
||||
}
|
||||
}
|
||||
impl_errno!(types::Errno);
|
||||
|
||||
impl foo::Foo for WasiCtx {
|
||||
fn bar(&mut self, an_int: u32, an_float: f32) -> Result<(), types::Errno> {
|
||||
println!("BAR: {} {}", an_int, an_float);
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn baz(
|
||||
&mut self,
|
||||
input1: types::Excuse,
|
||||
@@ -154,22 +142,6 @@ impl foo::Foo for WasiCtx {
|
||||
Ok(res)
|
||||
}
|
||||
}
|
||||
// Errno is used as a first return value in the functions above, therefore
|
||||
// it must implement GuestErrorType with type Context = WasiCtx.
|
||||
// The context type should let you do logging or debugging or whatever you need
|
||||
// with these errors. We just push them to vecs.
|
||||
impl GuestErrorType for types::Errno {
|
||||
type Context = WasiCtx;
|
||||
fn success() -> types::Errno {
|
||||
types::Errno::Ok
|
||||
}
|
||||
fn from_error(e: GuestError, ctx: &mut WasiCtx) -> types::Errno {
|
||||
eprintln!("GUEST ERROR: {:?}", e);
|
||||
ctx.guest_errors.push(e);
|
||||
types::Errno::InvalidArg
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug)]
|
||||
struct BatExercise {
|
||||
pub input: u32,
|
||||
|
||||
@@ -1,10 +1,4 @@
|
||||
(typename $errno
|
||||
(enum u32
|
||||
$ok
|
||||
$invalid_arg
|
||||
$dont_want_to
|
||||
$physically_unable
|
||||
$picket_line))
|
||||
(use "errno.witx")
|
||||
|
||||
(typename $excuse
|
||||
(enum u8
|
||||
@@ -44,10 +38,6 @@
|
||||
(typename $excuse_array (array (@witx pointer $excuse)))
|
||||
|
||||
(module $foo
|
||||
(@interface func (export "bar")
|
||||
(param $an_int u32)
|
||||
(param $an_float f32)
|
||||
(result $error $errno))
|
||||
(@interface func (export "baz")
|
||||
(param $an_excuse $excuse)
|
||||
(param $an_excuse_by_reference (@witx pointer $excuse))
|
||||
|
||||
61
tests/trivial.rs
Normal file
61
tests/trivial.rs
Normal file
@@ -0,0 +1,61 @@
|
||||
use proptest::prelude::*;
|
||||
use std::convert::TryFrom;
|
||||
use wiggle_runtime::{
|
||||
GuestArray, GuestError, GuestErrorType, GuestPtr, GuestPtrMut, GuestRef, GuestRefMut,
|
||||
};
|
||||
use wiggle_test::{HostMemory, MemArea};
|
||||
|
||||
mod ctx;
|
||||
use ctx::WasiCtx;
|
||||
|
||||
wiggle_generate::from_witx!({
|
||||
witx: ["tests/trivial.witx"],
|
||||
ctx: WasiCtx,
|
||||
});
|
||||
|
||||
impl_errno!(types::Errno);
|
||||
|
||||
impl trivial::Trivial for WasiCtx {
|
||||
fn int_float_args(&mut self, an_int: u32, an_float: f32) -> Result<(), types::Errno> {
|
||||
println!("INT FLOAT ARGS: {} {}", an_int, an_float);
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
|
||||
// There's nothing meaningful to test here - this just demonstrates the test machinery
|
||||
|
||||
#[derive(Debug)]
|
||||
struct IntFloatExercise {
|
||||
pub an_int: u32,
|
||||
pub an_float: f32,
|
||||
}
|
||||
|
||||
impl IntFloatExercise {
|
||||
pub fn test(&self) {
|
||||
let mut ctx = WasiCtx::new();
|
||||
let mut host_memory = HostMemory::new();
|
||||
let mut guest_memory = host_memory.guest_memory();
|
||||
|
||||
let e = trivial::int_float_args(
|
||||
&mut ctx,
|
||||
&mut guest_memory,
|
||||
self.an_int as i32,
|
||||
self.an_float,
|
||||
);
|
||||
|
||||
assert_eq!(e, types::Errno::Ok.into(), "int_float_args error");
|
||||
}
|
||||
|
||||
pub fn strat() -> BoxedStrategy<Self> {
|
||||
(prop::num::u32::ANY, prop::num::f32::ANY)
|
||||
.prop_map(|(an_int, an_float)| IntFloatExercise { an_int, an_float })
|
||||
.boxed()
|
||||
}
|
||||
}
|
||||
|
||||
proptest! {
|
||||
#[test]
|
||||
fn int_float_exercise(e in IntFloatExercise::strat()) {
|
||||
e.test()
|
||||
}
|
||||
}
|
||||
8
tests/trivial.witx
Normal file
8
tests/trivial.witx
Normal file
@@ -0,0 +1,8 @@
|
||||
(use "errno.witx")
|
||||
|
||||
(module $trivial
|
||||
(@interface func (export "int_float_args")
|
||||
(param $an_int u32)
|
||||
(param $an_float f32)
|
||||
(result $error $errno))
|
||||
)
|
||||
Reference in New Issue
Block a user