-
Notifications
You must be signed in to change notification settings - Fork 144
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Figure out a way to do doctests #797
Comments
I'm wondering about a proc macro which, like However its job would be to convert this sort of code: /// ```
/// /* C++ code:
/// int do_something() {}
/// */
/// include_cpp!(...)
/// fn main() {
/// ffi::do_something()
/// }
/// ``` into something like: /// ```
/// autocxx::build::try_build("int do_something() {}", parse_quote! { include_cpp!(...) fn main() { ffi::do_something() }
/// ```
such that at doctest time, a whole `trybuild` event occurs, creating a separate binary and executing it just like we do for integration tests. |
OK I figured out how to do this. It's just a bit of work.
#[autocxx_doc_test]
mod example {
static CXX: &str = "inline void do_math() {};";
use autocxx::prelude::*;
include_cpp!( /* ... */ )
fn main() {
ffi::do_math()
}
}
inline void do_math() {}; Rust: use autocxx::prelude::*;
include_cpp!( /* ... */ )
fn main() {
ffi::do_math()
}
do_run_test_manual("inline void do_math() {};", "", quote! {
use autocxx::prelude::*;
include_cpp!( /* ... */ )
fn main() {
ffi::do_math()
}
}, None, None); (possibly this could even be a macro-by-example macro) I've done some experimentation, and It ought to result in each test case running in an independent executable, linked with the C++, just like we have for our integration tests. This should result in a similar situation to the integration tests, where the first doctest to run is super-duper slow because The little 'play' button in the mdbook won't work, because it appears to act on the preprocessed code. |
Something roughly like the above is now done in #769. |
Right now our documentation code snippets are marked
nocompile
or use a secret autocxx directiveparseonly!
so they don't actually do anything. There are two reason:include_cpp!
macro.A
(for instance), we will end up withcxx
generating all the C ABI interfaces for Rust to interact withA
.The first problem may be solvable using a procedural macro. I don't know exactly how doc tests work, but the
aquamarine
proc macro clearly does substantial things to doc attributes so we possibly could too (e.g. recognize a special tag for an area of C++ which then gets passed viainclude_cpp
intoautocxx-build
or something).I have no good ideas for how to solve the second problem. The best idea I can come up with is to avoid compiling all the C++ code, and instead make weak symbols for all the C symbols which
cxx
would have generated, which immediately abort. This would prevent users from running the doc tests but would at least fully confirm they compile.It really bugs me that we can't have fully-functioning code snippets, mixing C++ and Rust, in the docs.
This will get worse with #769 which adds a book.
The text was updated successfully, but these errors were encountered: