From 79fb28512f6c7d3d4b90dd2294e563d27f2fb7a9 Mon Sep 17 00:00:00 2001 From: Tobias Witek Date: Sat, 20 Jun 2020 15:11:53 +0200 Subject: [PATCH] [tests] black -t py34 --- tests/__init__.py | 1 - tests/core/test_config.py | 22 ++++++++++++-- tests/core/test_decorators.py | 11 +++++++ tests/core/test_event.py | 12 ++++++-- tests/core/test_output.py | 49 +++++++++++++++++++------------ tests/core/test_theme.py | 25 ++++++++++++++-- tests/core/test_widget.py | 13 +++++++- tests/modules/contrib/test_mpd.py | 39 +++++++++++++++++------- tests/util/test_algorithm.py | 5 ++++ tests/util/test_cli.py | 6 ++++ 10 files changed, 144 insertions(+), 39 deletions(-) diff --git a/tests/__init__.py b/tests/__init__.py index afddb20..8df5d95 100644 --- a/tests/__init__.py +++ b/tests/__init__.py @@ -1,4 +1,3 @@ import bumblebee_status.discover bumblebee_status.discover.discover() - diff --git a/tests/core/test_config.py b/tests/core/test_config.py index 774b806..762c674 100644 --- a/tests/core/test_config.py +++ b/tests/core/test_config.py @@ -3,10 +3,12 @@ import pytest import core.config + @pytest.fixture def defaultConfig(): return core.config.Config([]) + def test_module(): modules = ["module-1", "module-2", "module-3"] @@ -14,6 +16,7 @@ def test_module(): assert cfg.modules() == modules + def test_module_ordering_maintained(): modules = ["module-1", "module-5", "module-7"] more_modules = ["module-0", "module-2", "aaa"] @@ -22,37 +25,45 @@ def test_module_ordering_maintained(): assert cfg.modules() == modules + more_modules + def test_default_interval(defaultConfig): assert defaultConfig.interval() == 1 + def test_interval(): interval = 4 cfg = core.config.Config(["-p", "interval={}".format(interval)]) assert cfg.interval() == interval + def test_floating_interval(): interval = 4.5 cfg = core.config.Config(["-p", "interval={}".format(interval)]) assert cfg.interval() == interval + def test_default_theme(defaultConfig): assert defaultConfig.theme() == "default" + def test_theme(): theme_name = "sample-theme" cfg = core.config.Config(["-t", theme_name]) assert cfg.theme() == theme_name + def test_default_iconset(defaultConfig): assert defaultConfig.iconset() == "auto" + def test_iconset(): iconset_name = "random-iconset" cfg = core.config.Config(["-i", iconset_name]) assert cfg.iconset() == iconset_name + def test_reverse(defaultConfig): assert defaultConfig.reverse() == False @@ -60,6 +71,7 @@ def test_reverse(defaultConfig): assert cfg.reverse() == True + def test_logfile(defaultConfig): assert defaultConfig.logfile() is None @@ -68,7 +80,6 @@ def test_logfile(defaultConfig): assert cfg.logfile() == logfile - def test_all_modules(): modules = core.config.all_modules() assert len(modules) > 0 @@ -84,8 +95,10 @@ def test_all_modules(): "modules", ) ) - assert os.path.exists(os.path.join(base, "contrib", pyname)) \ - or os.path.exists(os.path.join(base, "core", pyname)) + assert os.path.exists(os.path.join(base, "contrib", pyname)) or os.path.exists( + os.path.join(base, "core", pyname) + ) + def test_list_output(mocker): mocker.patch("core.config.sys") @@ -93,10 +106,13 @@ def test_list_output(mocker): cfg = core.config.Config(["-l", "modules"]) cfg = core.config.Config(["-l", "modules-rst"]) + def test_missing_parameter(): cfg = core.config.Config(["-p", "test.key"]) assert cfg.get("test.key") == None assert cfg.get("test.key", "no-value-set") == "no-value-set" + + # # vim: tabstop=8 expandtab shiftwidth=4 softtabstop=4 diff --git a/tests/core/test_decorators.py b/tests/core/test_decorators.py index 25f8b9c..976ad0a 100644 --- a/tests/core/test_decorators.py +++ b/tests/core/test_decorators.py @@ -5,6 +5,7 @@ import core.widget import core.module import core.config + @pytest.fixture def module(): class TestModule(core.module.Module): @@ -17,28 +18,34 @@ def module(): @core.decorators.scrollable def get(self, widget): return self.text + module = TestModule() module.set("scrolling.width", 10) return module + def test_never(module): assert module.parameter("interval") == "never" + def test_no_text(module): assert module.text == "" assert module.get(module.widget()) == "" + def test_smaller(module): module.text = "test" assert module.parameter("scrolling.width") > len(module.text) assert module.get(module.widget()) == module.text + def test_bigger(module): module.text = "this is a really really long sample text" maxwidth = module.parameter("scrolling.width") assert maxwidth < len(module.text) assert module.get(module.widget()) == module.text[:maxwidth] + def test_bounce(module): module.text = "abcd" module.set("scrolling.width", 2) @@ -52,6 +59,7 @@ def test_bounce(module): assert module.get(module.widget()) == "bc" assert module.get(module.widget()) == "ab" + def test_nobounce(module): module.set("scrolling.bounce", False) module.set("scrolling.width", 2) @@ -64,6 +72,7 @@ def test_nobounce(module): assert module.get(module.widget()) == "bc" assert module.get(module.widget()) == "cd" + def test_completely_changed_data(module): module.text = "abcd" module.set("scrolling.width", 2) @@ -75,6 +84,7 @@ def test_completely_changed_data(module): assert module.get(module.widget()) == "wx" assert module.get(module.widget()) == "xy" + def test_slightly_changed_data(module): module.text = "this is a sample song (0:00)" module.set("scrolling.width", 10) @@ -89,6 +99,7 @@ def test_slightly_changed_data(module): module.text = "this is a different song (0:13)" assert module.get(module.widget()) == module.text[0:10] + def test_n_plus_one(module): module.text = "10 letters" module.set("scrolling.width", 9) diff --git a/tests/core/test_event.py b/tests/core/test_event.py index b64478f..b082e77 100644 --- a/tests/core/test_event.py +++ b/tests/core/test_event.py @@ -2,9 +2,10 @@ import pytest import core.event + @pytest.fixture def someEvent(): - class Event(): + class Event: def __init__(self): core.event.clear() self.id = "some event" @@ -32,6 +33,7 @@ def test_simple_callback(someEvent): assert someEvent.called == 2 + def test_args_callback(someEvent): core.event.register(someEvent.id, someEvent.callback, "a", "b") core.event.trigger(someEvent.id) @@ -40,6 +42,7 @@ def test_args_callback(someEvent): assert len(someEvent.call_args) == 1 assert someEvent.call_args[0] == ["a", "b"] + def test_kwargs_callback(someEvent): core.event.register( someEvent.id, someEvent.callback, "a", "b", key1="test", key2="another" @@ -50,7 +53,8 @@ def test_kwargs_callback(someEvent): assert len(someEvent.call_args) == 1 assert someEvent.call_args[0] == ["a", "b"] assert len(someEvent.call_kwargs) == 1 - assert someEvent.call_kwargs[0] == { "key1": "test", "key2": "another" } + assert someEvent.call_kwargs[0] == {"key1": "test", "key2": "another"} + def test_arg_trigger(someEvent): core.event.register(someEvent.id, someEvent.callback) @@ -60,6 +64,7 @@ def test_arg_trigger(someEvent): assert len(someEvent.call_args) == 1 assert someEvent.call_args[0] == ["a", "b"] + def test_kwargs_trigger(someEvent): core.event.register(someEvent.id, someEvent.callback) core.event.trigger(someEvent.id, "a", "c", key1="test", key2="something") @@ -68,6 +73,7 @@ def test_kwargs_trigger(someEvent): assert len(someEvent.call_args) == 1 assert someEvent.call_args[0] == ["a", "c"] assert len(someEvent.call_kwargs) == 1 - assert someEvent.call_kwargs[0] == { "key1": "test", "key2": "something" } + assert someEvent.call_kwargs[0] == {"key1": "test", "key2": "something"} + # vim: tabstop=8 expandtab shiftwidth=4 softtabstop=4 diff --git a/tests/core/test_output.py b/tests/core/test_output.py index 69c8b15..fcd9e22 100644 --- a/tests/core/test_output.py +++ b/tests/core/test_output.py @@ -10,43 +10,44 @@ import core.module class SampleModule(core.module.Module): pass + @pytest.fixture(autouse=True) def clear_events(): core.event.clear() + @pytest.fixture def i3(): return core.output.i3() + @pytest.fixture def module_a(mocker): widget = mocker.MagicMock() widget.full_text.return_value = "test" - return SampleModule( - config=core.config.Config([]), widgets=[widget, widget, widget] - ) + return SampleModule(config=core.config.Config([]), widgets=[widget, widget, widget]) + @pytest.fixture def paddedTheme(): return core.theme.Theme(raw_data={"defaults": {"padding": " "}}) + @pytest.fixture def separatorTheme(): return core.theme.Theme( - raw_data={ - "defaults": {"separator": "***", "fg": "red", "bg": "blue"} - } + raw_data={"defaults": {"separator": "***", "fg": "red", "bg": "blue"}} ) + @pytest.fixture def block_a(separatorTheme, module_a): return core.output.block( - theme=separatorTheme, - module=module_a, - widget=module_a.widget(), + theme=separatorTheme, module=module_a, widget=module_a.widget(), ) -#def setUp(self): + +# def setUp(self): # self.i3 = core.output.i3() # widget = unittest.mock.MagicMock() # widget.full_text.return_value = "test" @@ -63,21 +64,26 @@ def test_start(i3): assert data["click_events"] == True assert all_data["suffix"] == "\n[" + def test_stop(i3): assert i3.stop()["suffix"] == "\n]" + def test_no_modules_by_default(i3): assert i3.modules() == [] + def test_register_single_module(i3, module_a): i3.modules(module_a) assert i3.modules() == [module_a] + def test_register_multiple_modules(i3, module_a): i3.modules([module_a, module_a, module_a]) assert i3.modules() == [module_a, module_a, module_a] + def test_draw_existing_module(mocker, i3): i3.test_draw = mocker.MagicMock( return_value={"blocks": {"test": True}, "suffix": "end"} @@ -85,31 +91,34 @@ def test_draw_existing_module(mocker, i3): i3.draw("test_draw") i3.test_draw.assert_called_once_with() + def test_empty_status_line(i3): data = i3.statusline() assert data["blocks"] == [] assert data["suffix"] == "," + def test_statusline(i3, module_a): i3.modules([module_a, module_a, module_a]) i3.update() data = i3.statusline() - assert len(data["blocks"]) == len(module_a.widgets())*3 + assert len(data["blocks"]) == len(module_a.widgets()) * 3 + def test_padding(i3, paddedTheme, module_a): i3.theme(paddedTheme) - blk = core.output.block( - i3.theme(), module_a, module_a.widget() - ) + blk = core.output.block(i3.theme(), module_a, module_a.widget()) blk.set("full_text", "abc") result = blk.dict()["full_text"] assert result == " abc " + def test_no_separator(i3, module_a): result = i3.separator_block(module_a, module_a.widget()) assert result == [] + def test_separator(i3, separatorTheme, module_a): i3.theme(separatorTheme) result = i3.separator_block(module_a, module_a.widget()) @@ -119,12 +128,14 @@ def test_separator(i3, separatorTheme, module_a): assert result[0].dict().get("_decorator") == True assert result[0].dict()["color"] == separatorTheme.get("bg", module_a.widget()) + def test_dump_json(mocker): obj = mocker.MagicMock() obj.dict = mocker.MagicMock() core.output.dump_json(obj) obj.dict_assert_called_once_with() + def test_assign(): src = {"a": "x", "b": "y", "c": "z"} dst = {} @@ -138,29 +149,31 @@ def test_assign(): core.output.assign(src, dst, "blub", default="def") assert dst["blub"] == "def" + def test_pango_detection(block_a): assert block_a.is_pango({}) == False - assert block_a.is_pango({ "pango": {} }) == True + assert block_a.is_pango({"pango": {}}) == True + def test_pangoize(block_a): assert block_a.pangoize("test") == "test" assert not "markup" in block_a.dict() - pango = block_a.pangoize( - {"pango": {"attr": "blub", "x": "y", "full_text": "test"}} - ) + pango = block_a.pangoize({"pango": {"attr": "blub", "x": "y", "full_text": "test"}}) assert 'attr="blub"' in pango assert 'x="y"' in pango assert "test" in pango assert block_a.dict()["markup"] == "pango" + def test_padding(block_a): block_a.set("padding", "***") block_a.set("full_text", "test") assert block_a.dict()["full_text"] == "***test***" + def test_pre_suffix(block_a): block_a.set("padding", "*") block_a.set("prefix", "pre") diff --git a/tests/core/test_theme.py b/tests/core/test_theme.py index 1ec1799..5f4f931 100644 --- a/tests/core/test_theme.py +++ b/tests/core/test_theme.py @@ -12,14 +12,17 @@ class SampleModule(core.module.Module): super().__init__(config, theme, widgets) self.name = "test" + @pytest.fixture(autouse=True) def clear_events(): core.event.clear() + @pytest.fixture def defaultsTheme(): return {"defaults": {"fg": "red", "bg": "black"}} + @pytest.fixture def cycleTheme(): return { @@ -30,22 +33,27 @@ def cycleTheme(): ] } + @pytest.fixture def colorTheme(): return {"colors": [{"red": "#ff0000", "blue": "#0000ff"}]} + @pytest.fixture def walTheme(): return {"colors": ["wal"]} + @pytest.fixture def cycleValueTheme(): return {"defaults": {"fg": ["red", "green", "blue"]}} + @pytest.fixture def stateTheme(): return {"warning": {"fg": "yellow"}, "critical": {"fg": "red"}} + @pytest.fixture def overlayTheme(): return { @@ -53,20 +61,24 @@ def overlayTheme(): "test": {"load": {"prefix": "b"}, "prefix": "x"}, } + def test_invalid_theme(): with pytest.raises(RuntimeError): core.theme.Theme("this-theme-does-not-exist") + def test_valid_theme(): theme = core.theme.Theme("default") assert theme.name == "default" + def test_defaults(defaultsTheme): theme = core.theme.Theme(raw_data=defaultsTheme) assert theme.get("fg") == defaultsTheme["defaults"]["fg"] assert theme.get("bg") == defaultsTheme["defaults"]["bg"] + def test_cycle(mocker, cycleTheme): theme = core.theme.Theme(raw_data=cycleTheme) @@ -90,6 +102,7 @@ def test_cycle(mocker, cycleTheme): assert theme.get("fg") == cycleTheme["cycle"][0]["fg"] assert theme.get("bg") == cycleTheme["cycle"][0]["bg"] + def test_custom_iconset(defaultsTheme): theme = core.theme.Theme(raw_data=defaultsTheme) @@ -101,7 +114,8 @@ def test_custom_iconset(defaultsTheme): ) assert theme.get("padding") == "aaa" - assert theme.get("fg") == "blue" # test override + assert theme.get("fg") == "blue" # test override + def test_colors(defaultsTheme, colorTheme): theme = core.theme.Theme(raw_data=defaultsTheme) @@ -110,6 +124,7 @@ def test_colors(defaultsTheme, colorTheme): theme = core.theme.Theme(raw_data=colorTheme) assert theme.keywords() == colorTheme["colors"][0] + def test_wal_colors(mocker, walTheme): io = mocker.patch("core.theme.io") os = mocker.patch("core.theme.os") @@ -124,6 +139,7 @@ def test_wal_colors(mocker, walTheme): assert theme.keywords() == {"red": "#ff0000"} + def test_wal_special(mocker, walTheme): io = mocker.patch("core.theme.io") os = mocker.patch("core.theme.os") @@ -137,17 +153,19 @@ def test_wal_special(mocker, walTheme): assert theme.keywords() == {"background": "#ff0000"} + def test_cycle_value(cycleValueTheme): widget = core.widget.Widget() expected = cycleValueTheme["defaults"]["fg"] theme = core.theme.Theme(raw_data=cycleValueTheme) for i in range(0, len(expected) * 3): - assert theme.get("fg", widget) == expected[i%len(expected)] + assert theme.get("fg", widget) == expected[i % len(expected)] # ensure multiple invocations are OK - assert theme.get("fg", widget) == expected[i%len(expected)] + assert theme.get("fg", widget) == expected[i % len(expected)] core.event.trigger("draw") + def test_state(stateTheme): widget = core.widget.Widget() theme = core.theme.Theme(raw_data=stateTheme) @@ -160,6 +178,7 @@ def test_state(stateTheme): widget.state = types.MethodType(lambda self: ["critical"], widget) assert theme.get("fg", widget) == stateTheme["critical"]["fg"] + def test_overlay(overlayTheme): widget = core.widget.Widget() module = SampleModule(widget) diff --git a/tests/core/test_widget.py b/tests/core/test_widget.py index 9a51021..ecaeee7 100644 --- a/tests/core/test_widget.py +++ b/tests/core/test_widget.py @@ -18,11 +18,13 @@ class SampleModule(core.module.Module): def state(self, widget): return self.states + @pytest.fixture def widget_a(): return core.widget.Widget("some random value") -#class widget(unittest.TestCase): + +# class widget(unittest.TestCase): # def setUp(self): # self.someValue = "some random value" # self.someOtherValue = "some different value" @@ -34,38 +36,46 @@ def widget_a(): # # self.assertNotEqual(self.someValue, self.someOtherValue) + def test_text_fulltext(): widget = core.widget.Widget(full_text="this is some value") assert widget.full_text() == "this is some value" + 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" + 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) + 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) + def test_state_defaults_to_empty(widget_a): assert widget_a.state() == [] + def test_single_widget_state(widget_a): widget_a.set("state", "state1") assert widget_a.state() == ["state1"] + def test_multiple_widget_states(widget_a): widget_a.set("state", ["state1", "state2"]) assert widget_a.state() == ["state1", "state2"] + def test_widget_module_state(widget_a): module = SampleModule(widgets=widget_a) widget_a.set("state", ["state1", "state2"]) @@ -76,6 +86,7 @@ def test_widget_module_state(widget_a): module.states = ["a", "b"] assert widget_a.state() == ["state1", "state2", "a", "b"] + def test_multiple_widget_themes(): widget1 = core.widget.Widget(full_text="a") widget2 = core.widget.Widget(full_text="b") diff --git a/tests/modules/contrib/test_mpd.py b/tests/modules/contrib/test_mpd.py index 26722e3..1fb48da 100644 --- a/tests/modules/contrib/test_mpd.py +++ b/tests/modules/contrib/test_mpd.py @@ -3,46 +3,65 @@ import pytest import core.config import modules.contrib.mpd + @pytest.fixture def mpd_module(): return modules.contrib.mpd.Module(config=core.config.Config([]), theme=None) + def _test_state(mocker, mpd_module, widget_name, expected_output): widget = mocker.Mock() widget.name = widget_name return mpd_module.state(widget) == expected_output + def test_states(mocker, mpd_module): - assert _test_state(mocker, mpd_module, 'mpd.repeat', 'repeat-off') - assert _test_state(mocker, mpd_module, 'mpd.shuffle', 'shuffle-off') - assert _test_state(mocker, mpd_module, 'mpd.prev', 'prev') - assert _test_state(mocker, mpd_module, 'mpd.next', 'next') + assert _test_state(mocker, mpd_module, "mpd.repeat", "repeat-off") + assert _test_state(mocker, mpd_module, "mpd.shuffle", "shuffle-off") + assert _test_state(mocker, mpd_module, "mpd.prev", "prev") + assert _test_state(mocker, mpd_module, "mpd.next", "next") + def test_no_host(mpd_module): - assert mpd_module._hostcmd == '' + assert mpd_module._hostcmd == "" + def test_host(): - module_with_host = modules.contrib.mpd.Module(config=core.config.Config(['-p', 'mpd.host=sample-host']), theme=None) - assert module_with_host._hostcmd == ' -h sample-host' + module_with_host = modules.contrib.mpd.Module( + config=core.config.Config(["-p", "mpd.host=sample-host"]), theme=None + ) + assert module_with_host._hostcmd == " -h sample-host" + def test_host2(mocker): cli = mocker.patch("modules.contrib.mpd.util.cli") - module_with_host = modules.contrib.mpd.Module(config=core.config.Config(['-p', 'mpd.host=sample-host']), theme=None) + module_with_host = modules.contrib.mpd.Module( + config=core.config.Config(["-p", "mpd.host=sample-host"]), theme=None + ) module_with_host.update() args, kwargs = cli.execute.call_args assert " -h sample-host" in args[0] and "mpc" in args[0] + def test_bad_layout(): - pytest.raises(KeyError, modules.contrib.mpd.Module, config=core.config.Config(['-p', 'mpd.layout="mpd.inexistent"']), theme=None) + pytest.raises( + KeyError, + modules.contrib.mpd.Module, + config=core.config.Config(["-p", 'mpd.layout="mpd.inexistent"']), + theme=None, + ) + def test_hidden_on_creation(mpd_module): assert mpd_module.hidden() + def test_update_calls_load_song(mocker, mpd_module): - mocker.patch.object(mpd_module, '_load_song') + mocker.patch.object(mpd_module, "_load_song") mpd_module.update() mpd_module._load_song.assert_called_with() + def test_default_layout(mpd_module): assert mpd_module._layout == "mpd.prev mpd.main mpd.next mpd.shuffle mpd.repeat" diff --git a/tests/util/test_algorithm.py b/tests/util/test_algorithm.py index 617187c..18d7a04 100644 --- a/tests/util/test_algorithm.py +++ b/tests/util/test_algorithm.py @@ -2,22 +2,27 @@ import pytest from util.algorithm import * + @pytest.fixture def someData(): return {"a": 100, "b": 200, "c": [1, 2, 3]} + @pytest.fixture def differentData(): return {"x": 20, "y": "bla", "z": ["a", "b"]} + @pytest.fixture def moreData(): return {"n": 100} + @pytest.fixture def overlapData(): return {"a": 200, "c": [1, 2, 4]} + def test_merge_with_empty(someData): assert merge(someData, {}) == someData assert merge(someData, None) == None diff --git a/tests/util/test_cli.py b/tests/util/test_cli.py index b2d8f81..ee25d71 100644 --- a/tests/util/test_cli.py +++ b/tests/util/test_cli.py @@ -2,24 +2,30 @@ import pytest import util.cli + def test_valid_command(): assert util.cli.execute("echo test") == "test\n" + def test_utf_command(): rv = util.cli.execute("echo ÖPmŧß") assert util.cli.execute("echo ÖPmŧß") == "ÖPmŧß\n" + def test_invalid_command(): with pytest.raises(RuntimeError): util.cli.execute("i-do-not-exist") + def test_command_exit_code(): with pytest.raises(RuntimeError): util.cli.execute("cat i-do-not-exist") + def test_command_exit_code_no_error(): util.cli.execute("cat i-do-not-exist", ignore_errors=True) + def test_async(): assert util.cli.execute("echo test", wait=False) == ""