| 1 | import v.reflection |
| 2 | |
| 3 | @[test_struct] |
| 4 | struct Test { |
| 5 | m map[int]string @[test] |
| 6 | n ?string @[test2; test3] |
| 7 | } |
| 8 | |
| 9 | enum Flags { |
| 10 | foo |
| 11 | bar |
| 12 | } |
| 13 | |
| 14 | type MySum = f64 | int |
| 15 | |
| 16 | type MyAlias = int |
| 17 | |
| 18 | fn foo() ?string { |
| 19 | return '' |
| 20 | } |
| 21 | |
| 22 | fn bar() !string { |
| 23 | return '' |
| 24 | } |
| 25 | |
| 26 | fn baz() (int, f64, string) { |
| 27 | return 1, 2.0, 'foo' |
| 28 | } |
| 29 | |
| 30 | fn test_flag_option() { |
| 31 | funcs := reflection.get_funcs().filter(it.name == 'foo') |
| 32 | assert funcs[0].return_typ.has_flag(.option) |
| 33 | } |
| 34 | |
| 35 | fn test_flag_result() { |
| 36 | funcs := reflection.get_funcs().filter(it.name == 'bar') |
| 37 | assert funcs[0].return_typ.has_flag(.result) |
| 38 | } |
| 39 | |
| 40 | fn test_array_sym() { |
| 41 | var := ['abc', 'def'] |
| 42 | typ := reflection.type_of(var) |
| 43 | assert typ.sym.kind == .array |
| 44 | assert typ.sym.language == .v |
| 45 | assert typ.sym.methods.len > 0 |
| 46 | assert typ.sym.methods.any(it.name == 'join') |
| 47 | assert typ.sym.name == '[]string' |
| 48 | assert (typ.sym.info as reflection.Array).nr_dims == 1 |
| 49 | assert (typ.sym.info as reflection.Array).elem_type == typeof[string]().idx |
| 50 | } |
| 51 | |
| 52 | fn test_sumtype_sym() { |
| 53 | var := MySum(1) |
| 54 | typ := reflection.type_of(var) |
| 55 | assert typ.sym.kind == .sum_type |
| 56 | assert (typ.sym.info as reflection.SumType).variants[0] == typeof[f64]().idx |
| 57 | assert (typ.sym.info as reflection.SumType).variants[1] == typeof[int]().idx |
| 58 | } |
| 59 | |
| 60 | fn test_alias_sym() { |
| 61 | var := MyAlias(1) |
| 62 | typ := reflection.type_of(var) |
| 63 | assert typ.sym.kind == .alias |
| 64 | assert typ.sym.language == .v |
| 65 | assert (typ.sym.info as reflection.Alias).parent_idx == typeof[int]().idx |
| 66 | assert typ.sym.methods.len == 0 |
| 67 | } |
| 68 | |
| 69 | fn test_multi_return_sym() { |
| 70 | func := reflection.get_funcs().filter(it.name == 'baz')[0] |
| 71 | assert func.name == 'baz' |
| 72 | assert func.args.len == 0 |
| 73 | assert func.is_variadic == false |
| 74 | assert func.return_typ.has_flag(.option) == false |
| 75 | assert func.return_typ.has_flag(.result) == false |
| 76 | assert func.return_typ.has_flag(.shared_f) == false |
| 77 | assert func.receiver_typ == 0 |
| 78 | assert func.is_pub == false |
| 79 | |
| 80 | typ := reflection.get_type(int(func.return_typ))? |
| 81 | assert typ.name == '(int, f64, string)' |
| 82 | assert typ.sym.mod == '' |
| 83 | assert typ.sym.language == .v |
| 84 | assert typ.sym.kind == .multi_return |
| 85 | } |
| 86 | |
| 87 | fn test_enum_sym() { |
| 88 | var := reflection.type_of(Flags.foo) |
| 89 | assert var.sym.name == 'Flags' |
| 90 | assert var.sym.mod == 'main' |
| 91 | assert var.sym.parent_idx == 0 |
| 92 | assert var.sym.kind == .enum |
| 93 | assert var.sym.language == .v |
| 94 | assert (var.sym.info as reflection.Enum).vals == ['foo', 'bar'] |
| 95 | } |
| 96 | |
| 97 | fn test_struct_sym() { |
| 98 | var := reflection.type_of(Test{}) |
| 99 | assert var.sym.kind == .struct |
| 100 | assert var.sym.mod == 'main' |
| 101 | assert (var.sym.info as reflection.Struct).attrs.len == 1 |
| 102 | assert (var.sym.info as reflection.Struct).attrs == [ |
| 103 | VAttribute{ |
| 104 | name: 'test_struct' |
| 105 | kind: .plain |
| 106 | }, |
| 107 | ] |
| 108 | |
| 109 | field := (var.sym.info as reflection.Struct).fields[0] |
| 110 | field_typ := field.typ |
| 111 | field_sym := reflection.get_type(field_typ.idx())? |
| 112 | assert field_sym.name == 'map[int]string' |
| 113 | assert field_sym.sym.kind == .map |
| 114 | assert (field_sym.sym.info as reflection.Map).key_type.idx() == typeof[int]().idx |
| 115 | assert (field_sym.sym.info as reflection.Map).value_type.idx() == typeof[string]().idx |
| 116 | assert field.attrs.len == 1 |
| 117 | assert field.attrs == [ |
| 118 | VAttribute{ |
| 119 | name: 'test' |
| 120 | kind: .plain |
| 121 | }, |
| 122 | ] |
| 123 | |
| 124 | field2 := (var.sym.info as reflection.Struct).fields[1] |
| 125 | field2_typ := (var.sym.info as reflection.Struct).fields[1].typ |
| 126 | assert field2_typ.has_flag(.option) |
| 127 | assert field2.name == 'n' |
| 128 | assert field2.attrs.len == 2 |
| 129 | assert field2.attrs == [ |
| 130 | VAttribute{ |
| 131 | name: 'test2' |
| 132 | kind: .plain |
| 133 | }, |
| 134 | VAttribute{ |
| 135 | name: 'test3' |
| 136 | kind: .plain |
| 137 | }, |
| 138 | ] |
| 139 | assert field2.is_pub == false |
| 140 | } |
| 141 | |