diff --git a/src/lib.rs b/src/lib.rs index af037f2..49650e8 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -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) { @@ -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) { @@ -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) { @@ -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. diff --git a/src/testdata/goldie_assert-custom.golden b/src/testdata/goldie_assert-custom.golden new file mode 100644 index 0000000..c11b405 --- /dev/null +++ b/src/testdata/goldie_assert-custom.golden @@ -0,0 +1 @@ +custom testing... diff --git a/src/testdata/goldie_assert_debug-custom.golden b/src/testdata/goldie_assert_debug-custom.golden new file mode 100644 index 0000000..b3a27cb --- /dev/null +++ b/src/testdata/goldie_assert_debug-custom.golden @@ -0,0 +1,4 @@ +User { + name: "custom Steve", + surname: "custom Harrington", +} \ No newline at end of file diff --git a/src/testdata/goldie_assert_json-custom.golden b/src/testdata/goldie_assert_json-custom.golden new file mode 100644 index 0000000..cbb509a --- /dev/null +++ b/src/testdata/goldie_assert_json-custom.golden @@ -0,0 +1,4 @@ +{ + "name": "custom Steve", + "surname": "custom Harrington" +} \ No newline at end of file diff --git a/src/testdata/goldie_assert_template-custom.golden b/src/testdata/goldie_assert_template-custom.golden new file mode 100644 index 0000000..82eee69 --- /dev/null +++ b/src/testdata/goldie_assert_template-custom.golden @@ -0,0 +1 @@ +Such {{ test }} diff --git a/src/tests.rs b/src/tests.rs index 2e8d216..1d6b810 100644 --- a/src/tests.rs +++ b/src/tests.rs @@ -38,6 +38,11 @@ fn goldie_assert() { crate::assert!("testing...\n"); } +#[test] +fn goldie_assert_custom_path() { + crate::assert!("goldie_assert-custom", "custom testing...\n"); +} + #[test] fn goldie_assert_debug() { #[allow(dead_code)] @@ -54,6 +59,22 @@ fn goldie_assert_debug() { crate::assert_debug!(&u); } +#[test] +fn goldie_assert_debug_custom_path() { + #[allow(dead_code)] + #[derive(Debug)] + struct User { + name: &'static str, + surname: &'static str, + } + + let u = User { + name: "custom Steve", + surname: "custom Harrington", + }; + + crate::assert_debug!("goldie_assert_debug-custom", &u); +} #[test] fn goldie_assert_template() { @@ -65,6 +86,16 @@ fn goldie_assert_template() { crate::assert_template!(&ctx, "Such testing...\n"); } +#[test] +fn goldie_assert_template_custom_path() { + #[derive(Serialize)] + struct Context { + test: &'static str, + } + let ctx = Context { test: "custom testing..." }; + crate::assert_template!("goldie_assert_template-custom", &ctx, "Such custom testing...\n"); +} + #[test] fn goldie_assert_json() { #[derive(Serialize)] @@ -80,3 +111,19 @@ fn goldie_assert_json() { crate::assert_json!(&u); } + +#[test] +fn goldie_assert_json_custom_path() { + #[derive(Serialize)] + struct User { + name: &'static str, + surname: &'static str, + } + + let u = User { + name: "custom Steve", + surname: "custom Harrington", + }; + + crate::assert_json!("goldie_assert_json-custom", &u); +}