String assertions
String assertions operate on string types:
// Compare two strings and diff the results
assert_str_eq!("hello, world", "hello");
// Ensure that the second string is contained within the first
assert_str_contains!("hello, world", "hello");
// Ensure that the first string starts with the second
assert_str_starts_with!("hello, world", "hello");
// Ensure that the first string ends with the second
assert_str_ends_with!("hello, world", "world");
// Ensure that the first string matches the second regex
assert_str_matches!("hello, world", "[a-z, ]+");
Regular expressions for assert_str_matches
follow the rules for the regex
crate.
Details (advanced)
Both arguments for all these asserts do not have to be a particular string type, but must implement the AsRef<str>
trait.
Assertion | Panic condition |
---|---|
assert_str_contains |
!x.as_ref().contains(y.as_ref()) |
assert_str_starts_with |
!x.as_ref().starts_with(y.as_ref()) |
assert_str_ends_with |
!x.as_ref().ends_with(y.as_ref()) |
assert_str_matches |
Regex::new(y.as_ref())?.is_match(x.as_ref()) |