Skip to content

Commit

Permalink
simplify add method, complex adds can use [] (#194)
Browse files Browse the repository at this point in the history
* simplify add method, complex adds can use []

* cleanup test layout
  • Loading branch information
jwkvam committed Feb 3, 2018
1 parent 4f04e73 commit 3af7cad
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 53 deletions.
42 changes: 23 additions & 19 deletions bowtie/_app.py
Original file line number Diff line number Diff line change
Expand Up @@ -323,24 +323,37 @@ def __setitem__(self, key, widget):
raise GridIndexError(
'Cannot index with {}, pass in a int or a slice.'.format(column_key)
)
self.add(
self._add(
widget, row_start=row_start, column_start=column_start,
row_end=row_end, column_end=column_end
)

elif isinstance(key, slice):
start, end = _slice_to_start_end(key, len(self.rows))
self.add(
self._add(
widget, row_start=start, column_start=0,
row_end=end, column_end=len(self.columns)
)
elif isinstance(key, int):
self.add(widget, row_start=key, column_start=0, column_end=len(self.columns))
self._add(widget, row_start=key, column_start=0, column_end=len(self.columns))
else:
raise GridIndexError('Invalid index {}'.format(key))

def add(self, widget, row_start=None, column_start=None,
row_end=None, column_end=None):
def add(self, widget):
"""Add a widget to the grid in the next available cell.
Searches over columns then rows for available cells.
Parameters
----------
widget : bowtie._Component
A Bowtie widget instance.
"""
self._add(widget)

def _add(self, widget, row_start=None, column_start=None,
row_end=None, column_end=None):
"""Add a widget to the grid.
Zero-based index and exclusive.
Expand Down Expand Up @@ -577,28 +590,19 @@ def __setitem__(self, key, value):
"""Add widget to the root view."""
self.root.__setitem__(key, value)

def add(self, widget, row_start=None, column_start=None,
row_end=None, column_end=None):
"""Add a widget to the grid.
def add(self, widget):
"""Add a widget to the grid in the next available cell.
Zero-based index and inclusive.
Searches over columns then rows for available cells.
Parameters
----------
widget : bowtie._Component
A Bowtie widget instance.
row_start : int, optional
Starting row for the widget.
column_start : int, optional
Starting column for the widget.
row_end : int, optional
Ending row for the widget.
column_end : int, optional
Ending column for the widget.
"""
self.root.add(widget, row_start=row_start, column_start=column_start,
row_end=row_end, column_end=column_end)
# pylint: disable=protected-access
self.root._add(widget)

def add_sidebar(self, widget):
"""Add a widget to the sidebar.
Expand Down
45 changes: 11 additions & 34 deletions bowtie/tests/test_layout.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

from bowtie import App
from bowtie.control import Button
from bowtie._app import MissingRowOrColumn, GridIndexError, UsedCellsError, NoUnusedCellsError
from bowtie._app import GridIndexError, UsedCellsError, NoUnusedCellsError


@pytest.fixture(scope='module')
Expand All @@ -20,31 +20,6 @@ def app():
return App(rows=2, columns=2)


def test_no_row(buttons):
"""Test missing row."""

app = App(rows=2, columns=3)
with pytest.raises(MissingRowOrColumn):
app.add(buttons[0], column_start=0, column_end=3)

with pytest.raises(MissingRowOrColumn):
app.add(buttons[0], column_start=0)

with pytest.raises(MissingRowOrColumn):
app.add(buttons[0], column_end=1)


def test_no_column(buttons):
"""Test missing column."""

app = App(rows=2, columns=3)
with pytest.raises(MissingRowOrColumn):
app.add(buttons[0], row_start=0, row_end=2)

with pytest.raises(MissingRowOrColumn):
app.add(buttons[0], row_start=0)


def test_all_used(buttons):
"""Test all cells are used."""

Expand Down Expand Up @@ -84,29 +59,31 @@ def test_used(buttons):
app.add(buttons[i])

with pytest.raises(UsedCellsError):
app.add(buttons[3], row_start=0, column_start=0)
app[0, 0] = buttons[3]

with pytest.raises(UsedCellsError):
app.add(buttons[3], row_start=0, column_start=1, row_end=1)
app[0:1, 1] = buttons[3]

with pytest.raises(UsedCellsError):
app.add(buttons[3], row_start=1, column_start=0, column_end=1)
app[1, 0:1] = buttons[3]

app.add(buttons[3], row_start=1, column_start=1)
app[1, 1] = buttons[3]


def test_grid_index(buttons):
"""Test grid indexing checks."""

app = App(rows=2, columns=2)
with pytest.raises(GridIndexError):
app.add(buttons[0], row_start=-5)
app[-5] = buttons[0]

with pytest.raises(MissingRowOrColumn):
app.add(buttons[0], row_start=-1)
app[-1] = buttons[0]

with pytest.raises(GridIndexError):
app.add(buttons[0], row_start=2)
app[2] = buttons[0]

with pytest.raises(UsedCellsError):
app[1] = buttons[0]


def test_getitem(buttons):
Expand Down

0 comments on commit 3af7cad

Please sign in to comment.