I am trying to get rust to test an example from my inline documentation. I wrote the following code:
#[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);
}
Then I run
rustdoc --test src/main.rs
Which gave me:
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
How can I get rustc to find flip and Color. The test runs fine in the main function. I have also tried the command:
cargo test
but that did not run any tests.
I have tried add the following line to the example:
/// use crate::{flip, Color};
making:
// 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);
///```
but that gives an error
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
I have also tried to Color and flip as public:
#[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);
}
but that gave the same error.
use crate::{flip, Color};
– Heat