Skip to content
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
47 changes: 26 additions & 21 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -260,27 +260,32 @@ for `bool`.

for strings of type `String` and `str`:

| assertion | description |
|-----------------------------|--------------------------------------------------------------------------------|
| is_empty | verify that a string is empty |
| is_not_empty | verify that a string is not empty |
| has_length | verify that a string has exactly the expected length |
| has_length_in_range | verify that a string has a length that is in the expected range |
| has_length_less_than | verify that a string has a length less than the expected length |
| has_length_greater_than | verify that a string has a length greater than the expected length |
| has_at_most_length | verify that a string has a length less than or equal to the expected length |
| has_at_least_length | verify that a string has a length greater than or equal to the expected length |
| has_char_count | verify that a string contains exactly the expected number of characters |
| has_char_count_in_range | verify that a string contains a number of characters in the expected range |
| has_char_count_less_than | verify that a string contains less than the expected number of characters |
| has_char_count_greater_than | verify that a string contains more than the expected number of characters |
| has_at_most_char_count | verify that a string contains at most the expected number of characters |
| has_at_least_char_count | verify that a string contains at least the expected number of characters |
| contains | verify that a string contains the expected substring or character |
| starts_with | verify that a string starts with the expected substring or character |
| ends_with | verify that a string ends with the expected substring or character |
| contains_any_of | verify that a string contains any character from a collection of `char`s |
| matches | verify that a string matches the given regex (requires `regex` feature) |
| assertion | description |
|-----------------------------|----------------------------------------------------------------------------------|
| is_empty | verify that a string is empty |
| is_not_empty | verify that a string is not empty |
| has_length | verify that a string has exactly the expected length |
| has_length_in_range | verify that a string has a length that is in the expected range |
| has_length_less_than | verify that a string has a length less than the expected length |
| has_length_greater_than | verify that a string has a length greater than the expected length |
| has_at_most_length | verify that a string has a length less than or equal to the expected length |
| has_at_least_length | verify that a string has a length greater than or equal to the expected length |
| has_char_count | verify that a string contains exactly the expected number of characters |
| has_char_count_in_range | verify that a string contains a number of characters in the expected range |
| has_char_count_less_than | verify that a string contains less than the expected number of characters |
| has_char_count_greater_than | verify that a string contains more than the expected number of characters |
| has_at_most_char_count | verify that a string contains at most the expected number of characters |
| has_at_least_char_count | verify that a string contains at least the expected number of characters |
| contains | verify that a string contains the expected substring or character |
| does_not_contain | verify that a string does not contain the expected substring or character |
| starts_with | verify that a string starts with the expected substring or character |
| does_not_start_with | verify that a string does not start with the expected substring or character |
| ends_with | verify that a string ends with the expected substring or character |
| does_not_end_with | verify that a string does not end with the expected substring or character |
| contains_any_of | verify that a string contains any character from a collection of `char`s |
| does_not_contain_any_of | verify that a string does not contain any character from a collection of `char`s |
| matches | verify that a string matches the given regex (requires `regex` feature) |
| does_not_match | verify that a string does not match the given regex (requires `regex` feature) |

for strings of type `CString` and `CStr`:

Expand Down
106 changes: 103 additions & 3 deletions src/assertions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2234,6 +2234,13 @@ pub trait AssertErrorHasSource<'a, R> {
/// assert_that!(subject).starts_with('d');
/// assert_that!(subject).ends_with("t eum");
/// assert_that!(subject).ends_with('m');
///
/// assert_that!(subject).does_not_contain("pat");
/// assert_that!(subject).does_not_contain('k');
/// assert_that!(subject).does_not_start_with("omi");
/// assert_that!(subject).does_not_start_with('o');
/// assert_that!(subject).does_not_end_with("meum");
/// assert_that!(subject).does_not_end_with('u');
/// ```
pub trait AssertStringPattern<E> {
/// Verifies that a string contains a substring or character.
Expand All @@ -2251,6 +2258,21 @@ pub trait AssertStringPattern<E> {
#[track_caller]
fn contains(self, pattern: E) -> Self;

/// Verifies that a string does not contain a substring or character.
///
/// # Examples
///
/// ```
/// use asserting::prelude::*;
///
/// let subject = "consequat nihil sanctus commodo";
///
/// assert_that!(subject).does_not_contain("nixil");
/// assert_that!(subject).does_not_contain('v');
/// ```
#[track_caller]
fn does_not_contain(self, pattern: E) -> Self;

/// Verifies that a string starts with a substring or character.
///
/// # Examples
Expand All @@ -2266,6 +2288,21 @@ pub trait AssertStringPattern<E> {
#[track_caller]
fn starts_with(self, pattern: E) -> Self;

/// Verifies that a string does not start with a substring or character.
///
/// # Examples
///
/// ```
/// use asserting::prelude::*;
///
/// let subject = "ex nulla nostrud proident";
///
/// assert_that!(subject).does_not_start_with("nulla");
/// assert_that!(subject).does_not_start_with('v');
/// ```
#[track_caller]
fn does_not_start_with(self, pattern: E) -> Self;

/// Verifies that a string ends with a substring or character.
///
/// # Examples
Expand All @@ -2280,6 +2317,19 @@ pub trait AssertStringPattern<E> {
/// ```
#[track_caller]
fn ends_with(self, pattern: E) -> Self;

/// Verifies that a string does not end with a substring or character.
///
/// ```
/// use asserting::prelude::*;
///
/// let subject = "sunt lorem at duo";
///
/// assert_that!(subject).does_not_end_with("duos");
/// assert_that!(subject).does_not_end_with('v');
/// ```
#[track_caller]
fn does_not_end_with(self, pattern: E) -> Self;
}

/// Assert that a string contains any char from a collection of chars.
Expand All @@ -2294,9 +2344,14 @@ pub trait AssertStringPattern<E> {
/// assert_that!(subject).contains_any_of(['a', 'b', 'm', 'z']);
/// assert_that!(subject).contains_any_of(&['a', 'b', 'm', 'z']);
/// assert_that!(subject).contains_any_of(&['a', 'b', 'm', 'z'][..]);
///
/// assert_that!(subject).does_not_contain_any_of(['x', 'y', 'z']);
/// assert_that!(subject).does_not_contain_any_of(&['x', 'y', 'z']);
/// assert_that!(subject).does_not_contain_any_of(&['x', 'y', 'z'][..]);
/// ```
pub trait AssertStringContainsAnyOf<E> {
/// Verifies that a string contains any char from a collection of chars.
/// Verifies that a string contains any char from a collection of
/// characters.
///
/// # Examples
///
Expand All @@ -2310,7 +2365,24 @@ pub trait AssertStringContainsAnyOf<E> {
/// assert_that!(subject).contains_any_of(&['a', 'b', 'm', 'z'][..]);
/// ```
#[track_caller]
fn contains_any_of(self, pattern: E) -> Self;
fn contains_any_of(self, expected: E) -> Self;

/// Verifies that a string does not contain any char from a collection of
/// characters.
///
/// # Examples
///
/// ```
/// use asserting::prelude::*;
///
/// let subject = "sunt lorem at duo";
///
/// assert_that!(subject).does_not_contain_any_of(['v', 'w', 'x']);
/// assert_that!(subject).does_not_contain_any_of(&['v', 'w', 'x']);
/// assert_that!(subject).does_not_contain_any_of(&['v', 'w', 'x'][..]);
/// ```
#[track_caller]
fn does_not_contain_any_of(self, expected: E) -> Self;
}

/// Assert that a string matches a regex pattern.
Expand All @@ -2325,12 +2397,13 @@ pub trait AssertStringContainsAnyOf<E> {
/// use asserting::prelude::*;
///
/// assert_that("tation odio placerat in").matches(r"\b\w{8}\b");
/// assert_that("tation odio placerat in").does_not_match(r"^[A-Z0-9 ]+$");
/// # }
/// ```
#[cfg(feature = "regex")]
#[cfg_attr(docsrs, doc(cfg(feature = "regex")))]
pub trait AssertStringMatches {
/// Verifies that a string matches the given regex pattern.
/// Verifies that a string matches a regex pattern.
///
/// # Example
///
Expand All @@ -2344,8 +2417,35 @@ pub trait AssertStringMatches {
/// assert_that("tation odio placerat in").matches(r"\b\w{8}\b");
/// # }
/// ```
///
/// # Panics
///
/// This method panics if the given regex pattern is invalid or exceeds the
/// size limit.
#[track_caller]
fn matches(self, regex_pattern: &str) -> Self;

/// Verifies that a string does not match a regex pattern.
///
/// # Example
///
/// ```
/// # #[cfg(not(feature = "regex"))]
/// # fn main() {}
/// # #[cfg(feature = "regex")]
/// # fn main() {
/// use asserting::prelude::*;
///
/// assert_that("tation odio placerat in").does_not_match(r"^[A-Z0-9 ]+$");
/// # }
/// ```
///
/// # Panics
///
/// This method panics if the given regex pattern is invalid or exceeds the
/// size limit.
#[track_caller]
fn does_not_match(self, regex_pattern: &str) -> Self;
}

/// Assert that an iterator or collection contains the expected value.
Expand Down
6 changes: 3 additions & 3 deletions src/char/mod.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use crate::assertions::AssertChar;
use crate::colored::{mark_missing_substr, mark_unexpected_char};
use crate::colored::{mark_missing_string, mark_unexpected_char};
use crate::expectations::{
IsAlphabetic, IsAlphanumeric, IsAscii, IsControlChar, IsDigit, IsLowerCase, IsUpperCase,
IsWhitespace,
Expand Down Expand Up @@ -100,7 +100,7 @@ impl Expectation<char> for IsLowerCase {
("", actual.to_lowercase().to_string())
};
let marked_actual = mark_unexpected_char(*actual, format);
let marked_expected = mark_missing_substr(&expected, format);
let marked_expected = mark_missing_string(&expected, format);
format!("expected {expression} to be {not}lowercase\n but was: {marked_actual}\n expected: {marked_expected}")
}
}
Expand Down Expand Up @@ -141,7 +141,7 @@ impl Expectation<char> for IsUpperCase {
("", actual.to_uppercase().to_string())
};
let marked_actual = mark_unexpected_char(*actual, format);
let marked_expected = mark_missing_substr(&expected, format);
let marked_expected = mark_missing_string(&expected, format);
format!("expected {expression} to be {not}uppercase\n but was: {marked_actual}\n expected: {marked_expected}")
}
}
Expand Down
Loading
Loading