Skip to content

Commit

Permalink
feat(snap): Allow overriding the inferred type
Browse files Browse the repository at this point in the history
The problem today is we mutate the data according to the type so the
implicit coercion is lossy.
We need to insert logic before that happens.
The alt was to load as binary but that would be too annoying.
  • Loading branch information
epage committed Feb 14, 2024
1 parent a25bd22 commit a463b1b
Showing 1 changed file with 15 additions and 1 deletion.
16 changes: 15 additions & 1 deletion crates/snapbox/src/data.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,10 @@
///
/// ```
/// # use snapbox::file;
/// file!["./test_data/bar.html"];
/// file!["./test_data/bar.json"];
/// file!["./test_data/bar.json": Text];
/// file![_];
/// file![_: Json];
/// ```
#[macro_export]
macro_rules! file {
Expand All @@ -16,11 +18,23 @@ macro_rules! file {
path.push(rel_path);
$crate::Data::read_from(&path, None)
}};
[_ : $type:ident] => {{
let stem = ::std::path::Path::new(::std::file!()).file_stem().unwrap();
let rel_path = ::std::format!("snapshots/{}-{}.txt", stem.to_str().unwrap(), line!());
let mut path = $crate::current_dir!();
path.push(rel_path);
$crate::Data::read_from(&path, Some($crate::DataFormat:: $type))
}};
[$path:literal] => {{
let mut path = $crate::current_dir!();
path.push($path);
$crate::Data::read_from(&path, None)
}};
[$path:literal : $type:ident] => {{
let mut path = $crate::current_dir!();
path.push($path);
$crate::Data::read_from(&path, Some($crate::DataFormat:: $type))
}};
}

/// Test fixture, actual output, or expected result
Expand Down

0 comments on commit a463b1b

Please sign in to comment.