Crates.io | doubter |
lib.rs | doubter |
version | 0.1.0 |
source | src |
created_at | 2018-10-11 06:36:52.237029 |
updated_at | 2018-10-15 07:32:50.958554 |
description | A helper crate for testing Rust code blocks in Markdown |
homepage | |
repository | https://github.com/ubnt-intrepid/doubter.git |
max_upload_size | |
id | 89203 |
size | 2,874 |
doubter
Test Rust code blocks in your Markdown files.
This crate executes the code blocks in Markdown files by embedding them into
the Rust source and builds as a crate.
The advantage of this approach is that the dependency resolution are done by cargo
.
This means that that dependency problems associated with updating the Rust toolchain do not occur.
doubter
embeds the target Markdown files into the Rust code as the public doc comments.
Therefore, it is necessary to create a new crate for testing code blocks separately from the published crates.
This crate(s) are usually registered in [workspace.members]
.
Add the dependencies for doubter
to Cargo.toml
.
If some external crates are required in code blocks, specify them as the members of [dev-dependencies]
:
[dependencies]
doubter = "0.1.0"
[dev-dependencies]
rand = "0.5"
Then, modify src/lib.rs
to specify the path to target Markdown files.
#[macro_use]
extern crate doubter;
generate_doc_tests! {
include = "README.md",
include = "docs/**/*.md",
}
The macro generate_doc_tests!(...)
takes a comma-separated list of fields.
The following field keys are currently supported:
include
- stringmode
- string, optional"raw"
(default) : embeds the Markdown files in Rust source as it is."extract"
: extracts code blocks and emit as doctest per blocks.use_external_doc
- string or boolean, optional#[doc(include = "...")]
to embed Markdown files.
When this filed is enabled, the value of mode
is forced to "raw"
.Currently, the implementation of function style procedural macro is using
proc-macro-hack
. The definition of procedural macro via custom Derive has some restrictions and theuse
-style import does not work as expected. You can switch the implementation to the Macros 1.2 by disabling the feature flaghack
(this feature flag is enabled by default):[dependencies.doubter] version = "0.1.0" default-features = false
build.rs
)There are some restrictions on the use of procedural macros
(for example, literals passed to the field cannot be calculated by using another macros).
doubter
provides a low level API for generating test codes from build.rs
.
At first, moves the dependency on doubter
to [build-dependencies]
:
-[dependencies]
+[build-dependencies]
doubter = "0.1.0"
The code for generating test cases in build.rs
looks like as follows:
extern crate doubter;
fn main() {
let config = doubter::Config {
includes: vec![...],
mode: None,
use_external_doc: false,
};
let out_path = std::env::var_os("OUT_DIR")
.map(std::path::PathBuf::from)
.unwrap()
.join("doubter-tests.rs");
let mut file = std::fs::OpenOptions::new()
.write(true).create(true).truncate(true)
.open(out_path)
.unwrap();
doubter::generate_doc_tests(config, &mut file).unwrap();
}
Finally, includes the generated source into lib.rs
as follows:
include!(concat!(env!("OUT_DIR"), "/doubter-tests.rs"));
See the test crates inside of crates/
.
doubter
is released under the MIT license.