From 109091cf801d27583292d104f6f841129c6a3448 Mon Sep 17 00:00:00 2001 From: Jeff Date: Tue, 22 Aug 2023 12:31:45 -0400 Subject: [PATCH] Add assertion errors; Write tests --- src/error.rs | 11 ++++++++++ src/macros/gui.rs | 2 +- src/macros/logic.rs | 50 +++++++++++++++++++++++++++++++++++++++++++++ src/macros/mod.rs | 5 ++--- src/macros/test.rs | 42 ------------------------------------- tests/logic.rs | 28 +++++++++++++++++++++++++ 6 files changed, 92 insertions(+), 46 deletions(-) delete mode 100644 src/macros/test.rs create mode 100644 tests/logic.rs diff --git a/src/error.rs b/src/error.rs index af7e115..79ad1a3 100644 --- a/src/error.rs +++ b/src/error.rs @@ -14,6 +14,15 @@ pub type Result = std::result::Result; #[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 {}.", diff --git a/src/macros/gui.rs b/src/macros/gui.rs index 612a611..b293f69 100644 --- a/src/macros/gui.rs +++ b/src/macros/gui.rs @@ -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(_) diff --git a/src/macros/logic.rs b/src/macros/logic.rs index 696bc88..3053e40 100644 --- a/src/macros/logic.rs +++ b/src/macros/logic.rs @@ -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 { + 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 { + 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 { diff --git a/src/macros/mod.rs b/src/macros/mod.rs index c7910d2..f5a4710 100644 --- a/src/macros/mod.rs +++ b/src/macros/mod.rs @@ -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, ]; diff --git a/src/macros/test.rs b/src/macros/test.rs deleted file mode 100644 index 1efe29a..0000000 --- a/src/macros/test.rs +++ /dev/null @@ -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 { - 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 { - let arguments = argument.as_fixed_len_list(2)?; - assert_eq!(arguments[0], arguments[1]); - - Ok(Value::Empty) - } -} diff --git a/tests/logic.rs b/tests/logic.rs new file mode 100644 index 0000000..9ad3f62 --- /dev/null +++ b/tests/logic.rs @@ -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(); +}