Add assertion errors; Write tests

This commit is contained in:
Jeff 2023-08-22 12:31:45 -04:00
parent ef57a42eb6
commit 109091cf80
6 changed files with 92 additions and 46 deletions

View File

@ -14,6 +14,15 @@ pub type Result<T> = std::result::Result<T, Error>;
#[derive(Debug, Clone, PartialEq)]
#[non_exhaustive]
pub enum Error {
/// The 'assert' macro did not resolve successfully.
AssertEqualFailed {
expected: Value,
actual: Value,
},
/// The 'assert' macro did not resolve successfully.
AssertFailed,
/// A row was inserted to a table with the wrong amount of values.
WrongColumnAmount {
expected: usize,
@ -454,6 +463,8 @@ impl fmt::Display for Error {
use Error::*;
match self {
AssertEqualFailed {expected, actual } => write!(f, "Equality assertion failed. {expected} does not equal {actual}."),
AssertFailed => write!(f, "Assertion failed. A false value was passed to \"assert\"."),
ExpectedOperatorArgumentAmount { expected, actual } => write!(
f,
"An operator expected {} arguments, but got {}.",

View File

@ -66,7 +66,7 @@ impl Macro for BarGraph {
height = Some(float);
}
}
Value::Integer(integer) => {}
Value::Integer(_integer) => {}
Value::String(string) => name = Some(string),
Value::Boolean(_)
| Value::List(_)

View File

@ -1,5 +1,55 @@
use crate::{Error, Macro, MacroInfo, Result, Value, ValueType};
pub struct Assert;
impl Macro for Assert {
fn info(&self) -> MacroInfo<'static> {
MacroInfo {
identifier: "assert",
description: "Panic if a boolean is false.",
group: "test",
inputs: vec![],
}
}
fn run(&self, argument: &Value) -> Result<Value> {
let boolean = argument.as_boolean()?;
if boolean {
Ok(Value::Empty)
} else {
Err(Error::AssertFailed)
}
}
}
pub struct AssertEqual;
impl Macro for AssertEqual {
fn info(&self) -> MacroInfo<'static> {
MacroInfo {
identifier: "assert_equal",
description: "Panic if two values do not match.",
group: "test",
inputs: vec![],
}
}
fn run(&self, argument: &Value) -> Result<Value> {
let arguments = argument.as_fixed_len_list(2)?;
if arguments[0] == arguments[1] {
Ok(Value::Empty)
} else {
Err(Error::AssertEqualFailed { expected: arguments[0].clone(), actual: arguments[1].clone() })
}
}
}
pub struct If;
impl Macro for If {

View File

@ -34,7 +34,6 @@ pub mod network;
pub mod package_management;
pub mod random;
pub mod system;
pub mod test;
pub mod time;
/// Master list of all macros.
@ -94,8 +93,8 @@ pub const MACRO_LIST: [&'static dyn Macro; 56] = [
&random::RandomInteger,
&random::RandomString,
&system::CpuSpeed,
&test::Assert,
&test::AssertEqual,
&logic::Assert,
&logic::AssertEqual,
&time::Local,
&time::Now,
];

View File

@ -1,42 +0,0 @@
use crate::{Macro, MacroInfo, Result, Value};
pub struct Assert;
impl Macro for Assert {
fn info(&self) -> MacroInfo<'static> {
MacroInfo {
identifier: "assert",
description: "Panic if a boolean is false.",
group: "test",
inputs: vec![],
}
}
fn run(&self, argument: &Value) -> Result<Value> {
let boolean = argument.as_boolean()?;
assert!(boolean);
Ok(Value::Empty)
}
}
pub struct AssertEqual;
impl Macro for AssertEqual {
fn info(&self) -> MacroInfo<'static> {
MacroInfo {
identifier: "assert_equal",
description: "Panic if two values do not match.",
group: "test",
inputs: vec![],
}
}
fn run(&self, argument: &Value) -> Result<Value> {
let arguments = argument.as_fixed_len_list(2)?;
assert_eq!(arguments[0], arguments[1]);
Ok(Value::Empty)
}
}

28
tests/logic.rs Normal file
View File

@ -0,0 +1,28 @@
use whale_lib::*;
#[test]
fn assert() {
eval("assert(true)").unwrap();
eval("assert(false)").unwrap_err();
}
#[test]
fn assert_equal() {
eval("assert_eq(true, true)").unwrap();
eval("assert_eq(true, false)").unwrap_err();
}
#[test]
fn r#if() {
eval("if(true, assert(true))").unwrap();
let value = eval("if(true, 1)").unwrap();
assert!(value.is_empty());
}
#[test]
fn r#if_else() {
eval("if(true, assert(true), assert(false))").unwrap();
eval("if(false, assert(false), assert(true))").unwrap();
}