Skip to content

Commit

Permalink
Move assert_err into test_util
Browse files Browse the repository at this point in the history
  • Loading branch information
LucasPickering committed May 1, 2024
1 parent e404701 commit d484ad6
Show file tree
Hide file tree
Showing 8 changed files with 23 additions and 24 deletions.
2 changes: 1 addition & 1 deletion src/collection/recipe_tree.rs
Original file line number Diff line number Diff line change
Expand Up @@ -230,7 +230,7 @@ impl From<&Vec<&RecipeId>> for RecipeLookupKey {
#[cfg(test)]
mod tests {
use super::*;
use crate::{test_util::*, util::assert_err};
use crate::test_util::*;
use factori::create;
use indexmap::indexmap;
use itertools::Itertools;
Expand Down
2 changes: 1 addition & 1 deletion src/http/content_type.rs
Original file line number Diff line number Diff line change
Expand Up @@ -172,7 +172,7 @@ impl ContentType {
#[cfg(test)]
mod tests {
use super::*;
use crate::{test_util::_Factori_Builder_Response, util::assert_err};
use crate::test_util::*;
use factori::create;
use reqwest::header::{
HeaderMap, HeaderValue, InvalidHeaderValue, CONTENT_TYPE,
Expand Down
2 changes: 1 addition & 1 deletion src/http/query.rs
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ impl Query {
#[cfg(test)]
mod tests {
use super::*;
use crate::{http::Json, util::assert_err};
use crate::{http::Json, test_util::*};
use rstest::rstest;
use serde_json::json;

Expand Down
1 change: 0 additions & 1 deletion src/template.rs
Original file line number Diff line number Diff line change
Expand Up @@ -183,7 +183,6 @@ mod tests {
config::Config,
http::{ContentType, RequestRecord},
test_util::*,
util::assert_err,
};
use chrono::Utc;
use factori::create;
Expand Down
2 changes: 1 addition & 1 deletion src/template/parse.rs
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,7 @@ fn take_until_or_eof<'a>(
#[cfg(test)]
mod tests {
use super::*;
use crate::util::assert_err;
use crate::test_util::*;
use itertools::Itertools;
use rstest::rstest;

Expand Down
18 changes: 18 additions & 0 deletions src/test_util.rs
Original file line number Diff line number Diff line change
Expand Up @@ -186,3 +186,21 @@ pub fn header_map<'a>(
})
.collect()
}

/// Assert a result is the `Err` variant, and the stringified error contains
/// the given message
macro_rules! assert_err {
($e:expr, $msg:expr) => {{
use itertools::Itertools as _;

let msg = $msg;
// Include all source errors so wrappers don't hide the important stuff
let error: anyhow::Error = $e.unwrap_err().into();
let actual = error.chain().map(ToString::to_string).join(": ");
assert!(
actual.contains(msg),
"Expected error message to contain {msg:?}, but was: {actual:?}"
)
}};
}
pub(crate) use assert_err;
2 changes: 1 addition & 1 deletion src/tui/input.rs
Original file line number Diff line number Diff line change
Expand Up @@ -536,7 +536,7 @@ fn stringify_key_modifier(modifier: KeyModifiers) -> Cow<'static, str> {
#[cfg(test)]
mod tests {
use super::*;
use crate::util::assert_err;
use crate::test_util::*;
use crossterm::event::MediaKeyCode;
use rstest::rstest;
use serde_test::{assert_de_tokens, assert_de_tokens_error, Token};
Expand Down
18 changes: 0 additions & 18 deletions src/util.rs
Original file line number Diff line number Diff line change
Expand Up @@ -302,24 +302,6 @@ where
{
}

#[cfg(test)]
macro_rules! assert_err {
($e:expr, $msg:expr) => {{
use itertools::Itertools as _;

let msg = $msg;
// Include all source errors so wrappers don't hide the important stuff
let error: anyhow::Error = $e.unwrap_err().into();
let actual = error.chain().map(ToString::to_string).join(": ");
assert!(
actual.contains(msg),
"Expected error message to contain {msg:?}, but was: {actual:?}"
)
}};
}
#[cfg(test)]
pub(crate) use assert_err;

#[cfg(test)]
mod tests {
use super::*;
Expand Down

0 comments on commit d484ad6

Please sign in to comment.