Actually, this bit about tests needs to go after intro of use

This commit is contained in:
Carol (Nichols || Goulding)
2016-08-26 17:41:35 -07:00
parent afc4bc2541
commit d63d88ddeb
2 changed files with 90 additions and 85 deletions
@@ -183,91 +183,6 @@ src/network/server.rs:1 fn connect() {
Only one last warning! Try to fix this one on your own!
## Visibility in tests
Remember when we created our crate that Cargo made a `tests` module for us? Let's talk about that now. It was in `src/lib.rs`:
Filename: src/lib.rs
```rust,ignore
pub mod client;
pub mod network;
#[cfg(test)]
mod tests {
#[test]
fn it_works() {
}
}
```
We'll explain more about testing in Chapter XX, but parts of this should make
sense now: we have a module named `tests` that lives next to our other modules
and contains one function named `it_works()`. Even though there are special
annotations, the `tests` module is just another module!
Since tests are for exercising the code within our library, let's try to call
our `client::connect()` function from this `it_works()` function, even though
we're not going to be checking any functionality right now:
```rust
#[cfg(test)]
mod tests {
#[test]
fn it_works() {
client::connect();
}
}
```
Run the tests by invoking the `cargo test` command:
```bash
$ cargo test
Compiling modules v0.1.0 (file:///projects/modules)
src/lib.rs:9:9: 9:24 error: failed to resolve. Use of undeclared type or module
`client` [E0433]
src/lib.rs:9 client::connect();
^~~~~~~~~~~~~~~
src/lib.rs:9:9: 9:24 help: run `rustc --explain E0433` to see a detailed
explanation
```
Why doesn't this work? It's not because we don't have `modules::` in front of
the function like we had in `src/main.rs`: we are definitely within the
`modules` library crate here. It's because we have to be explicit about the
names we want to `use` in scope, even with sibling modules in the same library.
We need to bring `client` in scope. `use` is relative to the current module,
`tests`. We can move up a module level using the `super` module to refer to the
parent module of `tests`, and then we can access the sibling `client` module:
```rust
#[cfg(test)]
mod tests {
use super::client;
#[test]
fn it_works() {
client::connect();
}
}
```
If we run `cargo test` again, the test will pass and the first part of the test
result output will be:
```bash
$ cargo test
Compiling modules v0.1.0 (file:///projects/modules)
Running target/debug/modules-92007ddb5330fa5a
running 1 test
test tests::it_works ... ok
test result: ok. 1 passed; 0 failed; 0 ignored; 0 measured
```
## Privacy rules
FIXME: these are the rules:
+90
View File
@@ -142,3 +142,93 @@ src/main.rs:8 use TrafficLight::*;
If, instead, we were explicit about the variants we wanted to import, our code
would not have stopped compiling when we upgraded the `traffic_light` crate.
## Using `super` to access a parent module
Remember when we created our crate that Cargo made a `tests` module for us? Let's talk about that now. It was in `src/lib.rs`:
Filename: src/lib.rs
```rust,ignore
pub mod client;
pub mod network;
#[cfg(test)]
mod tests {
#[test]
fn it_works() {
}
}
```
We'll explain more about testing in Chapter XX, but parts of this should make
sense now: we have a module named `tests` that lives next to our other modules
and contains one function named `it_works()`. Even though there are special
annotations, the `tests` module is just another module!
Since tests are for exercising the code within our library, let's try to call
our `client::connect()` function from this `it_works()` function, even though
we're not going to be checking any functionality right now:
```rust
#[cfg(test)]
mod tests {
#[test]
fn it_works() {
client::connect();
}
}
```
Run the tests by invoking the `cargo test` command:
```bash
$ cargo test
Compiling modules v0.1.0 (file:///projects/modules)
src/lib.rs:9:9: 9:24 error: failed to resolve. Use of undeclared type or module
`client` [E0433]
src/lib.rs:9 client::connect();
^~~~~~~~~~~~~~~
src/lib.rs:9:9: 9:24 help: run `rustc --explain E0433` to see a detailed
explanation
```
Why doesn't this work? It's not because we don't have `modules::` in front of
the function like we had in `src/main.rs`: we are definitely within the
`modules` library crate here. It's because we have to be explicit about the
names we want to `use` in scope, even with sibling modules in the same library.
We need to bring `client` in scope.
`use` is relative to the current module, `tests`. We can move up a module level
using `super` to refer to the parent module of `tests`, and from there we can
access the sibling `client` module:
```rust
#[cfg(test)]
mod tests {
use super::client;
#[test]
fn it_works() {
client::connect();
}
}
```
If we run `cargo test` again, the test will pass and the first part of the test
result output will be:
```bash
$ cargo test
Compiling modules v0.1.0 (file:///projects/modules)
Running target/debug/modules-92007ddb5330fa5a
running 1 test
test tests::it_works ... ok
test result: ok. 1 passed; 0 failed; 0 ignored; 0 measured
```
`super` is generally only used with tests because...?