From 7aaa84853d8991d895f53d27630a2a1c10bd9531 Mon Sep 17 00:00:00 2001 From: Noritada Kobayashi Date: Sun, 25 Sep 2022 06:32:41 +0900 Subject: [PATCH] Add test code to show preprocessor developers what the interface data looks like (#1897) This test code will show preprocessor developers what the input data looks like and how to test the preprocessing results. --- Cargo.toml | 4 +++ examples/nop-preprocessor.rs | 54 ++++++++++++++++++++++++++++++++++++ 2 files changed, 58 insertions(+) diff --git a/Cargo.toml b/Cargo.toml index 07b2ed78..5b564ec9 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -65,3 +65,7 @@ search = ["elasticlunr-rs", "ammonia"] [[bin]] doc = false name = "mdbook" + +[[example]] +name = "nop-preprocessor" +test = true diff --git a/examples/nop-preprocessor.rs b/examples/nop-preprocessor.rs index ace40093..a5e47daa 100644 --- a/examples/nop-preprocessor.rs +++ b/examples/nop-preprocessor.rs @@ -101,4 +101,58 @@ mod nop_lib { renderer != "not-supported" } } + + #[cfg(test)] + mod test { + use super::*; + + #[test] + fn nop_preprocessor_run() { + let input_json = r##"[ + { + "root": "/path/to/book", + "config": { + "book": { + "authors": ["AUTHOR"], + "language": "en", + "multilingual": false, + "src": "src", + "title": "TITLE" + }, + "preprocessor": { + "nop": {} + } + }, + "renderer": "html", + "mdbook_version": "0.4.21" + }, + { + "sections": [ + { + "Chapter": { + "name": "Chapter 1", + "content": "# Chapter 1\n", + "number": [1], + "sub_items": [], + "path": "chapter_1.md", + "source_path": "chapter_1.md", + "parent_names": [] + } + } + ], + "__non_exhaustive": null + } + ]"##; + let input_json = input_json.as_bytes(); + + let (ctx, book) = mdbook::preprocess::CmdPreprocessor::parse_input(input_json).unwrap(); + let expected_book = book.clone(); + let result = Nop::new().run(&ctx, book); + assert!(result.is_ok()); + + // The nop-preprocessor should not have made any changes to the book content. + let actual_book = result.unwrap(); + assert_eq!(actual_book, expected_book); + } + } }