v2 / vlib / v / gen / js / jsgen_test.v
136 lines · 124 sloc · 4.29 KB · da93f26fa66bcadfc15f6033d1acd1fce2d73525
Raw
1// vtest build: tinyc
2import os
3
4const test_dir = os.join_path('vlib', 'v', 'gen', 'js', 'tests')
5const output_dir = os.join_path(os.vtmp_dir(), '_js_tests/')
6const v_options = '-b js -w'
7const node_options = ''
8
9fn testsuite_end() {
10 os.rmdir_all(output_dir) or {}
11}
12
13const there_is_node_available = is_nodejs_working()
14
15const there_is_grep_available = is_grep_working()
16
17fn test_example_compilation() {
18 vexe := os.getenv('VEXE')
19 os.chdir(os.dir(vexe)) or {}
20 os.mkdir_all(output_dir) or { panic(err) }
21 files := find_test_files()
22 for file in files {
23 path := os.join_path(test_dir, file)
24 println('Testing ${file}')
25 mut v_options_file := v_options
26 mut node_options_file := node_options
27 should_create_source_map := file.ends_with('_sourcemap.v')
28 if should_create_source_map {
29 println('activate -sourcemap creation')
30 v_options_file += ' -sourcemap' // activate souremap generation
31
32 println('add node option: --enable-source-maps') // requires node >=12.12.0
33 node_options_file += ' --enable-source-maps' // activate souremap generation
34 }
35 jsfile := os.join_path_single(output_dir, '${file}.js')
36 v_code :=
37 os.system('${os.quoted_path(vexe)} ${v_options_file} -o ${os.quoted_path(jsfile)} ${os.quoted_path(path)}')
38 if v_code != 0 {
39 assert false
40 }
41 // Compilation failed
42 assert v_code == 0
43 if !there_is_node_available {
44 println(' ... skipping running ${file}, there is no NodeJS present')
45 continue
46 }
47 js_code := os.system('node ${os.quoted_path(jsfile)}')
48 if js_code != 0 {
49 assert false
50 }
51 // Running failed
52 assert js_code == 0
53 if should_create_source_map {
54 if there_is_grep_available {
55 grep_code_sourcemap_found :=
56 os.system('grep -q -E "//#\\ssourceMappingURL=data:application/json;base64,[-A-Za-z0-9+/=]+$" ${os.quoted_path(jsfile)}')
57 assert grep_code_sourcemap_found == 0
58 println('file has a source map embedded')
59 } else {
60 println(' ... skipping testing for sourcemap ${file}, there is no grep present')
61 }
62 }
63 }
64}
65
66fn test_issue_11379_js_browser_builtin_print_call_does_not_panic() {
67 vexe := os.getenv('VEXE')
68 os.chdir(os.dir(vexe)) or {}
69 os.mkdir_all(output_dir) or { panic(err) }
70 program := os.join_path(test_dir, 'testdata', 'js_browser_builtin_print_regression.v')
71 output := os.join_path(output_dir, 'js_browser_builtin_print_regression.js')
72 res :=
73 os.execute('${os.quoted_path(vexe)} -enable-globals -b js_browser -o ${os.quoted_path(output)} ${os.quoted_path(program)}')
74 assert res.exit_code == 0, res.output
75 assert !res.output.contains('V panic:'), res.output
76 assert os.exists(output)
77}
78
79fn test_issue_20667_js_can_compile_game_of_life_example() {
80 vexe := os.getenv('VEXE')
81 os.chdir(os.dir(vexe)) or {}
82 os.mkdir_all(output_dir) or { panic(err) }
83 program := os.join_path('examples', 'game_of_life', 'life.v')
84 output := os.join_path(output_dir, 'game_of_life.js')
85 res :=
86 os.execute('${os.quoted_path(vexe)} -prod -b js -w -o ${os.quoted_path(output)} ${os.quoted_path(program)}')
87 assert res.exit_code == 0, res.output
88 assert os.exists(output)
89}
90
91fn test_issue_23554_js_browser_interpolated_at_exprs_do_not_emit_nested_templates() {
92 vexe := os.getenv('VEXE')
93 os.chdir(os.dir(vexe)) or {}
94 os.mkdir_all(output_dir) or { panic(err) }
95 program := os.join_path(output_dir, 'issue_23554.v')
96 output := os.join_path(output_dir, 'issue_23554.js')
97 source := [
98 'fn main() {',
99 "\tprintln('\${@LOCATION}')",
100 "\tprintln('\${@FILE_LINE}')",
101 "\tprintln('X \${@LOCATION}')",
102 "\tprintln('X \${@FILE_LINE}')",
103 '}',
104 ].join_lines()
105 os.write_file(program, source) or { panic(err) }
106 res :=
107 os.execute('${os.quoted_path(vexe)} -b js_browser -o ${os.quoted_path(output)} ${os.quoted_path(program)}')
108 assert res.exit_code == 0, res.output
109 js := os.read_file(output) or { panic(err) }
110 assert !js.contains('new string(`' + '$' + '{"')
111 assert os.exists(output)
112}
113
114fn find_test_files() []string {
115 files := os.ls(test_dir) or { panic(err) }
116 // The life example never exits, so tests would hang with it, skip
117 mut tests := files.filter(it.ends_with('.v')).filter(it != 'life.v')
118 tests.sort()
119 return tests
120}
121
122fn is_nodejs_working() bool {
123 node_res := os.execute('node --version')
124 if node_res.exit_code != 0 {
125 return false
126 }
127 return true
128}
129
130fn is_grep_working() bool {
131 node_res := os.execute('grep --version')
132 if node_res.exit_code != 0 {
133 return false
134 }
135 return true
136}
137