use crate::utils;
use colored::*;
#[cfg(feature = "regex")]
pub mod err_matches_regex;
#[cfg(feature = "regex")]
pub mod matches_regex;
pub mod equal;
pub mod err;
pub mod greater_than;
pub mod greater_than_or_equal;
pub mod lesser_than;
pub mod lesser_than_or_equal;
pub mod matches_inline_snapshot;
pub mod matches_snapshot;
pub mod ok;
#[derive(Debug)]
pub struct Assertion {
pub description: Option<String>,
pub name: String,
pub args_str: String,
pub failure_message: String,
}
impl Assertion {
pub fn panic(&self) {
panic!(self.get_failure_message());
}
pub fn get_failure_message(&self) -> String {
let message = format!(
"
{separator}
{assertion_expression}
{description}
{failure_message}
{separator}
",
assertion_expression = self.assertion_expression(),
description = utils::add_linebreaks(
self.description
.as_ref()
.unwrap_or(&"Assertion Failure!".to_string())
),
failure_message = self.failure_message,
separator = utils::terminal_separator_line().dimmed(),
);
message
}
pub fn assertion_expression(&self) -> String {
format!(
"{assertion_name}({args});",
assertion_name = format!("{}!", self.name).yellow(),
args = self.args_str
)
}
}
pub fn make_assertion(
name: &str,
args_str: String,
failure_message: Option<String>,
description: Option<&str>,
) -> Option<Assertion> {
if let Some(failure_message) = failure_message {
let assertion = Assertion {
description: description.map(|d| d.into()),
failure_message,
name: name.to_string(),
args_str,
};
if crate::config::should_panic() {
assertion.panic();
}
Some(assertion)
} else {
None
}
}
#[macro_export]
macro_rules! assert_equal {
($left:expr, $right:expr) => {{
use $crate::__macros__::colored::*;
let args_str = format!(
"{}, {}",
stringify!($left).red(),
stringify!($right).green(),
);
$crate::assertions::make_assertion(
"assert_equal",
args_str,
$crate::assertions::equal::assert_equal($left, $right),
None,
)
}};
($left:expr, $right:expr, $description:expr) => {{
use $crate::__macros__::colored::*;
let args_str = format!(
"{}, {}, {}",
stringify!($left).red(),
stringify!($right).green(),
stringify!($description).dimmed(),
);
$crate::assertions::make_assertion(
"assert_equal",
args_str,
$crate::assertions::equal::assert_equal($left, $right),
Some(&$description),
)
}};
}
#[macro_export]
macro_rules! assert_greater_than {
($left:expr, $right:expr) => {{
use $crate::__macros__::colored::*;
let args_str = format!(
"{}, {}",
stringify!($left).red(),
stringify!($right).green(),
);
$crate::assertions::make_assertion(
"assert_greater_than",
args_str,
$crate::assertions::greater_than::assert_greater_than($left, $right),
None,
)
}};
($left:expr, $right:expr, $description:expr) => {{
use $crate::__macros__::colored::*;
let args_str = format!(
"{}, {}, {}",
stringify!($left).red(),
stringify!($right).green(),
stringify!($description).dimmed(),
);
$crate::assertions::make_assertion(
"assert_greater_than",
args_str,
$crate::assertions::greater_than::assert_greater_than($left, $right),
Some(&$description),
)
}};
}
#[macro_export]
macro_rules! assert_greater_than_or_equal {
($left:expr, $right:expr) => {{
use $crate::__macros__::colored::*;
let args_str = format!(
"{}, {}",
stringify!($left).red(),
stringify!($right).green(),
);
$crate::assertions::make_assertion(
"assert_greater_than_or_equal",
args_str,
$crate::assertions::greater_than_or_equal::assert_greater_than_or_equal($left, $right),
None,
)
}};
($left:expr, $right:expr, $description:expr) => {{
use $crate::__macros__::colored::*;
let args_str = format!(
"{}, {}, {}",
stringify!($left).red(),
stringify!($right).green(),
stringify!($description).dimmed(),
);
$crate::assertions::make_assertion(
"assert_greater_than_or_equal",
args_str,
$crate::assertions::greater_than_or_equal::assert_greater_than_or_equal($left, $right),
Some(&$description),
)
}};
}
#[macro_export]
macro_rules! assert_lesser_than {
($left:expr, $right:expr) => {{
use $crate::__macros__::colored::*;
let args_str = format!(
"{}, {}",
stringify!($left).red(),
stringify!($right).green(),
);
$crate::assertions::make_assertion(
"assert_lesser_than",
args_str,
$crate::assertions::lesser_than::assert_lesser_than($left, $right),
None,
)
}};
($left:expr, $right:expr, $description:expr) => {{
use $crate::__macros__::colored::*;
let args_str = format!(
"{}, {}, {}",
stringify!($left).red(),
stringify!($right).green(),
stringify!($description).dimmed(),
);
$crate::assertions::make_assertion(
"assert_lesser_than",
args_str,
$crate::assertions::lesser_than::assert_lesser_than($left, $right),
Some(&$description),
)
}};
}
#[macro_export]
macro_rules! assert_lesser_than_or_equal {
($left:expr, $right:expr) => {{
use $crate::__macros__::colored::*;
let args_str = format!(
"{}, {}",
stringify!($left).red(),
stringify!($right).green(),
);
$crate::assertions::make_assertion(
"assert_lesser_than_or_equal",
args_str,
$crate::assertions::lesser_than_or_equal::assert_lesser_than_or_equal($left, $right),
None,
)
}};
($left:expr, $right:expr, $description:expr) => {{
use $crate::__macros__::colored::*;
let args_str = format!(
"{}, {}, {}",
stringify!($left).red(),
stringify!($right).green(),
stringify!($description).dimmed(),
);
$crate::assertions::make_assertion(
"assert_lesser_than_or_equal",
args_str,
$crate::assertions::lesser_than_or_equal::assert_lesser_than_or_equal($left, $right),
Some(&$description),
)
}};
}
#[cfg(feature = "regex")]
#[macro_export]
macro_rules! assert_matches_regex {
($s:expr, $regex:expr) => {{
use $crate::__macros__::colored::*;
let args_str = format!("{}, {}", stringify!($s).red(), stringify!($regex).green());
$crate::assertions::make_assertion(
"assert_matches_regex",
args_str,
$crate::assertions::matches_regex::assert_matches_regex($s, $regex),
None,
)
}};
($s:expr, $regex:expr, $description:expr) => {{
use $crate::__macros__::colored::*;
let args_str = format!(
"{}, {}, {}",
stringify!($s).red(),
stringify!($regex).green(),
stringify!($description).dimmed()
);
$crate::assertions::make_assertion(
"assert_matches_regex",
args_str,
$crate::assertions::matches_regex::assert_matches_regex($s, $regex),
Some($description),
)
}};
}
#[cfg(feature = "regex")]
#[macro_export]
macro_rules! assert_err_matches_regex {
($err:expr, $regex:expr) => {{
use $crate::__macros__::colored::*;
let args_str = format!("{}, {}", stringify!($err).red(), stringify!($regex).green(),);
$crate::assertions::make_assertion(
"assert_err_matches_regex",
args_str,
$crate::assertions::err_matches_regex::assert_err_matches_regex($err, $regex),
None,
)
}};
($s:expr, $regex:expr, $context:expr) => {{
use $crate::__macros__::colored::*;
let args_str = format!(
"{}, {}, {}",
stringify!($err).red(),
stringify!($regex).green(),
stringify!($description).dimmed(),
);
$crate::assertions::make_assertion(
"assert_err_matches_regex",
args_str,
$crate::assertions::err_matches_regex::assert_err_matches_regex($err, $regex),
Some($description),
)
}};
}
#[macro_export]
macro_rules! assert_matches_snapshot {
($to_snap:expr) => {{
use $crate::__macros__::colored::*;
let line = line!();
let column = column!();
let file = file!();
let args_str = format!("{}", stringify!($to_snap).red(),);
$crate::assertions::make_assertion(
"assert_matches_snapshot",
args_str,
$crate::assertions::matches_snapshot::snap_internal($to_snap, line, column, file),
None,
)
}};
($to_snap:expr, $description:expr) => {{
use $crate::__macros__::colored::*;
let line = line!();
let column = column!();
let file = file!();
let args_str = format!(
"{}, {}",
stringify!($to_snap).red(),
stringify!($description).dimmed(),
);
$crate::assertions::make_assertion(
"assert_matches_snapshot",
args_str,
$crate::assertions::matches_snapshot::snap_internal($to_snap, line, column, file),
Some($description),
)
}};
}
#[macro_export]
macro_rules! assert_matches_inline_snapshot {
($to_snap:expr) => {{
use $crate::__macros__::colored::*;
let line = line!();
let column = column!();
let file = file!();
let args_str = format!("{}", stringify!($to_snap).red(),);
let s: String = $to_snap.into();
$crate::assertions::make_assertion(
"assert_matches_inline_snapshot",
args_str,
$crate::assertions::matches_inline_snapshot::matches_inline_snapshot(
s, None, line, column, file,
),
None,
)
}};
($to_snap:expr, $inline_snap:literal) => {{
use $crate::__macros__::colored::*;
let line = line!();
let column = column!();
let file = file!();
let args_str = format!(
"{}, {}",
stringify!($to_snap).red(),
stringify!($inline_snap).green(),
);
let s: String = $to_snap.into();
$crate::assertions::make_assertion(
"assert_matches_inline_snapshot",
args_str,
$crate::assertions::matches_inline_snapshot::matches_inline_snapshot(
s,
Some($inline_snap),
line,
column,
file,
),
None,
)
}};
}
#[macro_export]
macro_rules! assert_ok {
($left:expr) => {{
use $crate::__macros__::colored::*;
let args_str = format!("{}", stringify!($left).red());
$crate::assertions::make_assertion(
"assert_ok",
args_str,
$crate::assertions::ok::assert_ok($left),
None,
)
}};
($left:expr, $description:expr) => {{
use $crate::__macros__::colored::*;
let args_str = format!(
"{}, {}",
stringify!($left).red(),
stringify!($description).dimmed(),
);
$crate::assertions::make_assertion(
"assert_ok",
args_str,
$crate::assertions::ok::assert_ok($left),
Some(&$description),
)
}};
}
#[macro_export]
macro_rules! assert_err {
($left:expr) => {{
use $crate::__macros__::colored::*;
let args_str = format!("{}", stringify!($left).red());
$crate::assertions::make_assertion(
"assert_err",
args_str,
$crate::assertions::err::assert_err($left),
None,
)
}};
($left:expr, $description:expr) => {{
use $crate::__macros__::colored::*;
let args_str = format!(
"{}, {}",
stringify!($left).red(),
stringify!($description).dimmed(),
);
$crate::assertions::make_assertion(
"assert_err",
args_str,
$crate::assertions::err::assert_err($left),
Some(&$description),
)
}};
}