1
0

Implement assert_equal

This commit is contained in:
Jeff 2023-11-30 10:00:40 -05:00
parent 21099a4092
commit 9b693ba41b
4 changed files with 35 additions and 8 deletions

View File

@ -1,4 +1,4 @@
use crate::{BuiltInFunction, Error, Result, Value};
use crate::{BuiltInFunction, Error, Map, Result, Value};
pub struct Assert;
@ -7,7 +7,7 @@ impl BuiltInFunction for Assert {
"assert"
}
fn run(&self, arguments: &[Value]) -> Result<Value> {
fn run(&self, arguments: &[Value], _context: &Map) -> Result<Value> {
for argument in arguments {
if !argument.as_boolean()? {
return Err(Error::AssertFailed);
@ -17,3 +17,27 @@ impl BuiltInFunction for Assert {
Ok(Value::Empty)
}
}
pub struct AssertEqual;
impl BuiltInFunction for AssertEqual {
fn name(&self) -> &'static str {
"assert_equal"
}
fn run(&self, arguments: &[Value], _context: &Map) -> Result<Value> {
Error::expect_built_in_function_argument_amount(self, 2, arguments.len())?;
let left = arguments.get(0).unwrap();
let right = arguments.get(1).unwrap();
if left == right {
Ok(Value::Empty)
} else {
Err(Error::AssertEqualFailed {
expected: left.clone(),
actual: right.clone(),
})
}
}
}

View File

@ -1,10 +1,13 @@
use crate::{Map, Result, Value};
mod assert;
mod fs;
mod output;
mod r#type;
pub const BUILT_IN_FUNCTIONS: [&dyn BuiltInFunction; 5] = [
pub const BUILT_IN_FUNCTIONS: [&dyn BuiltInFunction; 7] = [
&assert::Assert,
&assert::AssertEqual,
&fs::Read,
&fs::Write,
&fs::Append,

View File

@ -8,7 +8,7 @@ impl BuiltInFunction for Type {
}
fn run(&self, arguments: &[Value], context: &Map) -> Result<Value> {
Error::expect_function_argument_amount("type", 1, arguments.len())?;
Error::expect_built_in_function_argument_amount(self, 1, arguments.len())?;
if arguments.len() == 1 {
let type_definition = arguments.first().unwrap().r#type(context)?;

View File

@ -5,7 +5,7 @@
use tree_sitter::{Node, Point};
use crate::{value::Value, Identifier, TypeDefinition};
use crate::{value::Value, BuiltInFunction, Identifier, TypeDefinition};
use std::{fmt, io, num::ParseFloatError, string::FromUtf8Error, sync::PoisonError, time};
@ -166,8 +166,8 @@ impl Error {
}
}
pub fn expect_function_argument_amount(
tool_name: &'static str,
pub fn expect_built_in_function_argument_amount<F: BuiltInFunction>(
function: &F,
expected: usize,
actual: usize,
) -> Result<()> {
@ -175,7 +175,7 @@ impl Error {
Ok(())
} else {
Err(Error::ExpectedToolArgumentAmount {
tool_name,
tool_name: function.name(),
expected,
actual,
})