aboutsummaryrefslogtreecommitdiff
path: root/v_windows/v/old/vlib/v/tests/option_2_test.v
blob: ebb9c7635c0e42733eb873df97347c5e5e5d4f05 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
fn f(n int) ?int {
	if n < 0 {
		return error('negative arg')
	}
	return n
}

fn test_lhs_option() {
	mut a := [0, 1, 2, 3, 4]
	a[f(-1) or { 2 }] = 7
	assert a == [0, 1, 7, 3, 4]
}

fn ret_no_opt(n int) int {
	return f(n) or { panic(err.msg) }
}

fn test_opt_return_no_opt() {
	aa := ret_no_opt(3)
	assert aa == 3
}

fn test_range_for_and_array_push() {
	mut a := []int{}
	for n in f(-3) or { -1 } .. f(3) or { 12 } {
		a << f(n) or { -5 }
	}
	assert a == [-5, 0, 1, 2]
}

fn test_channel_push() {
	ch := chan f64{cap: 2}
	ch <- 12.25
	ch.close()
	mut res := []f64{cap: 3}
	for _ in 0 .. 3 {
		res << <-ch or { -6.75 }
	}
	assert res == [12.25, -6.75, -6.75]
}

fn test_thread_wait() {
	thrs := [
		go f(3),
		go f(-7),
		go f(12),
	]
	mut res := []int{cap: 3}
	for t in thrs {
		res << t.wait() or { -13 }
	}
	assert res == [3, -13, 12]
}

fn test_nested_opt() {
	a := f(f(f(-3) or { -7 }) or { 4 }) or { 17 }
	assert a == 4
}

fn foo() ?string {
	return 'hi'
}

fn test_opt_subexp_field() ? {
	assert foo() ?.len == 2
}