Skip to content

Commit

Permalink
Predicate test
Browse files Browse the repository at this point in the history
Signed-off-by: Alex Snaps <[email protected]>
  • Loading branch information
alexsnaps committed Oct 21, 2024
1 parent bf4f2f2 commit 92cc309
Showing 1 changed file with 14 additions and 0 deletions.
14 changes: 14 additions & 0 deletions src/data/cel.rs
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,12 @@ pub struct Predicate {
}

impl Predicate {
pub fn new(predicate: &str) -> Result<Self, ParseError> {
Ok(Self {
expression: Expression::new(predicate)?,
})
}

pub fn test(&self) -> bool {
match self.expression.eval() {
Value::Bool(result) => result,
Expand Down Expand Up @@ -408,9 +414,17 @@ pub mod data {

#[cfg(test)]
mod tests {
use crate::data::cel::Predicate;
use crate::data::known_attribute_for;
use cel_interpreter::objects::ValueType;

#[test]
fn predicates() {
let predicate = Predicate::new("source.port == 65432").expect("This is valid CEL!");
super::super::property::test::TEST_PROPERTY_VALUE.set(Some(65432_i64.to_le_bytes().into()));
assert!(predicate.test());
}

#[test]
fn attribute_resolve() {
super::super::property::test::TEST_PROPERTY_VALUE.set(Some(80_i64.to_le_bytes().into()));
Expand Down

0 comments on commit 92cc309

Please sign in to comment.