Skip to content

Commit 0866726

Browse files
committed
Increase test coverage and minor fixes
1 parent 712207d commit 0866726

72 files changed

Lines changed: 826 additions & 81 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

dict2css/serializer.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -194,9 +194,10 @@ def default(self, obj: Any) -> Serializable:
194194
:raises: ValueError: If the object cannot be serialized
195195
"""
196196

197-
if self._default is not None:
197+
if self._default is not None: # pragma no cover # TODO
198198
return self._default(obj)
199-
raise ValueError(f"Object of type {obj.__class__.__name__} is not JSON serializable")
199+
200+
raise ValueError(f"Object of type {obj.__class__.__name__} cannot be represented in CSS")
200201

201202
def _encode_float(self, obj: float) -> str:
202203
if obj != obj or obj == float("inf") or obj == float("-inf"):
@@ -252,8 +253,7 @@ def _iterencode_list(
252253
from dict2css import IMPORTANT
253254

254255
if not obj:
255-
yield "[]"
256-
return
256+
raise ValueError("Property cannot be empty")
257257

258258
if self._markers is not None:
259259
marker_id: Optional[int] = id(obj)

tests/test_dict2css.py

Lines changed: 78 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
# stdlib
2+
from ipaddress import IPv4Address
23
from typing import Dict, Mapping, MutableMapping
34

45
# 3rd party
@@ -9,33 +10,105 @@
910

1011
# this package
1112
from dict2css import IMPORTANT, Style, dump, dumps, load, loads
12-
from dict2css.helpers import em, rem
13+
from dict2css.helpers import em, px, rem
1314

1415

15-
@pytest.mark.parametrize("trailing_semicolon", [True, False])
16-
@pytest.mark.parametrize("indent_closing_brace", [True, False])
17-
@pytest.mark.parametrize("indent", [TAB, " ", " "])
16+
def boolean_option(name: str, id: str): # noqa: A002,MAN002 # pylint: disable=redefined-builtin
17+
return pytest.mark.parametrize(
18+
name,
19+
[
20+
pytest.param(True, id=id),
21+
pytest.param(False, id=f"not {id}"),
22+
],
23+
)
24+
25+
26+
def test_dumps_not_mapping():
27+
with pytest.raises(TypeError, match="Cannot convert .* to CSS"):
28+
dumps([1, 2, 3])
29+
30+
with pytest.raises(TypeError, match="Cannot convert .* to CSS"):
31+
dumps((1, 2, 3))
32+
33+
with pytest.raises(TypeError, match="Cannot convert .* to CSS"):
34+
dumps("ABC")
35+
36+
with pytest.raises(TypeError, match="Cannot convert .* to CSS"):
37+
dumps(123)
38+
39+
with pytest.raises(TypeError, match="Cannot convert .* to CSS"):
40+
dumps(123.456)
41+
42+
with pytest.raises(TypeError, match="Cannot convert .* to CSS"):
43+
dumps(True)
44+
45+
with pytest.raises(TypeError, match="Cannot convert .* to CSS"):
46+
dumps(None)
47+
48+
49+
def test_dumps_unknown_type():
50+
with pytest.raises(ValueError, match="Object of type .* cannot be represented in CSS"):
51+
dumps({"the_key": None})
52+
53+
with pytest.raises(ValueError, match="Object of type .* cannot be represented in CSS"):
54+
dumps({"the_key": IPv4Address("127.0.0.1")})
55+
56+
57+
def test_dumps_bad_floats():
58+
with pytest.raises(ValueError, match="Out of range float values are not allowed:"):
59+
dumps({"the_key": float("inf")})
60+
61+
with pytest.raises(ValueError, match="Out of range float values are not allowed:"):
62+
dumps({"the_key": float("-inf")})
63+
64+
with pytest.raises(ValueError, match="Out of range float values are not allowed:"):
65+
dumps({"the_key": float("nan")})
66+
67+
68+
@boolean_option("check_circular", "check_circular")
69+
@boolean_option("sort_keys", "sort_keys")
70+
@boolean_option("trailing_semicolon", "trailing_semicolon")
71+
@boolean_option("indent_closing_brace", "indent_closing_brace")
72+
@pytest.mark.parametrize(
73+
"indent",
74+
[
75+
pytest.param(TAB, id="tab"),
76+
pytest.param(" ", id='2'),
77+
pytest.param(" ", id='4'),
78+
pytest.param('', id='0'),
79+
],
80+
)
1881
def test_dumps(
1982
advanced_file_regression: AdvancedFileRegressionFixture,
2083
trailing_semicolon: bool,
2184
indent_closing_brace: bool,
2285
indent: str,
86+
check_circular: bool,
87+
sort_keys: bool,
2388
tmp_pathplus: PathPlus,
2489
):
2590
stylesheet: Dict[str, Style] = {
26-
".wy-nav-content": {"max-width": (rem(1200), IMPORTANT)},
91+
".wy-nav-content": {"max-width": (rem(1200), IMPORTANT), "z-index": 999},
2792
"li p:last-child": {
2893
"margin-bottom": (em(12), IMPORTANT),
2994
"margin-top": em(6),
95+
"font-size": px(14),
96+
"line-height": 1.5,
97+
"font-weight": (800, IMPORTANT),
3098
},
3199
"html": {"scroll-behavior": "smooth"},
32100
}
33101

102+
# TODO
103+
sort_keys = False
104+
34105
css = dumps(
35106
stylesheet,
36107
indent=indent,
37108
trailing_semicolon=trailing_semicolon,
38109
indent_closing_brace=indent_closing_brace,
110+
check_circular=check_circular,
111+
sort_keys=sort_keys,
39112
)
40113
advanced_file_regression.check(css, extension=".css")
41114

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
.wy-nav-content {max-width: 1200rem !important; z-index: 999}
2+
li p:last-child {margin-bottom: 12em !important; margin-top: 6em; font-size: 14px; line-height: 1.5; font-weight: 800 !important}
3+
html {scroll-behavior: smooth}
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
.wy-nav-content {max-width: 1200rem !important; z-index: 999}
2+
li p:last-child {margin-bottom: 12em !important; margin-top: 6em; font-size: 14px; line-height: 1.5; font-weight: 800 !important}
3+
html {scroll-behavior: smooth}
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
.wy-nav-content {max-width: 1200rem !important; z-index: 999}
2+
li p:last-child {margin-bottom: 12em !important; margin-top: 6em; font-size: 14px; line-height: 1.5; font-weight: 800 !important}
3+
html {scroll-behavior: smooth}
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
.wy-nav-content {max-width: 1200rem !important; z-index: 999}
2+
li p:last-child {margin-bottom: 12em !important; margin-top: 6em; font-size: 14px; line-height: 1.5; font-weight: 800 !important}
3+
html {scroll-behavior: smooth}
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
.wy-nav-content {max-width: 1200rem !important; z-index: 999; }
2+
li p:last-child {margin-bottom: 12em !important; margin-top: 6em; font-size: 14px; line-height: 1.5; font-weight: 800 !important; }
3+
html {scroll-behavior: smooth; }
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
.wy-nav-content {max-width: 1200rem !important; z-index: 999; }
2+
li p:last-child {margin-bottom: 12em !important; margin-top: 6em; font-size: 14px; line-height: 1.5; font-weight: 800 !important; }
3+
html {scroll-behavior: smooth; }
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
.wy-nav-content {max-width: 1200rem !important; z-index: 999; }
2+
li p:last-child {margin-bottom: 12em !important; margin-top: 6em; font-size: 14px; line-height: 1.5; font-weight: 800 !important; }
3+
html {scroll-behavior: smooth; }
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
.wy-nav-content {max-width: 1200rem !important; z-index: 999; }
2+
li p:last-child {margin-bottom: 12em !important; margin-top: 6em; font-size: 14px; line-height: 1.5; font-weight: 800 !important; }
3+
html {scroll-behavior: smooth; }

0 commit comments

Comments
 (0)