1 $DIR/auxiliary/doctest_crate.rs:
2 LL| |/// A function run only from within doctests
3 LL| 3|pub fn fn_run_in_doctests(conditional: usize) {
4 LL| 3| match conditional {
5 LL| 1| 1 => assert_eq!(1, 1), // this is run,
6 LL| 1| 2 => assert_eq!(1, 1), // this,
7 LL| 1| 3 => assert_eq!(1, 1), // and this too
8 LL| 0| _ => assert_eq!(1, 2), // however this is not
13 LL| |// aux-build:doctest_crate.rs
15 LL| |//! This test ensures that code from doctests is properly re-mapped.
16 LL| |//! See <https://github.com/rust-lang/rust/issues/79417> for more info.
18 LL| |//! Just some random code:
21 LL| |//! // this is executed!
22 LL| 1|//! assert_eq!(1, 1);
24 LL| |//! // this is not!
25 LL| 0|//! assert_eq!(1, 2);
29 LL| |//! doctest testing external code:
31 LL| 1|//! extern crate doctest_crate;
32 LL| 1|//! doctest_crate::fn_run_in_doctests(1);
35 LL| |//! doctest returning a result:
37 LL| 2|//! #[derive(Debug, PartialEq)]
39 LL| 1|//! struct SomeError {
40 LL| 1|//! msg: String,
42 LL| 1|//! let mut res = Err(SomeError { msg: String::from("a message") });
43 LL| 1|//! if res.is_ok() {
46 LL| 1|//! if *res.as_ref().unwrap_err() == *res.as_ref().unwrap_err() {
47 LL| 1|//! println!("{:?}", res);
50 LL| 1|//! if *res.as_ref().unwrap_err() == *res.as_ref().unwrap_err() {
51 LL| 1|//! res = Ok(1);
54 LL| 1|//! res = Ok(0);
56 LL| |//! // need to be explicit because rustdoc cant infer the return type
57 LL| 1|//! Ok::<(), SomeError>(())
60 LL| |//! doctest with custom main:
62 LL| 1|//! fn some_func() {
63 LL| 1|//! println!("called some_func()");
66 LL| 0|//! #[derive(Debug)]
67 LL| |//! struct SomeError;
69 LL| |//! extern crate doctest_crate;
71 LL| 1|//! fn doctest_main() -> Result<(), SomeError> {
72 LL| 1|//! some_func();
73 LL| 1|//! doctest_crate::fn_run_in_doctests(2);
77 LL| |//! // this `main` is not shown as covered, as it clashes with all the other
78 LL| |//! // `main` functions that were automatically generated for doctests
79 LL| |//! fn main() -> Result<(), SomeError> {
80 LL| |//! doctest_main()
84 LL| |/// doctest attached to fn testing external code:
86 LL| 1|/// extern crate doctest_crate;
87 LL| 1|/// doctest_crate::fn_run_in_doctests(3);
92 LL| 1| assert_eq!(1, 1);
94 LL| 0| assert_eq!(1, 2);
98 LL| |// FIXME(Swatinem): Fix known issue that coverage code region columns need to be offset by the
99 LL| |// doc comment line prefix (`///` or `//!`) and any additional indent (before or after the doc
100 LL| |// comment characters). This test produces `llvm-cov show` results demonstrating the problem.
102 LL| |// One of the above tests now includes: `derive(Debug, PartialEq)`, producing an `llvm-cov show`
103 LL| |// result with a distinct count for `Debug`, denoted by `^1`, but the caret points to the wrong
104 LL| |// column. Similarly, the `if` blocks without `else` blocks show `^0`, which should point at, or
105 LL| |// one character past, the `if` block's closing brace. In both cases, these are most likely off
106 LL| |// by the number of characters stripped from the beginning of each doc comment line: indent
107 LL| |// whitespace, if any, doc comment prefix (`//!` in this case) and (I assume) one space character
108 LL| |// (?). Note, when viewing `llvm-cov show` results in `--color` mode, the column offset errors are
109 LL| |// more pronounced, and show up in more places, with background color used to show some distinct
110 LL| |// code regions with different coverage counts.
112 LL| |// NOTE: Since the doc comment line prefix may vary, one possible solution is to replace each
113 LL| |// character stripped from the beginning of doc comment lines with a space. This will give coverage
114 LL| |// results the correct column offsets, and I think it should compile correctly, but I don't know
115 LL| |// what affect it might have on diagnostic messages from the compiler, and whether anyone would care
116 LL| |// if the indentation changed. I don't know if there is a more viable solution.