From 995efc22d50ac2f685ef9a6d9e7a165d436ac3dd Mon Sep 17 00:00:00 2001 From: Michael Bryan Date: Thu, 30 Aug 2018 21:51:18 +0800 Subject: [PATCH] Made sure preprocessors get their renderer's name --- tests/build_process.rs | 80 ++++++++++++++++++++++++++++++++++++++++++ tests/testing.rs | 33 ----------------- 2 files changed, 80 insertions(+), 33 deletions(-) create mode 100644 tests/build_process.rs diff --git a/tests/build_process.rs b/tests/build_process.rs new file mode 100644 index 00000000..85750ab3 --- /dev/null +++ b/tests/build_process.rs @@ -0,0 +1,80 @@ +extern crate mdbook; + +mod dummy_book; + +use dummy_book::DummyBook; +use mdbook::book::Book; +use mdbook::config::Config; +use mdbook::errors::*; +use mdbook::preprocess::{Preprocessor, PreprocessorContext}; +use mdbook::renderer::{RenderContext, Renderer}; +use mdbook::MDBook; +use std::sync::{Arc, Mutex}; + +struct Spy(Arc>); + +#[derive(Debug, Default)] +struct Inner { + run_count: usize, + rendered_with: Vec, +} + +impl Preprocessor for Spy { + fn name(&self) -> &str { + "dummy" + } + + fn run(&self, ctx: &PreprocessorContext, book: Book) -> Result { + let mut inner = self.0.lock().unwrap(); + inner.run_count += 1; + inner.rendered_with.push(ctx.renderer.clone()); + Ok(book) + } +} + +impl Renderer for Spy { + fn name(&self) -> &str { + "dummy" + } + + fn render(&self, _ctx: &RenderContext) -> Result<()> { + let mut inner = self.0.lock().unwrap(); + inner.run_count += 1; + Ok(()) + } +} + +#[test] +fn mdbook_runs_preprocessors() { + let spy: Arc> = Default::default(); + + let temp = DummyBook::new().build().unwrap(); + let cfg = Config::default(); + + let mut book = MDBook::load_with_config(temp.path(), cfg).unwrap(); + book.with_preprecessor(Spy(Arc::clone(&spy))); + book.build().unwrap(); + + let inner = spy.lock().unwrap(); + assert_eq!(inner.run_count, 1); + assert_eq!(inner.rendered_with.len(), 1); + assert_eq!( + "html", inner.rendered_with[0], + "We should have been run with the default HTML renderer" + ); +} + +#[test] +fn mdbook_runs_renderers() { + let spy: Arc> = Default::default(); + + let temp = DummyBook::new().build().unwrap(); + let cfg = Config::default(); + + let mut book = MDBook::load_with_config(temp.path(), cfg).unwrap(); + book.with_renderer(Spy(Arc::clone(&spy))); + book.build().unwrap(); + + let inner = spy.lock().unwrap(); + assert_eq!(inner.run_count, 1); +} diff --git a/tests/testing.rs b/tests/testing.rs index 84a9e21c..23e17371 100644 --- a/tests/testing.rs +++ b/tests/testing.rs @@ -4,14 +4,8 @@ mod dummy_book; use dummy_book::DummyBook; -use mdbook::book::Book; -use mdbook::config::Config; -use mdbook::errors::*; -use mdbook::preprocess::{Preprocessor, PreprocessorContext}; use mdbook::MDBook; -use std::sync::{Arc, Mutex}; - #[test] fn mdbook_can_correctly_test_a_passing_book() { let temp = DummyBook::new().with_passing_test(true).build().unwrap(); @@ -27,30 +21,3 @@ fn mdbook_detects_book_with_failing_tests() { assert!(md.test(vec![]).is_err()); } - -#[test] -fn mdbook_runs_preprocessors() { - let has_run: Arc> = Arc::new(Mutex::new(false)); - - struct DummyPreprocessor(Arc>); - - impl Preprocessor for DummyPreprocessor { - fn name(&self) -> &str { - "dummy" - } - - fn run(&self, _ctx: &PreprocessorContext, book: Book) -> Result { - *self.0.lock().unwrap() = true; - Ok(book) - } - } - - let temp = DummyBook::new().build().unwrap(); - let cfg = Config::default(); - - let mut book = MDBook::load_with_config(temp.path(), cfg).unwrap(); - book.with_preprecessor(DummyPreprocessor(Arc::clone(&has_run))); - book.build().unwrap(); - - assert!(*has_run.lock().unwrap()) -}