Skip to content

Commit

Permalink
Ensure specified position is >= 1
Browse files Browse the repository at this point in the history
  • Loading branch information
avylove committed Feb 20, 2021
1 parent 4c90d30 commit 0cb9835
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 1 deletion.
6 changes: 5 additions & 1 deletion enlighten/_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -240,15 +240,19 @@ def _add_counter(self, counter_class, *args, **kwargs): # pylint: disable=too-m
new._pinned = True
pinned[position] = new

# Check position
# Position specified
elif position is not None:
if position < 1:
raise ValueError('Counter position %d is less than 1.' % position)
if position in pinned:
raise ValueError('Counter position %d is already occupied.' % position)
if position > self.height:
raise ValueError('Counter position %d is greater than terminal height.' % position)
new._pinned = True # pylint: disable=protected-access
self.counters[new] = position
pinned[position] = new

# Dynamic placement
else:
# Set for now, but will change
self.counters[new] = 0
Expand Down
3 changes: 3 additions & 0 deletions tests/test_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -186,6 +186,9 @@ def test_counter_position(self):
counter1 = manager.counter(position=4)
self.assertEqual(manager.counters[counter1], 4)

with self.assertRaisesRegex(ValueError, 'Counter position 0 is less than 1'):
manager.counter(position=0)

with self.assertRaisesRegex(ValueError, 'Counter position 4 is already occupied'):
manager.counter(position=4)

Expand Down

0 comments on commit 0cb9835

Please sign in to comment.