Crates.io | quote-doctest |
lib.rs | quote-doctest |
version | 0.3.2 |
source | src |
created_at | 2022-04-01 03:21:02.149012 |
updated_at | 2022-04-12 00:00:31.361495 |
description | A simple doctest generator for quote |
homepage | |
repository | https://github.com/nu11ptr/flexgen/tree/master/doc_test |
max_upload_size | |
id | 560075 |
size | 25,770 |
A simple doctest and doc comment generator for quote
Currently, quote does not support interpolation inside of comments, which means no customized doctests. This crate provides a simple mechanism to generate doctests and doc comments for inclusion in generated code.
[dependencies]
quote-doctest = "0.3"
Using the doc_test
macro, we can take any TokenStream
and turn it into
a doctest TokenStream
that can be interpolated in any quote
macro
invocation.
The doc_comment
function takes any string and turns it into one or more
comments inside a TokenStream
.
use quote::quote;
use quote_doctest::{doc_comment, doc_test, FormatDocTest};
fn main() {
// Takes any `TokenStream` as input (but typically `quote` would be used)
let test = doc_test!(quote! {
_comment_!("Calling fibonacci with 10 returns 55");
assert_eq!(fibonacci(10), 55);
_blank_!();
_comment_!("Calling fibonacci with 1 simply returns 1");
assert_eq!(fibonacci(1), 1);
}).unwrap();
let comment = doc_comment("This compares fib inputs and outputs:\n\n");
// Interpolates into a regular `quote` invocation
let actual = quote! {
#comment
#test
fn fibonacci(n: u64) -> u64 {
match n {
0 => 1,
1 => 1,
n => fibonacci(n - 1) + fibonacci(n - 2),
}
}
};
// This is what is generated:
let expected = quote! {
/// This compares fib inputs and outputs:
///
/// ```
/// // Calling fibonacci with 10 returns 55
/// assert_eq!(fibonacci(10), 55);
///
/// // Calling fibonacci with 1 simply returns 1
/// assert_eq!(fibonacci(1), 1);
/// ```
fn fibonacci(n: u64) -> u64 {
match n {
0 => 1,
1 => 1,
n => fibonacci(n - 1) + fibonacci(n - 2),
}
}
};
assert_eq!(expected.format_tokens().unwrap(), actual.format_tokens().unwrap());
}
rustfmt
for formatting the doctests
rustfmt
, it honors the RUSTFMT
environment variable if setThis project is licensed optionally under either: