Skip to content

A way to display *why* a predicate failed #60

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 7 commits into from
Jul 28, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,9 @@ difference = { version = "2.0", optional = true }
normalize-line-endings = { version = "0.2.2", optional = true }
regex = { version="1.0", optional = true }
float-cmp = { version="0.4", optional = true }
treeline = { version = "0.1", optional = true }

[features]
default = ["difference", "regex", "float-cmp", "normalize-line-endings"]
default = ["difference", "regex", "float-cmp", "normalize-line-endings", "tree"]
unstable = []
tree = ["treeline",]
15 changes: 15 additions & 0 deletions examples/case_tree.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
extern crate predicates;

use predicates::prelude::*;
use predicates::tree::CaseTreeExt;

fn main() {
let pred = predicate::ne(5).not().and(predicate::ge(5));

let var = 5;
let case = pred.find_case(true, &var);
if let Some(case) = case {
println!("var is {}", var);
println!("{}", case.tree());
}
}
254 changes: 254 additions & 0 deletions src/boolean.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
use std::fmt;
use std::marker::PhantomData;

use reflection;
use Predicate;

/// Predicate that combines two `Predicate`s, returning the AND of the results.
Expand Down Expand Up @@ -53,6 +54,39 @@ where
fn eval(&self, item: &Item) -> bool {
self.a.eval(item) && self.b.eval(item)
}

fn find_case<'a>(&'a self, expected: bool, variable: &Item) -> Option<reflection::Case<'a>> {
let child_a = self.a.find_case(expected, variable);
match (expected, child_a) {
(true, Some(child_a)) => self.b.find_case(expected, variable).map(|child_b| {
reflection::Case::new(Some(self), expected)
.add_child(child_a)
.add_child(child_b)
}),
(true, None) => None,
(false, Some(child_a)) => {
Some(reflection::Case::new(Some(self), expected).add_child(child_a))
}
(false, None) => self.b
.find_case(expected, variable)
.map(|child_b| reflection::Case::new(Some(self), expected).add_child(child_b)),
}
}
}

impl<M1, M2, Item> reflection::PredicateReflection for AndPredicate<M1, M2, Item>
where
M1: Predicate<Item>,
M2: Predicate<Item>,
Item: ?Sized,
{
fn children<'a>(&'a self) -> Box<Iterator<Item = reflection::Child<'a>> + 'a> {
let params = vec![
reflection::Child::new("left", &self.a),
reflection::Child::new("right", &self.b),
];
Box::new(params.into_iter())
}
}

impl<M1, M2, Item> fmt::Display for AndPredicate<M1, M2, Item>
Expand All @@ -66,6 +100,91 @@ where
}
}

#[cfg(test)]
mod test_and {
use prelude::*;

#[test]
fn find_case_true() {
assert!(
predicate::always()
.and(predicate::always())
.find_case(true, &5)
.is_some()
);
}

#[test]
fn find_case_true_left_fail() {
assert!(
predicate::never()
.and(predicate::always())
.find_case(true, &5)
.is_none()
);
}

#[test]
fn find_case_true_right_fail() {
assert!(
predicate::always()
.and(predicate::never())
.find_case(true, &5)
.is_none()
);
}

#[test]
fn find_case_true_fails() {
assert!(
predicate::never()
.and(predicate::never())
.find_case(true, &5)
.is_none()
);
}

#[test]
fn find_case_false() {
assert!(
predicate::never()
.and(predicate::never())
.find_case(false, &5)
.is_some()
);
}

#[test]
fn find_case_false_fails() {
assert!(
predicate::always()
.and(predicate::always())
.find_case(false, &5)
.is_none()
);
}

#[test]
fn find_case_false_left_fail() {
assert!(
predicate::never()
.and(predicate::always())
.find_case(false, &5)
.is_some()
);
}

#[test]
fn find_case_false_right_fail() {
assert!(
predicate::always()
.and(predicate::never())
.find_case(false, &5)
.is_some()
);
}
}

/// Predicate that combines two `Predicate`s, returning the OR of the results.
///
/// This is created by the `Predicate::or` function.
Expand Down Expand Up @@ -106,6 +225,39 @@ where
fn eval(&self, item: &Item) -> bool {
self.a.eval(item) || self.b.eval(item)
}

fn find_case<'a>(&'a self, expected: bool, variable: &Item) -> Option<reflection::Case<'a>> {
let child_a = self.a.find_case(expected, variable);
match (expected, child_a) {
(true, Some(child_a)) => {
Some(reflection::Case::new(Some(self), expected).add_child(child_a))
}
(true, None) => self.b
.find_case(expected, variable)
.map(|child_b| reflection::Case::new(Some(self), expected).add_child(child_b)),
(false, Some(child_a)) => self.b.find_case(expected, variable).map(|child_b| {
reflection::Case::new(Some(self), expected)
.add_child(child_a)
.add_child(child_b)
}),
(false, None) => None,
}
}
}

impl<M1, M2, Item> reflection::PredicateReflection for OrPredicate<M1, M2, Item>
where
M1: Predicate<Item>,
M2: Predicate<Item>,
Item: ?Sized,
{
fn children<'a>(&'a self) -> Box<Iterator<Item = reflection::Child<'a>> + 'a> {
let params = vec![
reflection::Child::new("left", &self.a),
reflection::Child::new("right", &self.b),
];
Box::new(params.into_iter())
}
}

impl<M1, M2, Item> fmt::Display for OrPredicate<M1, M2, Item>
Expand All @@ -119,6 +271,91 @@ where
}
}

#[cfg(test)]
mod test_or {
use prelude::*;

#[test]
fn find_case_true() {
assert!(
predicate::always()
.or(predicate::always())
.find_case(true, &5)
.is_some()
);
}

#[test]
fn find_case_true_left_fail() {
assert!(
predicate::never()
.or(predicate::always())
.find_case(true, &5)
.is_some()
);
}

#[test]
fn find_case_true_right_fail() {
assert!(
predicate::always()
.or(predicate::never())
.find_case(true, &5)
.is_some()
);
}

#[test]
fn find_case_true_fails() {
assert!(
predicate::never()
.or(predicate::never())
.find_case(true, &5)
.is_none()
);
}

#[test]
fn find_case_false() {
assert!(
predicate::never()
.or(predicate::never())
.find_case(false, &5)
.is_some()
);
}

#[test]
fn find_case_false_fails() {
assert!(
predicate::always()
.or(predicate::always())
.find_case(false, &5)
.is_none()
);
}

#[test]
fn find_case_false_left_fail() {
assert!(
predicate::never()
.or(predicate::always())
.find_case(false, &5)
.is_none()
);
}

#[test]
fn find_case_false_right_fail() {
assert!(
predicate::always()
.or(predicate::never())
.find_case(false, &5)
.is_none()
);
}
}

/// Predicate that returns a `Predicate` taking the logical NOT of the result.
///
/// This is created by the `Predicate::not` function.
Expand Down Expand Up @@ -154,6 +391,23 @@ where
fn eval(&self, item: &Item) -> bool {
!self.inner.eval(item)
}

fn find_case<'a>(&'a self, expected: bool, variable: &Item) -> Option<reflection::Case<'a>> {
self.inner
.find_case(!expected, variable)
.map(|child| reflection::Case::new(Some(self), expected).add_child(child))
}
}

impl<M, Item> reflection::PredicateReflection for NotPredicate<M, Item>
where
M: Predicate<Item>,
Item: ?Sized,
{
fn children<'a>(&'a self) -> Box<Iterator<Item = reflection::Child<'a>> + 'a> {
let params = vec![reflection::Child::new("predicate", &self.inner)];
Box::new(params.into_iter())
}
}

impl<M, Item> fmt::Display for NotPredicate<M, Item>
Expand Down
19 changes: 19 additions & 0 deletions src/boxed.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@

use std::fmt;

use core;
use reflection;
use Predicate;

/// `Predicate` that wraps another `Predicate` as a trait object, allowing
Expand Down Expand Up @@ -40,6 +42,19 @@ where
}
}

impl<Item> reflection::PredicateReflection for BoxPredicate<Item>
where
Item: ?Sized,
{
fn parameters<'a>(&'a self) -> Box<Iterator<Item = reflection::Parameter<'a>> + 'a> {
self.0.parameters()
}

fn children<'a>(&'a self) -> Box<Iterator<Item = reflection::Child<'a>> + 'a> {
self.0.children()
}
}

impl<Item> fmt::Display for BoxPredicate<Item>
where
Item: ?Sized,
Expand All @@ -56,6 +71,10 @@ where
fn eval(&self, variable: &Item) -> bool {
self.0.eval(variable)
}

fn find_case<'a>(&'a self, expected: bool, variable: &Item) -> Option<reflection::Case<'a>> {
core::default_find_case(self, expected, variable)
}
}

/// `Predicate` extension for boxing a `Predicate`.
Expand Down
13 changes: 13 additions & 0 deletions src/constant.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@
use std::fmt;
use std::marker::PhantomData;

use core;
use reflection;
use Predicate;

/// Predicate that always returns a constant (boolean) result.
Expand All @@ -26,6 +28,17 @@ impl<Item> Predicate<Item> for BooleanPredicate<Item> {
fn eval(&self, _variable: &Item) -> bool {
self.retval
}

fn find_case<'a>(&'a self, expected: bool, variable: &Item) -> Option<reflection::Case<'a>> {
core::default_find_case(self, expected, variable)
}
}

impl<Item> reflection::PredicateReflection for BooleanPredicate<Item> {
fn parameters<'a>(&'a self) -> Box<Iterator<Item = reflection::Parameter<'a>> + 'a> {
let params = vec![reflection::Parameter::new("value", &self.retval)];
Box::new(params.into_iter())
}
}

impl<Item> fmt::Display for BooleanPredicate<Item> {
Expand Down
Loading