[core/decorators] Add more tests

This commit is contained in:
Tobias Witek 2020-02-27 21:39:04 +01:00
parent 162398c6b6
commit b99ac07ef0
3 changed files with 27 additions and 8 deletions

View file

@ -13,21 +13,21 @@ def scrollable(func):
bounce = util.format.asbool(module.parameter('scrolling.bounce', True)) bounce = util.format.asbool(module.parameter('scrolling.bounce', True))
scroll_speed = util.format.asint(module.parameter('scrolling.speed', 1)) scroll_speed = util.format.asint(module.parameter('scrolling.speed', 1))
start = widget.get('scrolling.start', -1) start = widget.get('scrolling.start', 0)
direction = widget.get('scrolling.direction', 'right')
start += scroll_speed if direction == 'right' else -(scroll_speed)
if width + start > len(text) + (scroll_speed - 1): if start + width > len(text):
if bounce: if bounce:
widget.set('scrolling.direction', 'left') widget.set('scrolling.direction', 'left')
start -= scroll_speed*2
else: else:
start = 0 start = 0
elif start <= 0: elif start < 0:
if bounce: if bounce:
widget.set('scrolling.direction', 'right') widget.set('scrolling.direction', 'right')
else: direction = widget.get('scrolling.direction', 'right')
start = len(text) if direction == 'left':
widget.set('scrolling.start', start) scroll_speed = -scroll_speed
widget.set('scrolling.start', start + scroll_speed)
text = text[start:width+start] text = text[start:width+start]
return text return text

View file

@ -80,6 +80,7 @@ class i3(object):
'full_text': self.__decorate(module, widget, text), 'full_text': self.__decorate(module, widget, text),
'color': self._theme.fg(widget), 'color': self._theme.fg(widget),
'background': self._theme.bg(widget), 'background': self._theme.bg(widget),
'min_width': widget.get('theme.minwidth'),
}) })
return [attr] return [attr]

View file

@ -36,4 +36,22 @@ class config(unittest.TestCase):
self.assertGreater(len(self.module.text), self.width) self.assertGreater(len(self.module.text), self.width)
self.assertEqual(self.module.text[:self.width], self.module.get(self.widget)) self.assertEqual(self.module.text[:self.width], self.module.get(self.widget))
def test_bounce(self):
self.module.text = 'abcd'
self.module.set('width', 2)
self.assertEqual('ab', self.module.get(self.widget))
self.assertEqual('bc', self.module.get(self.widget))
self.assertEqual('cd', self.module.get(self.widget))
self.assertEqual('bc', self.module.get(self.widget))
self.assertEqual('ab', self.module.get(self.widget))
def test_nobounce(self):
self.module.set('scrolling.bounce', False)
self.module.text = 'abcd'
self.module.set('width', 2)
self.assertEqual('ab', self.module.get(self.widget))
self.assertEqual('bc', self.module.get(self.widget))
self.assertEqual('cd', self.module.get(self.widget))
self.assertEqual('ab', self.module.get(self.widget))
# vim: tabstop=8 expandtab shiftwidth=4 softtabstop=4 # vim: tabstop=8 expandtab shiftwidth=4 softtabstop=4