我正在编写一个宏的测试,我想要导出。只要我将测试保留在单个文件中,测试就可以正常工作,但是一旦我将测试模块放在一个单独的文件中,就会出现错误。
export/src/lib.rs
pub mod my_mod {
#[macro_export]
macro_rules! my_macro {
( $x:expr ) => { $x + 1 };
}
pub fn my_func(x: isize) -> isize {
my_macro!(x)
}
}
export/tests/lib.rs
#[macro_use]
extern crate export;
mod my_test_mod {
use export::my_mod;
#[test]
fn test_func() {
assert_eq!(my_mod::my_func(1), 2);
}
#[test]
fn test_macro() {
assert_eq!(my_macro!(1), 2);
}
}
运行cargo test
表明两个测试都通过了。如果我将my_test_mod
提取到一个文件中,它就无法编译。
export/src/lib.rs
不变
export/tests/lib.rs
#[macro_use]
extern crate export;
mod my_test_mod;
export/tests/my_test_mod.rs
use export::my_mod;
#[test]
fn test_func() {
assert_eq!(my_mod::my_func(1), 2);
}
#[test]
fn test_macro() {
assert_eq!(my_macro!(1), 2); // error: macro undefined: 'my_macro!'
}
这让我出现了一个错误,提示宏未定义。
macro_export
,而且宏在使用之前被导入了;能否帮我找到重复的部分? - Shepmaster