Skip to content

Commit

Permalink
Implement custom paths for all assertions
Browse files Browse the repository at this point in the history
Implement a new macro case for each assertion and the _new_goldie macros. This path extends the current _function_path
return value. What this achieves is custom named files in the same place golden tests would be previously written to.
  • Loading branch information
Trent Graw committed Nov 30, 2024
1 parent 81daa56 commit ff698ed
Showing 1 changed file with 30 additions and 0 deletions.
30 changes: 30 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,12 @@ use serde::{Deserialize, Serialize};
/// Assert the golden file matches.
#[macro_export]
macro_rules! assert {
($goldie_path: expr, $actual: expr) => {{
let g = $crate::_new_goldie!($goldie_path);
if let Err(err) = g.assert($actual) {
::std::panic!("{}", err);
}
}};
($actual:expr) => {{
let g = $crate::_new_goldie!();
if let Err(err) = g.assert($actual) {
Expand All @@ -83,11 +89,23 @@ macro_rules! assert_debug {
::std::panic!("{}", err);
}
}};
($goldie_path: expr, $actual:expr) => {{
let g = $crate::_new_goldie!($goldie_path);
if let Err(err) = g.assert_debug($actual) {
::std::panic!("{}", err);
}
}};
}

/// Assert the templated golden file matches.
#[macro_export]
macro_rules! assert_template {
($goldie_path: expr, $ctx:expr , $actual:expr) => {{
let g = $crate::_new_goldie!($goldie_path);
if let Err(err) = g.assert_template($ctx, $actual) {
::std::panic!("{}", err);
}
}};
($ctx:expr, $actual:expr) => {{
let g = $crate::_new_goldie!();
if let Err(err) = g.assert_template($ctx, $actual) {
Expand All @@ -99,6 +117,12 @@ macro_rules! assert_template {
/// Assert the JSON golden file matches.
#[macro_export]
macro_rules! assert_json {
($goldie_path: expr, $actual:expr) => {{
let g = $crate::_new_goldie!($goldie_path);
if let Err(err) = g.assert_json($actual) {
::std::panic!("{}", err);
}
}};
($actual:expr) => {{
let g = $crate::_new_goldie!();
if let Err(err) = g.assert_json($actual) {
Expand All @@ -118,6 +142,12 @@ macro_rules! _new_goldie {
let function_path = $crate::_function_path!();
$crate::Goldie::new(source_file, function_path)
}};
($goldie_path: expr) => {{
let source_file = $crate::cargo_workspace_dir(env!("CARGO_MANIFEST_DIR")).join(file!());
let function_path = $crate::_function_path!();
let function_path = format!("{}::{}", function_path, $goldie_path);
$crate::Goldie::new(source_file, function_path)
}};
}

/// Returns the fully qualified path to the current item.
Expand Down

0 comments on commit ff698ed

Please sign in to comment.