Skip to content

Commit 2b2fc48

Browse files
committed
wordy: WordProblem::answer() now returns Option
1 parent 3907ed7 commit 2b2fc48

File tree

3 files changed

+20
-20
lines changed

3 files changed

+20
-20
lines changed

exercises/wordy/example.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -32,20 +32,20 @@ impl WordProblem {
3232
}
3333
}
3434

35-
pub fn answer(&self) -> Result<isize, ()> {
35+
pub fn answer(&self) -> Option<isize> {
3636
let mut t = self.tokens();
3737
let mut result: isize = 0;
3838
let mut opr = "plus".to_string();
3939

4040
if t.len() <= 1 {
41-
Err(())
41+
None
4242
} else {
4343
while t.len() > 1 {
4444
result = self.evaluate(result, opr, self.operand(&t.remove(0)));
4545
opr = self.operator(&t.remove(0));
4646
}
4747
result = self.evaluate(result, opr, self.operand(&t.remove(0)));
48-
Ok(result)
48+
Some(result)
4949
}
5050
}
5151

exercises/wordy/src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ impl WordProblem {
88
);
99
}
1010

11-
pub fn answer(&self) -> Result<i32, String> {
11+
pub fn answer(&self) -> Option<i32> {
1212
unimplemented!(
1313
"Return the result of the transmitted command, or an error, if the command is invalid."
1414
);

exercises/wordy/tests/wordy.rs

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -5,110 +5,110 @@ use wordy::*;
55
#[test]
66
fn addition() {
77
let command = "What is 1 plus 1?";
8-
assert_eq!(Ok(2), WordProblem::new(command).answer());
8+
assert_eq!(Some(2), WordProblem::new(command).answer());
99
}
1010

1111
#[test]
1212
#[ignore]
1313
fn more_addition() {
1414
let command = "What is 53 plus 2?";
15-
assert_eq!(Ok(55), WordProblem::new(command).answer());
15+
assert_eq!(Some(55), WordProblem::new(command).answer());
1616
}
1717

1818
#[test]
1919
#[ignore]
2020
fn addition_with_negative_numbers() {
2121
let command = "What is -1 plus -10?";
22-
assert_eq!(Ok(-11), WordProblem::new(command).answer());
22+
assert_eq!(Some(-11), WordProblem::new(command).answer());
2323
}
2424

2525
#[test]
2626
#[ignore]
2727
fn large_addition() {
2828
let command = "What is 123 plus 45678?";
29-
assert_eq!(Ok(45801), WordProblem::new(command).answer());
29+
assert_eq!(Some(45801), WordProblem::new(command).answer());
3030
}
3131

3232
#[test]
3333
#[ignore]
3434
fn subtraction() {
3535
let command = "What is 4 minus -12?";
36-
assert_eq!(Ok(16), WordProblem::new(command).answer());
36+
assert_eq!(Some(16), WordProblem::new(command).answer());
3737
}
3838

3939
#[test]
4040
#[ignore]
4141
fn multiplication() {
4242
let command = "What is -3 multiplied by 25?";
43-
assert_eq!(Ok(-75), WordProblem::new(command).answer());
43+
assert_eq!(Some(-75), WordProblem::new(command).answer());
4444
}
4545

4646
#[test]
4747
#[ignore]
4848
fn division() {
4949
let command = "What is 33 divided by -3?";
50-
assert_eq!(Ok(-11), WordProblem::new(command).answer());
50+
assert_eq!(Some(-11), WordProblem::new(command).answer());
5151
}
5252

5353
#[test]
5454
#[ignore]
5555
fn multiple_additions() {
5656
let command = "What is 1 plus 1 plus 1?";
57-
assert_eq!(Ok(3), WordProblem::new(command).answer());
57+
assert_eq!(Some(3), WordProblem::new(command).answer());
5858
}
5959

6060
#[test]
6161
#[ignore]
6262
fn addition_and_subtraction() {
6363
let command = "What is 1 plus 5 minus -2?";
64-
assert_eq!(Ok(8), WordProblem::new(command).answer());
64+
assert_eq!(Some(8), WordProblem::new(command).answer());
6565
}
6666

6767
#[test]
6868
#[ignore]
6969
fn multiple_subtraction() {
7070
let command = "What is 20 minus 4 minus 13?";
71-
assert_eq!(Ok(3), WordProblem::new(command).answer());
71+
assert_eq!(Some(3), WordProblem::new(command).answer());
7272
}
7373

7474
#[test]
7575
#[ignore]
7676
fn subtraction_then_addition() {
7777
let command = "What is 17 minus 6 plus 3?";
78-
assert_eq!(Ok(14), WordProblem::new(command).answer());
78+
assert_eq!(Some(14), WordProblem::new(command).answer());
7979
}
8080

8181
#[test]
8282
#[ignore]
8383
fn multiple_multiplications() {
8484
let command = "What is 2 multiplied by -2 multiplied by 3?";
85-
assert_eq!(Ok(-12), WordProblem::new(command).answer());
85+
assert_eq!(Some(-12), WordProblem::new(command).answer());
8686
}
8787

8888
#[test]
8989
#[ignore]
9090
fn addition_and_multiplication() {
9191
let command = "What is -3 plus 7 multiplied by -2?";
92-
assert_eq!(Ok(-8), WordProblem::new(command).answer());
92+
assert_eq!(Some(-8), WordProblem::new(command).answer());
9393
}
9494

9595
#[test]
9696
#[ignore]
9797
fn multiple_divisions() {
9898
let command = "What is -12 divided by 2 divided by -3?";
99-
assert_eq!(Ok(2), WordProblem::new(command).answer());
99+
assert_eq!(Some(2), WordProblem::new(command).answer());
100100
}
101101

102102
#[test]
103103
#[ignore]
104104
fn unknown_operation() {
105105
let command = "What is 52 cubed?";
106-
assert!(WordProblem::new(command).answer().is_err());
106+
assert!(WordProblem::new(command).answer().is_none());
107107
}
108108

109109
#[test]
110110
#[ignore]
111111
fn non_math_question() {
112112
let command = "Who is the President of the United States?";
113-
assert!(WordProblem::new(command).answer().is_err());
113+
assert!(WordProblem::new(command).answer().is_none());
114114
}

0 commit comments

Comments
 (0)