Skip to content

Commit 8ef5d10

Browse files
committed
Import the error variants in the tests
1 parent 5217cdc commit 8ef5d10

File tree

2 files changed

+20
-42
lines changed

2 files changed

+20
-42
lines changed

exercises/23_conversions/from_str.rs

Lines changed: 10 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -52,10 +52,11 @@ fn main() {
5252
#[cfg(test)]
5353
mod tests {
5454
use super::*;
55+
use ParsePersonError::*;
5556

5657
#[test]
5758
fn empty_input() {
58-
assert_eq!("".parse::<Person>(), Err(ParsePersonError::BadLen));
59+
assert_eq!("".parse::<Person>(), Err(BadLen));
5960
}
6061

6162
#[test]
@@ -69,56 +70,44 @@ mod tests {
6970

7071
#[test]
7172
fn missing_age() {
72-
assert!(matches!(
73-
"John,".parse::<Person>(),
74-
Err(ParsePersonError::ParseInt(_)),
75-
));
73+
assert!(matches!("John,".parse::<Person>(), Err(ParseInt(_))));
7674
}
7775

7876
#[test]
7977
fn invalid_age() {
80-
assert!(matches!(
81-
"John,twenty".parse::<Person>(),
82-
Err(ParsePersonError::ParseInt(_)),
83-
));
78+
assert!(matches!("John,twenty".parse::<Person>(), Err(ParseInt(_))));
8479
}
8580

8681
#[test]
8782
fn missing_comma_and_age() {
88-
assert_eq!("John".parse::<Person>(), Err(ParsePersonError::BadLen));
83+
assert_eq!("John".parse::<Person>(), Err(BadLen));
8984
}
9085

9186
#[test]
9287
fn missing_name() {
93-
assert_eq!(",1".parse::<Person>(), Err(ParsePersonError::NoName));
88+
assert_eq!(",1".parse::<Person>(), Err(NoName));
9489
}
9590

9691
#[test]
9792
fn missing_name_and_age() {
98-
assert!(matches!(
99-
",".parse::<Person>(),
100-
Err(ParsePersonError::NoName | ParsePersonError::ParseInt(_)),
101-
));
93+
assert!(matches!(",".parse::<Person>(), Err(NoName | ParseInt(_))));
10294
}
10395

10496
#[test]
10597
fn missing_name_and_invalid_age() {
10698
assert!(matches!(
10799
",one".parse::<Person>(),
108-
Err(ParsePersonError::NoName | ParsePersonError::ParseInt(_)),
100+
Err(NoName | ParseInt(_)),
109101
));
110102
}
111103

112104
#[test]
113105
fn trailing_comma() {
114-
assert_eq!("John,32,".parse::<Person>(), Err(ParsePersonError::BadLen));
106+
assert_eq!("John,32,".parse::<Person>(), Err(BadLen));
115107
}
116108

117109
#[test]
118110
fn trailing_comma_and_some_string() {
119-
assert_eq!(
120-
"John,32,man".parse::<Person>(),
121-
Err(ParsePersonError::BadLen),
122-
);
111+
assert_eq!("John,32,man".parse::<Person>(), Err(BadLen));
123112
}
124113
}

solutions/23_conversions/from_str.rs

Lines changed: 10 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -56,10 +56,11 @@ fn main() {
5656
#[cfg(test)]
5757
mod tests {
5858
use super::*;
59+
use ParsePersonError::*;
5960

6061
#[test]
6162
fn empty_input() {
62-
assert_eq!("".parse::<Person>(), Err(ParsePersonError::BadLen));
63+
assert_eq!("".parse::<Person>(), Err(BadLen));
6364
}
6465

6566
#[test]
@@ -73,56 +74,44 @@ mod tests {
7374

7475
#[test]
7576
fn missing_age() {
76-
assert!(matches!(
77-
"John,".parse::<Person>(),
78-
Err(ParsePersonError::ParseInt(_)),
79-
));
77+
assert!(matches!("John,".parse::<Person>(), Err(ParseInt(_))));
8078
}
8179

8280
#[test]
8381
fn invalid_age() {
84-
assert!(matches!(
85-
"John,twenty".parse::<Person>(),
86-
Err(ParsePersonError::ParseInt(_)),
87-
));
82+
assert!(matches!("John,twenty".parse::<Person>(), Err(ParseInt(_))));
8883
}
8984

9085
#[test]
9186
fn missing_comma_and_age() {
92-
assert_eq!("John".parse::<Person>(), Err(ParsePersonError::BadLen));
87+
assert_eq!("John".parse::<Person>(), Err(BadLen));
9388
}
9489

9590
#[test]
9691
fn missing_name() {
97-
assert_eq!(",1".parse::<Person>(), Err(ParsePersonError::NoName));
92+
assert_eq!(",1".parse::<Person>(), Err(NoName));
9893
}
9994

10095
#[test]
10196
fn missing_name_and_age() {
102-
assert!(matches!(
103-
",".parse::<Person>(),
104-
Err(ParsePersonError::NoName | ParsePersonError::ParseInt(_)),
105-
));
97+
assert!(matches!(",".parse::<Person>(), Err(NoName | ParseInt(_))));
10698
}
10799

108100
#[test]
109101
fn missing_name_and_invalid_age() {
110102
assert!(matches!(
111103
",one".parse::<Person>(),
112-
Err(ParsePersonError::NoName | ParsePersonError::ParseInt(_)),
104+
Err(NoName | ParseInt(_)),
113105
));
114106
}
115107

116108
#[test]
117109
fn trailing_comma() {
118-
assert_eq!("John,32,".parse::<Person>(), Err(ParsePersonError::BadLen));
110+
assert_eq!("John,32,".parse::<Person>(), Err(BadLen));
119111
}
120112

121113
#[test]
122114
fn trailing_comma_and_some_string() {
123-
assert_eq!(
124-
"John,32,man".parse::<Person>(),
125-
Err(ParsePersonError::BadLen),
126-
);
115+
assert_eq!("John,32,man".parse::<Person>(), Err(BadLen));
127116
}
128117
}

0 commit comments

Comments
 (0)