Skip to content

Commit

Permalink
add custom assert messages
Browse files Browse the repository at this point in the history
  • Loading branch information
bwburnsides authored and hlorenzi committed Dec 27, 2023
1 parent a793015 commit c0eace2
Show file tree
Hide file tree
Showing 8 changed files with 112 additions and 14 deletions.
42 changes: 28 additions & 14 deletions src/expr/builtin_fn.rs
Original file line number Diff line number Diff line change
Expand Up @@ -83,25 +83,39 @@ pub fn eval_builtin_assert(
query: &mut expr::EvalFunctionQuery)
-> Result<expr::Value, ()>
{
query.ensure_arg_number(1)?;
query.ensure_min_max_arg_number(1, 2)?;

let condition = query.args[0].value.expect_bool(
query.report,
query.args[0].span)?;
let condition = query.args[0]
.value
.expect_bool(
query.report,
query.args[0].span)?;

if condition
if !condition
{
Ok(expr::Value::Void)
}
else
{
let msg = diagn::Message::error_span(
"assertion failed",
query.span);
let msg = {
if query.args.len() == 2
{
diagn::Message::error_span(
format!(
"assertion failed: {}",
query.args[1]
.value
.expect_string(query.report, query.args[1].span)?
.utf8_contents
.clone()),
query.span)
}
else {
diagn::Message::error_span("assertion failed", query.span)
}
};

Ok(expr::Value::FailedConstraint(
query.report.wrap_in_parents_capped(msg)))
return Ok(expr::Value::FailedConstraint(
query.report.wrap_in_parents_capped(msg)));
}

Ok(expr::Value::Void)
}


Expand Down
23 changes: 23 additions & 0 deletions src/expr/eval.rs
Original file line number Diff line number Diff line change
Expand Up @@ -219,6 +219,29 @@ impl<'a> EvalFunctionQuery<'a>
Ok(())
}
}

pub fn ensure_min_max_arg_number(
&mut self,
minimum_expected_arg_number: usize,
maximum_expected_arg_number: usize,
) -> Result<(), ()> {
if !((self.args.len() >= minimum_expected_arg_number)
&& (self.args.len() <= maximum_expected_arg_number))
{
self.report.error_span(
format!(
"function expected {} to {} arguments (but got {})",
minimum_expected_arg_number,
maximum_expected_arg_number,
self.args.len()
),
self.span,
);
Err(())
} else {
Ok(())
}
}
}


Expand Down
10 changes: 10 additions & 0 deletions tests/rule_assert/err_args_too_few.asm
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
#ruledef test
{
ld {x} =>
{
assert()
0x55 @ x`8
}
}

ld 0x15 ; error: failed / note:_:3: within / error:_:5: expected 1 to 2 arguments
10 changes: 10 additions & 0 deletions tests/rule_assert/err_args_too_many.asm
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
#ruledef test
{
ld {x} =>
{
assert(x < 0x10, "your custom message!", "another message!")
0x55 @ x`8
}
}

ld 0x15 ; error: failed / note:_:3: within / error:_:5: expected 1 to 2 arguments
10 changes: 10 additions & 0 deletions tests/rule_assert/err_condition_wrong_type.asm
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
#ruledef test
{
ld {x} =>
{
assert("your custom message!", x < 0x10)
0x55 @ x`8
}
}

ld 0x15 ; error: failed / note:_:3: within / error:_:5: expected boolean
10 changes: 10 additions & 0 deletions tests/rule_assert/err_message.asm
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
#ruledef test
{
ld {x} =>
{
assert(x < 0x10, "your custom message!")
0x55 @ x`8
}
}

ld 0x15 ; error: failed / note:_:3: within / error:_:5: your custom message!
10 changes: 10 additions & 0 deletions tests/rule_assert/err_message_wrong_type.asm
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
#ruledef test
{
ld {x} =>
{
assert(x < 0x10, 0x1234)
0x55 @ x`8
}
}

ld 0x15 ; error: failed / note:_:3: within / error:_:5: expected string
11 changes: 11 additions & 0 deletions tests/rule_assert/ok_message.asm
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
#ruledef test
{
ld {x} =>
{
assert(x < 0x10, "your custom message!")
0x55 @ x`8
}
}

ld 0x5 ; = 0x5505
ld -0x20 ; = 0x55e0

0 comments on commit c0eace2

Please sign in to comment.