Skip to content

Commit 95ccd92

Browse files
fiploxVolodymyr Patuta
andauthored
feat(try_from_into): Add tests (#571)
Co-authored-by: Volodymyr Patuta <6977238-fiplox@users.noreply.gitlab.com>
1 parent 197d3a3 commit 95ccd92

File tree

1 file changed

+56
-37
lines changed

1 file changed

+56
-37
lines changed

exercises/conversions/try_from_into.rs

Lines changed: 56 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
// You can read more about it at https://doc.rust-lang.org/std/convert/trait.TryFrom.html
55
use std::convert::{TryFrom, TryInto};
66

7-
#[derive(Debug)]
7+
#[derive(Debug, PartialEq)]
88
struct Color {
99
red: u8,
1010
green: u8,
@@ -25,22 +25,19 @@ struct Color {
2525
// Tuple implementation
2626
impl TryFrom<(i16, i16, i16)> for Color {
2727
type Error = String;
28-
fn try_from(tuple: (i16, i16, i16)) -> Result<Self, Self::Error> {
29-
}
28+
fn try_from(tuple: (i16, i16, i16)) -> Result<Self, Self::Error> {}
3029
}
3130

3231
// Array implementation
3332
impl TryFrom<[i16; 3]> for Color {
3433
type Error = String;
35-
fn try_from(arr: [i16; 3]) -> Result<Self, Self::Error> {
36-
}
34+
fn try_from(arr: [i16; 3]) -> Result<Self, Self::Error> {}
3735
}
3836

3937
// Slice implementation
4038
impl TryFrom<&[i16]> for Color {
4139
type Error = String;
42-
fn try_from(slice: &[i16]) -> Result<Self, Self::Error> {
43-
}
40+
fn try_from(slice: &[i16]) -> Result<Self, Self::Error> {}
4441
}
4542

4643
fn main() {
@@ -66,71 +63,93 @@ mod tests {
6663
use super::*;
6764

6865
#[test]
69-
#[should_panic]
7066
fn test_tuple_out_of_range_positive() {
71-
let _ = Color::try_from((256, 1000, 10000)).unwrap();
67+
assert!(Color::try_from((256, 1000, 10000)).is_err());
7268
}
7369
#[test]
74-
#[should_panic]
7570
fn test_tuple_out_of_range_negative() {
76-
let _ = Color::try_from((-1, -10, -256)).unwrap();
71+
assert!(Color::try_from((-1, -10, -256)).is_err());
72+
}
73+
#[test]
74+
fn test_tuple_sum() {
75+
assert!(Color::try_from((-1, 255, 255)).is_err());
7776
}
7877
#[test]
7978
fn test_tuple_correct() {
80-
let c: Color = (183, 65, 14).try_into().unwrap();
81-
assert_eq!(c.red, 183);
82-
assert_eq!(c.green, 65);
83-
assert_eq!(c.blue, 14);
79+
let c: Result<Color, String> = (183, 65, 14).try_into();
80+
assert_eq!(
81+
c,
82+
Ok(Color {
83+
red: 183,
84+
green: 65,
85+
blue: 14
86+
})
87+
);
8488
}
85-
8689
#[test]
87-
#[should_panic]
8890
fn test_array_out_of_range_positive() {
89-
let _: Color = [1000, 10000, 256].try_into().unwrap();
91+
let c: Color = [1000, 10000, 256].try_into();
92+
assert!(c.is_err());
9093
}
9194
#[test]
92-
#[should_panic]
9395
fn test_array_out_of_range_negative() {
94-
let _: Color = [-10, -256, -1].try_into().unwrap();
96+
let c: Color = [-10, -256, -1].try_into();
97+
assert!(c.is_err());
9598
}
9699
#[test]
100+
fn test_array_sum() {
101+
let c: Color = [-1, 255, 255].try_into();
102+
assert!(c.is_err());
103+
}
104+
#[test]
105+
#[test]
97106
fn test_array_correct() {
98-
let c: Color = [183, 65, 14].try_into().unwrap();
99-
assert_eq!(c.red, 183);
100-
assert_eq!(c.green, 65);
101-
assert_eq!(c.blue, 14);
107+
let c: Result<Color, String> = [183, 65, 14].try_into();
108+
assert_eq!(
109+
c,
110+
Ok(Color {
111+
red: 183,
112+
green: 65,
113+
blue: 14
114+
})
115+
);
102116
}
103-
104117
#[test]
105-
#[should_panic]
106118
fn test_slice_out_of_range_positive() {
107119
let arr = [10000, 256, 1000];
108-
let _ = Color::try_from(&arr[..]).unwrap();
120+
assert!(Color::try_from(&arr[..]).is_err());
109121
}
110122
#[test]
111-
#[should_panic]
112123
fn test_slice_out_of_range_negative() {
113124
let arr = [-256, -1, -10];
114-
let _ = Color::try_from(&arr[..]).unwrap();
125+
assert!(Color::try_from(&arr[..]).is_err());
126+
}
127+
#[test]
128+
fn test_slice_sum() {
129+
let arr = [-1, 255, 255];
130+
assert!(Color::try_from(&arr[..]).is_err());
115131
}
116132
#[test]
117133
fn test_slice_correct() {
118134
let v = vec![183, 65, 14];
119-
let c = Color::try_from(&v[..]).unwrap();
120-
assert_eq!(c.red, 183);
121-
assert_eq!(c.green, 65);
122-
assert_eq!(c.blue, 14);
135+
let c: Result<Color, String> = Color::try_from(&v[..]);
136+
assert_eq!(
137+
c,
138+
Ok(Color {
139+
red: 183,
140+
green: 65,
141+
blue: 14
142+
})
143+
);
123144
}
124145
#[test]
125-
#[should_panic]
126146
fn test_slice_excess_length() {
127147
let v = vec![0, 0, 0, 0];
128-
let _ = Color::try_from(&v[..]).unwrap();
148+
assert!(Color::try_from(&v[..]).is_err());
129149
}
130150
#[test]
131-
#[should_panic]
132151
fn test_slice_insufficient_length() {
133152
let v = vec![0, 0];
134-
let _ = Color::try_from(&v[..]).unwrap();
153+
assert!(Color::try_from(&v[..]).is_err());
135154
}
136155
}

0 commit comments

Comments
 (0)