Doctests can't be triggered for structs with lifetimes
anergictcell opened this issue · comments
I cannot run the doctests from SublimeText directly when a struct contains a lifetime parameter.
Code to reproduce:
pub struct Works {}
impl Works {
/// Creates self
///
/// # Examples
/// ```
/// use lsp_doctest::Works;
/// let x = Works::foo();
/// assert_eq!(x.bar(), 12);
/// ```
pub fn foo() -> Self {
Self {}
}
pub fn bar(&self) -> usize {
12
}
}
pub struct DoesntWork<'a> {
x: &'a usize
}
impl<'a> DoesntWork<'a> {
/// Creates self
///
/// # Examples
/// ```
/// use lsp_doctest::DoesntWork;
/// let n = 12usize;
/// let x = DoesntWork::foo(&n);
/// assert_eq!(x.bar(), &12);
/// ```
pub fn foo(x: &'a usize) -> Self {
Self {x}
}
pub fn bar(&self) -> &usize {
self.x
}
}
Clicking the "Run Doctest" for the Works
struct works with the following result:
Finished test [unoptimized + debuginfo] target(s) in 0.00s
Doc-tests lsp-doctest
running 1 test
test src/lib.rs - Works::foo (line 6) ... ok
test result: ok. 1 passed; 0 failed; 0 ignored; 0 measured; 1 filtered out; finished in 0.60s
process is terminated with return code 0.
Clicking the "Run Doctest" for the DoesntWork
struct does not work and returns the following result:
Finished test [unoptimized + debuginfo] target(s) in 0.00s
Doc-tests lsp-doctest
running 0 tests
test result: ok. 0 passed; 0 failed; 0 ignored; 0 measured; 2 filtered out; finished in 0.00s
process is terminated with return code 0.
Maybe this is related to the fact that the test is called DoesntWork<'a>::foo
? and the '
somewhat breaks the test command? Is there a way to see the actual command that SublimeText executes?
The tests work when run from the command line:
$ cargo test --doc
Compiling lsp-doctest v0.1.0 (./lsp-doctest)
Finished test [unoptimized + debuginfo] target(s) in 0.25s
Doc-tests lsp-doctest
running 2 tests
test src/lib.rs - Works::foo (line 6) ... ok
test src/lib.rs - DoesntWork<'a>::foo (line 27) ... ok
test result: ok. 2 passed; 0 failed; 0 ignored; 0 measured; 0 filtered out; finished in 0.55s
Addendum:
I did check the command line that is called to run doctest:
rustdoc \
--edition=2021 \
--crate-type lib \
--crate-name lsp_doctest \
--test lsp-doctest/src/lib.rs \
-L dependency=lsp-doctest/target/debug/deps \
-L dependency=lsp-doctest/target/debug/deps \
--test-args DoesntWork::foo \
--nocapture \
--extern lsp_doctest=lsp-doctest/target/debug/deps/liblsp_doctest-60746c6531973132.rlib -C embed-bitcode=no \
--error-format human
The DoesntWork::foo
filtering returns 0 results, because the test should be called DoesntWork<'a>::foo
In order to manually run the test on the command line, one needs to run:
cargo test -- DoesntWork\<\'a\>::foo
and escape the redirect <
and >
characters as well as the single quote '
This command comes from the rust-analyzer, not generated by this plugin. If it is generating the wrong command it will need to be fixed upstream.
I tested this same test case in VSCode's rust-analyzer plugin and it behaves the same way so this will likely need to be reported upstream https://github.com/rust-lang/rust-analyzer
Thanks for the quick response. I opened an issue on rust-analyzer now (rust-lang/rust-analyzer#14142)