From 5a85ac5d045acf41aaa8d1afc83fb3296d5543fe Mon Sep 17 00:00:00 2001
From: Cal Stepanian <61707847+cstepanian@users.noreply.github.com>
Date: Thu, 12 Sep 2024 19:30:34 -0400
Subject: [PATCH] feat: Allow any `Expr` at the top level of a Policy
Expression
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.
---
hipcheck/src/policy_exprs/expr.rs | 10 +++++++++-
hipcheck/src/policy_exprs/mod.rs | 8 ++++++++
2 files changed, 17 insertions(+), 1 deletion(-)
diff --git a/hipcheck/src/policy_exprs/expr.rs b/hipcheck/src/policy_exprs/expr.rs
index 83079de1..bd4c575e 100644
--- a/hipcheck/src/policy_exprs/expr.rs
+++ b/hipcheck/src/policy_exprs/expr.rs
@@ -183,7 +183,7 @@ fn parse_function(input: Input<'_>) -> IResult, Expr> {
pub fn parse(input: &str) -> Result {
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),
@@ -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)";
diff --git a/hipcheck/src/policy_exprs/mod.rs b/hipcheck/src/policy_exprs/mod.rs
index d838b656..c030275e 100644
--- a/hipcheck/src/policy_exprs/mod.rs
+++ b/hipcheck/src/policy_exprs/mod.rs
@@ -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)";