我正在尝试让 Rust 来测试我的内联文档中的示例。我写了以下代码:
#[derive(Clone)]
#[derive(PartialEq)]
#[derive(Debug)]
enum Color {
Black = 1,
Empty = 0,
White = -1
}
/// Chages Color to the next player
///
/// Returns: White if player is Black, Black if player is White and Empty if
/// player is Empty.
///
/// Examlple
/// ```
/// assert_eq!(flip(&Color::White),Color::Black);
///```
// Invariant Color must represent Black as 1, Empty as 0 and White as -1!
fn flip(player: &Color)->Color{
let intrepresentation :i8 = (player.clone() as i8) * (-1);
unsafe{
std::mem::transmute(intrepresentation)
}
}
fn main() {
assert_eq!(flip(&Color::White),Color::Black);
}
Run Code Online (Sandbox Code Playgroud)
然后我跑
rustdoc --test src/main.rs
Run Code Online (Sandbox Code Playgroud)
这给了我:
running 1 test
test src/main.rs - flip (line 16) ... FAILED
failures:
---- src/main.rs - flip (line 16) stdout ----
error[E0433]: failed to resolve: use of undeclared type or module `Color`
--> src/main.rs:17:18
|
3 | assert_eq!(flip(&Color::White),Color::Black);
| ^^^^^ use of undeclared type or module `Color`
error[E0433]: failed to resolve: use of undeclared type or module `Color`
--> src/main.rs:17:32
|
3 | assert_eq!(flip(&Color::White),Color::Black);
| ^^^^^ use of undeclared type or module `Color`
error[E0425]: cannot find function `flip` in this scope
--> src/main.rs:17:12
|
3 | assert_eq!(flip(&Color::White),Color::Black);
| ^^^^ not found in this scope
error: aborting due to 3 previous errors
Some errors have detailed explanations: E0425, E0433.
For more information about an error, try `rustc --explain E0425`.
Couldn't compile the test.
failures:
src/main.rs - flip (line 16)
test result: FAILED. 0 passed; 1 failed; 0 ignored; 0 measured; 0 filtered out
Run Code Online (Sandbox Code Playgroud)
我怎样才能让 rustc 找到翻转和颜色。测试在主函数中运行良好。我也尝试过命令:
cargo test
Run Code Online (Sandbox Code Playgroud)
但没有运行任何测试。
我尝试将以下行添加到示例中:
/// use crate::{flip, Color};
Run Code Online (Sandbox Code Playgroud)
制作:
// Chages Color to the next player
///
/// Returns: White if player is Black, Black if player is White and Empty if
/// player is Empty.
///
/// Examlple
/// ```
/// use crate::{flip, Color};
/// assert_eq!(flip(&Color::White),Color::Black);
///```
Run Code Online (Sandbox Code Playgroud)
但这给出了一个错误
martin@martin-laptop:~/test_code$ rustdoc --test src/main.rs
running 1 test
test src/main.rs - main (line 23) ... FAILED
failures:
---- src/main.rs - main (line 23) stdout ----
error[E0432]: unresolved import `crate::Color`
--> src/main.rs:24:14
|
3 | use crate::{ Color};
| ^^^^^ no `Color` in the root
error[E0425]: cannot find function `flip` in this scope
--> src/main.rs:25:12
|
4 | assert_eq!(flip(&Color::White),Color::Black);
| ^^^^ not found in this scope
error: aborting due to 2 previous errors
Some errors have detailed explanations: E0425, E0432.
For more information about an error, try `rustc --explain E0425`.
Couldn't compile the test.
failures:
src/main.rs - main (line 23)
test result: FAILED. 0 passed; 1 failed; 0 ignored; 0 measured; 0 filtered out
Run Code Online (Sandbox Code Playgroud)
我还尝试过将颜色和翻转公开:
#[derive(Clone)]
#[derive(PartialEq)]
#[derive(Debug)]
pub enum Color {
Black = 1,
Empty = 0,
White = -1
}
/// Chages Color to the next player
///
/// Returns: White if player is Black, Black if player is White and Empty if
/// player is Empty.
///
/// Examlple
/// ```
/// use crate::{flip, Color};
/// use std::env;
/// assert_eq!(flip(&Color::White),Color::Black);
///```
// Invariant Color must represent Black as 1, Empty as 0 and White as -1!
pub fn flip(player: &Color)->Color{
let intrepresentation :i8 = (player.clone() as i8) * (-1);
unsafe{
std::mem::transmute(intrepresentation)
}
}
fn main() {
assert_eq!(flip(&Color::White),Color::Black);
}
Run Code Online (Sandbox Code Playgroud)
但这给出了同样的错误。
文档测试(内部测试/// ```)被单独编译为自己的小程序。所以:
pub mod, pub fn, pub struct, ...main.rs\xe2\x80\x94\xc2\xa0 如果您的程序在其中,那么它是一个二进制板条箱。use名称,例如use my_library::Color;.如果你想测试不适合这个的东西,那么你应该使用#[test]测试来代替:
#[test]\nfn flip_test() {\n assert_eq!(flip(&Color::White), Color::Black);\n}\nRun Code Online (Sandbox Code Playgroud)\n位于程序中任何位置且具有该属性的任何函数#[test]都将作为测试运行。因此,它们可以访问私有项目,因为它们位于同一模块(或子模块;通常将它们放在tests同一文件中命名的模块中,使用mod tests { ... })。
您可以在Rust 编程语言:如何编写测试中找到有关如何编写测试函数和组织测试的更多信息。
\n\n\n我还尝试过将颜色和翻转公开:
\nRun Code Online (Sandbox Code Playgroud)\n/// use crate::{flip, Color};\n
这不起作用,因为crate引用当前的板条箱,对于文档测试来说,它是测试程序,而不是您的主板条箱。
| 归档时间: |
|
| 查看次数: |
1630 次 |
| 最近记录: |