Skip to content

Commit b792f72

Browse files
committed
Add type tests
1 parent c76381d commit b792f72

File tree

2 files changed

+150
-0
lines changed

2 files changed

+150
-0
lines changed

requirements.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,4 @@
11
pytest
22
widlparser
3+
pyodide-py
4+
pytest-mypy-testing

tests/test_types.py

Lines changed: 148 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,148 @@
1+
import pytest
2+
3+
from io import StringIO
4+
from collections.abc import Callable
5+
6+
7+
@pytest.mark.mypy_testing
8+
def type_exception():
9+
from js import DOMException
10+
11+
raise DOMException.new("oops")
12+
13+
14+
@pytest.mark.mypy_testing
15+
def type_xmlhttprequest(url: str) -> StringIO:
16+
from js import XMLHttpRequest
17+
18+
req = XMLHttpRequest.new()
19+
req.open("GET", url, False)
20+
reveal_type(req.open)
21+
req.send()
22+
# this were a bit more specific
23+
reveal_type(req.response) # R: Any
24+
return StringIO(req.response)
25+
26+
27+
@pytest.mark.mypy_testing
28+
def type_fetch() -> None:
29+
from js import fetch
30+
31+
32+
@pytest.mark.mypy_testing
33+
def type_eval():
34+
from js import eval
35+
36+
reveal_type(eval("abc")) # R: Any
37+
38+
39+
@pytest.mark.mypy_testing
40+
def type_timeout(callback: Callable[[], None], timeout: int):
41+
from js import setTimeout, clearTimeout, setInterval, clearInterval
42+
from pyodide.ffi import JsProxy
43+
44+
timeout_retval: int | JsProxy = setTimeout(callable, timeout)
45+
clearTimeout(timeout_retval)
46+
47+
setInterval(callable, timeout)
48+
clearInterval(timeout_retval)
49+
50+
51+
@pytest.mark.mypy_testing
52+
def type_object():
53+
from js import Object
54+
55+
a: JsProxy = Object.fromEntries([[1, 2]])
56+
57+
58+
@pytest.mark.mypy_testing
59+
def type_buffer(selenium):
60+
from js import Uint8Array, ArrayBuffer
61+
62+
a = Uint8Array.new(range(10))
63+
assert ArrayBuffer.isView(a)
64+
65+
from tempfile import TemporaryFile
66+
67+
with TemporaryFile() as f:
68+
a.to_file(f)
69+
f.seek(0)
70+
assert f.read() == a.to_bytes()
71+
72+
import js
73+
74+
assert js.Float64Array.BYTES_PER_ELEMENT == 8
75+
76+
77+
@pytest.mark.mypy_testing
78+
def type_json():
79+
from js import JSON, Array
80+
from pyodide.ffi import to_js
81+
import json
82+
83+
class Pair:
84+
__slots__ = ("first", "second")
85+
86+
def __init__(self, first, second):
87+
self.first = first
88+
self.second = second
89+
90+
p1 = Pair(1, 2)
91+
p2 = Pair(1, 2)
92+
p2.first = p2
93+
94+
def default_converter(value, convert, cacheConversion):
95+
result = Array.new()
96+
cacheConversion(value, result)
97+
result.push(convert(value.first))
98+
result.push(convert(value.second))
99+
return result
100+
101+
p1js = to_js(p1, default_converter=default_converter)
102+
p2js = to_js(p2, default_converter=default_converter)
103+
104+
assert json.loads(JSON.stringify(p1js)) == [1, 2]
105+
106+
107+
@pytest.mark.mypy_testing
108+
def type_document():
109+
from js import document
110+
111+
el = document.createElement("div")
112+
assert el.tagName == "DIV"
113+
assert bool(el)
114+
assert document.body
115+
assert not document.body.children
116+
document.body.appendChild(el)
117+
assert document.body.children
118+
assert len(document.body.children) == 1
119+
assert document.body.children[0] == el
120+
assert repr(document) == "[object HTMLDocument]"
121+
assert len(dir(el)) >= 200
122+
assert "appendChild" in dir(el)
123+
124+
125+
@pytest.mark.mypy_testing
126+
def type_canvas():
127+
from js import document
128+
129+
canvas = document.createElement("canvas")
130+
canvas.id = "canvas"
131+
canvas.width = 320
132+
canvas.height = 240
133+
134+
canvas.style.position = "fixed"
135+
canvas.style.bottom = "10px"
136+
canvas.style.right = "10px"
137+
138+
gl = canvas.getContext(
139+
"webgl2",
140+
powerPreference="high-performance",
141+
premultipliedAlpha=False,
142+
antialias=False,
143+
alpha=False,
144+
depth=False,
145+
stencil=False,
146+
)
147+
assert document.body
148+
document.body.appendChild(canvas)

0 commit comments

Comments
 (0)