Skip to content

Commit 27b7579

Browse files
committed
created task
1 parent 01fa21f commit 27b7579

File tree

2 files changed

+50
-0
lines changed

2 files changed

+50
-0
lines changed

exercises/tests/tests4.rs

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
// tests4.rs
2+
// Correct the tests to
3+
// Do not change Rectangle::new body
4+
// Execute `rustlings hint tests4` or use the `hint` watch subcommand for a hint.
5+
6+
// I AM NOT DONE
7+
8+
struct Rectangle {
9+
width: i32,
10+
height: i32
11+
}
12+
13+
impl Rectangle {
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+
let _rect = Rectangle::new(10, 10);
29+
}
30+
31+
#[test]
32+
fn negative_width() {
33+
let _rect = Rectangle::new(-10, 10);
34+
}
35+
36+
#[test]
37+
fn negative_height() {
38+
let _rect = Rectangle::new(10, -10);
39+
}
40+
}

info.toml

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -807,6 +807,16 @@ 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 special attribute to test function.
817+
You can refer to the docs: https://doc.rust-lang.org/stable/book/ch11-01-writing-tests.html"""
818+
819+
810820
# STANDARD LIBRARY TYPES
811821

812822
[[exercises]]

0 commit comments

Comments
 (0)