forked from pyodide/pyodide
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Experimental new test system (pyodide#1047)
- Loading branch information
Showing
8 changed files
with
165 additions
and
47 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,11 +1,10 @@ | ||
def test_jedi(selenium_standalone): | ||
selenium_standalone.load_package("jedi") | ||
result = selenium_standalone.run( | ||
""" | ||
import jedi | ||
script = jedi.Script("import json\\njson.lo", path='example.py') | ||
completions = script.complete(2, len('json.lo')) | ||
[el.name for el in completions] | ||
""" | ||
) | ||
assert result == ["load", "loads"] | ||
from pyodide_build.testing import run_in_pyodide | ||
|
||
|
||
@run_in_pyodide(standalone=True, packages=["jedi"]) | ||
def test_jedi(): | ||
import jedi | ||
|
||
script = jedi.Script("import json\njson.lo", path="example.py") | ||
completions = script.complete(2, len("json.lo")) | ||
assert [el.name for el in completions] == ["load", "loads"] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,8 @@ | ||
def test_regex(selenium, request): | ||
selenium.load_package("regex") | ||
assert selenium.run("import regex\nregex.search('o', 'foo').end()") == 2 | ||
from pyodide_build.testing import run_in_pyodide | ||
|
||
|
||
@run_in_pyodide(packages=["regex"]) | ||
def test_regex(): | ||
import regex | ||
|
||
assert regex.search("o", "foo").end() == 2 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
import pytest | ||
import inspect | ||
from typing import Optional, List, Callable | ||
|
||
|
||
def run_in_pyodide( | ||
_function: Optional[Callable] = None, | ||
standalone: bool = False, | ||
packages: List[str] = [], | ||
) -> Callable: | ||
""" | ||
This decorator can be called in two ways --- with arguments and without | ||
arguments. If it is called without arguments, then the `_function` kwarg | ||
catches the function the decorator is applied to. Otherewise, standalone | ||
and packages are the actual arguments to the decorator. | ||
See docs/testing.md for details on how to use this. | ||
Parameters | ||
---------- | ||
standalone : bool, default=False | ||
Whether to use a standalone selenium instance to run the test or not | ||
packages : List[str] | ||
List of packages to load before running the test | ||
""" | ||
|
||
def decorator(f): | ||
def inner(selenium): | ||
if len(packages) > 0: | ||
selenium.load_package(packages) | ||
lines, start_line = inspect.getsourcelines(f) | ||
# Remove first line, which is the decorator. Then pad with empty lines to fix line number. | ||
lines = ["\n"] * start_line + lines[1:] | ||
source = "".join(lines) | ||
|
||
err = None | ||
try: | ||
# When writing the function, we set the filename to the file | ||
# containing the source. This results in a more helpful | ||
# traceback | ||
selenium.run_js( | ||
"""pyodide._module.pyodide_py.eval_code({!r}, pyodide._module.globals, "last_expr", true, {!r})""".format( | ||
source, inspect.getsourcefile(f) | ||
) | ||
) | ||
# When invoking the function, use the default filename <eval> | ||
selenium.run_js( | ||
"""pyodide._module.pyodide_py.eval_code("{}()", pyodide._module.globals)""".format( | ||
f.__name__ | ||
) | ||
) | ||
except selenium.JavascriptException as e: | ||
err = e | ||
|
||
if err is not None: | ||
pytest.fail( | ||
"Error running function in pyodide\n\n" + str(err), | ||
pytrace=False, | ||
) | ||
|
||
if standalone: | ||
|
||
def wrapped_standalone(selenium_standalone): | ||
inner(selenium_standalone) | ||
|
||
return wrapped_standalone | ||
|
||
else: | ||
|
||
def wrapped(selenium): | ||
inner(selenium) | ||
|
||
return wrapped | ||
|
||
if _function is not None: | ||
return decorator(_function) | ||
else: | ||
return decorator |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,12 +1,12 @@ | ||
def test_bz2(selenium): | ||
selenium.run( | ||
""" | ||
import bz2 | ||
from pyodide_build.testing import run_in_pyodide | ||
|
||
text = "Hello test test test test this is a test test test" | ||
some_compressed_bytes = bz2.compress(text.encode('utf-8')) | ||
assert some_compressed_bytes != text | ||
decompressed_bytes = bz2.decompress(some_compressed_bytes) | ||
assert decompressed_bytes.decode('utf-8') == text | ||
""" | ||
) | ||
|
||
@run_in_pyodide | ||
def test_bz2(): | ||
import bz2 | ||
|
||
text = "Hello test test test test this is a test test test" | ||
some_compressed_bytes = bz2.compress(text.encode("utf-8")) | ||
assert some_compressed_bytes != text | ||
decompressed_bytes = bz2.decompress(some_compressed_bytes) | ||
assert decompressed_bytes.decode("utf-8") == text |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters