v2 / vlib / v / tests / consts / const_name_equals_fn_name_test.v
63 lines · 45 sloc · 1.25 KB · 6488041a749df9762348d019c4223908c476f2e2
Raw
1import term
2
3fn abc() {
4 println('xyz')
5}
6
7fn def() string {
8 return 'xyz'
9}
10
11const a_const_that_is_fn = abc
12
13const a_const_that_is_fn_returning_value = def
14
15const a_const_same_as_fn_in_another_module = term.yellow
16
17fn test_simple_fn_assigned_to_const_can_be_called() {
18 a_const_that_is_fn()
19 assert true
20}
21
22fn test_simple_fn_assigned_to_const_can_be_called_and_returns_value() {
23 assert def == a_const_that_is_fn_returning_value
24 assert def() == 'xyz'
25 assert a_const_that_is_fn_returning_value() == 'xyz'
26 assert def() == a_const_that_is_fn_returning_value()
27 assert a_const_that_is_fn_returning_value() == def()
28}
29
30//
31
32fn test_a_const_that_is_alias_to_fn_from_module() {
33 assert a_const_same_as_fn_in_another_module == term.yellow
34 assert term.yellow('x') == a_const_same_as_fn_in_another_module('x')
35 assert ptr_str(term.yellow) == ptr_str(a_const_same_as_fn_in_another_module)
36}
37
38//
39
40const pg = fn_generator()
41
42const pg2 = fn_generator()()
43
44fn fn_generator() fn () string {
45 return fn () string {
46 println('hello')
47 return 'ok'
48 }
49}
50
51fn test_a_const_can_be_assigned_a_fn_produced_by_a_fn_generator_and_the_const_can_be_used() {
52 assert fn_generator()() == 'ok'
53
54 x := fn_generator()
55 assert x() == 'ok'
56
57 y := pg
58 assert y() == 'ok'
59
60 assert pg() == 'ok'
61
62 assert pg2 == 'ok'
63}
64