From 1ee8ef3b4989f0c63847d47b2570b48656a7fcbb Mon Sep 17 00:00:00 2001 From: TehPers Date: Thu, 15 Aug 2024 13:06:36 -0700 Subject: [PATCH] Add test that README contains example from tests/ --- README.md | 7 +++++-- tests/readme.rs | 32 ++++++++++++++++++++++++++++++++ tests/readme_example.rs | 4 +++- 3 files changed, 40 insertions(+), 3 deletions(-) create mode 100644 tests/readme.rs diff --git a/README.md b/README.md index 147461c..ddf5888 100644 --- a/README.md +++ b/README.md @@ -5,11 +5,12 @@ Supports both synchronous and asynchronous assertions. ## 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)); @@ -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") } ``` + Error message: diff --git a/tests/readme.rs b/tests/readme.rs new file mode 100644 index 0000000..cae4ad1 --- /dev/null +++ b/tests/readme.rs @@ -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::>() + .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!(""))?; + let (contents, _) = rest.split_once(&format!(""))?; + Some(contents.trim()) +} diff --git a/tests/readme_example.rs b/tests/readme_example.rs index 91fda42..a9b9b54 100644 --- a/tests/readme_example.rs +++ b/tests/readme_example.rs @@ -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));