Skip to content

Commit 9a9b487

Browse files
authored
Merge pull request #1463 from poneciak57/feature/test4_panic
feat: add tests4.rs exercise
2 parents 01fa21f + 102d7f3 commit 9a9b487

File tree

2 files changed

+56
-0
lines changed

2 files changed

+56
-0
lines changed

exercises/tests/tests4.rs

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
// tests4.rs
2+
// Make sure that we're testing for the correct conditions!
3+
// Execute `rustlings hint tests4` or use the `hint` watch subcommand for a hint.
4+
5+
// I AM NOT DONE
6+
7+
struct Rectangle {
8+
width: i32,
9+
height: i32
10+
}
11+
12+
impl Rectangle {
13+
// Only change the test functions themselves
14+
pub fn new(width: i32, height: i32) -> Self {
15+
if width <= 0 || height <= 0 {
16+
panic!("Rectangle width and height cannot be negative!")
17+
}
18+
Rectangle {width, height}
19+
}
20+
}
21+
22+
#[cfg(test)]
23+
mod tests {
24+
use super::*;
25+
26+
#[test]
27+
fn correct_width_and_height() {
28+
// This test should check if the rectangle is the size that we pass into its constructor
29+
let rect = Rectangle::new(10, 20);
30+
assert_eq!(???, 10); // check width
31+
assert_eq!(???, 20); // check height
32+
}
33+
34+
#[test]
35+
fn negative_width() {
36+
// This test should check if program panics when we try to create rectangle with negative width
37+
let _rect = Rectangle::new(-10, 10);
38+
}
39+
40+
#[test]
41+
fn negative_height() {
42+
// This test should check if program panics when we try to create rectangle with negative height
43+
let _rect = Rectangle::new(10, -10);
44+
}
45+
}

info.toml

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -807,6 +807,17 @@ You can call a function right where you're passing arguments to `assert!` -- so
807807
something like `assert!(having_fun())`. If you want to check that you indeed get false, you
808808
can negate the result of what you're doing using `!`, like `assert!(!having_fun())`."""
809809

810+
[[exercises]]
811+
name = "tests4"
812+
path = "exercises/tests/tests4.rs"
813+
mode = "test"
814+
hint = """
815+
We expect method `Rectangle::new()` to panic for negative values.
816+
To handle that you need to add a special attribute to the test function.
817+
You can refer to the docs:
818+
https://doc.rust-lang.org/stable/book/ch11-01-writing-tests.html#checking-for-panics-with-should_panic"""
819+
820+
810821
# STANDARD LIBRARY TYPES
811822

812823
[[exercises]]

0 commit comments

Comments
 (0)