| 1 | import term |
| 2 | |
| 3 | fn abc() { |
| 4 | println('xyz') |
| 5 | } |
| 6 | |
| 7 | fn def() string { |
| 8 | return 'xyz' |
| 9 | } |
| 10 | |
| 11 | const a_const_that_is_fn = abc |
| 12 | |
| 13 | const a_const_that_is_fn_returning_value = def |
| 14 | |
| 15 | const a_const_same_as_fn_in_another_module = term.yellow |
| 16 | |
| 17 | fn test_simple_fn_assigned_to_const_can_be_called() { |
| 18 | a_const_that_is_fn() |
| 19 | assert true |
| 20 | } |
| 21 | |
| 22 | fn 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 | |
| 32 | fn 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 | |
| 40 | const pg = fn_generator() |
| 41 | |
| 42 | const pg2 = fn_generator()() |
| 43 | |
| 44 | fn fn_generator() fn () string { |
| 45 | return fn () string { |
| 46 | println('hello') |
| 47 | return 'ok' |
| 48 | } |
| 49 | } |
| 50 | |
| 51 | fn 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 | |