File tree Expand file tree Collapse file tree 2 files changed +56
-0
lines changed Expand file tree Collapse file tree 2 files changed +56
-0
lines changed Original file line number Diff line number Diff line change
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
+ }
Original file line number Diff line number Diff line change @@ -807,6 +807,17 @@ 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 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
+
810
821
# STANDARD LIBRARY TYPES
811
822
812
823
[[exercises ]]
You can’t perform that action at this time.
0 commit comments