summaryrefslogtreecommitdiff
path: root/exercises
diff options
context:
space:
mode:
authormagnusrodseth <59113973+magnusrodseth@users.noreply.github.com>2022-08-17 12:50:34 +0200
committermagnusrodseth <59113973+magnusrodseth@users.noreply.github.com>2022-08-17 12:50:34 +0200
commit52a29aa84be2a89894f2ad1f5ebdcf153c49f399 (patch)
treed75521e6316b37fe43e8f59dfb96f9a47f2010a6 /exercises
parentd0c7b06eff3e8959b261145e10f1aaa57ba1e4c3 (diff)
test: Convert main function to working tests
Diffstat (limited to 'exercises')
-rw-r--r--exercises/options/options2.rs41
1 files changed, 26 insertions, 15 deletions
diff --git a/exercises/options/options2.rs b/exercises/options/options2.rs
index 75b66a3..eca03f0 100644
--- a/exercises/options/options2.rs
+++ b/exercises/options/options2.rs
@@ -3,23 +3,34 @@
// I AM NOT DONE
-fn main() {
- let optional_word = Some(String::from("rustlings"));
- // TODO: Make this an if let statement whose value is "Some" type
- word = optional_word {
- println!("The word is: {}", word);
- } else {
- println!("The optional word doesn't contain anything");
- }
+#[cfg(test)]
+mod tests {
+ use super::*;
+
+ #[test]
+ fn simple_option() {
+ let target = "rustlings";
+ let optional_target = Some(target);
- let mut optional_integers_vec: Vec<Option<i8>> = Vec::new();
- for x in 1..10 {
- optional_integers_vec.push(Some(x));
+ // TODO: Make this an if let statement whose value is "Some" type
+ if let Some(word) = optional_target {
+ assert_eq!(word, target);
+ }
}
- // TODO: make this a while let statement - remember that vector.pop also adds another layer of Option<T>
- // You can stack `Option<T>`'s into while let and if let
- integer = optional_integers_vec.pop() {
- println!("current value: {}", integer);
+ #[test]
+ fn layered_option() {
+ let mut range = 10;
+ let mut optional_integers: Vec<Option<i8>> = Vec::new();
+ for i in 0..(range + 1) {
+ optional_integers.push(Some(i));
+ }
+
+ // TODO: make this a while let statement - remember that vector.pop also adds another layer of Option<T>
+ // You can stack `Option<T>`'s into while let and if let
+ while let Some(Some(integer)) = optional_integers.pop() {
+ assert_eq!(integer, range);
+ range -= 1;
+ }
}
}