From 03470a7531a73f152f569c4ebadc5eccb209b600 Mon Sep 17 00:00:00 2001 From: Eric Huss Date: Mon, 21 Apr 2025 18:42:20 -0700 Subject: [PATCH] Migrate able_to_include_files_in_chapters to BookTest --- tests/rendered_output.rs | 18 ------------- tests/testsuite/includes.rs | 25 +++++++++++++++++++ .../testsuite/includes/all_includes/book.toml | 6 +++++ .../includes/all_includes/src/SUMMARY.md | 8 ++++++ .../includes/all_includes/src/anchors.md | 5 ++++ .../includes/all_includes/src/example.rs | 6 +++++ .../includes/all_includes/src/includes.md | 4 +++ .../src/nested-test-with-anchors.rs | 6 +++++ .../partially-included-test-with-anchors.rs | 11 ++++++++ .../src/partially-included-test.rs | 7 ++++++ .../includes/all_includes/src/playground.md | 3 +++ .../includes/all_includes/src/recursive.md | 2 ++ .../all_includes/src/relative/includes.md | 3 +++ .../includes/all_includes/src/rustdoc.md | 13 ++++++++++ .../includes/all_includes/src/sample.md | 3 +++ tests/testsuite/main.rs | 1 + 16 files changed, 103 insertions(+), 18 deletions(-) create mode 100644 tests/testsuite/includes.rs create mode 100644 tests/testsuite/includes/all_includes/book.toml create mode 100644 tests/testsuite/includes/all_includes/src/SUMMARY.md create mode 100644 tests/testsuite/includes/all_includes/src/anchors.md create mode 100644 tests/testsuite/includes/all_includes/src/example.rs create mode 100644 tests/testsuite/includes/all_includes/src/includes.md create mode 100644 tests/testsuite/includes/all_includes/src/nested-test-with-anchors.rs create mode 100644 tests/testsuite/includes/all_includes/src/partially-included-test-with-anchors.rs create mode 100644 tests/testsuite/includes/all_includes/src/partially-included-test.rs create mode 100644 tests/testsuite/includes/all_includes/src/playground.md create mode 100644 tests/testsuite/includes/all_includes/src/recursive.md create mode 100644 tests/testsuite/includes/all_includes/src/relative/includes.md create mode 100644 tests/testsuite/includes/all_includes/src/rustdoc.md create mode 100644 tests/testsuite/includes/all_includes/src/sample.md diff --git a/tests/rendered_output.rs b/tests/rendered_output.rs index f648b72d..0a151a54 100644 --- a/tests/rendered_output.rs +++ b/tests/rendered_output.rs @@ -364,24 +364,6 @@ fn able_to_include_playground_files_in_chapters() { assert_doesnt_contain_strings(&second, &["{{#playground example.rs}}"]); } -/// This makes sure you can include a Rust file with `{{#include ../SUMMARY.md}}`. -#[test] -fn able_to_include_files_in_chapters() { - let temp = DummyBook::new().build().unwrap(); - let md = MDBook::load(temp.path()).unwrap(); - md.build().unwrap(); - - let includes = temp.path().join("book/first/includes.html"); - - let summary_strings = &[ - r##"

Summary

"##, - ">First Chapter", - ]; - assert_contains_strings(&includes, summary_strings); - - assert_doesnt_contain_strings(&includes, &["{{#include ../SUMMARY.md::}}"]); -} - /// Ensure cyclic includes are capped so that no exceptions occur #[test] fn recursive_includes_are_capped() { diff --git a/tests/testsuite/includes.rs b/tests/testsuite/includes.rs new file mode 100644 index 00000000..1e4a7c53 --- /dev/null +++ b/tests/testsuite/includes.rs @@ -0,0 +1,25 @@ +//! Tests for include preprocessor. + +use crate::prelude::*; + +// Basic test for #include. +#[test] +fn include() { + BookTest::from_dir("includes/all_includes") + .check_main_file( + "book/includes.html", + str![[r##" +

Basic Includes

+

Sample

+

This is a sample include.

+"##]], + ) + .check_main_file( + "book/relative/includes.html", + str![[r##" +

Relative Includes

+

Sample

+

This is a sample include.

+"##]], + ); +} diff --git a/tests/testsuite/includes/all_includes/book.toml b/tests/testsuite/includes/all_includes/book.toml new file mode 100644 index 00000000..622694be --- /dev/null +++ b/tests/testsuite/includes/all_includes/book.toml @@ -0,0 +1,6 @@ +[book] +authors = ["Eric Huss"] +language = "en" +multilingual = false +src = "src" +title = "all_includes" diff --git a/tests/testsuite/includes/all_includes/src/SUMMARY.md b/tests/testsuite/includes/all_includes/src/SUMMARY.md new file mode 100644 index 00000000..25a9a15f --- /dev/null +++ b/tests/testsuite/includes/all_includes/src/SUMMARY.md @@ -0,0 +1,8 @@ +# Summary + +- [Basic Includes](./includes.md) + - [Relative Includes](./relative/includes.md) +- [Recursive Includes](./recursive.md) +- [Include Anchors](./anchors.md) +- [Rustdoc Includes](./rustdoc.md) +- [Playground Includes](./playground.md) diff --git a/tests/testsuite/includes/all_includes/src/anchors.md b/tests/testsuite/includes/all_includes/src/anchors.md new file mode 100644 index 00000000..0ffbc788 --- /dev/null +++ b/tests/testsuite/includes/all_includes/src/anchors.md @@ -0,0 +1,5 @@ +# Include Anchors + +```rust +{{#include nested-test-with-anchors.rs:myanchor}} +``` diff --git a/tests/testsuite/includes/all_includes/src/example.rs b/tests/testsuite/includes/all_includes/src/example.rs new file mode 100644 index 00000000..6b49705c --- /dev/null +++ b/tests/testsuite/includes/all_includes/src/example.rs @@ -0,0 +1,6 @@ +fn main() { + println!("Hello World!"); +# +# // You can even hide lines! :D +# println!("I am hidden! Expand the code snippet to see me"); +} diff --git a/tests/testsuite/includes/all_includes/src/includes.md b/tests/testsuite/includes/all_includes/src/includes.md new file mode 100644 index 00000000..539aa939 --- /dev/null +++ b/tests/testsuite/includes/all_includes/src/includes.md @@ -0,0 +1,4 @@ +# Basic Includes + +{{#include sample.md}} + diff --git a/tests/testsuite/includes/all_includes/src/nested-test-with-anchors.rs b/tests/testsuite/includes/all_includes/src/nested-test-with-anchors.rs new file mode 100644 index 00000000..93c61dd4 --- /dev/null +++ b/tests/testsuite/includes/all_includes/src/nested-test-with-anchors.rs @@ -0,0 +1,6 @@ +// This is a test of includes with anchors. + +// ANCHOR: myanchor +// ANCHOR: unendinganchor +let x = 1; +// ANCHOR_END: myanchor diff --git a/tests/testsuite/includes/all_includes/src/partially-included-test-with-anchors.rs b/tests/testsuite/includes/all_includes/src/partially-included-test-with-anchors.rs new file mode 100644 index 00000000..8e732d0b --- /dev/null +++ b/tests/testsuite/includes/all_includes/src/partially-included-test-with-anchors.rs @@ -0,0 +1,11 @@ +fn some_other_function() { + // ANCHOR: unused-anchor-that-should-be-stripped + println!("unused anchor"); + // ANCHOR_END: unused-anchor-that-should-be-stripped +} + +// ANCHOR: rustdoc-include-anchor +fn main() { + some_other_function(); +} +// ANCHOR_END: rustdoc-include-anchor diff --git a/tests/testsuite/includes/all_includes/src/partially-included-test.rs b/tests/testsuite/includes/all_includes/src/partially-included-test.rs new file mode 100644 index 00000000..915651ea --- /dev/null +++ b/tests/testsuite/includes/all_includes/src/partially-included-test.rs @@ -0,0 +1,7 @@ +fn some_function() { + println!("some function"); +} + +fn main() { + some_function(); +} diff --git a/tests/testsuite/includes/all_includes/src/playground.md b/tests/testsuite/includes/all_includes/src/playground.md new file mode 100644 index 00000000..9c20530c --- /dev/null +++ b/tests/testsuite/includes/all_includes/src/playground.md @@ -0,0 +1,3 @@ +# Playground Includes + +{{#playground example.rs}} diff --git a/tests/testsuite/includes/all_includes/src/recursive.md b/tests/testsuite/includes/all_includes/src/recursive.md new file mode 100644 index 00000000..cb82a52f --- /dev/null +++ b/tests/testsuite/includes/all_includes/src/recursive.md @@ -0,0 +1,2 @@ +Around the world, around the world +{{#include recursive.md}} diff --git a/tests/testsuite/includes/all_includes/src/relative/includes.md b/tests/testsuite/includes/all_includes/src/relative/includes.md new file mode 100644 index 00000000..f0a6fce6 --- /dev/null +++ b/tests/testsuite/includes/all_includes/src/relative/includes.md @@ -0,0 +1,3 @@ +# Relative Includes + +{{#include ../sample.md}} diff --git a/tests/testsuite/includes/all_includes/src/rustdoc.md b/tests/testsuite/includes/all_includes/src/rustdoc.md new file mode 100644 index 00000000..8a342d90 --- /dev/null +++ b/tests/testsuite/includes/all_includes/src/rustdoc.md @@ -0,0 +1,13 @@ +# Rustdoc Includes + +## Rustdoc include adds the rest of the file as hidden + +```rust +{{#rustdoc_include partially-included-test.rs:5:7}} +``` + +## Rustdoc include works with anchors too + +```rust +{{#rustdoc_include partially-included-test-with-anchors.rs:rustdoc-include-anchor}} +``` diff --git a/tests/testsuite/includes/all_includes/src/sample.md b/tests/testsuite/includes/all_includes/src/sample.md new file mode 100644 index 00000000..91071ef8 --- /dev/null +++ b/tests/testsuite/includes/all_includes/src/sample.md @@ -0,0 +1,3 @@ +## Sample + +This is a sample include. diff --git a/tests/testsuite/main.rs b/tests/testsuite/main.rs index c8630267..d273b992 100644 --- a/tests/testsuite/main.rs +++ b/tests/testsuite/main.rs @@ -5,6 +5,7 @@ mod book_test; mod build; mod cli; +mod includes; mod prelude { pub use crate::book_test::BookTest;