bumblebee-status/tests/core/test_widget.py

109 lines
3 KiB
Python
Raw Permalink Normal View History

2020-06-20 14:53:44 +02:00
import pytest
import core.widget
2020-03-27 13:54:22 +01:00
import core.module
import core.config
2020-06-20 14:53:44 +02:00
class SampleModule(core.module.Module):
def __init__(self, widgets, config=core.config.Config([]), theme=None):
super().__init__(config, theme, widgets)
2020-03-27 13:54:22 +01:00
self.states = []
def update(self):
if self.fail:
raise Exception(self.error)
pass
def state(self, widget):
return self.states
2020-06-20 15:11:53 +02:00
2020-06-20 14:53:44 +02:00
@pytest.fixture
def widget_a():
return core.widget.Widget("some random value")
2020-06-20 15:11:53 +02:00
# class widget(unittest.TestCase):
2020-06-20 14:53:44 +02:00
# 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)
2020-06-20 15:11:53 +02:00
2020-06-20 14:53:44 +02:00
def test_text_fulltext():
widget = core.widget.Widget(full_text="this is some value")
assert widget.full_text() == "this is some value"
2020-06-20 15:11:53 +02:00
2020-06-20 14:53:44 +02:00
def test_set_fulltext(widget_a):
assert widget_a.full_text() != "new value"
widget_a.full_text("new value")
assert widget_a.full_text() == "new value"
2020-06-20 15:11:53 +02:00
2020-06-20 14:53:44 +02:00
def test_callable_fulltext(mocker):
callback = mocker.MagicMock(return_value="callback returns")
widget = core.widget.Widget(full_text=callback)
assert widget.full_text() == "callback returns"
callback.assert_called_once_with(widget)
2020-06-20 15:11:53 +02:00
2020-06-20 14:53:44 +02:00
def test_set_callable_fulltext(mocker, widget_a):
callback = mocker.MagicMock(return_value="this is a test")
widget_a.full_text(callback)
assert widget_a.full_text() == "this is a test"
callback.assert_called_once_with(widget_a)
2020-06-20 15:11:53 +02:00
2020-06-20 14:53:44 +02:00
def test_state_defaults_to_empty(widget_a):
assert widget_a.state() == []
2020-06-20 15:11:53 +02:00
2020-06-20 14:53:44 +02:00
def test_single_widget_state(widget_a):
widget_a.set("state", "state1")
assert widget_a.state() == ["state1"]
2020-06-20 15:11:53 +02:00
2020-06-20 14:53:44 +02:00
def test_multiple_widget_states(widget_a):
widget_a.set("state", ["state1", "state2"])
assert widget_a.state() == ["state1", "state2"]
2020-06-20 15:11:53 +02:00
2020-06-20 14:53:44 +02:00
def test_widget_module_state(widget_a):
module = SampleModule(widgets=widget_a)
widget_a.set("state", ["state1", "state2"])
module.states = "x"
assert widget_a.state() == ["state1", "state2", "x"]
module.states = ["a", "b"]
assert widget_a.state() == ["state1", "state2", "a", "b"]
2020-06-20 15:11:53 +02:00
2020-06-20 14:53:44 +02:00
def test_multiple_widget_themes():
widget1 = core.widget.Widget(full_text="a")
widget2 = core.widget.Widget(full_text="b")
widget3 = core.widget.Widget(full_text="c")
module = SampleModule(widgets=[widget1, widget2, widget3])
module.set("theme.test", "1,2,3")
module.set("theme.test2", "x")
assert widget1.theme("test") == "1"
assert widget2.theme("test") == "2"
assert widget3.theme("test") == "3"
assert widget1.theme("test2") == "x"
assert widget2.theme("test2") == None
assert widget3.theme("test2") == None
2020-05-02 14:14:12 +02:00
# vim: tabstop=8 expandtab shiftwidth=4 softtabstop=4