Testing 如何使用在线文档进行锈蚀测试

Testing 如何使用在线文档进行锈蚀测试,testing,rust,Testing,Rust,我试图让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 Whit

我试图让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);
}
  
然后我就跑

rustdoc --test src/main.rs 
这给了我:

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
如何让rustc找到翻转和颜色。测试在主功能中运行良好。我还尝试了以下命令:

cargo test
但这并没有运行任何测试

我尝试在示例中添加以下行:

   /// use crate::{flip, Color};
制作:

// 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);
///``` 
但这是一个错误

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 
我也尝试过公开涂色和翻页:

#[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);
}
/// use crate::{flip, Color};
但是这也产生了同样的错误。

文档测试(在
//`
内部的测试)被单独编译成自己的小程序。因此:

  • 他们只能访问公共项目:
    pub mod
    pub fn
    pub struct
  • 他们只能访问导出项目供其他板条箱使用的图书馆板条箱-如果您的程序位于
    main.rs
    中,则它是一个二进制板条箱
  • 您必须完全限定或
    使用
    名称,如
    使用my_library::Color
如果要测试不符合此条件的内容,则应使用
#[test]
测试:

#[测试]
fn翻转试验(){
断言(翻转(&颜色::白色),颜色::黑色);
}
位于程序中任何位置且属性为
#[test]
的任何函数都将作为测试运行。因此,它们可以访问私有项,因为它们位于同一个模块(或子模块;通常将它们放在同一文件中名为
tests
的模块中,使用
mod tests{…}

您可以在上找到有关如何编写测试函数和组织测试的更多信息


我也尝试过公开涂色和翻页:

#[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);
}
/// use crate::{flip, Color};

这不起作用,因为
板条箱
指的是当前板条箱,对于文档测试而言,它是测试程序
,而不是主板条箱。

为什么不添加使用行<代码>使用板条箱:{flip,Color}行的用途是什么?有点像pythonned中的import,将pub添加到Color和flip中,我想很抱歉,我不太习惯用rust编写文档,并且没有太多文档注意,文档测试只适用于库板条箱(可以与测试可执行文件链接),而不适用于程序板条箱。