Like integration tests and unlike unit tests, you can't use local imports in in your doctests:
/// # Examples
///
/// ```
/// use playground::sum; // can't import macro via local path, need to refer to
//7 // exporting crate through extern name inside doctest
///
/// sum!(1, 1, u8);
/// ```
///
#[macro_export]
macro_rules! sum {
($a:expr, $b:expr, $ty:ty) => {
$a as $ty + $b as $ty
}
}
Macros are not normal items and exporting them works a little different. For exported macros the module they are in does not matter, they are always exported from the top-level scope of the crate:
mod mod1 {
mod mod2 {
/// # Examples
///
/// ```
/// use playground::sum; // note how we still import from top-level, even
/// // though the macro is in some other module
///
/// sum!(1, 1, u8);
/// ```
///
#[macro_export]
macro_rules! sum {
($a:expr, $b:expr, $ty:ty) => {
$a as $ty + $b as $ty
};
}
}
}
That much I understand, what I don't understand is the fact that it works using it around the crate imported, as you noted, from the root, but specifically in docttest it fails to find it, imported like use mycrate::sum; or used directly let s = mycrate::sum!(
If you #[macro_export]ed sum you should be able to doctest it by importing it as mycrate::sum. So I assume there's something else causing you troubles, though I can't think of anything that might be the culprit.