split condition check to standalone module

This commit is contained in:
sagie gur ari 2019-12-31 18:31:38 +00:00
parent 9971e20750
commit 5ce4e251e5
4 changed files with 76 additions and 9 deletions

View file

@ -1,6 +1,6 @@
use crate::utils::instruction_query;
use crate::utils::state::{get_core_sub_state_for_command, get_list, get_sub_state};
use crate::utils::{eval, pckg};
use crate::utils::{condition, eval, pckg};
use duckscript::types::command::{Command, CommandResult, Commands, GoToValue};
use duckscript::types::error::ScriptError;
use duckscript::types::instruction::Instruction;
@ -156,15 +156,9 @@ fn eval_condition(
) -> Result<bool, String> {
match eval::eval(&arguments, state, variables, commands) {
CommandResult::Continue(value) => {
let failed = match value {
Some(value_str) => {
let lower_case = value_str.to_lowercase();
lower_case == "0" || lower_case == "false" || lower_case == "no"
}
None => true,
};
let passed = condition::is_true(value);
Ok(!failed)
Ok(passed)
}
CommandResult::Error(error) => Err(error.to_string()),
_ => Err("Invalid condition evaluation result.".to_string()),

View file

@ -0,0 +1,15 @@
#[cfg(test)]
#[path = "./condition_test.rs"]
mod condition_test;
pub(crate) fn is_true(value: Option<String>) -> bool {
let failed = match value {
Some(value_str) => {
let lower_case = value_str.to_lowercase();
lower_case == "" || lower_case == "0" || lower_case == "false" || lower_case == "no"
}
None => true,
};
!failed
}

View file

@ -0,0 +1,57 @@
use super::*;
#[test]
fn is_true_none() {
let passed = is_true(None);
assert!(!passed);
}
#[test]
fn is_true_0() {
let passed = is_true(Some("0".to_string()));
assert!(!passed);
}
#[test]
fn is_true_empty() {
let passed = is_true(Some("".to_string()));
assert!(!passed);
}
#[test]
fn is_true_no() {
let passed = is_true(Some("no".to_string()));
assert!(!passed);
}
#[test]
fn is_true_no_uppercase() {
let passed = is_true(Some("NO".to_string()));
assert!(!passed);
}
#[test]
fn is_true_false() {
let passed = is_true(Some("false".to_string()));
assert!(!passed);
}
#[test]
fn is_true_false_uppercase() {
let passed = is_true(Some("FALSE".to_string()));
assert!(!passed);
}
#[test]
fn is_true_valid() {
let passed = is_true(Some("some value".to_string()));
assert!(passed);
}

View file

@ -1,3 +1,4 @@
pub(crate) mod condition;
pub(crate) mod eval;
pub(crate) mod instruction_query;
pub(crate) mod io;