Migrate able_to_include_files_in_chapters to BookTest

This commit is contained in:
Eric Huss 2025-04-21 18:42:20 -07:00
parent dd778d50f9
commit 03470a7531
16 changed files with 103 additions and 18 deletions

View file

@ -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##"<h1 id="summary"><a class="header" href="#summary">Summary</a></h1>"##,
">First Chapter</a>",
];
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() {

View file

@ -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##"
<h1 id="basic-includes"><a class="header" href="#basic-includes">Basic Includes</a></h1>
<h2 id="sample"><a class="header" href="#sample">Sample</a></h2>
<p>This is a sample include.</p>
"##]],
)
.check_main_file(
"book/relative/includes.html",
str![[r##"
<h1 id="relative-includes"><a class="header" href="#relative-includes">Relative Includes</a></h1>
<h2 id="sample"><a class="header" href="#sample">Sample</a></h2>
<p>This is a sample include.</p>
"##]],
);
}

View file

@ -0,0 +1,6 @@
[book]
authors = ["Eric Huss"]
language = "en"
multilingual = false
src = "src"
title = "all_includes"

View file

@ -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)

View file

@ -0,0 +1,5 @@
# Include Anchors
```rust
{{#include nested-test-with-anchors.rs:myanchor}}
```

View file

@ -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");
}

View file

@ -0,0 +1,4 @@
# Basic Includes
{{#include sample.md}}

View file

@ -0,0 +1,6 @@
// This is a test of includes with anchors.
// ANCHOR: myanchor
// ANCHOR: unendinganchor
let x = 1;
// ANCHOR_END: myanchor

View file

@ -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

View file

@ -0,0 +1,7 @@
fn some_function() {
println!("some function");
}
fn main() {
some_function();
}

View file

@ -0,0 +1,3 @@
# Playground Includes
{{#playground example.rs}}

View file

@ -0,0 +1,2 @@
Around the world, around the world
{{#include recursive.md}}

View file

@ -0,0 +1,3 @@
# Relative Includes
{{#include ../sample.md}}

View file

@ -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}}
```

View file

@ -0,0 +1,3 @@
## Sample
This is a sample include.

View file

@ -5,6 +5,7 @@
mod book_test;
mod build;
mod cli;
mod includes;
mod prelude {
pub use crate::book_test::BookTest;