Skip to content

Commit

Permalink
Refactor pre/postRun tests. NFC
Browse files Browse the repository at this point in the history
  • Loading branch information
sbc100 committed Oct 2, 2024
1 parent 4c34a0d commit e3c9379
Showing 1 changed file with 29 additions and 40 deletions.
69 changes: 29 additions & 40 deletions test/test_other.py
Original file line number Diff line number Diff line change
Expand Up @@ -2719,7 +2719,12 @@ def test_get_proc_address_error_message(self):
err = self.expect_fail([EMCC, '-sGL_ENABLE_GET_PROC_ADDRESS=0', test_file('other/test_GetProcAddress_LEGACY_GL_EMULATION.c')])
self.assertContained('error: linker: Undefined symbol: SDL_GL_GetProcAddress(). Please pass -sGL_ENABLE_GET_PROC_ADDRESS at link time to link in SDL_GL_GetProcAddress().', err)

def test_prepost(self):
@parameterized({
'': (False, False),
'no_initial_run': (True, False),
'run_dep': (False, True),
})
def test_prepost(self, no_initial_run, run_dep):
create_file('main.c', '''
#include <stdio.h>
int main() {
Expand Down Expand Up @@ -2748,68 +2753,52 @@ def test_prepost(self):
self.assertNotContained('post-run\n', output)

# noInitialRun prevents run
for no_initial_run, run_dep in ((0, 0), (1, 0), (0, 1)):
print(no_initial_run, run_dep)
args = ['-sWASM_ASYNC_COMPILATION=0', '-sEXPORTED_RUNTIME_METHODS=callMain']
if no_initial_run:
args += ['-sINVOKE_RUN=0']
if run_dep:
create_file('pre.js', 'Module.preRun = () => addRunDependency("test");')
create_file('post.js', 'removeRunDependency("test");')
args += ['--pre-js', 'pre.js', '--post-js', 'post.js']

self.run_process([EMCC, 'main.c'] + args)
output = self.run_js('a.out.js')
self.assertContainedIf('hello from main', output, not no_initial_run)
args = ['-sWASM_ASYNC_COMPILATION=0', '-sEXPORTED_RUNTIME_METHODS=callMain']
if no_initial_run:
args += ['-sINVOKE_RUN=0']
if run_dep:
create_file('pre.js', 'Module.preRun = () => addRunDependency("test");')
create_file('post.js', 'removeRunDependency("test");')
args += ['--pre-js', 'pre.js', '--post-js', 'post.js']

if no_initial_run:
# Calling main later should still work, filesystem etc. must be set up.
print('call main later')
src = read_file('a.out.js')
src += '\nout("callMain -> " + Module.callMain());\n'
create_file('a.out.js', src)
self.assertContained('hello from main\ncallMain -> 0\n', self.run_js('a.out.js'))
self.run_process([EMCC, 'main.c'] + args)
output = self.run_js('a.out.js')
self.assertContainedIf('hello from main', output, not no_initial_run)

# Use postInit
if no_initial_run:
# Calling main later should still work, filesystem etc. must be set up.
print('call main later')
src = read_file('a.out.js')
src += '\nout("callMain -> " + Module.callMain());\n'
create_file('a.out.js', src)
self.assertContained('hello from main\ncallMain -> 0\n', self.run_js('a.out.js'))

def test_preinit(self):
create_file('pre.js', '''
var Module = {
preRun: () => out('pre-run'),
postRun: () => out('post-run'),
preInit: () => out('pre-init')
};
''')
self.do_runf('main.c',
'pre-init\npre-run\nhello from main\npost-run\n',
self.do_runf(test_file('hello_world.c'),
'pre-init\npre-run\nhello, world!\npost-run\n',
emcc_args=['--pre-js', 'pre.js'])

def test_prepost2(self):
create_file('main.c', '''
#include <stdio.h>
int main() {
printf("hello from main\\n");
return 0;
}
''')
create_file('pre.js', 'Module.preRun = () => out("pre-run");')
create_file('pre2.js', 'Module.postRun = () => out("post-run");')
self.do_runf('main.c', 'pre-run\nhello from main\npost-run\n',
self.do_runf(test_file('hello_world.c'), 'pre-run\nhello, world!\npost-run\n',
emcc_args=['--pre-js', 'pre.js', '--pre-js', 'pre2.js'])

def test_prepre(self):
create_file('main.c', '''
#include <stdio.h>
int main() {
printf("hello from main\\n");
return 0;
}
''')
create_file('pre.js', '''
Module.preRun = [() => out('pre-run')];
''')
create_file('pre2.js', '''
Module.preRun.push(() => out('prepre'));
''')
self.do_runf('main.c', 'prepre\npre-run\nhello from main\n',
self.do_runf(test_file('hello_world.c'), 'prepre\npre-run\nhello, world!\n',
emcc_args=['--pre-js', 'pre.js', '--pre-js', 'pre2.js'])

def test_extern_prepost(self):
Expand Down

0 comments on commit e3c9379

Please sign in to comment.