mirror of
https://github.com/loot/loot-condition-interpreter.git
synced 2026-07-27 14:16:09 -07:00
Merge branch 'not-expressions'
This commit is contained in:
+62
-1
@@ -221,6 +221,7 @@ enum Condition {
|
|||||||
Function(Function),
|
Function(Function),
|
||||||
InvertedFunction(Function),
|
InvertedFunction(Function),
|
||||||
Expression(Expression),
|
Expression(Expression),
|
||||||
|
InvertedExpression(Expression),
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Condition {
|
impl Condition {
|
||||||
@@ -229,6 +230,7 @@ impl Condition {
|
|||||||
Condition::Function(f) => f.eval(state),
|
Condition::Function(f) => f.eval(state),
|
||||||
Condition::InvertedFunction(f) => f.eval(state).map(|r| !r),
|
Condition::InvertedFunction(f) => f.eval(state).map(|r| !r),
|
||||||
Condition::Expression(e) => e.eval(state),
|
Condition::Expression(e) => e.eval(state),
|
||||||
|
Condition::InvertedExpression(e) => e.eval(state).map(|r| !r),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -247,6 +249,14 @@ impl Condition {
|
|||||||
),
|
),
|
||||||
Condition::Expression,
|
Condition::Expression,
|
||||||
),
|
),
|
||||||
|
map(
|
||||||
|
delimited(
|
||||||
|
map_err(preceded(whitespace(tag("not")), whitespace(tag("(")))),
|
||||||
|
parse_expression,
|
||||||
|
map_err(whitespace(tag(")"))),
|
||||||
|
),
|
||||||
|
Condition::InvertedExpression,
|
||||||
|
),
|
||||||
))(input)
|
))(input)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -258,6 +268,7 @@ impl fmt::Display for Condition {
|
|||||||
Function(function) => write!(f, "{}", function),
|
Function(function) => write!(f, "{}", function),
|
||||||
InvertedFunction(function) => write!(f, "not {}", function),
|
InvertedFunction(function) => write!(f, "not {}", function),
|
||||||
Expression(e) => write!(f, "({})", e),
|
Expression(e) => write!(f, "({})", e),
|
||||||
|
InvertedExpression(e) => write!(f, "not ({})", e),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -585,7 +596,7 @@ mod tests {
|
|||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn condition_parse_should_handle_a_inverted_function() {
|
fn condition_parse_should_handle_an_inverted_function() {
|
||||||
let result = Condition::parse("not file(\"Cargo.toml\")".into())
|
let result = Condition::parse("not file(\"Cargo.toml\")".into())
|
||||||
.unwrap()
|
.unwrap()
|
||||||
.1;
|
.1;
|
||||||
@@ -631,6 +642,36 @@ mod tests {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn condition_parse_should_handle_an_inverted_expression_in_parentheses() {
|
||||||
|
let result = Condition::parse("not(not file(\"Cargo.toml\"))".into())
|
||||||
|
.unwrap()
|
||||||
|
.1;
|
||||||
|
|
||||||
|
match result {
|
||||||
|
Condition::InvertedExpression(_) => {}
|
||||||
|
v => panic!(
|
||||||
|
"Expected an expression with two compound conditions, got {:?}",
|
||||||
|
v
|
||||||
|
),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn condition_parse_should_handle_an_inverted_expression_in_parentheses_with_whitespace() {
|
||||||
|
let result = Condition::parse("not ( not file(\"Cargo.toml\") )".into())
|
||||||
|
.unwrap()
|
||||||
|
.1;
|
||||||
|
|
||||||
|
match result {
|
||||||
|
Condition::InvertedExpression(_) => {}
|
||||||
|
v => panic!(
|
||||||
|
"Expected an expression with two compound conditions, got {:?}",
|
||||||
|
v
|
||||||
|
),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn condition_eval_should_return_function_eval_for_a_function_condition() {
|
fn condition_eval_should_return_function_eval_for_a_function_condition() {
|
||||||
let state = state(".");
|
let state = state(".");
|
||||||
@@ -669,6 +710,17 @@ mod tests {
|
|||||||
assert!(condition.eval(&state).unwrap());
|
assert!(condition.eval(&state).unwrap());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn condition_eval_should_return_inverse_of_expression_eval_for_a_not_expression_condition() {
|
||||||
|
let state = state(".");
|
||||||
|
|
||||||
|
let condition = Condition::InvertedExpression(Expression(vec![CompoundCondition(vec![
|
||||||
|
Condition::Function(Function::FilePath(PathBuf::from("Cargo.toml"))),
|
||||||
|
])]));
|
||||||
|
|
||||||
|
assert!(!condition.eval(&state).unwrap());
|
||||||
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn condition_fmt_should_format_function_correctly() {
|
fn condition_fmt_should_format_function_correctly() {
|
||||||
let condition = Condition::Function(Function::FilePath(PathBuf::from("Cargo.toml")));
|
let condition = Condition::Function(Function::FilePath(PathBuf::from("Cargo.toml")));
|
||||||
@@ -693,6 +745,15 @@ mod tests {
|
|||||||
assert_eq!("(file(\"Cargo.toml\"))", &format!("{}", condition));
|
assert_eq!("(file(\"Cargo.toml\"))", &format!("{}", condition));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn condition_fmt_should_format_inverted_expression_correctly() {
|
||||||
|
let condition = Condition::InvertedExpression(Expression(vec![CompoundCondition(vec![
|
||||||
|
Condition::Function(Function::FilePath(PathBuf::from("Cargo.toml"))),
|
||||||
|
])]));
|
||||||
|
|
||||||
|
assert_eq!("not (file(\"Cargo.toml\"))", &format!("{}", condition));
|
||||||
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn compound_condition_eval_should_be_true_if_all_conditions_are_true() {
|
fn compound_condition_eval_should_be_true_if_all_conditions_are_true() {
|
||||||
let state = state(".");
|
let state = state(".");
|
||||||
|
|||||||
Reference in New Issue
Block a user