Skip to content

Commit

Permalink
more tests
Browse files Browse the repository at this point in the history
  • Loading branch information
dzhibas committed Feb 17, 2024
1 parent 36888ea commit b1e4296
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 3 deletions.
6 changes: 4 additions & 2 deletions src/eval.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,11 @@ use std::collections::HashMap;

use crate::ast::{ArrayOp, AstNode, Atom, ComparisonOp, FnCall, LogicOp};

pub type Context<'a> = HashMap<&'a str, Atom>;

fn get_variable_value_from_context<'a>(
variable: &'a AstNode,
context: &'a HashMap<&str, Atom>,
context: &'a Context,
) -> Option<Atom> {
let res = match variable {
AstNode::Variable(Atom::Variable(v)) => context.get(v.as_str()),
Expand All @@ -25,7 +27,7 @@ fn get_variable_value_from_context<'a>(
res.cloned()
}

pub fn eval<'a>(expr: &AstNode, context: &HashMap<&str, Atom>) -> Result<bool, &'a str> {
pub fn eval<'a>(expr: &AstNode, context: &Context) -> Result<bool, &'a str> {
let mut result = false;
result = match expr {
// true || false
Expand Down
8 changes: 7 additions & 1 deletion tests/parser_test.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,16 @@
use std::collections::HashMap;

use bool_expr_parser_nom::ast::Atom;
use bool_expr_parser_nom::eval::Context;
use bool_expr_parser_nom::{self, eval::eval, parse::parse};
use chrono::NaiveDate;

/// library tests for public functions to parse and then evaluate
#[test]
fn test_hashmap_into() {
let out: Context = HashMap::from([("demo", "demo".into())]);
assert_eq!(true, out.get("demo").is_some());
assert_eq!(Atom::String("demo".into()), *out.get("demo").unwrap());
}

#[test]
fn test_parsing() {
Expand Down

0 comments on commit b1e4296

Please sign in to comment.