3
我正在爲要導出的宏編寫測試。只要我將測試保存在單個文件中,測試就能正常工作,但只要我將測試模塊放入單獨的文件中,就會出現錯誤。將測試模塊移動到單獨的文件時未定義的宏
出口/ 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)
}
}
出口/測試/ 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
提取到不再編譯的文件。
出口/ src目錄/ lib.rs
不變
出口/測試/ lib.rs
#[macro_use]
extern crate export;
mod my_test_mod;
出口/測試/ 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!'
}
這給我一個錯誤,宏未定義。
@LukasKalbertodt這個看起來有'macro_export',並且在它被使用之前導入宏;介意將點與我的副本連接起來? – Shepmaster
@Shepmaster Nevermind,我的錯誤。累了 ;-) –