check for non-existant rules

This commit is contained in:
Will McGugan
2022-04-20 17:14:57 +01:00
parent 2c85bd97bd
commit 2823718011
3 changed files with 13 additions and 4 deletions

View File

@@ -54,11 +54,11 @@ def get_box_model(
else: else:
width = max(0, width - styles.margin.width) width = max(0, width - styles.margin.width)
if styles.min_width: if has_rule("min_width"):
min_width = styles.min_width.resolve_dimension(container_size, parent_size) min_width = styles.min_width.resolve_dimension(container_size, parent_size)
width = max(width, min_width) width = max(width, min_width)
if styles.max_width: if has_rule("max_width"):
max_width = styles.max_width.resolve_dimension(container_size, parent_size) max_width = styles.max_width.resolve_dimension(container_size, parent_size)
width = min(width, max_width) width = min(width, max_width)
@@ -70,11 +70,11 @@ def get_box_model(
else: else:
height = max(0, height - styles.margin.height) height = max(0, height - styles.margin.height)
if styles.min_height: if has_rule("min_height"):
min_height = styles.min_height.resolve_dimension(container_size, parent_size) min_height = styles.min_height.resolve_dimension(container_size, parent_size)
height = max(height, min_height) height = max(height, min_height)
if styles.max_height: if has_rule("max_height"):
max_height = styles.max_height.resolve_dimension(container_size, parent_size) max_height = styles.max_height.resolve_dimension(container_size, parent_size)
height = min(width, max_height) height = min(width, max_height)

View File

@@ -132,6 +132,7 @@ class RulesMap(TypedDict, total=False):
RULE_NAMES = list(RulesMap.__annotations__.keys()) RULE_NAMES = list(RulesMap.__annotations__.keys())
RULE_NAMES_SET = frozenset(RULE_NAMES)
_rule_getter = attrgetter(*RULE_NAMES) _rule_getter = attrgetter(*RULE_NAMES)
@@ -416,6 +417,7 @@ class Styles(StylesBase):
return Styles(node=self.node, _rules=self.get_rules(), important=self.important) return Styles(node=self.node, _rules=self.get_rules(), important=self.important)
def has_rule(self, rule: str) -> bool: def has_rule(self, rule: str) -> bool:
assert rule in RULE_NAMES_SET, f"no such rule {rule!r}"
return rule in self._rules return rule in self._rules
def clear_rule(self, rule: str) -> bool: def clear_rule(self, rule: str) -> bool:

View File

@@ -317,6 +317,13 @@ def test_spacing_add():
Spacing(1, 2, 3, 4) + "foo" Spacing(1, 2, 3, 4) + "foo"
def test_spacing_sub():
assert Spacing(1, 2, 3, 4) - Spacing(5, 6, 7, 8) == Spacing(-4, -4, -4, -4)
with pytest.raises(TypeError):
Spacing(1, 2, 3, 4) - "foo"
def test_split(): def test_split():
assert Region(10, 5, 22, 15).split(10, 5) == ( assert Region(10, 5, 22, 15).split(10, 5) == (
Region(10, 5, 10, 5), Region(10, 5, 10, 5),