forked from reactive-python/reactpy
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest_utils.py
134 lines (104 loc) · 3.18 KB
/
test_utils.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
import pytest
import idom
from idom.utils import html_to_vdom
def test_basic_ref_behavior():
r = idom.Ref(1)
assert r.current == 1
r.current = 2
assert r.current == 2
assert r.set_current(3) == 2
assert r.current == 3
r = idom.Ref()
with pytest.raises(AttributeError):
r.current
r.current = 4
assert r.current == 4
def test_ref_equivalence():
assert idom.Ref([1, 2, 3]) == idom.Ref([1, 2, 3])
assert idom.Ref([1, 2, 3]) != idom.Ref([1, 2])
assert idom.Ref([1, 2, 3]) != [1, 2, 3]
assert idom.Ref() != idom.Ref()
assert idom.Ref() != idom.Ref(1)
def test_ref_repr():
assert repr(idom.Ref([1, 2, 3])) == "Ref([1, 2, 3])"
assert repr(idom.Ref()) == "Ref(<undefined>)"
@pytest.mark.parametrize(
"case",
[
{"source": "<div/>", "model": {"tagName": "div"}},
{
"source": "<div style='background-color:blue'/>",
"model": {
"tagName": "div",
"attributes": {"style": {"backgroundColor": "blue"}},
},
},
{
"source": "<div>Hello!</div>",
"model": {"tagName": "div", "children": ["Hello!"]},
},
{
"source": "<div>Hello!<p>World!</p></div>",
"model": {
"tagName": "div",
"children": ["Hello!", {"tagName": "p", "children": ["World!"]}],
},
},
],
)
def test_html_to_vdom(case):
assert html_to_vdom(case["source"]) == case["model"]
def test_html_to_vdom_transform():
source = "<p>hello <a>world</a> and <a>universe</a>lmao</p>"
def make_links_blue(node):
if node["tagName"] == "a":
node["attributes"] = {"style": {"color": "blue"}}
return node
expected = {
"tagName": "p",
"children": [
"hello ",
{
"tagName": "a",
"children": ["world"],
"attributes": {"style": {"color": "blue"}},
},
" and ",
{
"tagName": "a",
"children": ["universe"],
"attributes": {"style": {"color": "blue"}},
},
"lmao",
],
}
assert html_to_vdom(source, make_links_blue) == expected
def test_html_to_vdom_with_null_tag():
source = "<p>hello<br>world</p>"
expected = {
"tagName": "p",
"children": [
"hello",
{"tagName": "br"},
"world",
],
}
assert html_to_vdom(source) == expected
def test_html_to_vdom_with_style_attr():
source = '<p style="color: red; background-color : green; ">Hello World.</p>'
expected = {
"attributes": {"style": {"backgroundColor": "green", "color": "red"}},
"children": ["Hello World."],
"tagName": "p",
}
assert html_to_vdom(source) == expected
def test_html_to_vdom_with_no_parent_node():
source = "<p>Hello</p><div>World</div>"
expected = {
"tagName": "",
"children": [
{"tagName": "p", "children": ["Hello"]},
{"tagName": "div", "children": ["World"]},
],
}
assert html_to_vdom(source) == expected