use alloc::string::String;
use crate::prelude::StrSliceAssertions;
use crate::{AssertThat, Mode};
#[allow(clippy::return_self_not_must_use)]
#[cfg_attr(feature = "fluent", assertr_derive::fluent_aliases)]
pub trait StringAssertions {
#[cfg_attr(feature = "fluent", fluent_alias("not_be_blank"))]
fn is_not_blank(self) -> Self;
fn is_equal_to_ignoring_ascii_case(self, expected: impl AsRef<str>) -> Self;
fn contains(self, expected: impl AsRef<str>) -> Self;
fn does_not_contain(self, unexpected: impl AsRef<str>) -> Self;
fn starts_with(self, expected: impl AsRef<str>) -> Self;
fn does_not_start_with(self, unexpected: impl AsRef<str>) -> Self;
fn ends_with(self, expected: impl AsRef<str>) -> Self;
fn does_not_end_with(self, unexpected: impl AsRef<str>) -> Self;
}
impl<M: Mode> StringAssertions for AssertThat<'_, String, M> {
#[track_caller]
fn is_not_blank(self) -> Self {
self.derive(String::as_str).is_not_blank();
self
}
#[track_caller]
fn is_equal_to_ignoring_ascii_case(self, expected: impl AsRef<str>) -> Self {
self.derive(String::as_str)
.is_equal_to_ignoring_ascii_case(expected);
self
}
#[track_caller]
fn contains(self, expected: impl AsRef<str>) -> Self {
self.derive(String::as_str).contains(expected);
self
}
#[track_caller]
fn does_not_contain(self, unexpected: impl AsRef<str>) -> Self {
self.derive(String::as_str).does_not_contain(unexpected);
self
}
#[track_caller]
fn starts_with(self, expected: impl AsRef<str>) -> Self {
self.derive(String::as_str).starts_with(expected);
self
}
#[track_caller]
fn does_not_start_with(self, unexpected: impl AsRef<str>) -> Self {
self.derive(String::as_str).does_not_start_with(unexpected);
self
}
#[track_caller]
fn ends_with(self, expected: impl AsRef<str>) -> Self {
self.derive(String::as_str).ends_with(expected);
self
}
#[track_caller]
fn does_not_end_with(self, unexpected: impl AsRef<str>) -> Self {
self.derive(String::as_str).does_not_end_with(unexpected);
self
}
}
#[cfg(test)]
mod tests {
mod is_not_blank {
use crate::prelude::*;
use indoc::formatdoc;
#[test]
fn succeeds_when_not_blank() {
assert_that!(String::from("hello")).is_not_blank();
}
#[test]
fn panics_when_blank() {
assert_that_panic_by(|| {
assert_that!(String::from(" \t"))
.with_location(false)
.is_not_blank();
})
.has_type::<String>()
.is_equal_to(formatdoc! {r#"
-------- assertr --------
Actual: " \t"
is blank.
Expected it to contain at least one non-whitespace character.
-------- assertr --------
"#});
}
}
mod is_equal_to_ignoring_ascii_case {
use crate::prelude::*;
use indoc::formatdoc;
#[test]
fn succeeds_when_equal_ignoring_ascii_case() {
assert_that!(String::from("FoObAr")).is_equal_to_ignoring_ascii_case("fOoBaR");
}
#[test]
fn panics_when_not_equal_to_ignoring_ascii_case() {
assert_that_panic_by(|| {
assert_that!(String::from("foo"))
.with_location(false)
.is_equal_to_ignoring_ascii_case("bar");
})
.has_type::<String>()
.is_equal_to(formatdoc! {r#"
-------- assertr --------
Expected: "bar"
Actual: "foo"
Details: [
Actual is not equal to expected, even when ignoring ASCII casing.,
]
-------- assertr --------
"#});
}
}
mod contains {
use crate::prelude::*;
use indoc::formatdoc;
#[test]
fn succeeds_when_expected_is_contained() {
assert_that!(String::from("foobar")).contains("foo");
assert_that!(String::from("foobar")).contains("bar");
assert_that!(String::from("foobar")).contains("oob");
}
#[test]
fn panics_when_expected_is_not_contained() {
assert_that_panic_by(|| {
assert_that!(String::from("foo bar baz"))
.with_location(false)
.contains("42");
})
.has_type::<String>()
.is_equal_to(formatdoc! {r#"
-------- assertr --------
Actual: "foo bar baz"
does not contain
Expected: "42"
-------- assertr --------
"#});
}
}
mod does_not_contain {
use crate::prelude::*;
use indoc::formatdoc;
#[test]
fn succeeds_when_expected_is_not_contained() {
assert_that!(String::from("foobar")).does_not_contain("hello");
}
#[test]
fn panics_when_expected_is_contained() {
assert_that_panic_by(|| {
assert_that!(String::from("foo bar baz"))
.with_location(false)
.does_not_contain("ar b");
})
.has_type::<String>()
.is_equal_to(formatdoc! {r#"
-------- assertr --------
Actual: "foo bar baz"
contains
Unexpected: "ar b"
-------- assertr --------
"#});
}
}
mod starts_with {
use crate::prelude::*;
use indoc::formatdoc;
#[test]
fn succeeds_when_start_matches() {
assert_that!(String::from("foo bar baz")).starts_with("foo b");
}
#[test]
fn panics_when_start_does_not_match() {
assert_that_panic_by(|| {
assert_that!(String::from("foo bar baz"))
.with_location(false)
.starts_with("oo");
})
.has_type::<String>()
.is_equal_to(formatdoc! {r#"
-------- assertr --------
Actual: "foo bar baz"
does not start with
Expected: "oo"
-------- assertr --------
"#});
}
}
mod does_not_start_with {
use crate::prelude::*;
use indoc::formatdoc;
#[test]
fn succeeds_when_start_does_not_match() {
assert_that!(String::from("foo bar baz")).does_not_start_with("of");
}
#[test]
fn panics_when_start_matches() {
assert_that_panic_by(|| {
assert_that!(String::from("foo bar baz"))
.with_location(false)
.does_not_start_with("foo bar ba");
})
.has_type::<String>()
.is_equal_to(formatdoc! {r#"
-------- assertr --------
Actual: "foo bar baz"
starts with
Unexpected: "foo bar ba"
-------- assertr --------
"#});
}
}
mod ends_with {
use crate::prelude::*;
use indoc::formatdoc;
#[test]
fn succeeds_when_end_matches() {
assert_that!(String::from("foo bar baz")).ends_with("r baz");
}
#[test]
fn panics_when_end_is_different() {
assert_that_panic_by(|| {
assert_that!(String::from("foo bar baz"))
.with_location(false)
.ends_with("raz");
})
.has_type::<String>()
.is_equal_to(formatdoc! {r#"
-------- assertr --------
Actual: "foo bar baz"
does not end with
Expected: "raz"
-------- assertr --------
"#});
}
}
mod does_not_end_with {
use crate::prelude::*;
use indoc::formatdoc;
#[test]
fn succeeds_when_end_does_not_matches() {
assert_that!(String::from("foo bar baz")).does_not_end_with("bar");
}
#[test]
fn panics_when_end_matches() {
assert_that_panic_by(|| {
assert_that!(String::from("foo bar baz"))
.with_location(false)
.does_not_end_with(" baz");
})
.has_type::<String>()
.is_equal_to(formatdoc! {r#"
-------- assertr --------
Actual: "foo bar baz"
ends with
Unexpected: " baz"
-------- assertr --------
"#});
}
}
}