Skip to content

Commit

Permalink
test: fix all tests
Browse files Browse the repository at this point in the history
  • Loading branch information
thesayyn committed Feb 26, 2024
1 parent bf83e77 commit f97cabe
Show file tree
Hide file tree
Showing 5 changed files with 37 additions and 17 deletions.
3 changes: 2 additions & 1 deletion cel_spec/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ fn get_expected_value(value: &prost_reflect::DynamicMessage) -> String {
"uint64_value" => ("UInt(", format!("{}", field.1.as_u64().unwrap()), ")"),
"double_value" => ("Float(", format!("{}.into()", field.1.as_f64().unwrap()), ")"),
"map_value" => ("Map(", String::from("ordered_hash_map::OrderedHashMap::new().into()"), ")"),
"list_value" => ("List(", String::from("Vec::new().into()"), ")" ),
"bytes_value" => ("Bytes(std::rc::Rc::new(Vec::from(", format!("{:?}", field.1.as_bytes().unwrap().to_vec()), ")))"),
_ => ("Null", String::new(), "")
};
Expand Down Expand Up @@ -74,6 +75,6 @@ pub fn suite(attr: TokenStream) -> TokenStream {
ast.push_str("}");
break;
}
println!("{}", ast);
//println!("{}", ast);
ast.parse().unwrap()
}
13 changes: 11 additions & 2 deletions program/src/eval.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ impl Bag for Value {
}

pub struct Eval {
#[allow(dead_code)]
ctx: Context,
}

Expand Down Expand Up @@ -49,12 +50,20 @@ impl Eval {
for (kexpr, vexpr) in entries {
let k = self.eval(kexpr).unpack();
let v = self.eval(vexpr).unpack();
println!("k: {}, v: {}", &k, &v);
map.insert(k, v);
}
Value::Map(Rc::new(map))
}

fn eval_list(&self, elems: Vec<Expression>) -> Value {
let mut list = Vec::with_capacity(elems.len());
for expr in elems {
let v = self.eval(expr).unpack();
list.push(v);
}
Value::List(Rc::new(list))
}

pub fn eval(&self, expr: Expression) -> impl Bag {
match expr {
Expression::Atom(atom) => Value::from(atom),
Expand Down Expand Up @@ -88,7 +97,7 @@ impl Eval {
Expression::And(_, _) => todo!(),
Expression::Unary(_, _) => todo!(),
Expression::Member(expr, member) => self.eval_member(*expr, *member).unpack(),
Expression::List(_) => todo!(),
Expression::List(elems) => self.eval_list(elems),
Expression::Map(entries) => self.eval_map(entries),
Expression::Ident(r) => Value::String(r),
}
Expand Down
30 changes: 20 additions & 10 deletions program/src/program.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,15 +41,25 @@ impl Program {
}


#[test]
fn basic_test() {
let true_cases = [
"6 == 6",
"6 + 12 == 18",
"(6 - 12) == -6",
r#"r"""#
];
for case in true_cases {
assert!(Program::new(case).expect("failed to compile").execute(Context::default()));

#[cfg(test)]
pub mod tests {
macro_rules! string {
($q:literal) => {
crate::Value::String(std::rc::Rc::new($q.into()))
};
}
macro_rules! eval_program {
($expr:literal) => ({
let program = crate::program::Program::new($expr);
assert!(program.is_ok(), "failed to create the program {:?}", program.err());
let program = program.unwrap();
program.eval(crate::context::Context::default())
});
}

#[test]
fn basic_test() {
assert_eq!(eval_program!(r#"r"""#), string!(""));
}
}
6 changes: 4 additions & 2 deletions program/src/value.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,10 @@ pub enum Value {
Bool(bool),
Bytes(Rc<Vec<u8>>),
String(Rc<String>),
Map(Rc<OrderedHashMap<Value, Value>>)
Map(Rc<OrderedHashMap<Value, Value>>),
List(Rc<Vec<Value>>),
}


impl Into<bool> for Value {
fn into(self) -> bool {
match self {
Expand All @@ -28,6 +28,7 @@ impl Into<bool> for Value {
Value::Bytes(v) => v.len() > 0,
Value::String(v) => v.len() > 0,
Value::Map(v) => v.len() > 0,
Value::List(v) => v.len() > 0
}
}
}
Expand Down Expand Up @@ -65,6 +66,7 @@ impl std::fmt::Display for Value {
Value::Bytes(v) => write!(f, "bytes(len = {})", v.len()),
Value::String(v) => write!(f, "string({})", v),
Value::Map(v) => write!(f, "map(len = {})", v.len()),
Value::List(v) => write!(f, "list(len = {})", v.len()),
}
}
}
Expand Down
2 changes: 0 additions & 2 deletions program/tests/test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,3 @@
use cel_spec;

cel_spec::suite!(name = "basic");


0 comments on commit f97cabe

Please sign in to comment.