一个具有doctests的Rust项目将如何实现条件编译? [英] How would one achieve conditional compilation with Rust projects that have doctests?

查看:190
本文介绍了一个具有doctests的Rust项目将如何实现条件编译?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经使用条件编译来更改函数的类型签名,现在无法在两个功能"模式下运行相同的doctest,因此我需要一种退出doctest的方法.

I've used conditional compilation to change the type signature of a function, and now the same doctest can't be run for both "feature" modes, so I need a way to opt-out of the doctests.

我尝试将普通测试中使用的#[cfg_attr(feature = "rss_loose", ignore)]///rust,ignore合并为///rust,cfg_attr(feature = "rss_loose", ignore),但这似乎不起作用.

I've tried merging #[cfg_attr(feature = "rss_loose", ignore)] used in normal tests and ///rust,ignore to make ///rust,cfg_attr(feature = "rss_loose", ignore) but this doesn't seem to work.

推荐答案

只需编写两组不同的文档和测试,它们将按原样工作:

Just write two different sets of documentation and tests and it will all work as-is:

/// ```
/// 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

这篇关于一个具有doctests的Rust项目将如何实现条件编译?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆