summaryrefslogtreecommitdiffstats
path: root/tests/run-coverage-rustdoc/doctest.coverage
blob: 5797784f411ccb2513febf14eb6977fbcf6e1281 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
$DIR/auxiliary/doctest_crate.rs:
   LL|       |/// A function run only from within doctests
   LL|      3|pub fn fn_run_in_doctests(conditional: usize) {
   LL|      3|    match conditional {
   LL|      1|        1 => assert_eq!(1, 1), // this is run,
   LL|      1|        2 => assert_eq!(1, 1), // this,
   LL|      1|        3 => assert_eq!(1, 1), // and this too
   LL|      0|        _ => assert_eq!(1, 2), // however this is not
   LL|       |    }
   LL|      3|}

$DIR/doctest.rs:
   LL|       |// aux-build:doctest_crate.rs
   LL|       |
   LL|       |//! This test ensures that code from doctests is properly re-mapped.
   LL|       |//! See <https://github.com/rust-lang/rust/issues/79417> for more info.
   LL|       |//!
   LL|       |//! Just some random code:
   LL|      1|//! ```
   LL|      1|//! if true {
   LL|       |//!     // this is executed!
   LL|      1|//!     assert_eq!(1, 1);
   LL|       |//! } else {
   LL|       |//!     // this is not!
   LL|      0|//!     assert_eq!(1, 2);
   LL|       |//! }
   LL|      1|//! ```
   LL|       |//!
   LL|       |//! doctest testing external code:
   LL|       |//! ```
   LL|      1|//! extern crate doctest_crate;
   LL|      1|//! doctest_crate::fn_run_in_doctests(1);
   LL|      1|//! ```
   LL|       |//!
   LL|       |//! doctest returning a result:
   LL|      1|//! ```
   LL|      2|//! #[derive(Debug, PartialEq)]
                       ^1
   LL|      1|//! struct SomeError {
   LL|      1|//!     msg: String,
   LL|      1|//! }
   LL|      1|//! let mut res = Err(SomeError { msg: String::from("a message") });
   LL|      1|//! if res.is_ok() {
   LL|      0|//!     res?;
   LL|       |//! } else {
   LL|      1|//!     if *res.as_ref().unwrap_err() == *res.as_ref().unwrap_err() {
   LL|      1|//!         println!("{:?}", res);
   LL|      1|//!     }
                   ^0
   LL|      1|//!     if *res.as_ref().unwrap_err() == *res.as_ref().unwrap_err() {
   LL|      1|//!         res = Ok(1);
   LL|      1|//!     }
                   ^0
   LL|      1|//!     res = Ok(0);
   LL|       |//! }
   LL|       |//! // need to be explicit because rustdoc cant infer the return type
   LL|      1|//! Ok::<(), SomeError>(())
   LL|      1|//! ```
   LL|       |//!
   LL|       |//! doctest with custom main:
   LL|       |//! ```
   LL|      1|//! fn some_func() {
   LL|      1|//!     println!("called some_func()");
   LL|      1|//! }
   LL|       |//!
   LL|      0|//! #[derive(Debug)]
   LL|       |//! struct SomeError;
   LL|       |//!
   LL|       |//! extern crate doctest_crate;
   LL|       |//!
   LL|      1|//! fn doctest_main() -> Result<(), SomeError> {
   LL|      1|//!     some_func();
   LL|      1|//!     doctest_crate::fn_run_in_doctests(2);
   LL|      1|//!     Ok(())
   LL|      1|//! }
   LL|       |//!
   LL|       |//! // this `main` is not shown as covered, as it clashes with all the other
   LL|       |//! // `main` functions that were automatically generated for doctests
   LL|       |//! fn main() -> Result<(), SomeError> {
   LL|       |//!     doctest_main()
   LL|       |//! }
   LL|       |//! ```
   LL|       |
   LL|       |/// doctest attached to fn testing external code:
   LL|       |/// ```
   LL|      1|/// extern crate doctest_crate;
   LL|      1|/// doctest_crate::fn_run_in_doctests(3);
   LL|      1|/// ```
   LL|       |///
   LL|      1|fn main() {
   LL|      1|    if true {
   LL|      1|        assert_eq!(1, 1);
   LL|       |    } else {
   LL|      0|        assert_eq!(1, 2);
   LL|       |    }
   LL|      1|}
   LL|       |
   LL|       |// FIXME(Swatinem): Fix known issue that coverage code region columns need to be offset by the
   LL|       |// doc comment line prefix (`///` or `//!`) and any additional indent (before or after the doc
   LL|       |// comment characters). This test produces `llvm-cov show` results demonstrating the problem.
   LL|       |//
   LL|       |// One of the above tests now includes: `derive(Debug, PartialEq)`, producing an `llvm-cov show`
   LL|       |// result with a distinct count for `Debug`, denoted by `^1`, but the caret points to the wrong
   LL|       |// column. Similarly, the `if` blocks without `else` blocks show `^0`, which should point at, or
   LL|       |// one character past, the `if` block's closing brace. In both cases, these are most likely off
   LL|       |// by the number of characters stripped from the beginning of each doc comment line: indent
   LL|       |// whitespace, if any, doc comment prefix (`//!` in this case) and (I assume) one space character
   LL|       |// (?). Note, when viewing `llvm-cov show` results in `--color` mode, the column offset errors are
   LL|       |// more pronounced, and show up in more places, with background color used to show some distinct
   LL|       |// code regions with different coverage counts.
   LL|       |//
   LL|       |// NOTE: Since the doc comment line prefix may vary, one possible solution is to replace each
   LL|       |// character stripped from the beginning of doc comment lines with a space. This will give coverage
   LL|       |// results the correct column offsets, and I think it should compile correctly, but I don't know
   LL|       |// what affect it might have on diagnostic messages from the compiler, and whether anyone would care
   LL|       |// if the indentation changed. I don't know if there is a more viable solution.