mirror of
https://github.com/pyscript/pyscript.git
synced 2025-12-20 02:37:41 -05:00
* kill the PyScript class and the weird pyscript instance; from the user point of view its functionalities are still available as pyscript.*, but pyscript is not the module, not the instance of PyScript * simplify the code in _set_version_info, while I'm at it * start to implement DeprecatedGlobal * DeprecatedGlobal.__getattr__ * don't show the same warning twice * DeprecatedGlobal.__call__ * make it possible to specify a different warning message for every global * WIP: carefully use DeprecatedGlobal to show reasonable warning messages depending on which name you are accessing to. More names to follow * deprecate more names * deprecate private names * depreacte direct usage of console and document * deprecate the PyScript class * use a better error message * fix test_pyscript.py * introduce a __repr__ for DeprecatedGlobal * add an helper to ensure that we don't show any error or warning on the page * WIP: ensure that examples don't use depreacted features. Many tests are failing * don't deprecate Element * don't use the global micropip to install packages, else we trigger a warning * use a better error message for micropip * fix test_todo_pylist to avoid using deprecated globals * fix test_webgl_raycaster * fix tests * make HTML globally available * add MIME_RENDERERS and MIME_METHODS * fix the typing of Micropip, thanks to @FabioRosado
208 lines
5.6 KiB
Python
208 lines
5.6 KiB
Python
import sys
|
|
import textwrap
|
|
from unittest.mock import Mock
|
|
|
|
import pyscript
|
|
|
|
|
|
class TestElement:
|
|
def test_id_is_correct(self):
|
|
el = pyscript.Element("something")
|
|
assert el.id == "something"
|
|
|
|
def test_element(self, monkeypatch):
|
|
el = pyscript.Element("something")
|
|
js_mock = Mock()
|
|
js_mock.document = Mock()
|
|
call_result = "some_result"
|
|
js_mock.document.querySelector = Mock(return_value=call_result)
|
|
monkeypatch.setattr(pyscript, "js", js_mock)
|
|
assert not el._element
|
|
real_element = el.element
|
|
assert real_element
|
|
assert js_mock.document.querySelector.call_count == 1
|
|
js_mock.document.querySelector.assert_called_with("#something")
|
|
assert real_element == call_result
|
|
|
|
|
|
def test_format_mime_str():
|
|
obj = "just a string"
|
|
out, mime = pyscript.format_mime(obj)
|
|
assert out == obj
|
|
assert mime == "text/plain"
|
|
|
|
|
|
def test_format_mime_str_escaping():
|
|
obj = "<p>hello</p>"
|
|
out, mime = pyscript.format_mime(obj)
|
|
assert out == "<p>hello</p>"
|
|
assert mime == "text/plain"
|
|
|
|
|
|
def test_format_mime_repr_escaping():
|
|
out, mime = pyscript.format_mime(sys)
|
|
assert out == "<module 'sys' (built-in)>"
|
|
assert mime == "text/plain"
|
|
|
|
|
|
def test_format_mime_HTML():
|
|
obj = pyscript.HTML("<p>hello</p>")
|
|
out, mime = pyscript.format_mime(obj)
|
|
assert out == "<p>hello</p>"
|
|
assert mime == "text/html"
|
|
|
|
|
|
def test_uses_top_level_await():
|
|
# Basic Case
|
|
src = "x = 1"
|
|
assert pyscript.uses_top_level_await(src) is False
|
|
|
|
# Comments are not top-level await
|
|
src = textwrap.dedent(
|
|
"""
|
|
#await async for async with asyncio
|
|
"""
|
|
)
|
|
|
|
assert pyscript.uses_top_level_await(src) is False
|
|
|
|
# Top-level-await cases
|
|
src = textwrap.dedent(
|
|
"""
|
|
async def foo():
|
|
pass
|
|
await foo
|
|
"""
|
|
)
|
|
assert pyscript.uses_top_level_await(src) is True
|
|
|
|
src = textwrap.dedent(
|
|
"""
|
|
async with object():
|
|
pass
|
|
"""
|
|
)
|
|
assert pyscript.uses_top_level_await(src) is True
|
|
|
|
src = textwrap.dedent(
|
|
"""
|
|
async for _ in range(10):
|
|
pass
|
|
"""
|
|
)
|
|
assert pyscript.uses_top_level_await(src) is True
|
|
|
|
# Acceptable await/async for/async with cases
|
|
src = textwrap.dedent(
|
|
"""
|
|
async def foo():
|
|
await foo()
|
|
"""
|
|
)
|
|
assert pyscript.uses_top_level_await(src) is False
|
|
|
|
src = textwrap.dedent(
|
|
"""
|
|
async def foo():
|
|
async with object():
|
|
pass
|
|
"""
|
|
)
|
|
assert pyscript.uses_top_level_await(src) is False
|
|
|
|
src = textwrap.dedent(
|
|
"""
|
|
async def foo():
|
|
async for _ in range(10):
|
|
pass
|
|
"""
|
|
)
|
|
assert pyscript.uses_top_level_await(src) is False
|
|
|
|
|
|
def test_set_version_info():
|
|
version_string = "1234.56.78.ABCD"
|
|
pyscript._set_version_info(version_string)
|
|
assert pyscript.__version__ == version_string
|
|
assert pyscript.version_info == (1234, 56, 78, "ABCD")
|
|
#
|
|
# for backwards compatibility, should be killed eventually
|
|
assert pyscript.PyScript.__version__ == pyscript.__version__
|
|
assert pyscript.PyScript.version_info == pyscript.version_info
|
|
|
|
|
|
class MyDeprecatedGlobal(pyscript.DeprecatedGlobal):
|
|
"""
|
|
A subclass of DeprecatedGlobal, for tests.
|
|
|
|
Instead of showing warnings into the DOM (which we don't have inside unit
|
|
tests), we record the warnings into a field.
|
|
"""
|
|
|
|
def __init__(self, *args, **kwargs):
|
|
super().__init__(*args, **kwargs)
|
|
self.warnings = []
|
|
|
|
def _show_warning(self, message):
|
|
self.warnings.append(message)
|
|
|
|
|
|
class TestDeprecatedGlobal:
|
|
def test_repr(self):
|
|
glob = MyDeprecatedGlobal("foo", None, "my message")
|
|
assert repr(glob) == "<DeprecatedGlobal('foo')>"
|
|
|
|
def test_show_warning_override(self):
|
|
"""
|
|
Test that our overriding of _show_warning actually works.
|
|
"""
|
|
glob = MyDeprecatedGlobal("foo", None, "my message")
|
|
glob._show_warning("foo")
|
|
glob._show_warning("bar")
|
|
assert glob.warnings == ["foo", "bar"]
|
|
|
|
def test_getattr(self):
|
|
class MyFakeObject:
|
|
name = "FooBar"
|
|
|
|
glob = MyDeprecatedGlobal("MyFakeObject", MyFakeObject, "this is my warning")
|
|
assert glob.name == "FooBar"
|
|
assert glob.warnings == ["this is my warning"]
|
|
|
|
def test_dont_show_warning_twice(self):
|
|
class MyFakeObject:
|
|
name = "foo"
|
|
surname = "bar"
|
|
|
|
glob = MyDeprecatedGlobal("MyFakeObject", MyFakeObject, "this is my warning")
|
|
assert glob.name == "foo"
|
|
assert glob.surname == "bar"
|
|
assert len(glob.warnings) == 1
|
|
|
|
def test_call(self):
|
|
def foo(x, y):
|
|
return x + y
|
|
|
|
glob = MyDeprecatedGlobal("foo", foo, "this is my warning")
|
|
assert glob(1, y=2) == 3
|
|
assert glob.warnings == ["this is my warning"]
|
|
|
|
def test_iter(self):
|
|
d = {"a": 1, "b": 2, "c": 3}
|
|
glob = MyDeprecatedGlobal("d", d, "this is my warning")
|
|
assert list(glob) == ["a", "b", "c"]
|
|
assert glob.warnings == ["this is my warning"]
|
|
|
|
def test_getitem(self):
|
|
d = {"a": 1, "b": 2, "c": 3}
|
|
glob = MyDeprecatedGlobal("d", d, "this is my warning")
|
|
assert glob["a"] == 1
|
|
assert glob.warnings == ["this is my warning"]
|
|
|
|
def test_setitem(self):
|
|
d = {"a": 1, "b": 2, "c": 3}
|
|
glob = MyDeprecatedGlobal("d", d, "this is my warning")
|
|
glob["a"] = 100
|
|
assert glob.warnings == ["this is my warning"]
|
|
assert glob["a"] == 100
|