| 1 | import v.reflection |
| 2 | |
| 3 | type MyInt = int |
| 4 | |
| 5 | type MySumType = f64 | int |
| 6 | |
| 7 | enum TestEnum { |
| 8 | foo |
| 9 | bar |
| 10 | } |
| 11 | |
| 12 | struct User { |
| 13 | name string |
| 14 | } |
| 15 | |
| 16 | fn (u User) get_name() string { |
| 17 | return u.name |
| 18 | } |
| 19 | |
| 20 | fn test2(arg []string) {} |
| 21 | |
| 22 | @[noreturn] |
| 23 | fn test3(a reflection.Function) { |
| 24 | panic('foo') |
| 25 | } |
| 26 | |
| 27 | fn test_module_existing() { |
| 28 | assert 'v.reflection' in reflection.get_modules().map(it.name) |
| 29 | } |
| 30 | |
| 31 | fn test_func_attribute() { |
| 32 | func := reflection.get_funcs().filter(it.name == 'test3')[0] |
| 33 | assert func.is_variadic == false |
| 34 | assert func.attrs == [ |
| 35 | VAttribute{ |
| 36 | name: 'noreturn' |
| 37 | kind: .plain |
| 38 | }, |
| 39 | ] |
| 40 | assert reflection.get_funcs().filter(it.name == 'test2')[0].attrs.len == 0 |
| 41 | } |
| 42 | |
| 43 | fn test_func_name() { |
| 44 | assert reflection.get_funcs().filter(it.name == 'test2')[0].name == 'test2' |
| 45 | } |
| 46 | |
| 47 | fn test_type_name() { |
| 48 | ret_typ := reflection.get_funcs().filter(it.name == 'test3')[0].return_typ |
| 49 | assert reflection.type_name(int(ret_typ)) == 'void' |
| 50 | assert reflection.get_type(int(ret_typ))?.name == 'void' |
| 51 | assert reflection.get_type_symbol(int(ret_typ))?.name == 'void' |
| 52 | assert reflection.type_name(int(reflection.get_funcs().filter(it.name == 'test3')[0].args[0].typ)) == 'Function' |
| 53 | } |
| 54 | |
| 55 | fn test_type_symbol() { |
| 56 | ret_typ := reflection.get_funcs().filter(it.name == 'test3')[0].return_typ |
| 57 | assert reflection.get_type_symbol(int(ret_typ))?.language == .v |
| 58 | } |
| 59 | |
| 60 | fn test_method() { |
| 61 | method := reflection.get_funcs().filter(it.name == 'get_name')[0] |
| 62 | assert reflection.type_name(int(method.return_typ)) == 'string' |
| 63 | println(reflection.get_type(int(method.receiver_typ))?.name) |
| 64 | assert reflection.get_type(int(method.receiver_typ))?.name == 'User' |
| 65 | } |
| 66 | |
| 67 | fn test_enum() { |
| 68 | assert reflection.get_enums().filter(it.name == 'TestEnum')[0].name == 'TestEnum' |
| 69 | } |
| 70 | |
| 71 | fn test_get_aliases() { |
| 72 | assert reflection.get_aliases().filter(it.name == 'MyInt')[0].name == 'MyInt' |
| 73 | } |
| 74 | |
| 75 | fn test_get_sum_types() { |
| 76 | assert reflection.get_sum_types().filter(it.name == 'MySumType')[0].name == 'MySumType' |
| 77 | } |
| 78 | |
| 79 | fn test_get_interfaces() { |
| 80 | assert reflection.get_interfaces().filter(it.name == 'IError')[0].name == 'IError' |
| 81 | } |
| 82 | |
| 83 | fn test_interfaces() { |
| 84 | assert reflection.get_interfaces().filter(it.name == 'IError')[0].methods.len == 2 |
| 85 | } |
| 86 | |
| 87 | fn test_enum_fields() { |
| 88 | assert (reflection.get_enums().filter(it.name == 'TestEnum')[0].sym.info as reflection.Enum).vals == [ |
| 89 | 'foo', |
| 90 | 'bar', |
| 91 | ] |
| 92 | } |
| 93 | |
| 94 | fn test_get_string_by_idx() { |
| 95 | file_idx := reflection.get_funcs().filter(it.name == 'all_after_last')[0].file_idx |
| 96 | assert reflection.get_string_by_idx(file_idx).ends_with('string.v') |
| 97 | } |
| 98 | |
| 99 | fn test_ref() { |
| 100 | cstr := c'abc' // &u8 |
| 101 | assert reflection.type_of(cstr).str().contains("name: 'u8'") |
| 102 | ptr_user := &User{} |
| 103 | assert reflection.type_of(ptr_user).str().contains("name: 'User'") |
| 104 | } |
| 105 | |