Skip to content

Commit a39b85c

Browse files
committed
More use of @parameterize in test/test_browser.py. NFC
1 parent 089d69c commit a39b85c

File tree

1 file changed

+132
-115
lines changed

1 file changed

+132
-115
lines changed

test/test_browser.py

Lines changed: 132 additions & 115 deletions
Original file line numberDiff line numberDiff line change
@@ -2784,15 +2784,19 @@ def test_webgl2_sokol_arraytex(self):
27842784
def test_sdl_touch(self, opts):
27852785
self.btest_exit('test_sdl_touch.c', args=opts + ['-DAUTOMATE_SUCCESS=1', '-lSDL', '-lGL'])
27862786

2787-
def test_html5_mouse(self):
2788-
for opts in [[], ['-O2', '-g1', '--closure=1']]:
2789-
print(opts)
2790-
self.btest('test_html5_mouse.c', args=opts + ['-DAUTOMATE_SUCCESS=1'], expected='0')
2787+
@parameterized({
2788+
'': ([],),
2789+
'closure': (['-O2', '-g1', '--closure=1'],),
2790+
})
2791+
def test_html5_mouse(self, opts):
2792+
self.btest('test_html5_mouse.c', args=opts + ['-DAUTOMATE_SUCCESS=1'], expected='0')
27912793

2792-
def test_sdl_mousewheel(self):
2793-
for opts in [[], ['-O2', '-g1', '--closure=1']]:
2794-
print(opts)
2795-
self.btest_exit('test_sdl_mousewheel.c', args=opts + ['-DAUTOMATE_SUCCESS=1', '-lSDL', '-lGL'])
2794+
@parameterized({
2795+
'': ([],),
2796+
'closure': (['-O2', '-g1', '--closure=1'],),
2797+
})
2798+
def test_sdl_mousewheel(self, opts):
2799+
self.btest_exit('test_sdl_mousewheel.c', args=opts + ['-DAUTOMATE_SUCCESS=1', '-lSDL', '-lGL'])
27962800

27972801
@also_with_wasmfs
27982802
def test_wget(self):
@@ -2807,62 +2811,59 @@ def test_wget_data(self):
28072811
'': ([],),
28082812
'es6': (['-sEXPORT_ES6'],),
28092813
})
2814+
@also_with_wasm2js
28102815
def test_locate_file(self, args):
28112816
self.set_setting('EXIT_RUNTIME')
2812-
for wasm in [0, 1]:
2813-
if not wasm:
2814-
self.require_wasm2js()
2815-
self.clear()
2816-
create_file('src.cpp', r'''
2817-
#include <stdio.h>
2818-
#include <string.h>
2819-
#include <assert.h>
2820-
int main() {
2821-
FILE *f = fopen("data.txt", "r");
2822-
assert(f && "could not open file");
2823-
char buf[100];
2824-
int num = fread(buf, 1, 20, f);
2825-
assert(num == 20 && "could not read 20 bytes");
2826-
buf[20] = 0;
2827-
fclose(f);
2828-
printf("|%s|\n", buf);
2829-
assert(strcmp("load me right before", buf) == 0);
2830-
return 0;
2831-
}
2832-
''')
2833-
create_file('data.txt', 'load me right before...')
2834-
create_file('pre.js', 'Module.locateFile = (x) => "sub/" + x;')
2835-
self.run_process([FILE_PACKAGER, 'test.data', '--preload', 'data.txt'], stdout=open('data.js', 'w'))
2836-
# put pre.js first, then the file packager data, so locateFile is there for the file loading code
2837-
self.compile_btest('src.cpp', ['-O2', '-g', '--pre-js', 'pre.js', '--pre-js', 'data.js', '-o', 'page.html', '-sFORCE_FILESYSTEM', '-sWASM=' + str(wasm)] + args, reporting=Reporting.JS_ONLY)
2838-
ensure_dir('sub')
2839-
if wasm:
2840-
shutil.move('page.wasm', Path('sub/page.wasm'))
2841-
shutil.move('test.data', Path('sub/test.data'))
2842-
self.run_browser('page.html', '/report_result?exit:0')
2817+
create_file('src.cpp', r'''
2818+
#include <stdio.h>
2819+
#include <string.h>
2820+
#include <assert.h>
2821+
int main() {
2822+
FILE *f = fopen("data.txt", "r");
2823+
assert(f && "could not open file");
2824+
char buf[100];
2825+
int num = fread(buf, 1, 20, f);
2826+
assert(num == 20 && "could not read 20 bytes");
2827+
buf[20] = 0;
2828+
fclose(f);
2829+
printf("|%s|\n", buf);
2830+
assert(strcmp("load me right before", buf) == 0);
2831+
return 0;
2832+
}
2833+
''')
2834+
create_file('data.txt', 'load me right before...')
2835+
create_file('pre.js', 'Module.locateFile = (x) => "sub/" + x;')
2836+
self.run_process([FILE_PACKAGER, 'test.data', '--preload', 'data.txt'], stdout=open('data.js', 'w'))
2837+
# put pre.js first, then the file packager data, so locateFile is there for the file loading code
2838+
self.compile_btest('src.cpp', ['-O2', '-g', '--pre-js', 'pre.js', '--pre-js', 'data.js', '-o', 'page.html', '-sFORCE_FILESYSTEM'] + args, reporting=Reporting.JS_ONLY)
2839+
ensure_dir('sub')
2840+
if self.is_wasm():
2841+
shutil.move('page.wasm', Path('sub/page.wasm'))
2842+
shutil.move('test.data', Path('sub/test.data'))
2843+
self.run_browser('page.html', '/report_result?exit:0')
28432844

2844-
# alternatively, put locateFile in the HTML
2845-
print('in html')
2845+
# alternatively, put locateFile in the HTML
2846+
print('in html')
28462847

2847-
create_file('shell.html', '''
2848-
<body>
2849-
<script>
2850-
var Module = {
2851-
locateFile: function(x) { return "sub/" + x }
2852-
};
2853-
</script>
2848+
create_file('shell.html', '''
2849+
<body>
2850+
<script>
2851+
var Module = {
2852+
locateFile: function(x) { return "sub/" + x }
2853+
};
2854+
</script>
28542855
2855-
{{{ SCRIPT }}}
2856-
</body>
2857-
''')
2856+
{{{ SCRIPT }}}
2857+
</body>
2858+
''')
28582859

2859-
def in_html(expected):
2860-
self.compile_btest('src.cpp', ['-O2', '-g', '--shell-file', 'shell.html', '--pre-js', 'data.js', '-o', 'page.html', '-sSAFE_HEAP', '-sASSERTIONS', '-sFORCE_FILESYSTEM', '-sWASM=' + str(wasm)] + args, reporting=Reporting.JS_ONLY)
2861-
if wasm:
2862-
shutil.move('page.wasm', Path('sub/page.wasm'))
2863-
self.run_browser('page.html', '/report_result?exit:' + expected)
2860+
def in_html(expected):
2861+
self.compile_btest('src.cpp', ['-O2', '-g', '--shell-file', 'shell.html', '--pre-js', 'data.js', '-o', 'page.html', '-sSAFE_HEAP', '-sASSERTIONS', '-sFORCE_FILESYSTEM'] + args, reporting=Reporting.JS_ONLY)
2862+
if self.is_wasm():
2863+
shutil.move('page.wasm', Path('sub/page.wasm'))
2864+
self.run_browser('page.html', '/report_result?exit:' + expected)
28642865

2865-
in_html('0')
2866+
in_html('0')
28662867

28672868
@requires_graphics_hardware
28682869
def test_glfw3_default_hints(self):
@@ -2878,10 +2879,13 @@ def test_glfw3(self, args):
28782879
print(opts)
28792880
self.btest('test_glfw3.c', args=['-sUSE_GLFW=3', '-lglfw', '-lGL'] + args + opts, expected='1')
28802881

2882+
@parameterized({
2883+
'': (['-sUSE_GLFW=2', '-DUSE_GLFW=2'],),
2884+
'glfw3': (['-sUSE_GLFW=2', '-DUSE_GLFW=2'],),
2885+
})
28812886
@requires_graphics_hardware
2882-
def test_glfw_events(self):
2883-
self.btest('test_glfw_events.c', args=['-sUSE_GLFW=2', "-DUSE_GLFW=2", '-lglfw', '-lGL'], expected='1')
2884-
self.btest('test_glfw_events.c', args=['-sUSE_GLFW=3', "-DUSE_GLFW=3", '-lglfw', '-lGL'], expected='1')
2887+
def test_glfw_events(self, args):
2888+
self.btest('test_glfw_events.c', args=args + ['-lglfw', '-lGL'], expected='1')
28852889

28862890
@requires_graphics_hardware
28872891
def test_glfw3_hi_dpi_aware(self):
@@ -3076,12 +3080,16 @@ def test_sdl2_mouse_offsets(self):
30763080
self.run_browser('page.html', '', '/report_result?exit:0')
30773081

30783082
def test_sdl2_threads(self):
3079-
self.btest_exit('test_sdl2_threads.c', args=['-pthread', '-sUSE_SDL=2', '-sPROXY_TO_PTHREAD'])
3083+
self.btest_exit('test_sdl2_threads.c', args=['-pthread', '-sUSE_SDL=2', '-sPROXY_TO_PTHREAD'])
30803084

30813085
@requires_graphics_hardware
3082-
def test_sdl2_glshader(self):
3083-
self.reftest('test_sdl2_glshader.c', 'test_sdl_glshader.png', args=['-sUSE_SDL=2', '-O2', '--closure=1', '-g1', '-sLEGACY_GL_EMULATION'])
3084-
self.reftest('test_sdl2_glshader.c', 'test_sdl_glshader.png', args=['-sUSE_SDL=2', '-O2', '-sLEGACY_GL_EMULATION'], also_proxied=True) # XXX closure fails on proxy
3086+
@parameterized({
3087+
'': ([], True),
3088+
# fails on proxy
3089+
'closure': (['--closure=1', '-g1'], False),
3090+
})
3091+
def test_sdl2_glshader(self, args, also_proxied):
3092+
self.reftest('test_sdl2_glshader.c', 'test_sdl_glshader.png', args=['-sUSE_SDL=2', '-O2', '-sLEGACY_GL_EMULATION'], also_proxied=also_proxied)
30853093

30863094
@requires_graphics_hardware
30873095
def test_sdl2_canvas_blank(self):
@@ -3333,20 +3341,26 @@ def test_async_2(self):
33333341
create_file('pre.js', 'Error.stackTraceLimit = 80;\n')
33343342
self.btest_exit('async_2.cpp', args=['-O3', '--pre-js', 'pre.js', '-sASYNCIFY', '-sSTACK_SIZE=1MB'])
33353343

3336-
def test_async_virtual(self):
3337-
for opts in [0, 3]:
3338-
print(opts)
3339-
self.btest_exit('async_virtual.cpp', args=['-O' + str(opts), '-profiling', '-sASYNCIFY'])
3344+
@parameterized({
3345+
'': ([],),
3346+
'O3': (['-03'],),
3347+
})
3348+
def test_async_virtual(self, args):
3349+
self.btest_exit('async_virtual.cpp', args=args + ['-profiling', '-sASYNCIFY'])
33403350

3351+
@parameterized({
3352+
'': ([],),
3353+
'O3': (['-03'],),
3354+
})
33413355
def test_async_virtual_2(self):
3342-
for opts in [0, 3]:
3343-
print(opts)
3344-
self.btest_exit('async_virtual_2.cpp', args=['-O' + str(opts), '-sASSERTIONS', '-sSAFE_HEAP', '-profiling', '-sASYNCIFY'])
3356+
self.btest_exit('async_virtual_2.cpp', args=args + ['-sASSERTIONS', '-sSAFE_HEAP', '-profiling', '-sASYNCIFY'])
33453357

3358+
@parameterized({
3359+
'': ([],),
3360+
'O3': (['-03'],),
3361+
})
33463362
def test_async_mainloop(self):
3347-
for opts in [0, 3]:
3348-
print(opts)
3349-
self.btest_exit('async_mainloop.cpp', args=['-O' + str(opts), '-sASYNCIFY'])
3363+
self.btest_exit('async_mainloop.cpp', args=args + ['-sASYNCIFY'])
33503364

33513365
@requires_sound_hardware
33523366
def test_sdl_audio_beep_sleep(self):
@@ -3389,44 +3403,44 @@ def test_minimal_runtime_modularize(self):
33893403
def test_minimal_runtime_export_name(self):
33903404
self.btest_exit('browser_test_hello_world.c', args=['-sEXPORT_NAME=Foo', '-sMINIMAL_RUNTIME'])
33913405

3392-
def test_modularize(self):
3393-
for opts in [
3394-
[],
3395-
['-O1'],
3396-
['-O2', '-profiling'],
3397-
['-O2'],
3398-
['-O2', '--closure=1']
3406+
@parameterized({
3407+
'': ([],),
3408+
'O1': (['-O1'],),
3409+
'O2': (['-O2'],),
3410+
'profiling': (['-O2', '-profiling'],),
3411+
'closure': (['-O2', '--closure=1'],),
3412+
})
3413+
def test_modularize(self, opts):
3414+
for args, code in [
3415+
# defaults
3416+
([], '''
3417+
let promise = Module();
3418+
if (!promise instanceof Promise) throw new Error('Return value should be a promise');
3419+
'''),
3420+
# use EXPORT_NAME
3421+
(['-sEXPORT_NAME="HelloWorld"'], '''
3422+
if (typeof Module !== "undefined") throw "what?!"; // do not pollute the global scope, we are modularized!
3423+
HelloWorld.noInitialRun = true; // errorneous module capture will load this and cause timeout
3424+
let promise = HelloWorld();
3425+
if (!promise instanceof Promise) throw new Error('Return value should be a promise');
3426+
'''),
3427+
# pass in a Module option (which prevents main(), which we then invoke ourselves)
3428+
(['-sEXPORT_NAME="HelloWorld"'], '''
3429+
HelloWorld({ noInitialRun: true }).then(hello => {
3430+
hello._main();
3431+
});
3432+
'''),
33993433
]:
3400-
for args, code in [
3401-
# defaults
3402-
([], '''
3403-
let promise = Module();
3404-
if (!promise instanceof Promise) throw new Error('Return value should be a promise');
3405-
'''),
3406-
# use EXPORT_NAME
3407-
(['-sEXPORT_NAME="HelloWorld"'], '''
3408-
if (typeof Module !== "undefined") throw "what?!"; // do not pollute the global scope, we are modularized!
3409-
HelloWorld.noInitialRun = true; // errorneous module capture will load this and cause timeout
3410-
let promise = HelloWorld();
3411-
if (!promise instanceof Promise) throw new Error('Return value should be a promise');
3412-
'''),
3413-
# pass in a Module option (which prevents main(), which we then invoke ourselves)
3414-
(['-sEXPORT_NAME="HelloWorld"'], '''
3415-
HelloWorld({ noInitialRun: true }).then(hello => {
3416-
hello._main();
3417-
});
3418-
'''),
3419-
]:
3420-
print('test on', opts, args, code)
3421-
# this test is synchronous, so avoid async startup due to wasm features
3422-
self.compile_btest('browser_test_hello_world.c', ['-sMODULARIZE', '-sSINGLE_FILE'] + args + opts)
3423-
create_file('a.html', '''
3424-
<script src="a.out.js"></script>
3425-
<script>
3426-
%s
3427-
</script>
3428-
''' % code)
3429-
self.run_browser('a.html', '/report_result?0')
3434+
print('test on', opts, args, code)
3435+
# this test is synchronous, so avoid async startup due to wasm features
3436+
self.compile_btest('browser_test_hello_world.c', ['-sMODULARIZE', '-sSINGLE_FILE'] + args + opts)
3437+
create_file('a.html', '''
3438+
<script src="a.out.js"></script>
3439+
<script>
3440+
%s
3441+
</script>
3442+
''' % code)
3443+
self.run_browser('a.html', '/report_result?0')
34303444

34313445
def test_modularize_network_error(self):
34323446
browser_reporting_js_path = test_file('browser_reporting.js')
@@ -3507,14 +3521,17 @@ def test_modularize_and_preload_files(self):
35073521
''' % totalMemory)
35083522
self.run_browser('a.html', '/report_result?exit:0')
35093523

3510-
def test_webidl(self):
3524+
@parameterized({
3525+
'': ([],),
3526+
'O1': (['-O1'],),
3527+
'O2': (['-O2'],),
3528+
})
3529+
def test_webidl(self, args):
35113530
# see original in test_core.py
35123531
self.run_process([WEBIDL_BINDER, test_file('webidl/test.idl'), 'glue'])
35133532
self.assertExists('glue.cpp')
35143533
self.assertExists('glue.js')
3515-
for opts in [[], ['-O1'], ['-O2']]:
3516-
print(opts)
3517-
self.btest('webidl/test.cpp', '1', args=['--post-js', 'glue.js', '-I.', '-DBROWSER'] + opts)
3534+
self.btest('webidl/test.cpp', '1', args=['--post-js', 'glue.js', '-I.', '-DBROWSER'] + args)
35183535

35193536
def test_dynamic_link(self):
35203537
create_file('main.c', r'''

0 commit comments

Comments
 (0)