mercredi 11 novembre 2020

How to use inline documentation for tests in rust

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.

Aucun commentaire:

Enregistrer un commentaire