From 908ef67109eb604f2863f05aa943755f96c1e296 Mon Sep 17 00:00:00 2001 From: tobi-wan-kenobi Date: Fri, 27 Mar 2020 13:54:22 +0100 Subject: [PATCH] [core/widget] bump test coverage --- core/widget.py | 12 +++--------- tests/core/test_widget.py | 35 +++++++++++++++++++++++++++++++++++ 2 files changed, 38 insertions(+), 9 deletions(-) diff --git a/core/widget.py b/core/widget.py index c512f3e..1328266 100644 --- a/core/widget.py +++ b/core/widget.py @@ -29,16 +29,10 @@ class Widget(util.store.Store, core.input.Object): rv = [] if self.get('state', None): tmp = self.get('state') - if isinstance(tmp, list): - rv.extend(tmp) - else: - rv.append(tmp) + rv = tmp[:] if isinstance(tmp, list) else [tmp] if self._module: tmp = self._module.state(self) - if isinstance(tmp, list): - rv.extend(tmp) - else: - rv.append(tmp) - return rv if isinstance(rv, list) else [rv] + rv.extend(tmp if isinstance(tmp, list) else [tmp]) + return rv # vim: tabstop=8 expandtab shiftwidth=4 softtabstop=4 diff --git a/tests/core/test_widget.py b/tests/core/test_widget.py index 905768e..7e68670 100644 --- a/tests/core/test_widget.py +++ b/tests/core/test_widget.py @@ -2,6 +2,21 @@ import unittest import unittest.mock import core.widget +import core.module +import core.config + +class TestModule(core.module.Module): + def __init__(self, widgets, config=core.config.Config([])): + super().__init__(config, widgets) + self.states = [] + + def update(self): + if self.fail: + raise Exception(self.error) + pass + + def state(self, widget): + return self.states class widget(unittest.TestCase): def setUp(self): @@ -35,4 +50,24 @@ class widget(unittest.TestCase): self.assertEqual(self.someWidget.full_text(), self.callbackReturnValue) self.someCallback.assert_called_once_with(self.someWidget) + def test_state_defaults_to_empty(self): + self.assertEqual([], self.someWidget.state()) + + def test_single_widget_state(self): + self.someWidget.set('state', 'state1') + self.assertEqual(['state1'], self.someWidget.state()) + + def test_multiple_widget_states(self): + self.someWidget.set('state', ['state1', 'state2']) + self.assertEqual(['state1', 'state2'], self.someWidget.state()) + + def test_widget_module_state(self): + module = TestModule(widgets=self.someWidget) + self.someWidget.set('state', ['state1', 'state2']) + + module.states = 'x' + self.assertEqual(['state1', 'state2', 'x'], self.someWidget.state()) + module.states = ['a', 'b'] + self.assertEqual(['state1', 'state2', 'a', 'b'], self.someWidget.state()) + # vim: tabstop=8 expandtab shiftwidth=4 softtabstop=4