Skip to content

Commit

Permalink
Merge pull request #4 from dzfrias/return-types
Browse files Browse the repository at this point in the history
Allow for return types
  • Loading branch information
Y-Nak authored Mar 2, 2024
2 parents 20397f0 + 3bc5a42 commit dceb0e4
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 2 deletions.
17 changes: 17 additions & 0 deletions dir-test/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,23 @@
//!
//! **NOTE**: The `dir_test_attr` attribute must be specified after the
//! `dir_test`.
//!
//! ### Return Types
//! Tests may have a return type, allowing for the [`Result<T, E>`] type to be used in the test.
//! See the relevant book link
//! [here](https://doc.rust-lang.org/book/ch11-01-writing-tests.html#using-resultt-e-in-tests).
//!
//! ```rust, no_run
//! use dir_test::{dir_test, Fixture};
//!
//! #[dir_test(
//! dir: "$CARGO_MANIFEST_DIR/fixtures",
//! glob: "**/*.txt",
//! )]
//! fn test(fixture: Fixture<&str>) -> std::io::Result<()> {
//! // ...
//! }
//! ```
/// A fixture contains a file content and its absolute path.
/// Content type is determined by the loader function specified in
Expand Down
5 changes: 3 additions & 2 deletions macros/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,7 @@ impl TestBuilder {
let test_name = self.test_name(file_path)?;
let file_path_str = file_path.to_string_lossy();
let test_func = &self.func.sig.ident;
let return_ty = &self.func.sig.output;
let test_attrs = &self.test_attrs;

let loader = match self.dir_test_arg.loader {
Expand All @@ -96,8 +97,8 @@ impl TestBuilder {
Ok(quote! {
#(#test_attrs)*
#[test]
fn #test_name() {
#test_func(::dir_test::Fixture::new(#loader(#file_path_str), #file_path_str));
fn #test_name() #return_ty {
#test_func(::dir_test::Fixture::new(#loader(#file_path_str), #file_path_str))
}
})
}
Expand Down

0 comments on commit dceb0e4

Please sign in to comment.