• Home
  • History
  • Annotate
Name Date Size #Lines LOC

..03-May-2022-

.cargo-checksum.jsonH A D03-May-202289 11

.cargo_vcs_info.jsonH A D01-Jan-197074 65

Cargo.tomlH A D01-Jan-19701.6 KiB6150

Cargo.toml.orig-cargoH A D04-Dec-2018979 4134

README.mdH A D05-Aug-20186.9 KiB255192

lib.rsH A D04-Dec-201831 KiB1,063847

README.md

1# Be a Rust Documentation Skeptic
2
3[![Unix build status](https://travis-ci.org/budziq/rust-skeptic.svg?branch=master)](https://travis-ci.org/budziq/rust-skeptic)
4[![Windows build status](https://ci.appveyor.com/api/projects/status/l1f74hon37wt2vce/branch/master?svg=true)](https://ci.appveyor.com/project/budziq/rust-skeptic/branch/master)
5[![crates.io](https://img.shields.io/crates/v/skeptic.svg)](https://crates.io/crates/skeptic)
6[![Documentation](https://docs.rs/skeptic/badge.svg)](https://docs.rs/skeptic)
7
8Test your Rust Markdown via Cargo.
9
10## Getting started
11
12Put this in `Cargo.toml` to add the `skeptic` dependency:
13
14```toml
15[build-dependencies]
16skeptic = "0.13"
17
18[dev-dependencies]
19skeptic = "0.13"
20```
21
22Also in `Cargo.toml`, to the `[package]` section add:
23
24```toml
25build = "build.rs"
26```
27
28That adds a [build script](http://doc.crates.io/build-script.html)
29through which you will tell Skeptic to build test cases from a set
30of Markdown files.
31
32In `build.rs` write this to test all Rust code blocks in `README.md`:
33
34```rust,no_run
35extern crate skeptic;
36
37fn main() {
38    // generates doc tests for `README.md`.
39    skeptic::generate_doc_tests(&["README.md"]);
40}
41```
42
43If you want to test multiple markdown files, you just need to build
44a list of filenames and supply that to `generate_doc_tests`. To help
45you, the method `markdown_files_of_directory` will create such a list,
46enumerating the markdown files in the specified directory. You can add
47more files to this list as you like:
48
49```rust,no_run
50extern crate skeptic;
51
52use skeptic::*;
53
54fn main() {
55    // Add all markdown files in directory "book/".
56    let mut mdbook_files = markdown_files_of_directory("book/");
57    // Also add "README.md" to the list of files.
58    mdbook_files.push("README.md".into());
59    generate_doc_tests(&mdbook_files);
60}
61```
62
63
64Finally, in `tests/skeptic.rs` put the following macros to tie the
65generated test cases to `cargo test`:
66
67```rust,ignore
68include!(concat!(env!("OUT_DIR"), "/skeptic-tests.rs"));
69```
70
71Now any Rust code blocks in `README.md` will be tested during `cargo
72test`.
73
74## Users' Guide
75
76Rust Skeptic is not based on rustdoc. It behaves similarly in many
77cases, but not all. Here's the lowdown on the Skeptic system.
78
79*Note: [this `README.md` file itself is tested by Rust
80Skeptic](https://github.com/budziq/rust-skeptic/blob/master/build.rs).
81Because it is illustrating how to use markdown syntax, the markup on
82this document itself is funky, and so is the output below,
83particularly when illustrating Markdown's code fences
84(<code>```rust</code>).*
85
86*You must ask for `rust` code blocks explicitly to get Rust testing*,
87with <code>```rust</code>. This is different from rustdoc, which
88assumes code blocks are Rust. The reason for this is that common
89Markdown parsers, like that used on GitHub, also do not assume Rust by
90default: you either get both Rust syntax highlighting and testing, or
91no Rust syntax highlighting and testing.
92
93So the below is not tested by Skeptic.
94
95````
96```
97let this_is_not_going_to_be_compiled_and_run = @all;
98It doesn't really matter what's in here.
99```
100````
101
102To indicate Rust code, code blocks are labeled `rust`:
103
104````rust,ignore
105```rust
106fn main() {
107   println!("Calm your skepticism. This example is verified.");
108}
109```
110````
111
112Skeptic will interpret other words in the code block's 'info string'
113(which should be separated by comma, `,`, to be
114GitHub-compatible). These words change how the test is interpreted:
115`ignore`, `no_run`, and `should_panic`.
116
117### `ignore` Info String
118
119The `ignore` info string causes the test to be completely ignored.  It will not
120be compiled or run during testing.  This can be useful if an example is written
121in Rust (and you want it highlighted as such) but it is known to be incomplete
122(so it cannot compile as-is).
123
124````rust,ignore
125```rust,ignore
126fn do_amazing_thing() -> i32 {
127   // TODO: How do I do this?
128   unimplemented! whatever I'm distracted, oh cookies!
129```
130````
131
132### `no_run` Info String
133
134The `no_run` info string causes the example code not to be run during testing.
135Code marked with `no_run` will however still be compiled.  This is useful for
136examples/test that may have side effects or dependencies which are not desirable
137in a testing situation.
138
139````rust,ignore
140```rust,no_run
141fn do_amazing_thing() -> i32 {
142   // TODO: How do I do this?
143   unimplemented!()
144}
145
146fn main() {
147   do_amazing_thing();
148}
149```
150````
151
152### `should_panic` Info String
153
154`should_panic` causes the test to only pass if it terminates because
155of a `panic!()`.
156
157````rust,ignore
158```rust,should_panic
159fn main() {
160   assert!(1 == 100);
161}
162```
163````
164
165## Skeptic Templates
166
167Unlike rustdoc, *Skeptic does not modify examples before testing by
168default*. Skeptic examples are placed in a '.rs' file, compiled, then
169run.
170
171This means that - *by default* - Skeptic examples require a `main`
172function, as in all the examples above. Implicit wrapping of examples
173in `main`, and custom injection of `extern crate` statements and crate
174attributes are controlled through templates.
175
176Templates for a document are located in a separate file, that lives
177next to the document on the filesystem, and has the same full name as
178the document file, but with an additional ".skt.md" template.
179
180So for example, this file, `README.md`, stores its templates
181in `README.md.skt.md`.
182
183This scheme allows the markdown to be displayed naturally by stock
184Markdown renderers without displaying the template itself. The weird
185file extension is similarly so that the templates themselves are
186interpreted as valid markdown.
187
188Consider this example:
189
190```rust,skt-foo
191let p = PathBuf::from("foo");
192println!("{:?}", p);
193```
194
195This example won't compile without defining `main` and importing
196`PathBuf`, but the example itself does not contain that
197boilerplate. Instead it is annotated `skt-foo`, for _skeptic template
198foo_, like so:
199
200````rust,ignore
201```rust,skt-foo
202let p = PathBuf::from("foo");
203println!("{:?}", p);
204```
205````
206
207This tells skeptic to look in the template file for another
208markdown block with the same `skt-foo` annotation, and compose
209them together using the standard Rust `format!` macro. Here's
210what the template looks like:
211
212````rust,ignore
213```rust,skt-foo
214use std::path::PathBuf;
215
216fn main() {{
217    {}
218}}
219```
220````
221
222Templates are [Rust format
223specifiers](http://doc.rust-lang.org/std/fmt/index.html) that must
224take a single argument (i.e. they need to contain the string
225"{}"). See [the (old) template example](template-example.md) for more
226on templates.
227
228Note that in a template, real braces need to be doubled.
229
230## The old-style, document-global template
231
232Within a document, a `rust` code block tagged `skeptic-template` will
233be used as the template for all examples in the doc that are not
234explicitly tagged.
235
236````rust,ignore
237```rust,skeptic-template
238use std::path::PathBuf;
239
240fn main() {{
241    {}
242}}
243```
244````
245
246## Rustdoc-style undisplayed lines with `# `
247
248Like rustdoc, skeptic will remove preceding `# ` from any lines of
249code before compiling them. Hiding such lines during display requires
250custom support in the markdown renderer.
251
252## License
253
254MIT/Apache-2.0
255