File tree Expand file tree Collapse file tree 2 files changed +50
-0
lines changed Expand file tree Collapse file tree 2 files changed +50
-0
lines changed Original file line number Diff line number Diff line change
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
+ }
Original file line number Diff line number Diff line change @@ -807,6 +807,16 @@ You can call a function right where you're passing arguments to `assert!` -- so
807
807
something like `assert!(having_fun())`. If you want to check that you indeed get false, you
808
808
can negate the result of what you're doing using `!`, like `assert!(!having_fun())`."""
809
809
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
+
810
820
# STANDARD LIBRARY TYPES
811
821
812
822
[[exercises ]]
You can’t perform that action at this time.
0 commit comments