v2 / vlib / v / tests / casts / cast_to_empty_interface_test.v
45 lines · 36 sloc · 1.16 KB · cf4efb7ebe8b465801ace27d0f5719410b02f6cb
Raw
1interface Any {}
2
3fn thing(any Any) string {
4 return match any {
5 int { 'int${any:17}' }
6 f64 { 'f64${any:20}' }
7 else { 'literal type tag?${any:10}' }
8 }
9}
10
11fn test_cast_to_empty_interface() {
12 mut ret_strings := []string{}
13 mut arr := [Any(int(11)), int(22), Any(8888), 9999, Any(f64(1.11)), f64(2.22), Any(8.88), 9.99]
14
15 for i in arr {
16 println(thing(i))
17 ret_strings << thing(i)
18 }
19
20 assert ret_strings.len == 8
21 assert ret_strings[0] == 'int 11'
22 assert ret_strings[1] == 'int 22'
23 assert ret_strings[2] == 'int 8888'
24 assert ret_strings[3] == 'int 9999'
25 assert ret_strings[4] == 'f64 1.11'
26 assert ret_strings[5] == 'f64 2.22'
27 assert ret_strings[6] == 'f64 8.88'
28 assert ret_strings[7] == 'f64 9.99'
29}
30
31type EmptyInterfaceCallback = fn ()
32
33fn empty_interface_callback() {}
34
35fn test_append_function_to_empty_interface_array() {
36 mut callbacks := []EmptyInterfaceCallback{}
37 callbacks << empty_interface_callback
38
39 mut values := []Any{}
40 values << callbacks
41 values << empty_interface_callback
42
43 assert values.len == 2
44 assert '${values}'.contains('Any(fn ())')
45}
46