mirror of
https://github.com/sagiegurari/duckscript
synced 2024-10-04 15:09:08 +00:00
split condition check to standalone module
This commit is contained in:
parent
9971e20750
commit
5ce4e251e5
|
@ -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()),
|
||||
|
|
15
duckscript_sdk/src/utils/condition.rs
Normal file
15
duckscript_sdk/src/utils/condition.rs
Normal 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
|
||||
}
|
57
duckscript_sdk/src/utils/condition_test.rs
Normal file
57
duckscript_sdk/src/utils/condition_test.rs
Normal 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);
|
||||
}
|
|
@ -1,3 +1,4 @@
|
|||
pub(crate) mod condition;
|
||||
pub(crate) mod eval;
|
||||
pub(crate) mod instruction_query;
|
||||
pub(crate) mod io;
|
||||
|
|
Loading…
Reference in a new issue