Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Impl custom golden paths #3

Open
wants to merge 3 commits into
base: trunk
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
1 change: 1 addition & 0 deletions src/testdata/goldie_assert-custom.golden
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
custom testing...
4 changes: 4 additions & 0 deletions src/testdata/goldie_assert_debug-custom.golden
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
User {
name: "custom Steve",
surname: "custom Harrington",
}
4 changes: 4 additions & 0 deletions src/testdata/goldie_assert_json-custom.golden
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"name": "custom Steve",
"surname": "custom Harrington"
}
1 change: 1 addition & 0 deletions src/testdata/goldie_assert_template-custom.golden
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Such {{ test }}
47 changes: 47 additions & 0 deletions src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)]
Expand All @@ -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() {
Expand All @@ -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)]
Expand All @@ -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);
}