pyodide/test/test_python.py

90 lines
3.2 KiB
Python
Raw Normal View History

2018-04-09 14:39:52 +00:00
import pathlib
2018-03-30 14:51:13 +00:00
import time
2018-04-09 14:39:52 +00:00
import pytest
2018-03-30 14:51:13 +00:00
def test_init(selenium):
assert 'Python initialization complete' in selenium.logs
assert len(selenium.driver.window_handles) == 1
def test_webbrowser(selenium):
selenium.run("import antigravity")
time.sleep(2)
assert len(selenium.driver.window_handles) == 2
def test_print(selenium):
selenium.run("print('This should be logged')")
assert 'This should be logged' in selenium.logs
2018-04-09 14:39:52 +00:00
2018-04-26 15:43:48 +00:00
def test_import_js(selenium):
result = selenium.run(
"from js import window\nwindow.title = 'Foo'\nwindow.title")
assert result == 'Foo'
def test_py_proxy(selenium):
selenium.run(
"class Foo:\n bar = 42\n def get_value(self):\n return 64\nf = Foo()\n")
assert selenium.run_js("return pyodide.pyimport('f').get_value()") == 64
assert selenium.run_js("return pyodide.pyimport('f').bar") == 42
assert selenium.run_js("return ('bar' in pyodide.pyimport('f'))") == True
selenium.run_js("f = pyodide.pyimport('f'); f.baz = 32")
assert selenium.run("f.baz") == 32
assert set(selenium.run_js(
"return Object.getOwnPropertyNames(pyodide.pyimport('f'))")) == set(
['$$', '__class__', '__delattr__', '__dict__', '__dir__',
'__doc__', '__eq__', '__format__', '__ge__', '__getattribute__',
'__gt__', '__hash__', '__init__', '__init_subclass__', '__le__',
'__lt__', '__module__', '__ne__', '__new__', '__reduce__',
'__reduce_ex__', '__repr__', '__setattr__', '__sizeof__',
'__str__', '__subclasshook__', '__weakref__', 'bar', 'baz',
'get_value'])
assert selenium.run("hasattr(f, 'baz')") == True
selenium.run_js("delete pyodide.pyimport('f').baz")
assert selenium.run("hasattr(f, 'baz')") == False
2018-04-09 14:39:52 +00:00
def test_run_core_python_test(python_test, selenium):
selenium.run(
"import sys\n"
"exitcode = -1\n"
2018-04-09 19:52:47 +00:00
"def exit(n=0):\n"
2018-04-09 14:39:52 +00:00
" global exitcode\n"
" exitcode = n\n"
" raise SystemExit()\n\n"
"sys.exit = exit\n")
# Undo the lazy modules setup -- it interferes with the CPython test
# harness
selenium.run(
"for k in list(sys.modules):\n"
" if k.startswith('numpy'):\n"
" del sys.modules[k]\n")
2018-04-09 14:39:52 +00:00
selenium.run(
"from test.libregrtest import main\n"
"main(['{}'], verbose=True, verbose3=True)".format(python_test))
exitcode = selenium.run("exitcode")
assert exitcode == 0
def pytest_generate_tests(metafunc):
if 'python_test' in metafunc.fixturenames:
test_modules = []
with open(
2018-04-20 13:53:09 +00:00
str(pathlib.Path(__file__).parents[0] / "python_tests.txt")) as fp:
2018-04-09 14:39:52 +00:00
for line in fp:
line = line.strip()
if line.startswith('#'):
continue
parts = line.split()
2018-04-09 14:39:52 +00:00
if len(parts) == 1:
test_modules.append(parts[0])
2018-04-19 18:01:05 +00:00
# XXX: The tests take too long to run, so we're just doing a
# sanity check on the first 25
if len(test_modules) > 25:
break
2018-04-09 14:39:52 +00:00
metafunc.parametrize("python_test", test_modules)