Skip to content
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

Add specialized predicates #18

Merged
merged 7 commits into from
Apr 11, 2018
Merged

Add specialized predicates #18

merged 7 commits into from
Apr 11, 2018

Conversation

epage
Copy link
Contributor

@epage epage commented Apr 1, 2018

  • All external crates are optional
  • Specialized functions are in a child module with a name for the type.
    • Names would be verbose anyways
    • This provides the user the opportunity to shorten it if they desire.

Particular notes

@epage epage requested a review from nastevens April 1, 2018 04:37
@epage
Copy link
Contributor Author

epage commented Apr 1, 2018

I think the main remaining predicates that assert_cli will want are

  • Creating a predicate from a file
  • A dir-diff predicate

After that, I'm going to explore ergonomics

  • EqPredicate stores T and its eval takes a &T. But what about Vec/slice, String/str, and PathBuf/path?
  • Adapters from one Item type to another, like wrapping a predicate on str with a predicate on [u8] that will do a from_utf8 on the variable before passing it down.

Copy link
Collaborator

@nastevens nastevens left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There are some things I'd like to see addressed before merging, but this is seriously good stuff.

@@ -0,0 +1,101 @@
use float_cmp::ApproxEq;
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

New files need a copyright notice - I recommend the following:

// Copyright (c) 2018 The predicates-rs Project Developers.
//
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
// option. This file may not be copied, modified, or distributed
// except according to those terms.

I will also plan to go back and modify the notices that list me as the copyright holder and replace with the statement above.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I personally rely on the project-wide license rather than per file.

If you prefer pre-file, we should make sure to get a CONTRIBUTING that mentions that.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The recommendation of the FSF (via the GPLv3) is to include the license statement in all source files, and having dealt with way more FOSS licensing BS than I care to think about, I just feel better having the license in all files to avoid any confusion.

I've opened #22 and #23 to track the other changes needed so the whole project is licensed to the "predicates-rs Project Developers" and to make sure we get a CONTRIBUTING document. For this PR I think just adding the statement above is sufficient.


/// Creates a new `Predicate` that ensures the path doesn't exist.
///
///
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Extra newline here

};
metadata
.map(|m| self.ft.eval(&m.file_type()))
.unwrap_or_default()
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would be more readable with .unwrap_or(false) - it took me three clicks to recognize that this is the default -> for type boolean -> which means false because false is the default for boolean.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In cases like this, I'm on the fence and I just happened to lean the other way :)

///
/// # Examples
///
/// ```ignore
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since all the tests should be false, wouldn't it be okay to un-ignore this doctest?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I guess it still has some value even if it isn't actually testing true for anything.

/// - `"\n" for line-level.
///
/// Default: `"\n"`
pub fn split<S>(mut self, split: S) -> Self
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Instead of forcing a potential allocation, this could use the copy-on-write Cow type

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm always on the fence on allocation vs indirection (Cow). I'll go ahead and switch it to Cow.

/// let predicate_fn = str::diff("Hello World");
/// assert_eq!(true, predicate_fn.eval("Hello World"));
/// ```
pub fn diff<S>(orig: S) -> DifferencePredicate
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The behavior of this predicate seems backwards to me based on the name. diff to me sounds like the predicate should be satisfied (return true) when the strings are different. But the behavior seems to be that the result is true when the strings are similar. Is there a better name that could be used for this - maybe like distance?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

While this has features for edit-distance, as a user, my real care about is showing the diff between the strings once #7 is available and I was going for a name that communicated that.

I can see how the name implies the opposite meaning. What if instead I reversed the behavior of eval instead? The downside is that almost everyone will now need to not it.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would it make sense to create two different predicates that handle the two different cases (looking to see if strings differ, looking to see if strings are the same)? The underlying type could easily be shared between two facades with clear naming.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Eh, sure. Updated.

}
}

/// Creates a new `Predicate` that diffs two strings.
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should actually be something like "applies a regular expression match to a string"

@nastevens
Copy link
Collaborator

EqPredicate stores T and its eval takes a &T. But what about Vec/slice, String/str, and PathBuf/path?

The Predicate trait could be changed to accept Borrow<T> instead of &T. Since Borrow<T> is implemented for &T, this probably won't even end up being a breaking change, and would allow things using String and Vec to be way more ergonomic.

@epage
Copy link
Contributor Author

epage commented Apr 2, 2018

Just force-pushed a fix for everything except diff.

@nastevens
Copy link
Collaborator

Looks like you picked up some stray commits (8c292d1 and 8afa6ae) during the last rebase - those commits already exist on master.

This will be more helpful when assert-rs#7 is implemented.

Fixes assert-rs#9
This is a short term API.  Different issues we'll need to consider when
evolving this include:
- Easy to create predicate
- The ability to customize the regex (e.g. case sensitivity, multi-line,
  ignoring whityespace).
  - It looks like the flags can be enabled with `(?flag)` inside the
    regex. Good enough?
- How error handling should work. Re-export? A single error type for
  the whole crate?

This is a part of assert-rs#12
@epage
Copy link
Contributor Author

epage commented Apr 4, 2018

Fixed

Can't believe I forgot to rebase master after rebase -i.

@epage
Copy link
Contributor Author

epage commented Apr 6, 2018

FYI This should now be ready to go

(I would have merged it already but you've set up the repo to require approval from a reviewer and I've wanted to respect that and not just disable it)

nastevens
nastevens previously approved these changes Apr 11, 2018
Copy link
Collaborator

@nastevens nastevens left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Found some typos - I'll leave it up to you if you want to fix before merging or just tack them onto a different PR. I don't want to hold you up more than I already have.


//! Float Predicates
//!
//! This module contains predicates specifiuc to string handling.
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

typo: "specifiuc"

impl IsClosePredicate {
/// Set the amount of error allowed.
///
/// Values `1`-`5` should work in most cases. Some times more control is needed and you will
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

typo: "Some times" -> "Sometimes"


//! Path Predicates
//!
//! This module contains predicates specifiuc to the file system.
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

typo: specifiuc


//! String Predicates
//!
//! This module contains predicates specifiuc to string handling.
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Another instance of this specifiuc copy-paste ;-)

@epage epage merged commit 083fe5d into assert-rs:master Apr 11, 2018
@epage epage deleted the pred branch April 11, 2018 14:38
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants