| 1 | import os |
| 2 | |
| 3 | const struct_alias_vexe = @VEXE |
| 4 | const struct_alias_tests_dir = os.dir(@FILE) |
| 5 | const struct_alias_v3_dir = os.dir(struct_alias_tests_dir) |
| 6 | const struct_alias_vlib_dir = os.dir(struct_alias_v3_dir) |
| 7 | const struct_alias_v3_src = os.join_path(struct_alias_v3_dir, 'v3.v') |
| 8 | |
| 9 | fn struct_alias_build_v3() string { |
| 10 | v3_bin := os.join_path(os.temp_dir(), 'v3_struct_alias_test_${os.getpid()}') |
| 11 | os.rm(v3_bin) or {} |
| 12 | build := |
| 13 | os.execute('${struct_alias_vexe} -gc none -path "${struct_alias_vlib_dir}|@vlib|@vmodules" -o ${v3_bin} ${struct_alias_v3_src}') |
| 14 | assert build.exit_code == 0, build.output |
| 15 | return v3_bin |
| 16 | } |
| 17 | |
| 18 | fn struct_alias_write_project() string { |
| 19 | root := os.join_path(os.temp_dir(), 'v3_struct_alias_${os.getpid()}') |
| 20 | os.rmdir_all(root) or {} |
| 21 | gg_dir := os.join_path(root, 'gg') |
| 22 | sgl_dir := os.join_path(root, 'sgl_like') |
| 23 | os.mkdir_all(gg_dir) or { panic(err) } |
| 24 | os.mkdir_all(sgl_dir) or { panic(err) } |
| 25 | os.write_file(os.join_path(gg_dir, 'gg.v'), 'module gg |
| 26 | |
| 27 | pub struct Context { |
| 28 | pub: |
| 29 | render_text int |
| 30 | foreign_only int = 99 |
| 31 | } |
| 32 | ') or { |
| 33 | panic(err) |
| 34 | } |
| 35 | os.write_file(os.join_path(sgl_dir, 'sgl_context.h'), '#ifndef V3_STRUCT_ALIAS_SGL_CONTEXT_H |
| 36 | #define V3_STRUCT_ALIAS_SGL_CONTEXT_H |
| 37 | struct sgl_context { |
| 38 | unsigned int id; |
| 39 | }; |
| 40 | #endif |
| 41 | ') or { |
| 42 | panic(err) |
| 43 | } |
| 44 | os.write_file(os.join_path(sgl_dir, 'sgl_like.v'), 'module sgl_like |
| 45 | |
| 46 | import gg |
| 47 | |
| 48 | #include "@DIR/sgl_context.h" |
| 49 | |
| 50 | pub struct C.sgl_context { |
| 51 | pub: |
| 52 | id u32 |
| 53 | } |
| 54 | |
| 55 | pub type Context = C.sgl_context |
| 56 | |
| 57 | pub struct LocalBase { |
| 58 | pub: |
| 59 | id u32 |
| 60 | } |
| 61 | |
| 62 | pub type LocalContext = LocalBase |
| 63 | |
| 64 | pub const context = Context{ |
| 65 | id: 0x00010001 |
| 66 | } |
| 67 | pub const local_context = LocalContext{ |
| 68 | id: 7 |
| 69 | } |
| 70 | |
| 71 | pub fn context_score() int { |
| 72 | return int(context.id + local_context.id) + sizeof(gg.Context) - sizeof(gg.Context) |
| 73 | } |
| 74 | ') or { |
| 75 | panic(err) |
| 76 | } |
| 77 | os.write_file(os.join_path(root, 'main.v'), 'module main |
| 78 | |
| 79 | import sgl_like |
| 80 | |
| 81 | struct Context { |
| 82 | render_text int = 11 |
| 83 | main_only int = 12 |
| 84 | } |
| 85 | |
| 86 | fn main() { |
| 87 | println(int_str(sgl_like.context_score())) |
| 88 | } |
| 89 | ') or { |
| 90 | panic(err) |
| 91 | } |
| 92 | return root |
| 93 | } |
| 94 | |
| 95 | fn test_struct_init_uses_local_alias_authority_before_short_name_fallback() { |
| 96 | v3_bin := struct_alias_build_v3() |
| 97 | root := struct_alias_write_project() |
| 98 | bin := os.join_path(root, 'out') |
| 99 | compile := os.execute('${v3_bin} ${root} -b c -o ${bin}') |
| 100 | assert compile.exit_code == 0, compile.output |
| 101 | run := os.execute(bin) |
| 102 | assert run.exit_code == 0, run.output |
| 103 | assert run.output.trim_space() == '65544' |
| 104 | generated := os.read_file(bin + '.c') or { panic(err) } |
| 105 | assert generated.contains('const struct sgl_context sgl_like__context = (struct sgl_context){'), generated |
| 106 | assert generated.contains('.id = (u32)(0x00010001)') || generated.contains('.id = 0x00010001'), generated |
| 107 | |
| 108 | assert generated.contains('const sgl_like__LocalBase sgl_like__local_context = (sgl_like__LocalBase){'), generated |
| 109 | |
| 110 | assert !generated.contains('sgl_like__context = (main__Context){'), generated |
| 111 | assert !generated.contains('(gg__Context){'), generated |
| 112 | assert !generated.contains('.render_text = 0x00010001'), generated |
| 113 | assert !generated.contains('.render_text = (u32)(0x00010001)'), generated |
| 114 | assert !generated.contains('.main_only = 0x00010001'), generated |
| 115 | assert !generated.contains('.main_only = (u32)(0x00010001)'), generated |
| 116 | } |
| 117 | |