Skip to content

Commit

Permalink
feat: add more comparison functions
Browse files Browse the repository at this point in the history
  • Loading branch information
nilslice committed May 4, 2024
1 parent 16aab73 commit e540fb7
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 1 deletion.
7 changes: 6 additions & 1 deletion examples/basic.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,11 @@ pub struct Count {

#[plugin_fn]
pub fn test() -> FnResult<()> {
xtp_test::assert_gt("gt test", 100, 1);
xtp_test::assert_lt("lt test", std::f64::MIN, std::f64::MAX);
xtp_test::assert_lte("gte test", 'Z', 'a');
xtp_test::assert_lte("lte test", 1 / 10, 1 / 10);

// call a function from some Extism plugin (you'll link these up in the CLI command to run the test),
// passing in some data and getting back a string (`callString` is a helper for string output)
let Json(res): Json<Count> = xtp_test::call("count_vowels", "some input")?;
Expand All @@ -20,7 +25,7 @@ pub fn test() -> FnResult<()> {
const TARGET_NS: u64 = 5e7 as u64;
xtp_test::assert(
"timing count_vowels nanos (10KB input)",
time_ns < TARGET_NS,
time_ns < TARGET_NS,
format!("{} > {}", time_ns, TARGET_NS),
);

Expand Down
36 changes: 36 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,42 @@ pub fn assert_ne<U: std::fmt::Debug, T: std::fmt::Debug + PartialEq<U>>(
assert(msg, x != y, format!("Expected {:?} != {:?}", x, y));
}

/// Assert that `x` is greater than `y`, naming the assertion with `msg`, which will be used as a label in the CLI runner.
pub fn assert_gt<U: std::fmt::Debug, T: std::fmt::Debug + PartialOrd<U>>(
msg: impl AsRef<str>,
x: T,
y: U,
) {
assert(msg, x > y, format!("Expected {:?} > {:?}", x, y));
}

/// Assert that `x` is greater than or equal to `y`, naming the assertion with `msg`, which will be used as a label in the CLI runner.
pub fn assert_gte<U: std::fmt::Debug, T: std::fmt::Debug + PartialOrd<U>>(
msg: impl AsRef<str>,
x: T,
y: U,
) {
assert(msg, x >= y, format!("Expected {:?} >= {:?}", x, y));
}

/// Assert that `x` is less than `y`, naming the assertion with `msg`, which will be used as a label in the CLI runner.
pub fn assert_lt<U: std::fmt::Debug, T: std::fmt::Debug + PartialOrd<U>>(
msg: impl AsRef<str>,
x: T,
y: U,
) {
assert(msg, x < y, format!("Expected {:?} < {:?}", x, y));
}

/// Assert that `x` is less than or equal to `y`, naming the assertion with `msg`, which will be used as a label in the CLI runner.
pub fn assert_lte<U: std::fmt::Debug, T: std::fmt::Debug + PartialOrd<U>>(
msg: impl AsRef<str>,
x: T,
y: U,
) {
assert(msg, x <= y, format!("Expected {:?} <= {:?}", x, y));
}

// Create a new test group. NOTE: these cannot be nested and starting a new group will end the last one.
fn start_group(name: impl AsRef<str>) {
let name_mem = Memory::from_bytes(name.as_ref()).expect("assert message Extism memory");
Expand Down

0 comments on commit e540fb7

Please sign in to comment.