v2 / vlib / v / tests / consts / const_test.v
48 lines · 39 sloc · 871 bytes · 6488041a749df9762348d019c4223908c476f2e2
Raw
1pub const a = b
2pub const ccc = a + b
3pub const b = 1
4pub const d = (e / 2) + 7
5pub const e = 9
6
7pub const x = 10
8
9fn test_const() {
10 assert d == 11
11
12 assert b == 1
13 assert a == 1
14 assert ccc == a + b
15 assert e == 9
16 assert d == (e / 2) + 7
17}
18
19// const option test
20struct Foo {
21 name string = 'foo'
22}
23
24fn foo_decode(name string) !Foo {
25 if name == 'baz' {
26 return error('baz is not allowed')
27 }
28 return Foo{name}
29}
30
31pub const def = foo_decode('baz') or { Foo{} }
32pub const bar = foo_decode('bar')!
33
34fn test_opt_const() {
35 assert def.name == 'foo'
36 assert bar.name == 'bar'
37}
38
39// const with expressions that compile to multiple C statements
40pub const abc = [1, 2, 3].map(it * it)
41pub const ghi = [1, 2, 3, 4, 5].filter(it % 2 == 0)
42pub const jkl = [`a`, `b`, `c`].contains(`d`)
43
44fn test_multistmt_const() {
45 assert abc[2] == 9
46 assert ghi.len == 2
47 assert jkl == false
48}
49