Skip to content

Commit

Permalink
Add test that README contains example from tests/
Browse files Browse the repository at this point in the history
  • Loading branch information
TehPers committed Aug 15, 2024
1 parent 8beba13 commit 1ee8ef3
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 3 deletions.
7 changes: 5 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,12 @@ Supports both synchronous and asynchronous assertions.

## Example

<!-- EXAMPLE -->
```rust
use expecters::prelude::*;

#[tokio::test]
async fn test() {
async fn test() {
expect!(1, as_display, to_equal("1"));
expect!(1..=5, count, to_equal(5));

Expand All @@ -18,13 +19,15 @@ async fn test() {
all,
when_ready,
to_end_with("0.png"),
).await;
)
.await;
}

async fn get_cat_url(id: u32) -> String {
format!("cats/{id}.png")
}
```
<!-- /EXAMPLE -->

Error message:

Expand Down
32 changes: 32 additions & 0 deletions tests/readme.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
use expecters::prelude::*;

const TEST_CONTENTS: &str = include_str!("./readme_example.rs");
const README_CONTENTS: &str = include_str!("../README.md");

#[test]
fn readme_example_is_correct() {
let example = get_section("EXAMPLE").expect("example section not found");
expect!(example, to_start_with("```rust"));
expect!(example, to_end_with("```"));

// Trim out code block styling and extra attributes and normalize line
// endings
let example = example["```rust".len()..example.len() - "```".len()]
.trim()
.replace("\r\n", "\n");
let test_contents = TEST_CONTENTS["#![cfg(feature = \"futures\")]".len()..]
.split("#[ignore]")
.map(|s| s.trim())
.collect::<Vec<_>>()
.join("\n")
.replace("\r\n", "\n");
let test_contents = test_contents.trim();

expect!(example, to_equal(test_contents));
}

fn get_section(name: &str) -> Option<&'static str> {
let (_, rest) = README_CONTENTS.split_once(&format!("<!-- {name} -->"))?;
let (contents, _) = rest.split_once(&format!("<!-- /{name} -->"))?;
Some(contents.trim())
}
4 changes: 3 additions & 1 deletion tests/readme_example.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
#![cfg(feature = "futures")]

use expecters::prelude::*;

#[tokio::test]
#[ignore = "run this manually to see the output from the README"]
#[ignore]
async fn test() {
expect!(1, as_display, to_equal("1"));
expect!(1..=5, count, to_equal(5));
Expand Down

0 comments on commit 1ee8ef3

Please sign in to comment.