-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add test that README contains example from tests/
- Loading branch information
Showing
3 changed files
with
40 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters