在doc测试中使用自定义宏

时间:2015-06-27 18:40:58

标签: unit-testing macros rust documentation

我试图让自定义宏在给定代码的文档测试中工作,但它没有拿起宏。我相信我已正确导出它,但我无法通过测试来接收它。有人可以帮忙吗?谢谢!

在doc / module中重新导出...

class AcmeDemoExtension extends Extension
{
    public function load(array $configs, ContainerBuilder $container)
    {
        $loader = new YamlFileLoader($container, new FileLocator(__DIR__ . '/../Resources'));
        $loader->load('services.yml');
    }
}

在定义中......

/// The `Measurement` trait and the `implement_measurement!` macro
/// provides a common way for various measurements to be implemented.
///
/// # Example
/// ```
/// #[macro_use] // <-- Not sure this is correct / necessary...
/// use measurements::measurement::*;
/// 
/// struct Cubits {
///     forearms: f64
/// }
/// 
/// impl Measurement for Cubits {
///     fn get_base_units(&self) -> f64 {
///         self.forearms
///     }
///     
///     fn from_base_units(units: f64) -> Self {
///         Cubits { forearms: units }
///     }
/// }
///
/// // Invoke the macro to automatically implement Add, Sub, etc...
/// implement_measurement! { Cubits }
/// ```
#[macro_use]
pub mod measurement;

}

更新

这个问题最终确实是重复的,但我觉得它的解决方案有一个更好的例子会有所帮助。以下是使我的doc测试使用宏的更改。如此处所示,您必须

  1. 添加pub use std::ops::{Add,Sub,Div,Mul}; pub use std::cmp::{Eq, PartialEq}; pub use std::cmp::{PartialOrd, Ordering}; pub trait Measurement { fn get_base_units(&self) -> f64; fn from_base_units(units: f64) -> Self; } #[macro_export] macro_rules! implement_measurement { ($($t:ty)*) => ($( impl Add for $t { type Output = Self; fn add(self, rhs: Self) -> Self { Self::from_base_units(self.get_base_units() + rhs.get_base_units()) } } // ... others ... )) 功能。这有助于移动您的箱子根。
  2. 参考您自己模块的main
  3. extern crate标记添加到包参考。您可以选择导入哪些宏(see the docs)。
  4. 代码:

    #[macro_use]

0 个答案:

没有答案