Skip to content

Commit

Permalink
fix and test inheritance of CompundWidgets (#130)
Browse files Browse the repository at this point in the history
* fix and test inheritance of CompundWidgets

* on pypy2.7, new version of soupsieve fails.
  • Loading branch information
CastixGitHub committed Aug 28, 2020
1 parent fd74800 commit 1f704c1
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 3 deletions.
3 changes: 3 additions & 0 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,9 @@ def get_description(fname='README.rst'):
_extra_kajiki + \
_extra_chameleon

if sys.version_info[0] == 2 and sys.version_info[1] == 7:
tests_require.append('soupsieve<2.0')

if sys.version_info[0] == 2 and sys.version_info[1] <= 6:
tests_require.append('WebTest<2.0.0')
else:
Expand Down
27 changes: 26 additions & 1 deletion tests/test_hierarchy.py
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,32 @@ class A:
assert(test.children.b.value == 10)
assert(test.children.c.value == 20)


def test_cw_duplicate(self):
try:
class Parent(twc.CompoundWidget):
class Left(twc.CompoundWidget):
id = None
c = twc.Widget()

class Right(twc.CompoundWidget):
id = None
c = twc.Widget()
assert False, 'Must raise'
except twc.WidgetError as ex:
assert "Duplicate id 'c'" in str(ex)

def test_cw_override(self):
class Grandpa(twc.CompoundWidget):
class Parent(twc.CompoundWidget):
a = twc.Widget(id='a', name='override_me')

class Child(Parent):
a = twc.Widget(id='a', name='A')
b = twc.Widget(id='b', name='B')
assert len(Grandpa.children.Child.children) == 2
assert Grandpa.children.Child.children[0].name == 'A'
assert Grandpa.children.Child.children[1].name == 'B'

#--
# Repeating Widget Bunch
#--
Expand Down
10 changes: 8 additions & 2 deletions tw2/core/widgets.py
Original file line number Diff line number Diff line change
Expand Up @@ -590,8 +590,12 @@ def post_define(cls):
cls._sub_compound = not getattr(cls, 'id', None)
if not hasattr(cls, 'children'):
return
joined_cld = []

super_children_ids = []
if hasattr(super(cls, cls), 'children'):
super_children_ids = [c.id for c in super(cls, cls).children if hasattr(c, 'id')]

joined_cld = []
for c in cls.children:
if not isinstance(c, type) or not issubclass(c, Widget):
raise pm.ParameterError("All children must be widgets")
Expand All @@ -601,7 +605,9 @@ def post_define(cls):
for c in cls.children_deep():
if getattr(c, 'id', None):
if c.id in ids:
raise core.WidgetError("Duplicate id '%s'" % c.id)
if c.id not in super_children_ids:
raise core.WidgetError("Duplicate id '%s'" % c.id)
joined_cld.pop([x.id for x in joined_cld].index(c.id))
ids.add(c.id)
cls.children = WidgetBunch(joined_cld)
cls.keyed_children = [
Expand Down

0 comments on commit 1f704c1

Please sign in to comment.