Skip to content

Commit 52a29aa

Browse files
committed
test: Convert main function to working tests
1 parent d0c7b06 commit 52a29aa

File tree

2 files changed

+27
-16
lines changed

2 files changed

+27
-16
lines changed

exercises/options/options2.rs

Lines changed: 26 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -3,23 +3,34 @@
33

44
// I AM NOT DONE
55

6-
fn main() {
7-
let optional_word = Some(String::from("rustlings"));
8-
// TODO: Make this an if let statement whose value is "Some" type
9-
word = optional_word {
10-
println!("The word is: {}", word);
11-
} else {
12-
println!("The optional word doesn't contain anything");
13-
}
6+
#[cfg(test)]
7+
mod tests {
8+
use super::*;
9+
10+
#[test]
11+
fn simple_option() {
12+
let target = "rustlings";
13+
let optional_target = Some(target);
1414

15-
let mut optional_integers_vec: Vec<Option<i8>> = Vec::new();
16-
for x in 1..10 {
17-
optional_integers_vec.push(Some(x));
15+
// TODO: Make this an if let statement whose value is "Some" type
16+
if let Some(word) = optional_target {
17+
assert_eq!(word, target);
18+
}
1819
}
1920

20-
// TODO: make this a while let statement - remember that vector.pop also adds another layer of Option<T>
21-
// You can stack `Option<T>`'s into while let and if let
22-
integer = optional_integers_vec.pop() {
23-
println!("current value: {}", integer);
21+
#[test]
22+
fn layered_option() {
23+
let mut range = 10;
24+
let mut optional_integers: Vec<Option<i8>> = Vec::new();
25+
for i in 0..(range + 1) {
26+
optional_integers.push(Some(i));
27+
}
28+
29+
// TODO: make this a while let statement - remember that vector.pop also adds another layer of Option<T>
30+
// You can stack `Option<T>`'s into while let and if let
31+
while let Some(Some(integer)) = optional_integers.pop() {
32+
assert_eq!(integer, range);
33+
range -= 1;
34+
}
2435
}
2536
}

info.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -545,7 +545,7 @@ is the easiest, but how do you do it safely so that it doesn't panic in your fac
545545
[[exercises]]
546546
name = "options2"
547547
path = "exercises/options/options2.rs"
548-
mode = "compile"
548+
mode = "test"
549549
hint = """
550550
check out:
551551
https://doc.rust-lang.org/rust-by-example/flow_control/if_let.html

0 commit comments

Comments
 (0)