This post has been de-listed
It is no longer included in search results and normal feeds (front page, hot posts, subreddit posts, etc). It remains visible only via the author's post history.
Quote doesn't seem to support string interpolation in doc attributes, so I made this little helper to generate doctests.
UPDATE: 0.2 is now out with many improvements. It now uses 'prettyplease' by default for formatting and can handle blank lines and comments in the doc test.
Here is an example of usage:
``rust
// Takes any
TokenStreamas 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 between fib inputs and outputs:\n\n").unwrap();
// 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),
}
}
};
```
It generates this:
rust
let expected = quote! {
/// This compares between 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),
}
}
};
Post Details
- Posted
- 2 years ago
- Reddit URL
- View post on reddit.com
- External URL
- reddit.com/r/rust/commen...