| 1 | fn f(n int) ?int { |
| 2 | if n < 0 { |
| 3 | return none |
| 4 | } |
| 5 | return n |
| 6 | } |
| 7 | |
| 8 | fn test_lhs_option() { |
| 9 | mut a := [0, 1, 2, 3, 4] |
| 10 | a[f(-1) or { 2 }] = 7 |
| 11 | assert a == [0, 1, 7, 3, 4] |
| 12 | } |
| 13 | |
| 14 | fn ret_no_opt(n int) int { |
| 15 | return f(n) or { panic(err) } |
| 16 | } |
| 17 | |
| 18 | fn test_opt_return_no_opt() { |
| 19 | aa := ret_no_opt(3) |
| 20 | assert aa == 3 |
| 21 | } |
| 22 | |
| 23 | fn test_range_for_and_array_push() { |
| 24 | mut a := []int{} |
| 25 | for n in f(-3) or { -1 } .. f(3) or { 12 } { |
| 26 | a << f(n) or { -5 } |
| 27 | } |
| 28 | assert a == [-5, 0, 1, 2] |
| 29 | } |
| 30 | |
| 31 | fn test_channel_push() { |
| 32 | ch := chan f64{cap: 2} |
| 33 | ch <- 12.25 |
| 34 | ch.close() |
| 35 | mut res := []f64{cap: 3} |
| 36 | for _ in 0 .. 3 { |
| 37 | res << <-ch or { -6.75 } |
| 38 | } |
| 39 | assert res == [12.25, -6.75, -6.75] |
| 40 | } |
| 41 | |
| 42 | fn test_thread_wait() { |
| 43 | thrs := [ |
| 44 | spawn f(3), |
| 45 | spawn f(-7), |
| 46 | spawn f(12), |
| 47 | ] |
| 48 | mut res := []int{cap: 3} |
| 49 | for t in thrs { |
| 50 | res << t.wait() or { -13 } |
| 51 | } |
| 52 | assert res == [3, -13, 12] |
| 53 | } |
| 54 | |
| 55 | fn test_nested_opt() { |
| 56 | a := f(f(f(-3) or { -7 }) or { 4 }) or { 17 } |
| 57 | assert a == 4 |
| 58 | } |
| 59 | |
| 60 | fn foo() ?string { |
| 61 | return 'hi' |
| 62 | } |
| 63 | |
| 64 | fn test_opt_subexp_field() { |
| 65 | assert foo()?.len == 2 |
| 66 | } |
| 67 | |
| 68 | fn test_mut_opt_none_if_branch() { |
| 69 | mut x := ?int(none) |
| 70 | |
| 71 | if x != none { |
| 72 | x = 10 |
| 73 | } else { |
| 74 | x = 5 |
| 75 | } |
| 76 | assert x? == 5 |
| 77 | } |
| 78 | |