mirror of
https://github.com/alrayyes/wiki.git
synced 2024-11-29 14:36:22 +00:00
179 lines
3 KiB
Markdown
179 lines
3 KiB
Markdown
|
---
|
||
|
id: 23e290f6-c964-4aee-a133-232291ecdba8
|
||
|
title: Tests in Rust
|
||
|
---
|
||
|
|
||
|
# Assertions
|
||
|
|
||
|
## assert!
|
||
|
|
||
|
``` rust
|
||
|
struct Rectangle {
|
||
|
width: u32,
|
||
|
height: u32,
|
||
|
}
|
||
|
|
||
|
impl Rectangle {
|
||
|
pub fn can_hold(&self, other: &Rectangle) -> bool {
|
||
|
self.width > other.width && self.height > other.height
|
||
|
}
|
||
|
}
|
||
|
|
||
|
pub struct Guess {
|
||
|
value: i32,
|
||
|
}
|
||
|
|
||
|
#[cfg(test)]
|
||
|
mod tests {
|
||
|
use super::*;
|
||
|
|
||
|
#[test]
|
||
|
fn larger_can_hold_smaller() {
|
||
|
let larger = Rectangle {
|
||
|
width: 8,
|
||
|
height: 7,
|
||
|
};
|
||
|
let smaller = Rectangle {
|
||
|
width: 5,
|
||
|
height: 1,
|
||
|
};
|
||
|
|
||
|
assert!(larger.can_hold(&smaller))
|
||
|
}
|
||
|
|
||
|
#[test]
|
||
|
fn smaller_cannot_hold_larger() {
|
||
|
let larger = Rectangle {
|
||
|
width: 8,
|
||
|
height: 7,
|
||
|
};
|
||
|
let smaller = Rectangle {
|
||
|
width: 5,
|
||
|
height: 1,
|
||
|
};
|
||
|
|
||
|
assert!(!smaller.can_hold(&larger))
|
||
|
}
|
||
|
}
|
||
|
```
|
||
|
|
||
|
## assert~eq~!
|
||
|
|
||
|
``` rust
|
||
|
#[cfg(test)]
|
||
|
mod tests {
|
||
|
#[test]
|
||
|
fn it_works() {
|
||
|
assert_eq!(2 + 2, 4);
|
||
|
}
|
||
|
}
|
||
|
```
|
||
|
|
||
|
# Results
|
||
|
|
||
|
## Contains
|
||
|
|
||
|
``` rust
|
||
|
pub fn greeting(name: &str) -> String {
|
||
|
format!("Hello {}!", name)
|
||
|
}
|
||
|
|
||
|
#[cfg(test)]
|
||
|
mod tests {
|
||
|
use super::*;
|
||
|
|
||
|
#[test]
|
||
|
fn greeting_contains_name() {
|
||
|
let result = greeting("Carrol");
|
||
|
assert!(
|
||
|
result.contains("Carrol"),
|
||
|
"Greeting did not countain name, value was `{}`",
|
||
|
result
|
||
|
)
|
||
|
}
|
||
|
}
|
||
|
```
|
||
|
|
||
|
# Annotations
|
||
|
|
||
|
## should~panic~
|
||
|
|
||
|
``` rust
|
||
|
pub struct Guess {
|
||
|
value: i32,
|
||
|
}
|
||
|
|
||
|
impl Guess {
|
||
|
pub fn new(value: i32) -> Guess {
|
||
|
if value < 1 {
|
||
|
panic!(
|
||
|
"Guess value must be greater than or equal to 1, got {}.",
|
||
|
value
|
||
|
);
|
||
|
} else if value > 100 {
|
||
|
panic!(
|
||
|
"Guess value must be less than or equal to 100, got {}.",
|
||
|
value
|
||
|
);
|
||
|
}
|
||
|
|
||
|
Guess { value }
|
||
|
}
|
||
|
}
|
||
|
|
||
|
#[cfg(test)]
|
||
|
mod tests {
|
||
|
use super::*;
|
||
|
|
||
|
#[test]
|
||
|
#[should_panic(expected = "Guess value must be less than or equal to 100")]
|
||
|
fn greater_than_100() {
|
||
|
Guess::new(200);
|
||
|
}
|
||
|
}
|
||
|
|
||
|
```
|
||
|
|
||
|
## ignore
|
||
|
|
||
|
This ignores tests unless specifically requested:
|
||
|
|
||
|
``` rust
|
||
|
#[test]
|
||
|
#[ignore]
|
||
|
fn expensive_test() {
|
||
|
// code that takes an hour to run
|
||
|
}
|
||
|
|
||
|
fn main() {}
|
||
|
```
|
||
|
|
||
|
# Integreation Tests
|
||
|
|
||
|
Integration tests are their own thing and I'm too lazy to take notes now
|
||
|
as it's 18:59 on a Friday. See relevant chapter in [the
|
||
|
book](https://doc.rust-lang.org/stable/book/ch11-03-test-organization.html)
|
||
|
for more information.
|
||
|
|
||
|
# Using Result \<T, E\> in Tests
|
||
|
|
||
|
This is possible for when you want to use the [question mark
|
||
|
operator](https://doc.rust-lang.org/edition-guide/rust-2018/error-handling-and-panics/the-question-mark-operator-for-easier-error-handling.html)
|
||
|
in your tests:
|
||
|
|
||
|
``` rust
|
||
|
#[cfg(test)]
|
||
|
mod tests {
|
||
|
use super::*;
|
||
|
|
||
|
#[test]
|
||
|
fn it_works() -> Result<(), String> {
|
||
|
if 2 + 2 == 4 {
|
||
|
Ok(())
|
||
|
} else {
|
||
|
Err(String::from("two plus two does not equal four"))
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
```
|