Skip to content

Commit

Permalink
Add str::contains_all function
Browse files Browse the repository at this point in the history
  • Loading branch information
Techassi committed Sep 9, 2023
1 parent 2203420 commit 699d288
Show file tree
Hide file tree
Showing 4 changed files with 67 additions and 1 deletion.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Expand Up @@ -7,6 +7,10 @@ and this project adheres to [Semantic Versioning](http://semver.org/).
<!-- next-header -->
## [Unreleased] - ReleaseDate

### Added

- Add `str::contains_all` function

## [3.0.3] - 2023-04-13

### Internal
Expand Down
2 changes: 2 additions & 0 deletions src/lib.rs
Expand Up @@ -143,6 +143,7 @@
//! - [`predicate::str::ends_with`]: Specified string must end with the given needle.
//! - [`predicate::str::contains`]: Specified string must contain the given needle.
//! - [`predicate::str::contains(...).count`]: Required number of times the needle must show up.
//! - [`predicate::str::contains_all`]: Specified string must contain all given needles.
//! - [`predicate::str::is_match`]: Specified string must match the given regex.
//! - [`predicate::str::is_match(...).count`]: Required number of times the match must show up.
//! - [`str_pred.trim`]: Trim whitespace before passing it to `str_pred`.
Expand Down Expand Up @@ -188,6 +189,7 @@
//! [`predicate::path::missing`]: prelude::predicate::path::missing()
//! [`predicate::str::contains(...).count`]: str::ContainsPredicate::count()
//! [`predicate::str::contains`]: prelude::predicate::str::contains()
//! [`predicate::str::contains_all`]: prelude::predicate::str::contains_all()
//! [`predicate::str::diff`]: prelude::predicate::str::diff()
//! [`predicate::str::ends_with`]: prelude::predicate::str::ends_with()
//! [`predicate::str::is_empty`]: prelude::predicate::str::is_empty()
Expand Down
2 changes: 1 addition & 1 deletion src/prelude.rs
Expand Up @@ -28,7 +28,7 @@ pub mod predicate {
/// This module contains predicates specific to string handling.
pub mod str {
pub use crate::str::is_empty;
pub use crate::str::{contains, ends_with, starts_with};
pub use crate::str::{contains, contains_all, ends_with, starts_with};

#[cfg(feature = "diff")]
pub use crate::str::diff;
Expand Down
60 changes: 60 additions & 0 deletions src/str/basics.rs
Expand Up @@ -288,3 +288,63 @@ where
pattern: pattern.into(),
}
}

/// Predicate that checks for all patterns.
///
/// This is created by `predicates::str:contains_all`.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct ContainsAllPredicate {
patterns: Vec<String>,
}

impl Predicate<str> for ContainsAllPredicate {
fn eval(&self, variable: &str) -> bool {
for pattern in &self.patterns {
if !variable.contains(pattern) {
return false;
}
}

true
}
}

impl reflection::PredicateReflection for ContainsAllPredicate {}

impl fmt::Display for ContainsAllPredicate {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
let palette = crate::Palette::new(f.alternate());
write!(
f,
"{}.{}({})",
palette.var("var"),
palette.description("contains_all"),
palette.expected(format!("{:?}", &self.patterns)),
)
}
}

/// Creates a new `Predicate` that ensures a str contains `pattern`
///
/// # Examples
///
/// ```
/// use predicates::prelude::*;
///
/// let predicate_fn = predicate::str::contains_all(vec!["One", "Two", "Three"]);
/// assert_eq!(true, predicate_fn.eval("One Two Three"));
/// assert_eq!(false, predicate_fn.eval("One Two Four"));
/// assert_eq!(false, predicate_fn.eval("Four Five Six"));
/// ```
pub fn contains_all<P, T>(patterns: P) -> ContainsAllPredicate
where
P: IntoIterator<Item = T>,
T: AsRef<str>,
{
let patterns: Vec<_> = patterns
.into_iter()
.map(|p| p.as_ref().to_string())
.collect();

ContainsAllPredicate { patterns }
}

0 comments on commit 699d288

Please sign in to comment.