Skip to content

Commit

Permalink
feat: Allow any Expr at the top level of a Policy Expression
Browse files Browse the repository at this point in the history
This will enable testing policy expressions with a plain `#t` or `#f` to force
success or failure, as well as permit a JSON Pointer at the top level.
  • Loading branch information
cstepanian committed Sep 13, 2024
1 parent 027d4ef commit 5a85ac5
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 1 deletion.
10 changes: 9 additions & 1 deletion hipcheck/src/policy_exprs/expr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -183,7 +183,7 @@ fn parse_function(input: Input<'_>) -> IResult<Input<'_>, Expr> {

pub fn parse(input: &str) -> Result<Expr> {
let tokens = Tokens::new(input);
let mut parser = all_consuming(parse_function);
let mut parser = all_consuming(parse_expr);

match parser(tokens).finish() {
Ok((rest, expr)) if rest.is_empty() => Ok(expr),
Expand Down Expand Up @@ -238,6 +238,14 @@ mod tests {
Expr::Array(vals)
}

#[test]
fn parse_bool() {
let input = "#t";
let expected = boolean(true).into_expr();
let result = parse(input).unwrap();
assert_eq!(result, expected);
}

#[test]
fn parse_function() {
let input = "(add 2 3)";
Expand Down
8 changes: 8 additions & 0 deletions hipcheck/src/policy_exprs/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,14 @@ mod tests {
use super::*;
use test_log::test;

#[test]
fn run_bool() {
let program = "#t";
let context = Value::Null;
let is_true = Executor::std().run(program, &context).unwrap();
assert!(is_true);
}

#[test]
fn run_basic() {
let program = "(eq (add 1 2) 3)";
Expand Down

0 comments on commit 5a85ac5

Please sign in to comment.