我已经使用条件编译来更改函数的类型签名,现在无法为两种“功能”模式运行相同的 doctest,因此我需要一种选择退出 doctest 的方法。
我尝试合并在正常测试中使用的#[cfg_attr(feature = "rss_loose", ignore)]
和///rust,ignore
来制作///rust,cfg_attr(feature = "rss_loose", ignore)
但这似乎不起作用。
最佳答案
只需编写两套不同的文档和测试,一切都将按原样运行:
/// ```
/// assert_eq!(42, dt::foo());
/// ```
#[cfg(not(feature = "alternate"))]
pub fn foo() -> u8 { 42 }
/// ```
/// assert_eq!(true, dt::foo());
/// ```
#[cfg(feature = "alternate")]
pub fn foo() -> bool { true }
$ cargo test
Compiling dt v0.1.0 (file:///private/tmp/dt)
Running target/debug/dt-c3e297f8592542b5
running 0 tests
test result: ok. 0 passed; 0 failed; 0 ignored; 0 measured
Doc-tests dt
running 1 test
test foo_0 ... ok
test result: ok. 1 passed; 0 failed; 0 ignored; 0 measured
$ cargo test --features=alternate
Compiling dt v0.1.0 (file:///private/tmp/dt)
Running target/debug/dt-c3e297f8592542b5
running 0 tests
test result: ok. 0 passed; 0 failed; 0 ignored; 0 measured
Doc-tests dt
running 1 test
test foo_0 ... ok
test result: ok. 1 passed; 0 failed; 0 ignored; 0 measured
关于rust - 如何使用具有 doctests 的 Rust 项目实现条件编译?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38292741/