From fee7cf7882238496c36d9ead2614fcf37f4c64a1 Mon Sep 17 00:00:00 2001 From: Tobias Witek Date: Sun, 26 Jan 2020 13:58:29 +0100 Subject: [PATCH] [core/widget] Add widget class To maintain backwards compatibility (and because I think it's an OK design choice), keep the widget concept (a single module can produce multiple widgets). --- core/widget.py | 15 +++++++++++++++ tests/core/test_widget.py | 38 ++++++++++++++++++++++++++++++++++++++ 2 files changed, 53 insertions(+) create mode 100644 core/widget.py create mode 100644 tests/core/test_widget.py diff --git a/core/widget.py b/core/widget.py new file mode 100644 index 0000000..a348e4a --- /dev/null +++ b/core/widget.py @@ -0,0 +1,15 @@ +import util.store + +class Widget(util.store.Store): + def __init__(self, full_text): + self._full_text = full_text + + def full_text(self, value=None): + if value: + self._full_text = value + else: + if callable(self._full_text): + return self._full_text(self) + return self._full_text + +# vim: tabstop=8 expandtab shiftwidth=4 softtabstop=4 diff --git a/tests/core/test_widget.py b/tests/core/test_widget.py new file mode 100644 index 0000000..3d300c4 --- /dev/null +++ b/tests/core/test_widget.py @@ -0,0 +1,38 @@ +import unittest +import unittest.mock + +import core.widget + +class widget(unittest.TestCase): + def setUp(self): + self.someValue = 'some random value' + self.someOtherValue = 'some different value' + self.callbackReturnValue = 'callback return value' + self.someWidget = core.widget.Widget(full_text=self.someValue) + self.someCallback = unittest.mock.MagicMock(return_value=self.callbackReturnValue) + + self.assertNotEqual(self.someValue, self.someOtherValue) + + def tearDown(self): + pass + + def test_text_fulltext(self): + newWidget = core.widget.Widget(full_text=self.someValue) + self.assertEqual(self.someValue, newWidget.full_text()) + + def test_set_fulltext(self): + self.assertNotEqual(self.someOtherValue, self.someWidget.full_text()) + self.someWidget.full_text(self.someOtherValue) + self.assertEqual(self.someOtherValue, self.someWidget.full_text()) + + def test_callable_fulltext(self): + newWidget = core.widget.Widget(full_text=self.someCallback) + self.assertEqual(newWidget.full_text(), self.callbackReturnValue) + self.someCallback.assert_called_once() + + def test_set_callable_fulltext(self): + self.someWidget.full_text(self.someCallback) + self.assertEqual(self.someWidget.full_text(), self.callbackReturnValue) + self.someCallback.assert_called_once() + +# vim: tabstop=8 expandtab shiftwidth=4 softtabstop=4