]> git.proxmox.com Git - rustc.git/blob - tests/run-coverage-rustdoc/doctest.coverage
New upstream version 1.74.1+dfsg1
[rustc.git] / tests / run-coverage-rustdoc / doctest.coverage
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
9 LL| | }
10 LL| 3|}
11
12 $DIR/doctest.rs:
13 LL| |// aux-build:doctest_crate.rs
14 LL| |
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.
17 LL| |//!
18 LL| |//! Just some random code:
19 LL| 1|//! ```
20 LL| 1|//! if true {
21 LL| |//! // this is executed!
22 LL| 1|//! assert_eq!(1, 1);
23 LL| |//! } else {
24 LL| |//! // this is not!
25 LL| 0|//! assert_eq!(1, 2);
26 LL| |//! }
27 LL| 1|//! ```
28 LL| |//!
29 LL| |//! doctest testing external code:
30 LL| |//! ```
31 LL| 1|//! extern crate doctest_crate;
32 LL| 1|//! doctest_crate::fn_run_in_doctests(1);
33 LL| 1|//! ```
34 LL| |//!
35 LL| |//! doctest returning a result:
36 LL| 1|//! ```
37 LL| 2|//! #[derive(Debug, PartialEq)]
38 ^1
39 LL| 1|//! struct SomeError {
40 LL| 1|//! msg: String,
41 LL| 1|//! }
42 LL| 1|//! let mut res = Err(SomeError { msg: String::from("a message") });
43 LL| 1|//! if res.is_ok() {
44 LL| 0|//! res?;
45 LL| |//! } else {
46 LL| 1|//! if *res.as_ref().unwrap_err() == *res.as_ref().unwrap_err() {
47 LL| 1|//! println!("{:?}", res);
48 LL| 1|//! }
49 ^0
50 LL| 1|//! if *res.as_ref().unwrap_err() == *res.as_ref().unwrap_err() {
51 LL| 1|//! res = Ok(1);
52 LL| 1|//! }
53 ^0
54 LL| 1|//! res = Ok(0);
55 LL| |//! }
56 LL| |//! // need to be explicit because rustdoc cant infer the return type
57 LL| 1|//! Ok::<(), SomeError>(())
58 LL| 1|//! ```
59 LL| |//!
60 LL| |//! doctest with custom main:
61 LL| |//! ```
62 LL| 1|//! fn some_func() {
63 LL| 1|//! println!("called some_func()");
64 LL| 1|//! }
65 LL| |//!
66 LL| 0|//! #[derive(Debug)]
67 LL| |//! struct SomeError;
68 LL| |//!
69 LL| |//! extern crate doctest_crate;
70 LL| |//!
71 LL| 1|//! fn doctest_main() -> Result<(), SomeError> {
72 LL| 1|//! some_func();
73 LL| 1|//! doctest_crate::fn_run_in_doctests(2);
74 LL| 1|//! Ok(())
75 LL| 1|//! }
76 LL| |//!
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()
81 LL| |//! }
82 LL| |//! ```
83 LL| |
84 LL| |/// doctest attached to fn testing external code:
85 LL| |/// ```
86 LL| 1|/// extern crate doctest_crate;
87 LL| 1|/// doctest_crate::fn_run_in_doctests(3);
88 LL| 1|/// ```
89 LL| |///
90 LL| 1|fn main() {
91 LL| 1| if true {
92 LL| 1| assert_eq!(1, 1);
93 LL| | } else {
94 LL| 0| assert_eq!(1, 2);
95 LL| | }
96 LL| 1|}
97 LL| |
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.
101 LL| |//
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.
111 LL| |//
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.
117