Skip to content

Commit

Permalink
Add crate doc, macro doc and improve readme with badges
Browse files Browse the repository at this point in the history
  • Loading branch information
maartenvdmeulen committed Nov 6, 2023
1 parent c97b032 commit 028fb8f
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 4 deletions.
12 changes: 10 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
# Test Each File

[![github](https://img.shields.io/badge/github-8da0cb?style=for-the-badge&labelColor=555555&logo=github)](https://github.com/binary-banter/test-each-file) [![crates-io](https://img.shields.io/badge/crates.io-fc8d62?style=for-the-badge&labelColor=555555&logo=rust)](https://crates.io/crates/test_each_file) [![docs-rs](https://img.shields.io/badge/docs.rs-66c2a5?style=for-the-badge&labelColor=555555&logo=docs.rs)](https://docs.rs/test_each_file)

Easily generate tests for files in a specified directory for comprehensive testing.

A simple example of the macro is shown below:

```rust
test_each_file! { in "./resources" => test }

Expand All @@ -12,7 +15,8 @@ fn test(content: &str) {
```

Given the following file structure:
```

```txt
- resources
- a.txt
- b.txt
Expand All @@ -23,6 +27,7 @@ Given the following file structure:
```

The macro expands to:

```rust
#[test]
fn a() {
Expand All @@ -48,6 +53,7 @@ mod extra {
## Generate submodule

The tests can automatically be inserted into a module, by using the `as` keyword. For example:

```rust
test_each_file! { in "./resources" as example => test }
```
Expand All @@ -70,7 +76,7 @@ fn test([input, output]: [&str; 2]) {

Both the `.in` and `.out` files must exist and be located in the same directory, as demonstrated below:

```
```txt
- resources
- a.in
- a.out
Expand All @@ -88,11 +94,13 @@ Note that `.in` and `.out` are just examples here - any number of unique extensi
## More examples

The expression that is called on each file can also be a closure, for example:

```rust
test_each_file! { in "./resources" => |c: &str| assert!(c.contains("Hello World")) }
```

All the options above can be combined, for example:

```rust
test_each_file! { for ["in", "out"] in "./resources" as example => |[a, b]: [&str; 2]| assert_eq!(a, b) }
```
8 changes: 6 additions & 2 deletions src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
#![doc = include_str!("../README.md")]
use pathdiff::diff_paths;
use proc_macro2::{Ident, Span, TokenStream};
use proc_macro_error::{abort, abort_call_site, proc_macro_error};
Expand All @@ -16,7 +17,7 @@ struct ForEachFile {
module: Option<Ident>,
function: Expr,
extensions: Vec<String>,
extensions_span: Vec<Span>,
_extensions_span: Vec<Span>,
}

impl Parse for ForEachFile {
Expand Down Expand Up @@ -57,7 +58,7 @@ impl Parse for ForEachFile {
module,
function,
extensions: extensions.unwrap_or_default(),
extensions_span: extensions_span.unwrap_or_default(),
_extensions_span: extensions_span.unwrap_or_default(),
})
}
}
Expand Down Expand Up @@ -145,6 +146,9 @@ fn generate_from_tree(tree: &Tree, parsed: &ForEachFile, stream: &mut TokenStrea
}
}

/// Easily generate tests for files in a specified directory for comprehensive testing.
///
/// See crate level documentation for details.
#[proc_macro]
#[proc_macro_error]
pub fn test_each_file(input: proc_macro::TokenStream) -> proc_macro::TokenStream {
Expand Down

0 comments on commit 028fb8f

Please sign in to comment.