2023-07-29 18:47:25 +00:00
|
|
|
use std::collections::HashMap;
|
2024-09-19 15:17:25 +00:00
|
|
|
use std::fmt::{Debug, Display, Formatter};
|
2024-01-09 16:57:52 +00:00
|
|
|
use std::future::Future;
|
2023-07-29 18:47:25 +00:00
|
|
|
use std::sync::Arc;
|
2024-01-09 16:57:52 +00:00
|
|
|
use std::thread::Builder;
|
2023-07-29 18:47:25 +00:00
|
|
|
|
2023-08-30 18:01:30 +00:00
|
|
|
use surrealdb::dbs::capabilities::Capabilities;
|
2023-07-29 18:47:25 +00:00
|
|
|
use surrealdb::dbs::Session;
|
2023-08-30 18:01:30 +00:00
|
|
|
use surrealdb::err::Error;
|
2023-07-29 18:47:25 +00:00
|
|
|
use surrealdb::iam::{Auth, Level, Role};
|
|
|
|
use surrealdb::kvs::Datastore;
|
2024-04-08 23:04:44 +00:00
|
|
|
use surrealdb_core::dbs::Response;
|
2024-06-05 15:21:49 +00:00
|
|
|
use surrealdb_core::sql::{value, Number, Value};
|
2023-07-29 18:47:25 +00:00
|
|
|
|
2023-08-30 18:01:30 +00:00
|
|
|
pub async fn new_ds() -> Result<Datastore, Error> {
|
2024-02-27 15:18:25 +00:00
|
|
|
Ok(Datastore::new("memory").await?.with_capabilities(Capabilities::all()).with_notifications())
|
2023-08-30 18:01:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[allow(dead_code)]
|
2023-07-29 18:47:25 +00:00
|
|
|
pub async fn iam_run_case(
|
|
|
|
prepare: &str,
|
|
|
|
test: &str,
|
|
|
|
check: &str,
|
2024-03-08 10:58:07 +00:00
|
|
|
check_expected_result: &[&str],
|
2023-07-29 18:47:25 +00:00
|
|
|
ds: &Datastore,
|
|
|
|
sess: &Session,
|
|
|
|
should_succeed: bool,
|
|
|
|
) -> Result<(), Box<dyn std::error::Error>> {
|
2024-05-22 13:57:25 +00:00
|
|
|
// Use the session as the test statement, but change the Auth to run the check with full permissions
|
2023-07-29 18:47:25 +00:00
|
|
|
let mut owner_sess = sess.clone();
|
|
|
|
owner_sess.au = Arc::new(Auth::for_root(Role::Owner));
|
|
|
|
|
|
|
|
// Prepare statement
|
|
|
|
{
|
|
|
|
if !prepare.is_empty() {
|
|
|
|
let resp = ds.execute(prepare, &owner_sess, None).await.unwrap();
|
|
|
|
for r in resp.into_iter() {
|
|
|
|
let tmp = r.output();
|
|
|
|
if tmp.is_err() {
|
|
|
|
return Err(format!("Prepare statement failed: {}", tmp.unwrap_err()).into());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Execute statement
|
|
|
|
let mut resp = ds.execute(test, sess, None).await.unwrap();
|
|
|
|
|
|
|
|
// Check datastore state first
|
|
|
|
{
|
|
|
|
let resp = ds.execute(check, &owner_sess, None).await.unwrap();
|
|
|
|
if resp.len() != check_expected_result.len() {
|
|
|
|
return Err(format!(
|
|
|
|
"Check statement failed for test: expected {} results, got {}",
|
|
|
|
check_expected_result.len(),
|
|
|
|
resp.len()
|
|
|
|
)
|
|
|
|
.into());
|
|
|
|
}
|
|
|
|
|
|
|
|
for (i, r) in resp.into_iter().enumerate() {
|
|
|
|
let tmp = r.output();
|
|
|
|
if tmp.is_err() {
|
|
|
|
return Err(
|
|
|
|
format!("Check statement errored for test: {}", tmp.unwrap_err()).into()
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
let tmp = tmp.unwrap().to_string();
|
2023-10-04 09:51:34 +00:00
|
|
|
if tmp != check_expected_result[i] {
|
2023-07-29 18:47:25 +00:00
|
|
|
return Err(format!(
|
|
|
|
"Check statement failed for test: expected value '{}' doesn't match '{}'",
|
|
|
|
check_expected_result[i], tmp
|
|
|
|
)
|
|
|
|
.into());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check statement result. If the statement should succeed, check that the result is Ok, otherwise check that the result is a 'Not Allowed' error
|
|
|
|
let res = resp.pop().unwrap().output();
|
|
|
|
if should_succeed {
|
|
|
|
if res.is_err() {
|
|
|
|
return Err(format!("Test statement failed: {}", res.unwrap_err()).into());
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if res.is_ok() {
|
|
|
|
return Err(
|
|
|
|
format!("Test statement succeeded when it should have failed: {:?}", res).into()
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
let err = res.unwrap_err().to_string();
|
|
|
|
if !err.contains("Not enough permissions to perform this action") {
|
|
|
|
return Err(format!("Test statement failed with unexpected error: {}", err).into());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
2023-10-04 09:51:34 +00:00
|
|
|
type CaseIter<'a> = std::slice::Iter<'a, ((Level, Role), (&'a str, &'a str), bool)>;
|
|
|
|
|
2023-08-30 18:01:30 +00:00
|
|
|
#[allow(dead_code)]
|
2023-07-29 18:47:25 +00:00
|
|
|
pub async fn iam_check_cases(
|
2023-10-04 09:51:34 +00:00
|
|
|
cases: CaseIter<'_>,
|
2023-07-29 18:47:25 +00:00
|
|
|
scenario: &HashMap<&str, &str>,
|
|
|
|
check_results: [Vec<&str>; 2],
|
|
|
|
) -> Result<(), Box<dyn std::error::Error>> {
|
|
|
|
let prepare = scenario.get("prepare").unwrap();
|
|
|
|
let test = scenario.get("test").unwrap();
|
|
|
|
let check = scenario.get("check").unwrap();
|
|
|
|
|
|
|
|
for ((level, role), (ns, db), should_succeed) in cases {
|
|
|
|
println!("* Testing '{test}' for '{level}Actor({role})' on '({ns}, {db})'");
|
|
|
|
let sess = Session::for_level(level.to_owned(), role.to_owned()).with_ns(ns).with_db(db);
|
|
|
|
let expected_result = if *should_succeed {
|
2024-03-08 10:58:07 +00:00
|
|
|
check_results.first().unwrap()
|
2023-07-29 18:47:25 +00:00
|
|
|
} else {
|
|
|
|
check_results.get(1).unwrap()
|
|
|
|
};
|
|
|
|
// Auth enabled
|
|
|
|
{
|
2023-08-30 18:01:30 +00:00
|
|
|
let ds = new_ds().await.unwrap().with_auth_enabled(true);
|
2023-10-04 09:51:34 +00:00
|
|
|
iam_run_case(prepare, test, check, expected_result, &ds, &sess, *should_succeed)
|
2023-07-29 18:47:25 +00:00
|
|
|
.await?;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Auth disabled
|
|
|
|
{
|
2023-08-30 18:01:30 +00:00
|
|
|
let ds = new_ds().await.unwrap().with_auth_enabled(false);
|
2023-10-04 09:51:34 +00:00
|
|
|
iam_run_case(prepare, test, check, expected_result, &ds, &sess, *should_succeed)
|
2023-07-29 18:47:25 +00:00
|
|
|
.await?;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Anonymous user
|
|
|
|
let ns = "NS";
|
|
|
|
let db = "DB";
|
|
|
|
for auth_enabled in [true, false].into_iter() {
|
|
|
|
{
|
|
|
|
println!(
|
|
|
|
"* Testing '{test}' for 'Anonymous' on '({ns}, {db})' with {auth_enabled}",
|
2023-10-04 09:51:34 +00:00
|
|
|
auth_enabled = if auth_enabled {
|
|
|
|
"auth enabled"
|
|
|
|
} else {
|
|
|
|
"auth disabled"
|
|
|
|
}
|
2023-07-29 18:47:25 +00:00
|
|
|
);
|
2023-08-30 18:01:30 +00:00
|
|
|
let ds = new_ds().await.unwrap().with_auth_enabled(auth_enabled);
|
2023-07-29 18:47:25 +00:00
|
|
|
let expected_result = if auth_enabled {
|
|
|
|
check_results.get(1).unwrap()
|
|
|
|
} else {
|
2024-03-08 10:58:07 +00:00
|
|
|
check_results.first().unwrap()
|
2023-07-29 18:47:25 +00:00
|
|
|
};
|
|
|
|
iam_run_case(
|
|
|
|
prepare,
|
|
|
|
test,
|
|
|
|
check,
|
2023-10-04 09:51:34 +00:00
|
|
|
expected_result,
|
2023-07-29 18:47:25 +00:00
|
|
|
&ds,
|
|
|
|
&Session::default().with_ns(ns).with_db(db),
|
|
|
|
!auth_enabled,
|
|
|
|
)
|
|
|
|
.await?;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|
2024-01-09 16:57:52 +00:00
|
|
|
|
|
|
|
#[allow(dead_code)]
|
|
|
|
pub fn with_enough_stack(
|
|
|
|
fut: impl Future<Output = Result<(), Error>> + Send + 'static,
|
|
|
|
) -> Result<(), Error> {
|
|
|
|
#[allow(unused_mut)]
|
|
|
|
let mut builder = Builder::new();
|
|
|
|
|
|
|
|
// Roughly how much stack is allocated for surreal server workers in release mode
|
|
|
|
#[cfg(not(debug_assertions))]
|
|
|
|
{
|
|
|
|
builder = builder.stack_size(10_000_000);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Same for debug mode
|
|
|
|
#[cfg(debug_assertions)]
|
|
|
|
{
|
|
|
|
builder = builder.stack_size(24_000_000);
|
|
|
|
}
|
|
|
|
|
|
|
|
builder
|
|
|
|
.spawn(|| {
|
|
|
|
let runtime = tokio::runtime::Builder::new_current_thread().build().unwrap();
|
|
|
|
runtime.block_on(fut)
|
|
|
|
})
|
|
|
|
.unwrap()
|
|
|
|
.join()
|
|
|
|
.unwrap()
|
|
|
|
}
|
2024-04-08 23:04:44 +00:00
|
|
|
|
2024-06-07 16:24:55 +00:00
|
|
|
#[allow(dead_code)]
|
|
|
|
fn skip_ok_pos(res: &mut Vec<Response>, pos: usize) -> Result<(), Error> {
|
|
|
|
assert!(!res.is_empty(), "At position {pos} - No more result!");
|
|
|
|
let r = res.remove(0).result;
|
|
|
|
let _ = r.is_err_and(|e| {
|
|
|
|
panic!("At position {pos} - Statement fails with: {e}");
|
|
|
|
});
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
2024-06-05 15:21:49 +00:00
|
|
|
/// Skip the specified number of successful results from a vector of responses.
|
|
|
|
/// This function will panic if there are not enough results in the vector or if an error occurs.
|
2024-04-08 23:04:44 +00:00
|
|
|
#[allow(dead_code)]
|
2024-06-07 16:24:55 +00:00
|
|
|
pub fn skip_ok(res: &mut Vec<Response>, skip: usize) -> Result<(), Error> {
|
2024-05-24 18:09:10 +00:00
|
|
|
for i in 0..skip {
|
2024-06-07 16:24:55 +00:00
|
|
|
skip_ok_pos(res, i)?;
|
2024-04-08 23:04:44 +00:00
|
|
|
}
|
2024-06-07 16:24:55 +00:00
|
|
|
Ok(())
|
2024-06-05 15:21:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Struct representing a test scenario.
|
|
|
|
///
|
|
|
|
/// # Fields
|
|
|
|
/// - `ds`: The datastore for the test.
|
|
|
|
/// - `session`: The session for the test.
|
|
|
|
/// - `responses`: The list of responses for the test.
|
|
|
|
/// - `pos`: The current position in the responses list.
|
|
|
|
#[allow(dead_code)]
|
|
|
|
pub struct Test {
|
|
|
|
pub ds: Datastore,
|
|
|
|
pub session: Session,
|
|
|
|
pub responses: Vec<Response>,
|
|
|
|
pos: usize,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Debug for Test {
|
|
|
|
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
|
|
|
|
write!(f, "Responses left: {:?}.", self.responses)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Test {
|
|
|
|
/// Creates a new instance of the `Self` struct with the given SQL query.
|
|
|
|
/// Arguments `sql` - A string slice representing the SQL query.
|
|
|
|
/// Panics if an error occurs.
|
|
|
|
#[allow(dead_code)]
|
2024-08-20 10:42:58 +00:00
|
|
|
pub async fn with_ds(ds: Datastore, sql: &str) -> Result<Self, Error> {
|
2024-06-05 15:21:49 +00:00
|
|
|
let session = Session::owner().with_ns("test").with_db("test");
|
|
|
|
let responses = ds.execute(sql, &session, None).await?;
|
|
|
|
Ok(Self {
|
|
|
|
ds,
|
|
|
|
session,
|
|
|
|
responses,
|
|
|
|
pos: 0,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2024-08-23 08:27:21 +00:00
|
|
|
#[allow(dead_code)]
|
2024-08-20 10:42:58 +00:00
|
|
|
pub async fn new(sql: &str) -> Result<Self, Error> {
|
|
|
|
Self::with_ds(new_ds().await?, sql).await
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Simulates restarting the Datastore
|
|
|
|
/// - Data are persistent (including memory store)
|
|
|
|
/// - Flushing caches (jwks, IndexStore, ...)
|
2024-08-23 08:27:21 +00:00
|
|
|
#[allow(dead_code)]
|
2024-08-20 10:42:58 +00:00
|
|
|
pub async fn restart(self, sql: &str) -> Result<Self, Error> {
|
|
|
|
Self::with_ds(self.ds.restart(), sql).await
|
|
|
|
}
|
|
|
|
|
2024-06-05 15:21:49 +00:00
|
|
|
/// Checks if the number of responses matches the expected size.
|
|
|
|
/// Panics if the number of responses does not match the expected size
|
|
|
|
#[allow(dead_code)]
|
2024-06-07 16:24:55 +00:00
|
|
|
pub fn expect_size(&mut self, expected: usize) -> Result<&mut Self, Error> {
|
2024-06-05 15:21:49 +00:00
|
|
|
assert_eq!(
|
|
|
|
self.responses.len(),
|
|
|
|
expected,
|
|
|
|
"Unexpected number of results: {} - Expected: {expected}",
|
|
|
|
self.responses.len()
|
|
|
|
);
|
2024-06-07 16:24:55 +00:00
|
|
|
Ok(self)
|
2024-06-05 15:21:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Retrieves the next response from the responses list.
|
|
|
|
/// This method will panic if the responses list is empty, indicating that there are no more responses to retrieve.
|
|
|
|
/// The panic message will include the last position in the responses list before it was emptied.
|
|
|
|
#[allow(dead_code)]
|
2024-07-05 09:34:43 +00:00
|
|
|
#[allow(clippy::should_implement_trait)]
|
2024-06-07 16:24:55 +00:00
|
|
|
pub fn next(&mut self) -> Result<Response, Error> {
|
|
|
|
assert!(!self.responses.is_empty(), "No response left - last position: {}", self.pos);
|
2024-06-05 15:21:49 +00:00
|
|
|
self.pos += 1;
|
2024-06-07 16:24:55 +00:00
|
|
|
Ok(self.responses.remove(0))
|
2024-06-05 15:21:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Retrieves the next value from the responses list.
|
|
|
|
/// This method will panic if the responses list is empty, indicating that there are no more responses to retrieve.
|
|
|
|
/// The panic message will include the last position in the responses list before it was emptied.
|
2024-06-07 16:24:55 +00:00
|
|
|
pub fn next_value(&mut self) -> Result<Value, Error> {
|
|
|
|
self.next()?.result
|
2024-06-05 15:21:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Skips a specified number of elements from the beginning of the `responses` vector
|
|
|
|
/// and updates the position.
|
|
|
|
#[allow(dead_code)]
|
2024-06-07 16:24:55 +00:00
|
|
|
pub fn skip_ok(&mut self, skip: usize) -> Result<&mut Self, Error> {
|
|
|
|
for _ in 0..skip {
|
|
|
|
skip_ok_pos(&mut self.responses, self.pos)?;
|
|
|
|
self.pos += 1;
|
|
|
|
}
|
|
|
|
Ok(self)
|
2024-06-05 15:21:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Expects the next value to be equal to the provided value.
|
|
|
|
/// Panics if the expected value is not equal to the actual value.
|
|
|
|
/// Compliant with NaN and Constants.
|
|
|
|
#[allow(dead_code)]
|
2024-09-19 15:17:25 +00:00
|
|
|
pub fn expect_value_info<I: Display>(
|
|
|
|
&mut self,
|
|
|
|
val: Value,
|
|
|
|
info: I,
|
|
|
|
) -> Result<&mut Self, Error> {
|
2024-06-07 16:24:55 +00:00
|
|
|
let tmp = self.next_value()?;
|
2024-06-05 15:21:49 +00:00
|
|
|
// Then check they are indeed the same values
|
|
|
|
//
|
|
|
|
// If it is a constant we need to transform it as a number
|
|
|
|
let val = if let Value::Constant(c) = val {
|
|
|
|
c.compute().unwrap_or_else(|e| panic!("Can't convert constant {c} - {e}"))
|
|
|
|
} else {
|
|
|
|
val
|
|
|
|
};
|
|
|
|
if val.is_nan() {
|
2024-09-19 15:17:25 +00:00
|
|
|
assert!(tmp.is_nan(), "Expected NaN but got {info}: {tmp}");
|
2024-06-05 15:21:49 +00:00
|
|
|
} else {
|
2024-09-19 15:17:25 +00:00
|
|
|
assert_eq!(tmp, val, "{info} {tmp:#}");
|
2024-06-05 15:21:49 +00:00
|
|
|
}
|
|
|
|
//
|
2024-06-07 16:24:55 +00:00
|
|
|
Ok(self)
|
2024-06-05 15:21:49 +00:00
|
|
|
}
|
|
|
|
|
2024-09-19 15:17:25 +00:00
|
|
|
#[allow(dead_code)]
|
|
|
|
pub fn expect_value(&mut self, val: Value) -> Result<&mut Self, Error> {
|
|
|
|
self.expect_value_info(val, "")
|
|
|
|
}
|
|
|
|
|
2024-06-05 15:21:49 +00:00
|
|
|
/// Expect values in the given slice to be present in the responses, following the same order.
|
|
|
|
#[allow(dead_code)]
|
2024-06-07 16:24:55 +00:00
|
|
|
pub fn expect_values(&mut self, values: &[Value]) -> Result<&mut Self, Error> {
|
2024-06-05 15:21:49 +00:00
|
|
|
for value in values {
|
2024-06-07 16:24:55 +00:00
|
|
|
self.expect_value(value.clone())?;
|
2024-06-05 15:21:49 +00:00
|
|
|
}
|
2024-06-07 16:24:55 +00:00
|
|
|
Ok(self)
|
2024-06-05 15:21:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Expect the given value to be equals to the next response.
|
|
|
|
#[allow(dead_code)]
|
2024-06-07 16:24:55 +00:00
|
|
|
pub fn expect_val(&mut self, val: &str) -> Result<&mut Self, Error> {
|
2024-09-19 15:17:25 +00:00
|
|
|
self.expect_val_info(val, "")
|
|
|
|
}
|
|
|
|
|
|
|
|
#[allow(dead_code)]
|
|
|
|
pub fn expect_val_info<I: Display>(&mut self, val: &str, info: I) -> Result<&mut Self, Error> {
|
|
|
|
self.expect_value_info(
|
|
|
|
value(val).unwrap_or_else(|_| panic!("INVALID VALUE {info}:\n{val}")),
|
|
|
|
info,
|
|
|
|
)
|
2024-06-05 15:21:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[allow(dead_code)]
|
|
|
|
/// Expect values in the given slice to be present in the responses, following the same order.
|
2024-06-07 16:24:55 +00:00
|
|
|
pub fn expect_vals(&mut self, vals: &[&str]) -> Result<&mut Self, Error> {
|
2024-06-05 15:21:49 +00:00
|
|
|
for val in vals {
|
2024-06-07 16:24:55 +00:00
|
|
|
self.expect_val(val)?;
|
2024-06-05 15:21:49 +00:00
|
|
|
}
|
2024-06-07 16:24:55 +00:00
|
|
|
Ok(self)
|
2024-06-05 15:21:49 +00:00
|
|
|
}
|
|
|
|
|
2024-06-07 16:24:55 +00:00
|
|
|
/// Expects the next result to be an error with the given check function returning true.
|
2024-06-05 15:21:49 +00:00
|
|
|
/// This function will panic if the next result is not an error or if the error
|
2024-06-07 16:24:55 +00:00
|
|
|
/// message does not pass the check.
|
2024-06-05 15:21:49 +00:00
|
|
|
#[allow(dead_code)]
|
2024-06-07 16:24:55 +00:00
|
|
|
pub fn expect_error_func<F: Fn(&Error) -> bool>(
|
|
|
|
&mut self,
|
|
|
|
check: F,
|
|
|
|
) -> Result<&mut Self, Error> {
|
|
|
|
let tmp = self.next()?.result;
|
|
|
|
match &tmp {
|
|
|
|
Ok(val) => {
|
|
|
|
panic!("At position {} - Expect error, but got OK: {val}", self.pos);
|
|
|
|
}
|
|
|
|
Err(e) => {
|
|
|
|
assert!(check(e), "At position {} - Err didn't match: {e}", self.pos)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
Ok(self)
|
|
|
|
}
|
|
|
|
|
|
|
|
#[allow(dead_code)]
|
|
|
|
/// Expects the next result to be an error with the specified error message.
|
|
|
|
pub fn expect_error(&mut self, error: &str) -> Result<&mut Self, Error> {
|
|
|
|
self.expect_error_func(|e| e.to_string() == error)
|
2024-06-05 15:21:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[allow(dead_code)]
|
2024-06-07 16:24:55 +00:00
|
|
|
pub fn expect_errors(&mut self, errors: &[&str]) -> Result<&mut Self, Error> {
|
2024-06-05 15:21:49 +00:00
|
|
|
for error in errors {
|
2024-06-07 16:24:55 +00:00
|
|
|
self.expect_error(error)?;
|
2024-06-05 15:21:49 +00:00
|
|
|
}
|
2024-06-07 16:24:55 +00:00
|
|
|
Ok(self)
|
2024-06-05 15:21:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Expects the next value to be a floating-point number and compares it with the given value.
|
|
|
|
///
|
|
|
|
/// # Arguments
|
|
|
|
///
|
|
|
|
/// * `val` - The expected floating-point value
|
|
|
|
/// * `precision` - The allowed difference between the expected and actual value
|
|
|
|
///
|
|
|
|
/// # Panics
|
|
|
|
///
|
|
|
|
/// Panics if the next value is not a number or if the difference
|
|
|
|
/// between the expected and actual value exceeds the precision.
|
|
|
|
#[allow(dead_code)]
|
2024-06-07 16:24:55 +00:00
|
|
|
pub fn expect_float(&mut self, val: f64, precision: f64) -> Result<&mut Self, Error> {
|
|
|
|
let tmp = self.next_value()?;
|
2024-06-05 15:21:49 +00:00
|
|
|
if let Value::Number(Number::Float(n)) = tmp {
|
|
|
|
let diff = (n - val).abs();
|
|
|
|
assert!(
|
|
|
|
diff <= precision,
|
|
|
|
"{tmp} does not match expected: {val} - diff: {diff} - precision: {precision}"
|
|
|
|
);
|
|
|
|
} else {
|
|
|
|
panic!("At position {}: Value {tmp} is not a number", self.pos);
|
|
|
|
}
|
2024-06-07 16:24:55 +00:00
|
|
|
Ok(self)
|
2024-06-05 15:21:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[allow(dead_code)]
|
2024-06-07 16:24:55 +00:00
|
|
|
pub fn expect_floats(&mut self, vals: &[f64], precision: f64) -> Result<&mut Self, Error> {
|
2024-06-05 15:21:49 +00:00
|
|
|
for val in vals {
|
2024-06-07 16:24:55 +00:00
|
|
|
self.expect_float(*val, precision)?;
|
2024-06-05 15:21:49 +00:00
|
|
|
}
|
2024-06-07 16:24:55 +00:00
|
|
|
Ok(self)
|
2024-06-05 15:21:49 +00:00
|
|
|
}
|
|
|
|
}
|