Implement assert_equal
This commit is contained in:
parent
21099a4092
commit
9b693ba41b
@ -1,4 +1,4 @@
|
|||||||
use crate::{BuiltInFunction, Error, Result, Value};
|
use crate::{BuiltInFunction, Error, Map, Result, Value};
|
||||||
|
|
||||||
pub struct Assert;
|
pub struct Assert;
|
||||||
|
|
||||||
@ -7,7 +7,7 @@ impl BuiltInFunction for Assert {
|
|||||||
"assert"
|
"assert"
|
||||||
}
|
}
|
||||||
|
|
||||||
fn run(&self, arguments: &[Value]) -> Result<Value> {
|
fn run(&self, arguments: &[Value], _context: &Map) -> Result<Value> {
|
||||||
for argument in arguments {
|
for argument in arguments {
|
||||||
if !argument.as_boolean()? {
|
if !argument.as_boolean()? {
|
||||||
return Err(Error::AssertFailed);
|
return Err(Error::AssertFailed);
|
||||||
@ -17,3 +17,27 @@ impl BuiltInFunction for Assert {
|
|||||||
Ok(Value::Empty)
|
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(),
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
@ -1,10 +1,13 @@
|
|||||||
use crate::{Map, Result, Value};
|
use crate::{Map, Result, Value};
|
||||||
|
|
||||||
|
mod assert;
|
||||||
mod fs;
|
mod fs;
|
||||||
mod output;
|
mod output;
|
||||||
mod r#type;
|
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::Read,
|
||||||
&fs::Write,
|
&fs::Write,
|
||||||
&fs::Append,
|
&fs::Append,
|
||||||
|
@ -8,7 +8,7 @@ impl BuiltInFunction for Type {
|
|||||||
}
|
}
|
||||||
|
|
||||||
fn run(&self, arguments: &[Value], context: &Map) -> Result<Value> {
|
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 {
|
if arguments.len() == 1 {
|
||||||
let type_definition = arguments.first().unwrap().r#type(context)?;
|
let type_definition = arguments.first().unwrap().r#type(context)?;
|
||||||
|
@ -5,7 +5,7 @@
|
|||||||
|
|
||||||
use tree_sitter::{Node, Point};
|
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};
|
use std::{fmt, io, num::ParseFloatError, string::FromUtf8Error, sync::PoisonError, time};
|
||||||
|
|
||||||
@ -166,8 +166,8 @@ impl Error {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn expect_function_argument_amount(
|
pub fn expect_built_in_function_argument_amount<F: BuiltInFunction>(
|
||||||
tool_name: &'static str,
|
function: &F,
|
||||||
expected: usize,
|
expected: usize,
|
||||||
actual: usize,
|
actual: usize,
|
||||||
) -> Result<()> {
|
) -> Result<()> {
|
||||||
@ -175,7 +175,7 @@ impl Error {
|
|||||||
Ok(())
|
Ok(())
|
||||||
} else {
|
} else {
|
||||||
Err(Error::ExpectedToolArgumentAmount {
|
Err(Error::ExpectedToolArgumentAmount {
|
||||||
tool_name,
|
tool_name: function.name(),
|
||||||
expected,
|
expected,
|
||||||
actual,
|
actual,
|
||||||
})
|
})
|
||||||
|
Loading…
Reference in New Issue
Block a user