From a48224b9ac287b9fe1b2e0783d0ccefe5b8c87df Mon Sep 17 00:00:00 2001 From: Anthony Defranceschi Date: Mon, 12 Mar 2018 21:22:03 +0100 Subject: [PATCH] Add a section for using assertions into doc tests. See #47945. --- src/doc/rustdoc/src/documentation-tests.md | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/src/doc/rustdoc/src/documentation-tests.md b/src/doc/rustdoc/src/documentation-tests.md index e5a603a3709f6..d907fbba0a087 100644 --- a/src/doc/rustdoc/src/documentation-tests.md +++ b/src/doc/rustdoc/src/documentation-tests.md @@ -31,6 +31,22 @@ let x = 5; There's some subtlety though! Read on for more details. +## Passing or failing a doctest + +Like regular unit tests, regular doctests are considered to "pass" +if they compile and run without panicking. +So if you want to demonstrate that some computation gives a certain result, +the `assert!` family of macros works the same as other Rust code: + +```rust +let foo = "foo"; + +assert_eq!(foo, "foo"); +``` + +This way, if the computation ever returns something different, +the code panics and the doctest fails. + ## Pre-processing examples In the example above, you'll note something strange: there's no `main`