[tests] switch to pytest
This commit is contained in:
parent
b2e92d816d
commit
39fa7788b4
37 changed files with 1009 additions and 2259 deletions
|
@ -8,10 +8,10 @@ python:
|
||||||
before_install:
|
before_install:
|
||||||
- sudo apt-get -qq update
|
- sudo apt-get -qq update
|
||||||
install:
|
install:
|
||||||
- pip install -U coverage==4.3
|
- pip install -U coverage==4.3 pytest
|
||||||
- pip install codeclimate-test-reporter
|
- pip install codeclimate-test-reporter
|
||||||
script:
|
script:
|
||||||
- coverage run --source=. -m unittest discover -v
|
- coverage run --source=. -m pytest tests -v
|
||||||
- CODECLIMATE_REPO_TOKEN=40cb00907f7a10e04868e856570bb997ab9c42fd3b63d980f2b2269433195fdf codeclimate-test-reporter
|
- CODECLIMATE_REPO_TOKEN=40cb00907f7a10e04868e856570bb997ab9c42fd3b63d980f2b2269433195fdf codeclimate-test-reporter
|
||||||
addons:
|
addons:
|
||||||
code_climate:
|
code_climate:
|
||||||
|
|
|
@ -1,4 +1,4 @@
|
||||||
#!/bin/bash
|
#!/bin/bash
|
||||||
|
|
||||||
coverage run --source=. -m unittest discover -v -b
|
coverage run --source=. -m pytest tests -v
|
||||||
coverage report -m
|
coverage report -m
|
||||||
|
|
|
@ -1,4 +0,0 @@
|
||||||
import bumblebee_status.discover
|
|
||||||
|
|
||||||
bumblebee_status.discover.discover()
|
|
||||||
|
|
|
@ -1,102 +0,0 @@
|
||||||
import os
|
|
||||||
import pytest
|
|
||||||
|
|
||||||
import core.config
|
|
||||||
|
|
||||||
@pytest.fixture
|
|
||||||
def defaultConfig():
|
|
||||||
return core.config.Config([])
|
|
||||||
|
|
||||||
def test_module():
|
|
||||||
modules = ["module-1", "module-2", "module-3"]
|
|
||||||
|
|
||||||
cfg = core.config.Config(["-m"] + modules)
|
|
||||||
|
|
||||||
assert cfg.modules() == modules
|
|
||||||
|
|
||||||
def test_module_ordering_maintained():
|
|
||||||
modules = ["module-1", "module-5", "module-7"]
|
|
||||||
more_modules = ["module-0", "module-2", "aaa"]
|
|
||||||
|
|
||||||
cfg = core.config.Config(["-m"] + modules + ["-m"] + more_modules)
|
|
||||||
|
|
||||||
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
|
|
||||||
|
|
||||||
cfg = core.config.Config(["-r"])
|
|
||||||
|
|
||||||
assert cfg.reverse() == True
|
|
||||||
|
|
||||||
def test_logfile(defaultConfig):
|
|
||||||
assert defaultConfig.logfile() is None
|
|
||||||
|
|
||||||
logfile = "some-random-logfile"
|
|
||||||
cfg = core.config.Config(["-f", logfile])
|
|
||||||
assert cfg.logfile() == logfile
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
def test_all_modules():
|
|
||||||
modules = core.config.all_modules()
|
|
||||||
assert len(modules) > 0
|
|
||||||
|
|
||||||
for module in modules:
|
|
||||||
pyname = "{}.py".format(module)
|
|
||||||
base = os.path.abspath(
|
|
||||||
os.path.join(
|
|
||||||
os.path.dirname(os.path.realpath(__file__)),
|
|
||||||
"..",
|
|
||||||
"..",
|
|
||||||
"bumblebee_status",
|
|
||||||
"modules",
|
|
||||||
)
|
|
||||||
)
|
|
||||||
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")
|
|
||||||
cfg = core.config.Config(["-l", "themes"])
|
|
||||||
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
|
|
|
@ -1,103 +0,0 @@
|
||||||
import pytest
|
|
||||||
|
|
||||||
import core.decorators
|
|
||||||
import core.widget
|
|
||||||
import core.module
|
|
||||||
import core.config
|
|
||||||
|
|
||||||
@pytest.fixture
|
|
||||||
def module():
|
|
||||||
class TestModule(core.module.Module):
|
|
||||||
@core.decorators.never
|
|
||||||
def __init__(self, config=None, theme=None):
|
|
||||||
config = core.config.Config([])
|
|
||||||
super().__init__(config, theme, core.widget.Widget(self.get))
|
|
||||||
self.text = ""
|
|
||||||
|
|
||||||
@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)
|
|
||||||
assert module.get(module.widget()) == "ab"
|
|
||||||
assert module.get(module.widget()) == "bc"
|
|
||||||
assert module.get(module.widget()) == "cd"
|
|
||||||
assert module.get(module.widget()) == "bc"
|
|
||||||
assert module.get(module.widget()) == "ab"
|
|
||||||
assert module.get(module.widget()) == "bc"
|
|
||||||
assert module.get(module.widget()) == "cd"
|
|
||||||
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)
|
|
||||||
module.text = "abcd"
|
|
||||||
|
|
||||||
assert module.get(module.widget()) == "ab"
|
|
||||||
assert module.get(module.widget()) == "bc"
|
|
||||||
assert module.get(module.widget()) == "cd"
|
|
||||||
assert module.get(module.widget()) == "ab"
|
|
||||||
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)
|
|
||||||
|
|
||||||
assert module.get(module.widget()) == "ab"
|
|
||||||
assert module.get(module.widget()) == "bc"
|
|
||||||
|
|
||||||
module.text = "wxyz"
|
|
||||||
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)
|
|
||||||
|
|
||||||
assert module.get(module.widget()) == module.text[0:10]
|
|
||||||
module.text = "this is a sample song (0:01)"
|
|
||||||
assert module.get(module.widget()) == module.text[1:11]
|
|
||||||
module.text = "this is a sample song (0:02)"
|
|
||||||
assert module.get(module.widget()) == module.text[2:12]
|
|
||||||
module.text = "this is a sample song (0:13)"
|
|
||||||
assert module.get(module.widget()) == module.text[3:13]
|
|
||||||
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)
|
|
||||||
|
|
||||||
assert module.get(module.widget()) == module.text[0:9]
|
|
||||||
assert module.get(module.widget()) == module.text[1:10]
|
|
||||||
assert module.get(module.widget()) == module.text[0:9]
|
|
||||||
assert module.get(module.widget()) == module.text[1:10]
|
|
||||||
assert module.get(module.widget()) == module.text[0:9]
|
|
||||||
|
|
||||||
|
|
||||||
# vim: tabstop=8 expandtab shiftwidth=4 softtabstop=4
|
|
|
@ -1,73 +0,0 @@
|
||||||
import pytest
|
|
||||||
|
|
||||||
import core.event
|
|
||||||
|
|
||||||
@pytest.fixture
|
|
||||||
def someEvent():
|
|
||||||
class Event():
|
|
||||||
def __init__(self):
|
|
||||||
core.event.clear()
|
|
||||||
self.id = "some event"
|
|
||||||
self.called = 0
|
|
||||||
self.call_args = []
|
|
||||||
self.call_kwargs = []
|
|
||||||
|
|
||||||
def callback(self, *args, **kwargs):
|
|
||||||
self.called += 1
|
|
||||||
if args:
|
|
||||||
self.call_args.append(list(args))
|
|
||||||
if kwargs:
|
|
||||||
self.call_kwargs.append(kwargs)
|
|
||||||
|
|
||||||
return Event()
|
|
||||||
|
|
||||||
|
|
||||||
def test_simple_callback(someEvent):
|
|
||||||
assert someEvent.called == 0
|
|
||||||
|
|
||||||
core.event.register(someEvent.id, someEvent.callback)
|
|
||||||
core.event.register(someEvent.id, someEvent.callback)
|
|
||||||
|
|
||||||
core.event.trigger(someEvent.id)
|
|
||||||
|
|
||||||
assert someEvent.called == 2
|
|
||||||
|
|
||||||
def test_args_callback(someEvent):
|
|
||||||
core.event.register(someEvent.id, someEvent.callback, "a", "b")
|
|
||||||
core.event.trigger(someEvent.id)
|
|
||||||
|
|
||||||
assert someEvent.called == 1
|
|
||||||
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"
|
|
||||||
)
|
|
||||||
core.event.trigger(someEvent.id)
|
|
||||||
|
|
||||||
assert someEvent.called == 1
|
|
||||||
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" }
|
|
||||||
|
|
||||||
def test_arg_trigger(someEvent):
|
|
||||||
core.event.register(someEvent.id, someEvent.callback)
|
|
||||||
core.event.trigger(someEvent.id, "a", "b")
|
|
||||||
|
|
||||||
assert someEvent.called == 1
|
|
||||||
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")
|
|
||||||
|
|
||||||
assert someEvent.called == 1
|
|
||||||
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" }
|
|
||||||
|
|
||||||
# vim: tabstop=8 expandtab shiftwidth=4 softtabstop=4
|
|
|
@ -1,104 +0,0 @@
|
||||||
import pytest
|
|
||||||
|
|
||||||
import core.input
|
|
||||||
|
|
||||||
|
|
||||||
@pytest.fixture
|
|
||||||
def obj():
|
|
||||||
return core.input.Object()
|
|
||||||
|
|
||||||
|
|
||||||
@pytest.fixture
|
|
||||||
def obj2():
|
|
||||||
return core.input.Object()
|
|
||||||
|
|
||||||
|
|
||||||
@pytest.fixture
|
|
||||||
def cb(mocker):
|
|
||||||
return mocker.MagicMock()
|
|
||||||
|
|
||||||
|
|
||||||
@pytest.fixture
|
|
||||||
def cb2(mocker):
|
|
||||||
return mocker.MagicMock()
|
|
||||||
|
|
||||||
|
|
||||||
def event(input_object):
|
|
||||||
return {"button": core.input.LEFT_MOUSE, "instance": input_object.id}
|
|
||||||
|
|
||||||
|
|
||||||
def event2(input_object):
|
|
||||||
return {"button": core.input.RIGHT_MOUSE, "instance": input_object.id}
|
|
||||||
|
|
||||||
|
|
||||||
def test_callable_gets_called(obj, cb):
|
|
||||||
core.input.register(obj, event(obj)["button"], cb)
|
|
||||||
core.input.trigger(event(obj))
|
|
||||||
|
|
||||||
cb.assert_called_once_with(event(obj))
|
|
||||||
|
|
||||||
|
|
||||||
def test_nonexistent_callback(obj, obj2, cb):
|
|
||||||
core.input.register(obj, event(obj)["button"], cb)
|
|
||||||
core.input.trigger(event(obj2))
|
|
||||||
|
|
||||||
cb.assert_not_called()
|
|
||||||
|
|
||||||
|
|
||||||
def test_different_events(obj, obj2, cb, cb2):
|
|
||||||
core.input.register(obj, event(obj)["button"], cb)
|
|
||||||
core.input.register(obj, event2(obj)["button"], cb)
|
|
||||||
core.input.register(obj2, event(obj)["button"], cb2)
|
|
||||||
core.input.register(obj2, event2(obj)["button"], cb2)
|
|
||||||
|
|
||||||
core.input.trigger(event(obj))
|
|
||||||
core.input.trigger(event2(obj))
|
|
||||||
|
|
||||||
cb.assert_any_call(event(obj))
|
|
||||||
cb.assert_any_call(event2(obj))
|
|
||||||
cb2.assert_not_called()
|
|
||||||
|
|
||||||
|
|
||||||
def test_multiple_registrations(obj, cb, cb2):
|
|
||||||
core.input.register(obj, event(obj)["button"], cb)
|
|
||||||
core.input.register(obj, event(obj)["button"], cb2)
|
|
||||||
|
|
||||||
core.input.trigger(event(obj))
|
|
||||||
|
|
||||||
cb.assert_called_once_with(event(obj))
|
|
||||||
cb2.assert_called_once_with(event(obj))
|
|
||||||
|
|
||||||
|
|
||||||
def test_event_names():
|
|
||||||
assert core.input.button_name(core.input.LEFT_MOUSE) == "left-mouse"
|
|
||||||
assert core.input.button_name(core.input.RIGHT_MOUSE) == "right-mouse"
|
|
||||||
assert core.input.button_name(core.input.MIDDLE_MOUSE) == "middle-mouse"
|
|
||||||
assert core.input.button_name(core.input.WHEEL_UP) == "wheel-up"
|
|
||||||
assert core.input.button_name(core.input.WHEEL_DOWN) == "wheel-down"
|
|
||||||
assert core.input.button_name(12345) == "n/a"
|
|
||||||
|
|
||||||
|
|
||||||
def test_non_callable_callback(mocker, obj):
|
|
||||||
cli = mocker.patch("core.input.util.cli")
|
|
||||||
cli.execute.return_value = ""
|
|
||||||
|
|
||||||
core.input.register(obj, event(obj)["button"], "sample-command")
|
|
||||||
|
|
||||||
core.input.trigger(event(obj))
|
|
||||||
|
|
||||||
cli.execute.assert_called_once_with("sample-command", wait=False, shell=True)
|
|
||||||
|
|
||||||
|
|
||||||
def test_non_existent_callback(mocker, obj):
|
|
||||||
cli = mocker.patch("core.input.util.cli")
|
|
||||||
cli.execute.return_value = ""
|
|
||||||
cli.execute.side_effect = RuntimeError("some-error")
|
|
||||||
|
|
||||||
core.input.register(obj, event(obj)["button"], "sample-command")
|
|
||||||
|
|
||||||
core.input.trigger(event(obj))
|
|
||||||
|
|
||||||
cli.execute.assert_called_once_with("sample-command", wait=False, shell=True)
|
|
||||||
|
|
||||||
|
|
||||||
# vim: tabstop=8 expandtab shiftwidth=4 softtabstop=4
|
|
|
@ -1,177 +0,0 @@
|
||||||
import pytest
|
|
||||||
|
|
||||||
import sys
|
|
||||||
import shlex
|
|
||||||
|
|
||||||
import core.module
|
|
||||||
import core.widget
|
|
||||||
import core.config
|
|
||||||
import core.input
|
|
||||||
|
|
||||||
|
|
||||||
@pytest.fixture(autouse=True)
|
|
||||||
def clear_events():
|
|
||||||
core.event.clear()
|
|
||||||
|
|
||||||
|
|
||||||
@pytest.fixture
|
|
||||||
def empty_config():
|
|
||||||
return core.config.Config([])
|
|
||||||
|
|
||||||
|
|
||||||
@pytest.fixture
|
|
||||||
def widget_a():
|
|
||||||
return core.widget.Widget("randomwWidget content", name="A")
|
|
||||||
|
|
||||||
|
|
||||||
@pytest.fixture
|
|
||||||
def widget_b():
|
|
||||||
return core.widget.Widget("another randomwWidget content", name="B")
|
|
||||||
|
|
||||||
|
|
||||||
class SampleModule(core.module.Module):
|
|
||||||
def update(self):
|
|
||||||
if self.fail:
|
|
||||||
raise Exception(self.error)
|
|
||||||
pass
|
|
||||||
|
|
||||||
|
|
||||||
def test_loadinvalid_module(mocker):
|
|
||||||
config = mocker.MagicMock()
|
|
||||||
module = core.module.load(module_name="i-do-not-exist", config=config)
|
|
||||||
assert module.__class__.__module__ == "core.module"
|
|
||||||
assert module.__class__.__name__ == "Error"
|
|
||||||
|
|
||||||
|
|
||||||
@pytest.mark.skipif(
|
|
||||||
sys.version_info.major == 3 and sys.version_info.minor in [4, 5],
|
|
||||||
reason="importlib error reporting in Python 3.{4,5} different",
|
|
||||||
)
|
|
||||||
def test_importerror(mocker):
|
|
||||||
importlib = mocker.patch("core.module.importlib")
|
|
||||||
importlib.import_module.side_effect = ImportError("some-error")
|
|
||||||
config = mocker.MagicMock()
|
|
||||||
|
|
||||||
module = core.module.load(module_name="test", config=config)
|
|
||||||
|
|
||||||
assert module.__class__.__name__ == "Error"
|
|
||||||
assert module.widget().full_text() == "test: some-error"
|
|
||||||
|
|
||||||
|
|
||||||
def test_loadvalid_module():
|
|
||||||
module = core.module.load(module_name="test")
|
|
||||||
assert module.__class__.__module__ == "modules.core.test"
|
|
||||||
assert module.__class__.__name__ == "Module"
|
|
||||||
assert module.state(None) == []
|
|
||||||
|
|
||||||
|
|
||||||
def test_empty_widgets():
|
|
||||||
module = core.module.Module(widgets=[])
|
|
||||||
assert module.widgets() == []
|
|
||||||
|
|
||||||
|
|
||||||
def test_error_widget():
|
|
||||||
cfg = core.config.Config(shlex.split("-p test_module.foo=5"))
|
|
||||||
module = core.module.Error("test-mod", "xyz", config=cfg)
|
|
||||||
full_text = module.full_text(module.widget())
|
|
||||||
|
|
||||||
assert module.state(None) == ["critical"]
|
|
||||||
assert "test-mod" in full_text
|
|
||||||
assert "xyz" in full_text
|
|
||||||
|
|
||||||
|
|
||||||
def test_single_widget(widget_a):
|
|
||||||
module = core.module.Module(widgets=widget_a)
|
|
||||||
assert module.widgets() == [widget_a]
|
|
||||||
|
|
||||||
|
|
||||||
def test_widget_list(widget_a, widget_b):
|
|
||||||
module = core.module.Module(widgets=[widget_a, widget_b])
|
|
||||||
assert module.widgets() == [widget_a, widget_b]
|
|
||||||
|
|
||||||
|
|
||||||
def test_module_Name():
|
|
||||||
module = SampleModule()
|
|
||||||
assert module.name == "test_module"
|
|
||||||
assert module.module_name == "test_module"
|
|
||||||
|
|
||||||
|
|
||||||
def testvalid_parameter():
|
|
||||||
cfg = core.config.Config(shlex.split("-p test_module.foo=5"))
|
|
||||||
module = SampleModule(config=cfg)
|
|
||||||
assert module.parameter("foo") == "5"
|
|
||||||
|
|
||||||
|
|
||||||
def test_default_parameter(empty_config):
|
|
||||||
module = SampleModule(config=empty_config)
|
|
||||||
assert module.parameter("foo", "default") == "default"
|
|
||||||
|
|
||||||
|
|
||||||
def test_default_is_none(empty_config):
|
|
||||||
module = SampleModule(config=empty_config)
|
|
||||||
assert module.parameter("foo") == None
|
|
||||||
|
|
||||||
|
|
||||||
def test_error_widget(empty_config):
|
|
||||||
module = SampleModule(config=empty_config)
|
|
||||||
module.fail = True
|
|
||||||
module.error = "!!"
|
|
||||||
module.update_wrapper()
|
|
||||||
assert len(module.widgets()) == 1
|
|
||||||
assert module.widget().full_text() == "error: !!"
|
|
||||||
|
|
||||||
|
|
||||||
def test_get_widget_by_name(empty_config, widget_a, widget_b):
|
|
||||||
module = SampleModule(config=empty_config, widgets=[widget_a, widget_b])
|
|
||||||
|
|
||||||
assert module.widget(widget_a.name) == widget_a
|
|
||||||
assert module.widget(widget_b.name) == widget_b
|
|
||||||
assert module.widget("i-do-not-exist") == None
|
|
||||||
assert module.widget() == widget_a
|
|
||||||
|
|
||||||
|
|
||||||
def test_default_thresholds(empty_config, widget_a, widget_b):
|
|
||||||
module = SampleModule(config=empty_config, widgets=[widget_a, widget_b])
|
|
||||||
|
|
||||||
assert module.threshold_state(100, 80, 99) == "critical"
|
|
||||||
assert module.threshold_state(100, 80, 100) == "warning"
|
|
||||||
assert module.threshold_state(81, 80, 100) == "warning"
|
|
||||||
assert module.threshold_state(80, 80, 100) == None
|
|
||||||
assert module.threshold_state(10, 80, 100) == None
|
|
||||||
|
|
||||||
|
|
||||||
def test_configured_callbacks(mocker, empty_config, widget_a, widget_b):
|
|
||||||
module = SampleModule(config=empty_config, widgets=[widget_a, widget_b])
|
|
||||||
|
|
||||||
cmd = "sample-tool arg1 arg2 arg3"
|
|
||||||
module.set("left-click", cmd)
|
|
||||||
module.register_callbacks()
|
|
||||||
|
|
||||||
cli = mocker.patch("core.input.util.cli")
|
|
||||||
cli.execute.return_value = ""
|
|
||||||
core.input.trigger(
|
|
||||||
{"button": core.input.LEFT_MOUSE, "instance": module.id,}
|
|
||||||
)
|
|
||||||
|
|
||||||
cli.execute.assert_called_once_with(cmd, wait=False, shell=True)
|
|
||||||
|
|
||||||
|
|
||||||
def test_configured_callbacks_with_parameters(mocker, empty_config, widget_a):
|
|
||||||
module = SampleModule(config=empty_config, widgets=[widget_a])
|
|
||||||
|
|
||||||
cmd = "sample-tool {instance} {name}"
|
|
||||||
module.set("left-click", cmd)
|
|
||||||
module.register_callbacks()
|
|
||||||
|
|
||||||
cli = mocker.patch("core.input.util.cli")
|
|
||||||
cli.execute.return_value = ""
|
|
||||||
core.input.trigger(
|
|
||||||
{"button": core.input.LEFT_MOUSE, "instance": module.id, "name": "sample-name",}
|
|
||||||
)
|
|
||||||
|
|
||||||
cli.execute.assert_called_once_with(
|
|
||||||
cmd.format(instance=module.id, name="sample-name"), wait=False, shell=True,
|
|
||||||
)
|
|
||||||
|
|
||||||
|
|
||||||
# vim: tabstop=8 expandtab shiftwidth=4 softtabstop=4
|
|
|
@ -1,173 +0,0 @@
|
||||||
import json
|
|
||||||
import pytest
|
|
||||||
|
|
||||||
import core.event
|
|
||||||
import core.config
|
|
||||||
import core.output
|
|
||||||
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]
|
|
||||||
)
|
|
||||||
|
|
||||||
@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"}
|
|
||||||
}
|
|
||||||
)
|
|
||||||
|
|
||||||
@pytest.fixture
|
|
||||||
def block_a(separatorTheme, module_a):
|
|
||||||
return core.output.block(
|
|
||||||
theme=separatorTheme,
|
|
||||||
module=module_a,
|
|
||||||
widget=module_a.widget(),
|
|
||||||
)
|
|
||||||
|
|
||||||
#def setUp(self):
|
|
||||||
# self.i3 = core.output.i3()
|
|
||||||
# widget = unittest.mock.MagicMock()
|
|
||||||
# widget.full_text.return_value = "test"
|
|
||||||
# self.someModule = SampleModule(
|
|
||||||
# config=core.config.Config([]), widgets=[widget, widget, widget]
|
|
||||||
# )
|
|
||||||
# self.separator = "***"
|
|
||||||
#
|
|
||||||
def test_start(i3):
|
|
||||||
all_data = i3.start()
|
|
||||||
data = all_data["blocks"]
|
|
||||||
|
|
||||||
assert data["version"] == 1
|
|
||||||
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"}
|
|
||||||
)
|
|
||||||
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
|
|
||||||
|
|
||||||
def test_padding(i3, paddedTheme, module_a):
|
|
||||||
i3.theme(paddedTheme)
|
|
||||||
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())
|
|
||||||
|
|
||||||
assert len(result) == 1
|
|
||||||
assert result[0].dict()["full_text"] == "***"
|
|
||||||
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 = {}
|
|
||||||
|
|
||||||
core.output.assign(src, dst, "a")
|
|
||||||
assert src["a"] == dst["a"]
|
|
||||||
|
|
||||||
core.output.assign(src, dst, "123", "b")
|
|
||||||
assert src["b"] == dst["123"]
|
|
||||||
|
|
||||||
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
|
|
||||||
|
|
||||||
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"}}
|
|
||||||
)
|
|
||||||
assert 'attr="blub"' in pango
|
|
||||||
assert 'x="y"' in pango
|
|
||||||
assert "<span " in pango
|
|
||||||
assert ">test</span>" 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")
|
|
||||||
block_a.set("suffix", "suf")
|
|
||||||
block_a.set("full_text", "test")
|
|
||||||
|
|
||||||
assert block_a.dict()["full_text"] == "*pre*test*suf*"
|
|
||||||
|
|
||||||
|
|
||||||
# vim: tabstop=8 expandtab shiftwidth=4 softtabstop=4
|
|
|
@ -1,175 +0,0 @@
|
||||||
import pytest
|
|
||||||
import types
|
|
||||||
|
|
||||||
import core.theme
|
|
||||||
import core.event
|
|
||||||
import core.widget
|
|
||||||
import core.module
|
|
||||||
|
|
||||||
|
|
||||||
class SampleModule(core.module.Module):
|
|
||||||
def __init__(self, widgets, config=core.config.Config([]), theme=None):
|
|
||||||
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 {
|
|
||||||
"cycle": [
|
|
||||||
{"fg": "red", "bg": "black"},
|
|
||||||
{"fg": "black", "bg": "red"},
|
|
||||||
{"fg": "white", "bg": "blue"},
|
|
||||||
]
|
|
||||||
}
|
|
||||||
|
|
||||||
@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 {
|
|
||||||
"load": {"prefix": "a"},
|
|
||||||
"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)
|
|
||||||
|
|
||||||
assert theme.get("bg", "previous") == None
|
|
||||||
assert theme.get("fg") == cycleTheme["cycle"][0]["fg"]
|
|
||||||
assert theme.get("bg") == cycleTheme["cycle"][0]["bg"]
|
|
||||||
|
|
||||||
core.event.trigger("next-widget")
|
|
||||||
|
|
||||||
assert theme.get("bg", "previous") == cycleTheme["cycle"][0]["bg"]
|
|
||||||
|
|
||||||
core.event.trigger("next-widget")
|
|
||||||
|
|
||||||
assert theme.get("fg") == cycleTheme["cycle"][2]["fg"]
|
|
||||||
assert theme.get("bg") == cycleTheme["cycle"][2]["bg"]
|
|
||||||
|
|
||||||
stdout = mocker.patch("core.output.sys.stdout")
|
|
||||||
|
|
||||||
core.event.trigger("draw")
|
|
||||||
|
|
||||||
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)
|
|
||||||
|
|
||||||
assert theme.get("padding") != "aaa"
|
|
||||||
assert theme.get("fg") != "blue"
|
|
||||||
|
|
||||||
theme = core.theme.Theme(
|
|
||||||
raw_data=defaultsTheme, iconset={"defaults": {"padding": "aaa", "fg": "blue"}}
|
|
||||||
)
|
|
||||||
|
|
||||||
assert theme.get("padding") == "aaa"
|
|
||||||
assert theme.get("fg") == "blue" # test override
|
|
||||||
|
|
||||||
def test_colors(defaultsTheme, colorTheme):
|
|
||||||
theme = core.theme.Theme(raw_data=defaultsTheme)
|
|
||||||
assert theme.keywords() == {}
|
|
||||||
|
|
||||||
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")
|
|
||||||
|
|
||||||
os.path.isfile.return_value = True
|
|
||||||
io.open.return_value = mocker.MagicMock()
|
|
||||||
io.open.return_value.__enter__.return_value.read.return_value = """
|
|
||||||
{ "colors": { "red": "#ff0000" } }
|
|
||||||
"""
|
|
||||||
|
|
||||||
theme = core.theme.Theme(raw_data=walTheme)
|
|
||||||
|
|
||||||
assert theme.keywords() == {"red": "#ff0000"}
|
|
||||||
|
|
||||||
def test_wal_special(mocker, walTheme):
|
|
||||||
io = mocker.patch("core.theme.io")
|
|
||||||
os = mocker.patch("core.theme.os")
|
|
||||||
|
|
||||||
os.path.isfile.return_value = True
|
|
||||||
io.open.return_value.__enter__.return_value.read.return_value = """
|
|
||||||
{ "special": { "background": "#ff0000" } }
|
|
||||||
"""
|
|
||||||
|
|
||||||
theme = core.theme.Theme(raw_data=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)]
|
|
||||||
# ensure multiple invocations are OK
|
|
||||||
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)
|
|
||||||
|
|
||||||
assert theme.get("fg", widget) == None
|
|
||||||
|
|
||||||
widget.state = types.MethodType(lambda self: ["warning"], widget)
|
|
||||||
assert theme.get("fg", widget) == stateTheme["warning"]["fg"]
|
|
||||||
|
|
||||||
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)
|
|
||||||
theme = core.theme.Theme(raw_data=overlayTheme)
|
|
||||||
|
|
||||||
assert theme.get("prefix", widget) == overlayTheme[module.name]["prefix"]
|
|
||||||
|
|
||||||
widget.state = types.MethodType(lambda self: ["load"], widget)
|
|
||||||
|
|
||||||
assert theme.get("prefix", widget) == overlayTheme[module.name]["load"]["prefix"]
|
|
||||||
|
|
||||||
|
|
||||||
# vim: tabstop=8 expandtab shiftwidth=4 softtabstop=4
|
|
|
@ -1,97 +0,0 @@
|
||||||
import pytest
|
|
||||||
|
|
||||||
import core.widget
|
|
||||||
import core.module
|
|
||||||
import core.config
|
|
||||||
|
|
||||||
|
|
||||||
class SampleModule(core.module.Module):
|
|
||||||
def __init__(self, widgets, config=core.config.Config([]), theme=None):
|
|
||||||
super().__init__(config, theme, widgets)
|
|
||||||
self.states = []
|
|
||||||
|
|
||||||
def update(self):
|
|
||||||
if self.fail:
|
|
||||||
raise Exception(self.error)
|
|
||||||
pass
|
|
||||||
|
|
||||||
def state(self, widget):
|
|
||||||
return self.states
|
|
||||||
|
|
||||||
@pytest.fixture
|
|
||||||
def widget_a():
|
|
||||||
return core.widget.Widget("some random value")
|
|
||||||
|
|
||||||
#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 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"])
|
|
||||||
|
|
||||||
module.states = "x"
|
|
||||||
assert widget_a.state() == ["state1", "state2", "x"]
|
|
||||||
|
|
||||||
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")
|
|
||||||
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
|
|
||||||
|
|
||||||
|
|
||||||
# vim: tabstop=8 expandtab shiftwidth=4 softtabstop=4
|
|
|
@ -1,50 +0,0 @@
|
||||||
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
|
|
||||||
|
|
||||||
def test_merge_no_overwrite(someData, differentData):
|
|
||||||
result = merge(someData, differentData)
|
|
||||||
for k in someData:
|
|
||||||
assert someData[k] == result[k]
|
|
||||||
for k in self.differentData:
|
|
||||||
assert differentData[k] == result[k]
|
|
||||||
|
|
||||||
def test_merge_multiple(someData, differentData, moreData):
|
|
||||||
result = merge(someData, differentData, moreData)
|
|
||||||
for k in someData:
|
|
||||||
assert someData[k] == result[k]
|
|
||||||
for k in differentData:
|
|
||||||
assert differentData[k] == result[k]
|
|
||||||
for k in moreData:
|
|
||||||
assert moreData[k] == result[k]
|
|
||||||
|
|
||||||
def merge_overlap(someData, overlapData):
|
|
||||||
result = merge(someData, overlapData)
|
|
||||||
for k in someData:
|
|
||||||
if not k in self.overlapData:
|
|
||||||
assert someData[k] == result[k]
|
|
||||||
for k in self.overlapData:
|
|
||||||
assert overlapData[k] == result[k]
|
|
||||||
|
|
||||||
|
|
||||||
# vim: tabstop=8 expandtab shiftwidth=4 softtabstop=4
|
|
|
@ -1,27 +0,0 @@
|
||||||
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) == ""
|
|
||||||
|
|
||||||
|
|
||||||
# vim: tabstop=8 expandtab shiftwidth=4 softtabstop=4
|
|
|
@ -1,113 +0,0 @@
|
||||||
import pytest
|
|
||||||
|
|
||||||
from util.format import *
|
|
||||||
|
|
||||||
|
|
||||||
def test_int_from_string():
|
|
||||||
assert asint("100") == 100
|
|
||||||
assert asint("-100") == -100
|
|
||||||
assert asint("0") == 0
|
|
||||||
|
|
||||||
|
|
||||||
def test_int_from_none():
|
|
||||||
assert asint(None) == 0
|
|
||||||
|
|
||||||
|
|
||||||
def test_int_from_int():
|
|
||||||
assert asint(100) == 100
|
|
||||||
assert asint(-100) == -100
|
|
||||||
assert asint(0) == 0
|
|
||||||
|
|
||||||
|
|
||||||
def test_int_minimum():
|
|
||||||
assert asint(100, minimum=10) == 100
|
|
||||||
assert asint(100, minimum=100) == 100
|
|
||||||
assert asint(5, minimum=10) == 10
|
|
||||||
|
|
||||||
|
|
||||||
def test_int_maximum():
|
|
||||||
assert asint(100, maximum=200) == 100
|
|
||||||
assert asint(100, maximum=100) == 100
|
|
||||||
assert asint(200, maximum=100) == 100
|
|
||||||
|
|
||||||
|
|
||||||
def test_true_from_str():
|
|
||||||
assert asbool("true") == True
|
|
||||||
assert asbool(True) == True
|
|
||||||
assert asbool("t") == True
|
|
||||||
assert asbool("1") == True
|
|
||||||
assert asbool("yes") == True
|
|
||||||
assert asbool("y") == True
|
|
||||||
assert asbool("on") == True
|
|
||||||
|
|
||||||
|
|
||||||
def test_false_from_str():
|
|
||||||
assert asbool("false") == False
|
|
||||||
assert asbool(False) == False
|
|
||||||
assert asbool("f") == False
|
|
||||||
assert asbool("0") == False
|
|
||||||
assert asbool("no") == False
|
|
||||||
assert asbool("n") == False
|
|
||||||
assert asbool("off") == False
|
|
||||||
assert asbool(None) == False
|
|
||||||
|
|
||||||
|
|
||||||
def test_list_from_None():
|
|
||||||
assert aslist(None) == []
|
|
||||||
|
|
||||||
|
|
||||||
def test_list_from_list():
|
|
||||||
assert aslist([1, 2, 3] == [1, 2, 3])
|
|
||||||
|
|
||||||
|
|
||||||
def test_list_from_str():
|
|
||||||
assert aslist("12,13,14") == ["12", "13", "14"]
|
|
||||||
|
|
||||||
|
|
||||||
def test_byteformat():
|
|
||||||
assert byte(500) == "500.00B"
|
|
||||||
assert byte(1024) == "1.00KiB"
|
|
||||||
assert byte(1024, "{:.0f}") == "1KiB"
|
|
||||||
assert byte(1024 + 512) == "1.50KiB"
|
|
||||||
assert byte(1024 * 1024 * 2 + 1024 * 512) == "2.50MiB"
|
|
||||||
assert byte(1024 * 1024 * 1024 * 4 + 1024 * 1024 * 512) == "4.50GiB"
|
|
||||||
assert byte(1024 * 1024 * 1024 * 1024 * 2) == "2048.00GiB"
|
|
||||||
|
|
||||||
|
|
||||||
def test_duration():
|
|
||||||
assert duration(4 * 60 * 60 + 20 * 60) == "04:20:00"
|
|
||||||
assert duration(4 * 60 * 60 + 20 * 60, unit=True) == "04:20:00h"
|
|
||||||
assert duration(4 * 60 * 60 + 20 * 60, compact=True, unit=True) == "04:20h"
|
|
||||||
|
|
||||||
assert duration(20 * 60) == "20:00"
|
|
||||||
assert duration(20 * 60, unit=True) == "20:00m"
|
|
||||||
assert duration(20 * 60, compact=True, unit=True) == "20:00m"
|
|
||||||
|
|
||||||
assert duration(20) == "00:20"
|
|
||||||
assert duration(20, unit=True) == "00:20m"
|
|
||||||
assert duration(20, compact=True, unit=True) == "00:20m"
|
|
||||||
|
|
||||||
assert duration(-1) == "n/a"
|
|
||||||
|
|
||||||
|
|
||||||
def test_seconds():
|
|
||||||
assert seconds(10) == 10
|
|
||||||
assert seconds("10") == 10
|
|
||||||
|
|
||||||
assert seconds("5m") == 300
|
|
||||||
assert seconds("5m20s") == 320
|
|
||||||
|
|
||||||
assert seconds("4h") == 4 * 3600
|
|
||||||
assert seconds("4h5m22s") == 4 * 3600 + 5 * 60 + 22
|
|
||||||
|
|
||||||
assert seconds("4h5m") == 4 * 3600 + 5 * 60
|
|
||||||
|
|
||||||
|
|
||||||
def test_temperature():
|
|
||||||
assert astemperature(10) == "10°C"
|
|
||||||
assert astemperature(10, "metric") == "10°C"
|
|
||||||
assert astemperature(-100, "imperial") == "-100°F"
|
|
||||||
assert astemperature(-100, "kelvin") == "-100°K"
|
|
||||||
|
|
||||||
|
|
||||||
# vim: tabstop=8 expandtab shiftwidth=4 softtabstop=4
|
|
|
@ -1,57 +0,0 @@
|
||||||
import pytest
|
|
||||||
import json
|
|
||||||
|
|
||||||
import util.location
|
|
||||||
|
|
||||||
|
|
||||||
@pytest.fixture
|
|
||||||
def urllib_req(mocker):
|
|
||||||
util.location.reset()
|
|
||||||
return mocker.patch("util.location.urllib.request")
|
|
||||||
|
|
||||||
|
|
||||||
@pytest.fixture
|
|
||||||
def primaryLocation():
|
|
||||||
return {
|
|
||||||
"country": "Middle Earth",
|
|
||||||
"longitude": "10.0",
|
|
||||||
"latitude": "20.5",
|
|
||||||
"ip": "127.0.0.1",
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
@pytest.fixture
|
|
||||||
def secondaryLocation():
|
|
||||||
return {
|
|
||||||
"country_name": "Rivia",
|
|
||||||
"longitude": "-10.0",
|
|
||||||
"latitude": "-23",
|
|
||||||
"ip": "127.0.0.6",
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
def test_primary_provider(urllib_req, primaryLocation):
|
|
||||||
urllib_req.urlopen.return_value.read.return_value = json.dumps(primaryLocation)
|
|
||||||
|
|
||||||
assert util.location.country() == primaryLocation["country"]
|
|
||||||
assert util.location.coordinates() == (
|
|
||||||
primaryLocation["latitude"],
|
|
||||||
primaryLocation["longitude"],
|
|
||||||
)
|
|
||||||
assert util.location.public_ip() == primaryLocation["ip"]
|
|
||||||
|
|
||||||
|
|
||||||
def test_secondary_provider(mocker, urllib_req, secondaryLocation):
|
|
||||||
urlopen = mocker.MagicMock()
|
|
||||||
urlopen.read.return_value = json.dumps(secondaryLocation)
|
|
||||||
urllib_req.urlopen.side_effect = [RuntimeError(), urlopen]
|
|
||||||
|
|
||||||
assert util.location.country() == secondaryLocation["country_name"]
|
|
||||||
assert util.location.coordinates() == (
|
|
||||||
secondaryLocation["latitude"],
|
|
||||||
secondaryLocation["longitude"],
|
|
||||||
)
|
|
||||||
assert util.location.public_ip() == secondaryLocation["ip"]
|
|
||||||
|
|
||||||
|
|
||||||
# vim: tabstop=8 expandtab shiftwidth=4 softtabstop=4
|
|
|
@ -1,48 +0,0 @@
|
||||||
import pytest
|
|
||||||
|
|
||||||
import util.store
|
|
||||||
|
|
||||||
|
|
||||||
@pytest.fixture
|
|
||||||
def emptyStore():
|
|
||||||
return util.store.Store()
|
|
||||||
|
|
||||||
|
|
||||||
@pytest.fixture
|
|
||||||
def store():
|
|
||||||
return util.store.Store()
|
|
||||||
|
|
||||||
|
|
||||||
def test_get_of_unset_key(emptyStore):
|
|
||||||
assert emptyStore.get("any-key") == None
|
|
||||||
assert emptyStore.get("any-key", "default-value") == "default-value"
|
|
||||||
|
|
||||||
|
|
||||||
def test_get_of_set_key(store):
|
|
||||||
store.set("key", "value")
|
|
||||||
assert store.get("key") == "value"
|
|
||||||
|
|
||||||
|
|
||||||
def test_overwrite_set(store):
|
|
||||||
store.set("key", "value 1")
|
|
||||||
store.set("key", "value 2")
|
|
||||||
|
|
||||||
assert store.get("key") == "value 2"
|
|
||||||
|
|
||||||
|
|
||||||
def test_unused_keys(store):
|
|
||||||
store.set("key 1", "value x")
|
|
||||||
store.set("key 2", "value y")
|
|
||||||
|
|
||||||
assert store.unused_keys() == sorted(["key 1", "key 2"])
|
|
||||||
|
|
||||||
store.get("key 2")
|
|
||||||
|
|
||||||
assert store.unused_keys() == ["key 1"]
|
|
||||||
|
|
||||||
store.get("key 1")
|
|
||||||
|
|
||||||
assert store.unused_keys() == []
|
|
||||||
|
|
||||||
|
|
||||||
# vim: tabstop=8 expandtab shiftwidth=4 softtabstop=4
|
|
|
@ -1,3 +1,4 @@
|
||||||
import bumblebee_status.discover
|
import bumblebee_status.discover
|
||||||
|
|
||||||
bumblebee_status.discover.discover()
|
bumblebee_status.discover.discover()
|
||||||
|
|
||||||
|
|
|
@ -1,89 +1,102 @@
|
||||||
import os
|
import os
|
||||||
import unittest
|
import pytest
|
||||||
|
|
||||||
import core.config
|
import core.config
|
||||||
|
|
||||||
|
@pytest.fixture
|
||||||
|
def defaultConfig():
|
||||||
|
return core.config.Config([])
|
||||||
|
|
||||||
class config(unittest.TestCase):
|
def test_module():
|
||||||
def setUp(self):
|
modules = ["module-1", "module-2", "module-3"]
|
||||||
self.someModules = ["b", "x", "a"]
|
|
||||||
self.moreModules = ["this", "module", "here"]
|
|
||||||
self.someTheme = "some-theme"
|
|
||||||
self.someIconset = "some-iconset"
|
|
||||||
self.defaultConfig = core.config.Config([])
|
|
||||||
|
|
||||||
def test_module(self):
|
cfg = core.config.Config(["-m"] + modules)
|
||||||
cfg = core.config.Config(["-m"] + self.someModules)
|
|
||||||
self.assertEqual(self.someModules, cfg.modules())
|
|
||||||
|
|
||||||
def test_module_ordering_maintained(self):
|
assert cfg.modules() == modules
|
||||||
cfg = core.config.Config(["-m"] + self.someModules + ["-m"] + self.moreModules)
|
|
||||||
self.assertEqual(self.someModules + self.moreModules, cfg.modules())
|
|
||||||
|
|
||||||
def test_default_interval(self):
|
def test_module_ordering_maintained():
|
||||||
self.assertEqual(1, self.defaultConfig.interval())
|
modules = ["module-1", "module-5", "module-7"]
|
||||||
|
more_modules = ["module-0", "module-2", "aaa"]
|
||||||
|
|
||||||
def test_interval(self):
|
cfg = core.config.Config(["-m"] + modules + ["-m"] + more_modules)
|
||||||
cfg = core.config.Config(["-p", "interval=4"])
|
|
||||||
self.assertEqual(4, cfg.interval())
|
|
||||||
|
|
||||||
def test_float_interval(self):
|
assert cfg.modules() == modules + more_modules
|
||||||
cfg = core.config.Config(["-p", "interval=0.5"])
|
|
||||||
self.assertEqual(0.5, cfg.interval())
|
|
||||||
|
|
||||||
def test_default_theme(self):
|
def test_default_interval(defaultConfig):
|
||||||
self.assertEqual("default", self.defaultConfig.theme())
|
assert defaultConfig.interval() == 1
|
||||||
|
|
||||||
def test_theme(self):
|
def test_interval():
|
||||||
cfg = core.config.Config(["-t", self.someTheme])
|
interval = 4
|
||||||
self.assertEqual(self.someTheme, cfg.theme())
|
cfg = core.config.Config(["-p", "interval={}".format(interval)])
|
||||||
|
|
||||||
def test_default_iconset(self):
|
assert cfg.interval() == interval
|
||||||
self.assertEqual("auto", self.defaultConfig.iconset())
|
|
||||||
|
|
||||||
def test_iconset(self):
|
def test_floating_interval():
|
||||||
cfg = core.config.Config(["-i", self.someIconset])
|
interval = 4.5
|
||||||
self.assertEqual(self.someIconset, cfg.iconset())
|
cfg = core.config.Config(["-p", "interval={}".format(interval)])
|
||||||
|
|
||||||
def test_right_to_left(self):
|
assert cfg.interval() == interval
|
||||||
cfg = core.config.Config(["-r"])
|
|
||||||
self.assertTrue(cfg.reverse())
|
|
||||||
self.assertFalse(self.defaultConfig.reverse())
|
|
||||||
|
|
||||||
def test_logfile(self):
|
def test_default_theme(defaultConfig):
|
||||||
cfg = core.config.Config(["-f", "my-custom-logfile"])
|
assert defaultConfig.theme() == "default"
|
||||||
self.assertEqual(None, self.defaultConfig.logfile())
|
|
||||||
self.assertEqual("my-custom-logfile", cfg.logfile())
|
|
||||||
|
|
||||||
def test_all_modules(self):
|
def test_theme():
|
||||||
modules = core.config.all_modules()
|
theme_name = "sample-theme"
|
||||||
self.assertGreater(len(modules), 0)
|
cfg = core.config.Config(["-t", theme_name])
|
||||||
for module in modules:
|
assert cfg.theme() == theme_name
|
||||||
pyname = "{}.py".format(module)
|
|
||||||
base = os.path.abspath(
|
def test_default_iconset(defaultConfig):
|
||||||
os.path.join(
|
assert defaultConfig.iconset() == "auto"
|
||||||
os.path.dirname(os.path.realpath(__file__)),
|
|
||||||
"..",
|
def test_iconset():
|
||||||
"..",
|
iconset_name = "random-iconset"
|
||||||
"bumblebee_status",
|
cfg = core.config.Config(["-i", iconset_name])
|
||||||
"modules",
|
assert cfg.iconset() == iconset_name
|
||||||
)
|
|
||||||
)
|
def test_reverse(defaultConfig):
|
||||||
self.assertTrue(
|
assert defaultConfig.reverse() == False
|
||||||
os.path.exists(os.path.join(base, "contrib", pyname))
|
|
||||||
or os.path.exists(os.path.join(base, "core", pyname))
|
cfg = core.config.Config(["-r"])
|
||||||
|
|
||||||
|
assert cfg.reverse() == True
|
||||||
|
|
||||||
|
def test_logfile(defaultConfig):
|
||||||
|
assert defaultConfig.logfile() is None
|
||||||
|
|
||||||
|
logfile = "some-random-logfile"
|
||||||
|
cfg = core.config.Config(["-f", logfile])
|
||||||
|
assert cfg.logfile() == logfile
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
def test_all_modules():
|
||||||
|
modules = core.config.all_modules()
|
||||||
|
assert len(modules) > 0
|
||||||
|
|
||||||
|
for module in modules:
|
||||||
|
pyname = "{}.py".format(module)
|
||||||
|
base = os.path.abspath(
|
||||||
|
os.path.join(
|
||||||
|
os.path.dirname(os.path.realpath(__file__)),
|
||||||
|
"..",
|
||||||
|
"..",
|
||||||
|
"bumblebee_status",
|
||||||
|
"modules",
|
||||||
)
|
)
|
||||||
|
)
|
||||||
|
assert os.path.exists(os.path.join(base, "contrib", pyname)) \
|
||||||
|
or os.path.exists(os.path.join(base, "core", pyname))
|
||||||
|
|
||||||
def test_list_output(self):
|
def test_list_output(mocker):
|
||||||
with unittest.mock.patch("core.config.sys") as sys:
|
mocker.patch("core.config.sys")
|
||||||
cfg = core.config.Config(["-l", "themes"])
|
cfg = core.config.Config(["-l", "themes"])
|
||||||
cfg = core.config.Config(["-l", "modules"])
|
cfg = core.config.Config(["-l", "modules"])
|
||||||
cfg = core.config.Config(["-l", "modules-rst"])
|
cfg = core.config.Config(["-l", "modules-rst"])
|
||||||
# TODO: think of some plausibility testing here
|
|
||||||
|
|
||||||
def test_missing_parameter(self):
|
|
||||||
cfg = core.config.Config(["-p", "test.key"])
|
|
||||||
self.assertEqual("no-value-set", cfg.get("test.key", "no-value-set"))
|
|
||||||
|
|
||||||
|
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
|
# vim: tabstop=8 expandtab shiftwidth=4 softtabstop=4
|
||||||
|
|
|
@ -1,99 +1,103 @@
|
||||||
import unittest
|
import pytest
|
||||||
|
|
||||||
import core.decorators
|
import core.decorators
|
||||||
import core.widget
|
import core.widget
|
||||||
import core.module
|
import core.module
|
||||||
import core.config
|
import core.config
|
||||||
|
|
||||||
|
@pytest.fixture
|
||||||
|
def module():
|
||||||
|
class TestModule(core.module.Module):
|
||||||
|
@core.decorators.never
|
||||||
|
def __init__(self, config=None, theme=None):
|
||||||
|
config = core.config.Config([])
|
||||||
|
super().__init__(config, theme, core.widget.Widget(self.get))
|
||||||
|
self.text = ""
|
||||||
|
|
||||||
class TestModule(core.module.Module):
|
@core.decorators.scrollable
|
||||||
@core.decorators.never
|
def get(self, widget):
|
||||||
def __init__(self, config=None, theme=None):
|
return self.text
|
||||||
config = core.config.Config([])
|
module = TestModule()
|
||||||
super().__init__(config, theme, core.widget.Widget(self.get))
|
module.set("scrolling.width", 10)
|
||||||
self.text = ""
|
return module
|
||||||
|
|
||||||
@core.decorators.scrollable
|
def test_never(module):
|
||||||
def get(self, widget):
|
assert module.parameter("interval") == "never"
|
||||||
return self.text
|
|
||||||
|
|
||||||
|
def test_no_text(module):
|
||||||
|
assert module.text == ""
|
||||||
|
assert module.get(module.widget()) == ""
|
||||||
|
|
||||||
class config(unittest.TestCase):
|
def test_smaller(module):
|
||||||
def setUp(self):
|
module.text = "test"
|
||||||
self.module = TestModule()
|
assert module.parameter("scrolling.width") > len(module.text)
|
||||||
self.widget = self.module.widget()
|
assert module.get(module.widget()) == module.text
|
||||||
self.width = 10
|
|
||||||
self.module.set("scrolling.width", self.width)
|
|
||||||
|
|
||||||
def test_never(self):
|
def test_bigger(module):
|
||||||
self.module = TestModule()
|
module.text = "this is a really really long sample text"
|
||||||
self.assertEqual("never", self.module.parameter("interval"))
|
maxwidth = module.parameter("scrolling.width")
|
||||||
|
assert maxwidth < len(module.text)
|
||||||
|
assert module.get(module.widget()) == module.text[:maxwidth]
|
||||||
|
|
||||||
def test_no_text(self):
|
def test_bounce(module):
|
||||||
self.assertEqual("", self.module.text)
|
module.text = "abcd"
|
||||||
self.assertEqual("", self.module.get(self.widget))
|
module.set("scrolling.width", 2)
|
||||||
|
assert module.get(module.widget()) == "ab"
|
||||||
|
assert module.get(module.widget()) == "bc"
|
||||||
|
assert module.get(module.widget()) == "cd"
|
||||||
|
assert module.get(module.widget()) == "bc"
|
||||||
|
assert module.get(module.widget()) == "ab"
|
||||||
|
assert module.get(module.widget()) == "bc"
|
||||||
|
assert module.get(module.widget()) == "cd"
|
||||||
|
assert module.get(module.widget()) == "bc"
|
||||||
|
assert module.get(module.widget()) == "ab"
|
||||||
|
|
||||||
def test_smaller(self):
|
def test_nobounce(module):
|
||||||
self.module.text = "test"
|
module.set("scrolling.bounce", False)
|
||||||
self.assertLess(len(self.module.text), self.width)
|
module.set("scrolling.width", 2)
|
||||||
self.assertEqual("test", self.module.get(self.widget))
|
module.text = "abcd"
|
||||||
|
|
||||||
def test_bigger(self):
|
assert module.get(module.widget()) == "ab"
|
||||||
self.module.text = "abcdefghijklmnopqrst"
|
assert module.get(module.widget()) == "bc"
|
||||||
self.assertGreater(len(self.module.text), self.width)
|
assert module.get(module.widget()) == "cd"
|
||||||
self.assertEqual(self.module.text[: self.width], self.module.get(self.widget))
|
assert module.get(module.widget()) == "ab"
|
||||||
|
assert module.get(module.widget()) == "bc"
|
||||||
|
assert module.get(module.widget()) == "cd"
|
||||||
|
|
||||||
def test_bounce(self):
|
def test_completely_changed_data(module):
|
||||||
self.module.text = "abcd"
|
module.text = "abcd"
|
||||||
self.module.set("scrolling.width", 2)
|
module.set("scrolling.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))
|
|
||||||
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):
|
assert module.get(module.widget()) == "ab"
|
||||||
self.module.set("scrolling.bounce", False)
|
assert module.get(module.widget()) == "bc"
|
||||||
self.module.text = "abcd"
|
|
||||||
self.module.set("scrolling.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))
|
|
||||||
self.assertEqual("bc", self.module.get(self.widget))
|
|
||||||
self.assertEqual("cd", self.module.get(self.widget))
|
|
||||||
|
|
||||||
def test_changed_data(self):
|
module.text = "wxyz"
|
||||||
self.module.text = "abcd"
|
assert module.get(module.widget()) == "wx"
|
||||||
self.module.set("scrolling.width", 2)
|
assert module.get(module.widget()) == "xy"
|
||||||
self.assertEqual("ab", self.module.get(self.widget))
|
|
||||||
self.assertEqual("bc", self.module.get(self.widget))
|
|
||||||
self.module.text = "wxyz"
|
|
||||||
self.assertEqual("wx", self.module.get(self.widget))
|
|
||||||
|
|
||||||
def test_minimum_changed_data(self):
|
def test_slightly_changed_data(module):
|
||||||
self.module.text = "this is a sample song (0:00)"
|
module.text = "this is a sample song (0:00)"
|
||||||
self.module.set("scrolling.width", 10)
|
module.set("scrolling.width", 10)
|
||||||
self.assertEqual(self.module.text[0:10], self.module.get(self.widget))
|
|
||||||
self.module.text = "this is a sample song (0:01)"
|
|
||||||
self.assertEqual(self.module.text[1:11], self.module.get(self.widget))
|
|
||||||
self.module.text = "this is a sample song (0:12)"
|
|
||||||
self.assertEqual(self.module.text[2:12], self.module.get(self.widget))
|
|
||||||
self.module.text = "this is a different song (0:12)"
|
|
||||||
self.assertEqual(self.module.text[0:10], self.module.get(self.widget))
|
|
||||||
|
|
||||||
def test_n_plus_one(self):
|
assert module.get(module.widget()) == module.text[0:10]
|
||||||
self.module.text = "10 letters"
|
module.text = "this is a sample song (0:01)"
|
||||||
self.module.set("scrolling.width", 9)
|
assert module.get(module.widget()) == module.text[1:11]
|
||||||
self.assertEqual(self.module.text[0:9], self.module.get(self.widget))
|
module.text = "this is a sample song (0:02)"
|
||||||
self.assertEqual(self.module.text[1:10], self.module.get(self.widget))
|
assert module.get(module.widget()) == module.text[2:12]
|
||||||
self.assertEqual(self.module.text[0:9], self.module.get(self.widget))
|
module.text = "this is a sample song (0:13)"
|
||||||
self.assertEqual(self.module.text[1:10], self.module.get(self.widget))
|
assert module.get(module.widget()) == module.text[3:13]
|
||||||
self.assertEqual(self.module.text[0:9], self.module.get(self.widget))
|
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)
|
||||||
|
|
||||||
|
assert module.get(module.widget()) == module.text[0:9]
|
||||||
|
assert module.get(module.widget()) == module.text[1:10]
|
||||||
|
assert module.get(module.widget()) == module.text[0:9]
|
||||||
|
assert module.get(module.widget()) == module.text[1:10]
|
||||||
|
assert module.get(module.widget()) == module.text[0:9]
|
||||||
|
|
||||||
|
|
||||||
# vim: tabstop=8 expandtab shiftwidth=4 softtabstop=4
|
# vim: tabstop=8 expandtab shiftwidth=4 softtabstop=4
|
||||||
|
|
|
@ -1,62 +1,73 @@
|
||||||
import unittest
|
import pytest
|
||||||
|
|
||||||
import core.event
|
import core.event
|
||||||
|
|
||||||
|
@pytest.fixture
|
||||||
|
def someEvent():
|
||||||
|
class Event():
|
||||||
|
def __init__(self):
|
||||||
|
core.event.clear()
|
||||||
|
self.id = "some event"
|
||||||
|
self.called = 0
|
||||||
|
self.call_args = []
|
||||||
|
self.call_kwargs = []
|
||||||
|
|
||||||
class event(unittest.TestCase):
|
def callback(self, *args, **kwargs):
|
||||||
def setUp(self):
|
self.called += 1
|
||||||
self.someEvent = "event"
|
if args:
|
||||||
self.called = {}
|
self.call_args.append(list(args))
|
||||||
self.params = []
|
if kwargs:
|
||||||
core.event.clear()
|
self.call_kwargs.append(kwargs)
|
||||||
|
|
||||||
def callback1(self):
|
return Event()
|
||||||
self.called["callback1"] = True
|
|
||||||
|
|
||||||
def callback2(self):
|
|
||||||
self.called["callback2"] = True
|
|
||||||
|
|
||||||
def callback_args(self, val1, val2):
|
def test_simple_callback(someEvent):
|
||||||
self.called["callback_args"] = True
|
assert someEvent.called == 0
|
||||||
self.params = [val1, val2]
|
|
||||||
|
|
||||||
def callback_kwargs(self, val1, val2, key1=None, key2=None):
|
core.event.register(someEvent.id, someEvent.callback)
|
||||||
self.called["callback_kwargs"] = True
|
core.event.register(someEvent.id, someEvent.callback)
|
||||||
self.params = [val1, val2, key1, key2]
|
|
||||||
|
|
||||||
def test_simple_callback(self):
|
core.event.trigger(someEvent.id)
|
||||||
core.event.register(self.someEvent, self.callback1)
|
|
||||||
core.event.register(self.someEvent, self.callback2)
|
|
||||||
|
|
||||||
core.event.trigger(self.someEvent)
|
assert someEvent.called == 2
|
||||||
|
|
||||||
self.assertEqual(2, len(self.called.keys()))
|
def test_args_callback(someEvent):
|
||||||
|
core.event.register(someEvent.id, someEvent.callback, "a", "b")
|
||||||
|
core.event.trigger(someEvent.id)
|
||||||
|
|
||||||
def test_arg_callback(self):
|
assert someEvent.called == 1
|
||||||
core.event.register(self.someEvent, self.callback_args, "a", "b")
|
assert len(someEvent.call_args) == 1
|
||||||
core.event.trigger(self.someEvent)
|
assert someEvent.call_args[0] == ["a", "b"]
|
||||||
self.assertEqual(1, len(self.called.keys()))
|
|
||||||
self.assertEqual(["a", "b"], self.params)
|
|
||||||
|
|
||||||
def test_kwargs_callback(self):
|
def test_kwargs_callback(someEvent):
|
||||||
core.event.register(
|
core.event.register(
|
||||||
self.someEvent, self.callback_kwargs, "a", "b", key1="test", key2="x"
|
someEvent.id, someEvent.callback, "a", "b", key1="test", key2="another"
|
||||||
)
|
)
|
||||||
core.event.trigger(self.someEvent)
|
core.event.trigger(someEvent.id)
|
||||||
self.assertEqual(1, len(self.called.keys()))
|
|
||||||
self.assertEqual(["a", "b", "test", "x"], self.params)
|
|
||||||
|
|
||||||
def test_arg_trigger(self):
|
assert someEvent.called == 1
|
||||||
core.event.register(self.someEvent, self.callback_args)
|
assert len(someEvent.call_args) == 1
|
||||||
core.event.trigger(self.someEvent, "a", "b")
|
assert someEvent.call_args[0] == ["a", "b"]
|
||||||
self.assertEqual(1, len(self.called.keys()))
|
assert len(someEvent.call_kwargs) == 1
|
||||||
self.assertEqual(["a", "b"], self.params)
|
assert someEvent.call_kwargs[0] == { "key1": "test", "key2": "another" }
|
||||||
|
|
||||||
def test_kwargs_trigger(self):
|
def test_arg_trigger(someEvent):
|
||||||
core.event.register(self.someEvent, self.callback_kwargs)
|
core.event.register(someEvent.id, someEvent.callback)
|
||||||
core.event.trigger(self.someEvent, "a", "b", key1="test", key2="x")
|
core.event.trigger(someEvent.id, "a", "b")
|
||||||
self.assertEqual(1, len(self.called.keys()))
|
|
||||||
self.assertEqual(["a", "b", "test", "x"], self.params)
|
|
||||||
|
|
||||||
|
assert someEvent.called == 1
|
||||||
|
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")
|
||||||
|
|
||||||
|
assert someEvent.called == 1
|
||||||
|
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" }
|
||||||
|
|
||||||
# vim: tabstop=8 expandtab shiftwidth=4 softtabstop=4
|
# vim: tabstop=8 expandtab shiftwidth=4 softtabstop=4
|
||||||
|
|
|
@ -1,90 +1,104 @@
|
||||||
import unittest
|
import pytest
|
||||||
|
|
||||||
import core.input
|
import core.input
|
||||||
|
|
||||||
|
|
||||||
class config(unittest.TestCase):
|
@pytest.fixture
|
||||||
def setUp(self):
|
def obj():
|
||||||
self.inputObject = core.input.Object()
|
return core.input.Object()
|
||||||
self.anotherObject = core.input.Object()
|
|
||||||
self.someEvent = {
|
|
||||||
"button": core.input.LEFT_MOUSE,
|
|
||||||
"instance": self.inputObject.id,
|
|
||||||
}
|
|
||||||
self.anotherEvent = {
|
|
||||||
"button": core.input.RIGHT_MOUSE,
|
|
||||||
"instance": self.inputObject.id,
|
|
||||||
}
|
|
||||||
self.callback = unittest.mock.MagicMock()
|
|
||||||
self.callback2 = unittest.mock.MagicMock()
|
|
||||||
self.someCommand = "some sample command"
|
|
||||||
|
|
||||||
def test_callable_gets_called(self):
|
|
||||||
core.input.register(self.inputObject, self.someEvent["button"], self.callback)
|
|
||||||
core.input.trigger(self.someEvent)
|
|
||||||
self.callback.assert_called_once_with(self.someEvent)
|
|
||||||
|
|
||||||
def test_nonexistent_callback(self):
|
@pytest.fixture
|
||||||
core.input.register(self.inputObject, self.someEvent["button"], self.callback)
|
def obj2():
|
||||||
core.input.trigger(self.anotherEvent)
|
return core.input.Object()
|
||||||
self.callback.assert_not_called()
|
|
||||||
|
|
||||||
def test_different_events(self):
|
|
||||||
core.input.register(self.inputObject, self.someEvent["button"], self.callback)
|
|
||||||
core.input.register(
|
|
||||||
self.inputObject, self.anotherEvent["button"], self.callback
|
|
||||||
)
|
|
||||||
core.input.register(
|
|
||||||
self.anotherObject, self.someEvent["button"], self.callback2
|
|
||||||
)
|
|
||||||
core.input.register(
|
|
||||||
self.anotherObject, self.anotherEvent["button"], self.callback2
|
|
||||||
)
|
|
||||||
core.input.trigger(self.someEvent)
|
|
||||||
core.input.trigger(self.anotherEvent)
|
|
||||||
self.callback.assert_any_call(self.someEvent)
|
|
||||||
self.callback.assert_any_call(self.anotherEvent)
|
|
||||||
self.callback2.assert_not_called()
|
|
||||||
|
|
||||||
def test_multiple_registrations(self):
|
@pytest.fixture
|
||||||
core.input.register(self.inputObject, self.someEvent["button"], self.callback)
|
def cb(mocker):
|
||||||
core.input.register(self.inputObject, self.someEvent["button"], self.callback2)
|
return mocker.MagicMock()
|
||||||
core.input.trigger(self.someEvent)
|
|
||||||
self.callback.assert_called_once_with(self.someEvent)
|
|
||||||
self.callback2.assert_called_once_with(self.someEvent)
|
|
||||||
|
|
||||||
def test_event_names(self):
|
|
||||||
self.assertEqual(core.input.button_name(core.input.LEFT_MOUSE), "left-mouse")
|
|
||||||
self.assertEqual(core.input.button_name(core.input.RIGHT_MOUSE), "right-mouse")
|
|
||||||
self.assertEqual(
|
|
||||||
core.input.button_name(core.input.MIDDLE_MOUSE), "middle-mouse"
|
|
||||||
)
|
|
||||||
self.assertEqual(core.input.button_name(core.input.WHEEL_UP), "wheel-up")
|
|
||||||
self.assertEqual(core.input.button_name(core.input.WHEEL_DOWN), "wheel-down")
|
|
||||||
self.assertEqual(core.input.button_name(12345), "n/a")
|
|
||||||
|
|
||||||
def test_non_callable_callback(self):
|
@pytest.fixture
|
||||||
with unittest.mock.patch("core.input.util.cli") as cli:
|
def cb2(mocker):
|
||||||
cli.execute.return_value = ""
|
return mocker.MagicMock()
|
||||||
core.input.register(
|
|
||||||
self.inputObject, self.someEvent["button"], self.someCommand
|
|
||||||
)
|
|
||||||
core.input.trigger(self.someEvent)
|
|
||||||
cli.execute.assert_called_once_with(
|
|
||||||
self.someCommand, wait=False, shell=True
|
|
||||||
)
|
|
||||||
|
|
||||||
def test_non_existent_callback(self):
|
|
||||||
with unittest.mock.patch("core.input.util.cli") as cli:
|
def event(input_object):
|
||||||
cli.execute.return_value = ""
|
return {"button": core.input.LEFT_MOUSE, "instance": input_object.id}
|
||||||
cli.execute.side_effect = RuntimeError("some-error")
|
|
||||||
core.input.register(
|
|
||||||
self.inputObject, self.someEvent["button"], self.someCommand
|
def event2(input_object):
|
||||||
)
|
return {"button": core.input.RIGHT_MOUSE, "instance": input_object.id}
|
||||||
try:
|
|
||||||
core.input.trigger(self.someEvent)
|
|
||||||
except Exception:
|
def test_callable_gets_called(obj, cb):
|
||||||
self.fail("input module propagated exception")
|
core.input.register(obj, event(obj)["button"], cb)
|
||||||
|
core.input.trigger(event(obj))
|
||||||
|
|
||||||
|
cb.assert_called_once_with(event(obj))
|
||||||
|
|
||||||
|
|
||||||
|
def test_nonexistent_callback(obj, obj2, cb):
|
||||||
|
core.input.register(obj, event(obj)["button"], cb)
|
||||||
|
core.input.trigger(event(obj2))
|
||||||
|
|
||||||
|
cb.assert_not_called()
|
||||||
|
|
||||||
|
|
||||||
|
def test_different_events(obj, obj2, cb, cb2):
|
||||||
|
core.input.register(obj, event(obj)["button"], cb)
|
||||||
|
core.input.register(obj, event2(obj)["button"], cb)
|
||||||
|
core.input.register(obj2, event(obj)["button"], cb2)
|
||||||
|
core.input.register(obj2, event2(obj)["button"], cb2)
|
||||||
|
|
||||||
|
core.input.trigger(event(obj))
|
||||||
|
core.input.trigger(event2(obj))
|
||||||
|
|
||||||
|
cb.assert_any_call(event(obj))
|
||||||
|
cb.assert_any_call(event2(obj))
|
||||||
|
cb2.assert_not_called()
|
||||||
|
|
||||||
|
|
||||||
|
def test_multiple_registrations(obj, cb, cb2):
|
||||||
|
core.input.register(obj, event(obj)["button"], cb)
|
||||||
|
core.input.register(obj, event(obj)["button"], cb2)
|
||||||
|
|
||||||
|
core.input.trigger(event(obj))
|
||||||
|
|
||||||
|
cb.assert_called_once_with(event(obj))
|
||||||
|
cb2.assert_called_once_with(event(obj))
|
||||||
|
|
||||||
|
|
||||||
|
def test_event_names():
|
||||||
|
assert core.input.button_name(core.input.LEFT_MOUSE) == "left-mouse"
|
||||||
|
assert core.input.button_name(core.input.RIGHT_MOUSE) == "right-mouse"
|
||||||
|
assert core.input.button_name(core.input.MIDDLE_MOUSE) == "middle-mouse"
|
||||||
|
assert core.input.button_name(core.input.WHEEL_UP) == "wheel-up"
|
||||||
|
assert core.input.button_name(core.input.WHEEL_DOWN) == "wheel-down"
|
||||||
|
assert core.input.button_name(12345) == "n/a"
|
||||||
|
|
||||||
|
|
||||||
|
def test_non_callable_callback(mocker, obj):
|
||||||
|
cli = mocker.patch("core.input.util.cli")
|
||||||
|
cli.execute.return_value = ""
|
||||||
|
|
||||||
|
core.input.register(obj, event(obj)["button"], "sample-command")
|
||||||
|
|
||||||
|
core.input.trigger(event(obj))
|
||||||
|
|
||||||
|
cli.execute.assert_called_once_with("sample-command", wait=False, shell=True)
|
||||||
|
|
||||||
|
|
||||||
|
def test_non_existent_callback(mocker, obj):
|
||||||
|
cli = mocker.patch("core.input.util.cli")
|
||||||
|
cli.execute.return_value = ""
|
||||||
|
cli.execute.side_effect = RuntimeError("some-error")
|
||||||
|
|
||||||
|
core.input.register(obj, event(obj)["button"], "sample-command")
|
||||||
|
|
||||||
|
core.input.trigger(event(obj))
|
||||||
|
|
||||||
|
cli.execute.assert_called_once_with("sample-command", wait=False, shell=True)
|
||||||
|
|
||||||
|
|
||||||
# vim: tabstop=8 expandtab shiftwidth=4 softtabstop=4
|
# vim: tabstop=8 expandtab shiftwidth=4 softtabstop=4
|
||||||
|
|
|
@ -1,4 +1,4 @@
|
||||||
import unittest
|
import pytest
|
||||||
|
|
||||||
import sys
|
import sys
|
||||||
import shlex
|
import shlex
|
||||||
|
@ -9,175 +9,169 @@ import core.config
|
||||||
import core.input
|
import core.input
|
||||||
|
|
||||||
|
|
||||||
class TestModule(core.module.Module):
|
@pytest.fixture(autouse=True)
|
||||||
|
def clear_events():
|
||||||
|
core.event.clear()
|
||||||
|
|
||||||
|
|
||||||
|
@pytest.fixture
|
||||||
|
def empty_config():
|
||||||
|
return core.config.Config([])
|
||||||
|
|
||||||
|
|
||||||
|
@pytest.fixture
|
||||||
|
def widget_a():
|
||||||
|
return core.widget.Widget("randomwWidget content", name="A")
|
||||||
|
|
||||||
|
|
||||||
|
@pytest.fixture
|
||||||
|
def widget_b():
|
||||||
|
return core.widget.Widget("another randomwWidget content", name="B")
|
||||||
|
|
||||||
|
|
||||||
|
class SampleModule(core.module.Module):
|
||||||
def update(self):
|
def update(self):
|
||||||
if self.fail:
|
if self.fail:
|
||||||
raise Exception(self.error)
|
raise Exception(self.error)
|
||||||
pass
|
pass
|
||||||
|
|
||||||
|
|
||||||
class module(unittest.TestCase):
|
def test_loadinvalid_module(mocker):
|
||||||
def setUp(self):
|
config = mocker.MagicMock()
|
||||||
core.event.clear()
|
module = core.module.load(module_name="i-do-not-exist", config=config)
|
||||||
self.invalidModuleName = "invalid-module-name"
|
assert module.__class__.__module__ == "core.module"
|
||||||
self.validModuleName = "test"
|
assert module.__class__.__name__ == "Error"
|
||||||
self.someWidget = core.widget.Widget("randomeWidgetContent", name="A")
|
|
||||||
self.anotherWidget = core.widget.Widget("more Widget content", name="B")
|
|
||||||
self.unusedWidgetName = "C"
|
|
||||||
|
|
||||||
def test_loadinvalid_module(self):
|
|
||||||
config = unittest.mock.MagicMock()
|
|
||||||
module = core.module.load(module_name=self.invalidModuleName, config=config)
|
|
||||||
self.assertEqual(
|
|
||||||
"core.module", module.__class__.__module__, "module must be a module object"
|
|
||||||
)
|
|
||||||
self.assertEqual(
|
|
||||||
"Error",
|
|
||||||
module.__class__.__name__,
|
|
||||||
"an invalid module must be a core.module.Error",
|
|
||||||
)
|
|
||||||
|
|
||||||
@unittest.skipIf(
|
@pytest.mark.skipif(
|
||||||
sys.version_info.major == 3 and sys.version_info.minor in [4, 5],
|
sys.version_info.major == 3 and sys.version_info.minor in [4, 5],
|
||||||
"importlib error reporting in Python 3.{4,5} different",
|
reason="importlib error reporting in Python 3.{4,5} different",
|
||||||
|
)
|
||||||
|
def test_importerror(mocker):
|
||||||
|
importlib = mocker.patch("core.module.importlib")
|
||||||
|
importlib.import_module.side_effect = ImportError("some-error")
|
||||||
|
config = mocker.MagicMock()
|
||||||
|
|
||||||
|
module = core.module.load(module_name="test", config=config)
|
||||||
|
|
||||||
|
assert module.__class__.__name__ == "Error"
|
||||||
|
assert module.widget().full_text() == "test: some-error"
|
||||||
|
|
||||||
|
|
||||||
|
def test_loadvalid_module():
|
||||||
|
module = core.module.load(module_name="test")
|
||||||
|
assert module.__class__.__module__ == "modules.core.test"
|
||||||
|
assert module.__class__.__name__ == "Module"
|
||||||
|
assert module.state(None) == []
|
||||||
|
|
||||||
|
|
||||||
|
def test_empty_widgets():
|
||||||
|
module = core.module.Module(widgets=[])
|
||||||
|
assert module.widgets() == []
|
||||||
|
|
||||||
|
|
||||||
|
def test_error_widget():
|
||||||
|
cfg = core.config.Config(shlex.split("-p test_module.foo=5"))
|
||||||
|
module = core.module.Error("test-mod", "xyz", config=cfg)
|
||||||
|
full_text = module.full_text(module.widget())
|
||||||
|
|
||||||
|
assert module.state(None) == ["critical"]
|
||||||
|
assert "test-mod" in full_text
|
||||||
|
assert "xyz" in full_text
|
||||||
|
|
||||||
|
|
||||||
|
def test_single_widget(widget_a):
|
||||||
|
module = core.module.Module(widgets=widget_a)
|
||||||
|
assert module.widgets() == [widget_a]
|
||||||
|
|
||||||
|
|
||||||
|
def test_widget_list(widget_a, widget_b):
|
||||||
|
module = core.module.Module(widgets=[widget_a, widget_b])
|
||||||
|
assert module.widgets() == [widget_a, widget_b]
|
||||||
|
|
||||||
|
|
||||||
|
def test_module_Name():
|
||||||
|
module = SampleModule()
|
||||||
|
assert module.name == "test_module"
|
||||||
|
assert module.module_name == "test_module"
|
||||||
|
|
||||||
|
|
||||||
|
def testvalid_parameter():
|
||||||
|
cfg = core.config.Config(shlex.split("-p test_module.foo=5"))
|
||||||
|
module = SampleModule(config=cfg)
|
||||||
|
assert module.parameter("foo") == "5"
|
||||||
|
|
||||||
|
|
||||||
|
def test_default_parameter(empty_config):
|
||||||
|
module = SampleModule(config=empty_config)
|
||||||
|
assert module.parameter("foo", "default") == "default"
|
||||||
|
|
||||||
|
|
||||||
|
def test_default_is_none(empty_config):
|
||||||
|
module = SampleModule(config=empty_config)
|
||||||
|
assert module.parameter("foo") == None
|
||||||
|
|
||||||
|
|
||||||
|
def test_error_widget(empty_config):
|
||||||
|
module = SampleModule(config=empty_config)
|
||||||
|
module.fail = True
|
||||||
|
module.error = "!!"
|
||||||
|
module.update_wrapper()
|
||||||
|
assert len(module.widgets()) == 1
|
||||||
|
assert module.widget().full_text() == "error: !!"
|
||||||
|
|
||||||
|
|
||||||
|
def test_get_widget_by_name(empty_config, widget_a, widget_b):
|
||||||
|
module = SampleModule(config=empty_config, widgets=[widget_a, widget_b])
|
||||||
|
|
||||||
|
assert module.widget(widget_a.name) == widget_a
|
||||||
|
assert module.widget(widget_b.name) == widget_b
|
||||||
|
assert module.widget("i-do-not-exist") == None
|
||||||
|
assert module.widget() == widget_a
|
||||||
|
|
||||||
|
|
||||||
|
def test_default_thresholds(empty_config, widget_a, widget_b):
|
||||||
|
module = SampleModule(config=empty_config, widgets=[widget_a, widget_b])
|
||||||
|
|
||||||
|
assert module.threshold_state(100, 80, 99) == "critical"
|
||||||
|
assert module.threshold_state(100, 80, 100) == "warning"
|
||||||
|
assert module.threshold_state(81, 80, 100) == "warning"
|
||||||
|
assert module.threshold_state(80, 80, 100) == None
|
||||||
|
assert module.threshold_state(10, 80, 100) == None
|
||||||
|
|
||||||
|
|
||||||
|
def test_configured_callbacks(mocker, empty_config, widget_a, widget_b):
|
||||||
|
module = SampleModule(config=empty_config, widgets=[widget_a, widget_b])
|
||||||
|
|
||||||
|
cmd = "sample-tool arg1 arg2 arg3"
|
||||||
|
module.set("left-click", cmd)
|
||||||
|
module.register_callbacks()
|
||||||
|
|
||||||
|
cli = mocker.patch("core.input.util.cli")
|
||||||
|
cli.execute.return_value = ""
|
||||||
|
core.input.trigger(
|
||||||
|
{"button": core.input.LEFT_MOUSE, "instance": module.id,}
|
||||||
)
|
)
|
||||||
def test_importerror(self):
|
|
||||||
with unittest.mock.patch("core.module.importlib") as importlib:
|
|
||||||
importlib.import_module.side_effect = ImportError("some-error")
|
|
||||||
|
|
||||||
config = unittest.mock.MagicMock()
|
cli.execute.assert_called_once_with(cmd, wait=False, shell=True)
|
||||||
module = core.module.load(module_name=self.validModuleName, config=config)
|
|
||||||
module.widget().full_text()
|
|
||||||
self.assertEqual(
|
|
||||||
"Error",
|
|
||||||
module.__class__.__name__,
|
|
||||||
"an invalid module must be a core.module.Error",
|
|
||||||
)
|
|
||||||
self.assertEqual(module.widget().full_text(), "test: some-error")
|
|
||||||
|
|
||||||
def test_loadvalid_module(self):
|
|
||||||
module = core.module.load(module_name=self.validModuleName)
|
|
||||||
self.assertEqual(
|
|
||||||
"modules.core.{}".format(self.validModuleName),
|
|
||||||
module.__class__.__module__,
|
|
||||||
"module must be a modules.core.<name> object",
|
|
||||||
)
|
|
||||||
self.assertEqual(
|
|
||||||
"Module",
|
|
||||||
module.__class__.__name__,
|
|
||||||
"a valid module must have a Module class",
|
|
||||||
)
|
|
||||||
self.assertEqual([], module.state(None), "default state of module is empty")
|
|
||||||
|
|
||||||
def test_empty_widgets(self):
|
def test_configured_callbacks_with_parameters(mocker, empty_config, widget_a):
|
||||||
module = core.module.Module(widgets=[])
|
module = SampleModule(config=empty_config, widgets=[widget_a])
|
||||||
self.assertEqual([], module.widgets())
|
|
||||||
|
|
||||||
def test_error_widget(self):
|
cmd = "sample-tool {instance} {name}"
|
||||||
cfg = core.config.Config(shlex.split("-p test_module.foo=5"))
|
module.set("left-click", cmd)
|
||||||
module = core.module.Error(cfg, "test-mod", "xyz")
|
module.register_callbacks()
|
||||||
self.assertEqual(
|
|
||||||
["critical"], module.state(None), "error module must have critical state"
|
|
||||||
)
|
|
||||||
full_text = module.full_text(module.widget())
|
|
||||||
self.assertTrue("test-mod" in full_text)
|
|
||||||
self.assertTrue("xyz" in full_text)
|
|
||||||
|
|
||||||
def test_single_widget(self):
|
cli = mocker.patch("core.input.util.cli")
|
||||||
module = core.module.Module(widgets=self.someWidget)
|
cli.execute.return_value = ""
|
||||||
self.assertEqual([self.someWidget], module.widgets())
|
core.input.trigger(
|
||||||
|
{"button": core.input.LEFT_MOUSE, "instance": module.id, "name": "sample-name",}
|
||||||
|
)
|
||||||
|
|
||||||
def test_widget_list(self):
|
cli.execute.assert_called_once_with(
|
||||||
module = core.module.Module(widgets=[self.someWidget, self.anotherWidget])
|
cmd.format(instance=module.id, name="sample-name"), wait=False, shell=True,
|
||||||
self.assertEqual([self.someWidget, self.anotherWidget], module.widgets())
|
)
|
||||||
|
|
||||||
def test_module_Name(self):
|
|
||||||
module = TestModule()
|
|
||||||
self.assertEqual("test_module", module.name, "module has wrong name")
|
|
||||||
self.assertEqual("test_module", module.module_name, "module has wrong name")
|
|
||||||
|
|
||||||
def testvalid_parameter(self):
|
|
||||||
cfg = core.config.Config(shlex.split("-p test_module.foo=5"))
|
|
||||||
module = TestModule(config=cfg)
|
|
||||||
self.assertEqual(5, int(module.parameter("foo")))
|
|
||||||
|
|
||||||
def test_default_parameter(self):
|
|
||||||
cfg = core.config.Config([])
|
|
||||||
module = TestModule(config=cfg)
|
|
||||||
self.assertEqual("default", module.parameter("foo", "default"))
|
|
||||||
|
|
||||||
def test_default_is_none(self):
|
|
||||||
cfg = core.config.Config([])
|
|
||||||
module = TestModule(config=cfg)
|
|
||||||
self.assertEqual(None, module.parameter("foo"))
|
|
||||||
|
|
||||||
def test_error_widget(self):
|
|
||||||
cfg = core.config.Config([])
|
|
||||||
module = TestModule(config=cfg)
|
|
||||||
module.fail = True
|
|
||||||
module.error = "!!"
|
|
||||||
module.update_wrapper()
|
|
||||||
self.assertEqual(1, len(module.widgets()))
|
|
||||||
self.assertEqual("error: !!", module.widget().full_text())
|
|
||||||
|
|
||||||
def test_get_widget_by_name(self):
|
|
||||||
cfg = core.config.Config([])
|
|
||||||
module = TestModule(config=cfg, widgets=[self.someWidget, self.anotherWidget])
|
|
||||||
|
|
||||||
self.assertEqual(self.someWidget, module.widget(self.someWidget.name))
|
|
||||||
self.assertEqual(self.anotherWidget, module.widget(self.anotherWidget.name))
|
|
||||||
self.assertEqual(None, module.widget(self.unusedWidgetName))
|
|
||||||
self.assertEqual(self.someWidget, module.widget())
|
|
||||||
|
|
||||||
def test_default_thresholds(self):
|
|
||||||
cfg = core.config.Config([])
|
|
||||||
module = TestModule(config=cfg, widgets=[self.someWidget, self.anotherWidget])
|
|
||||||
|
|
||||||
self.assertEqual("critical", module.threshold_state(100, 80, 99))
|
|
||||||
self.assertEqual("warning", module.threshold_state(100, 80, 100))
|
|
||||||
self.assertEqual("warning", module.threshold_state(81, 80, 100))
|
|
||||||
self.assertEqual(None, module.threshold_state(80, 80, 100))
|
|
||||||
self.assertEqual(None, module.threshold_state(10, 80, 100))
|
|
||||||
|
|
||||||
def test_configured_callbacks(self):
|
|
||||||
cfg = core.config.Config([])
|
|
||||||
module = TestModule(config=cfg, widgets=[self.someWidget, self.anotherWidget])
|
|
||||||
|
|
||||||
cmd = "sample-tool arg1 arg2 arg3"
|
|
||||||
module.set("left-click", cmd)
|
|
||||||
module.register_callbacks()
|
|
||||||
|
|
||||||
with unittest.mock.patch("core.input.util.cli") as cli:
|
|
||||||
cli.execute.return_value = ""
|
|
||||||
core.input.trigger(
|
|
||||||
{"button": core.input.LEFT_MOUSE, "instance": module.id,}
|
|
||||||
)
|
|
||||||
|
|
||||||
cli.execute.assert_called_once_with(cmd, wait=False, shell=True)
|
|
||||||
|
|
||||||
def test_configured_callbacks_with_parameters(self):
|
|
||||||
cfg = core.config.Config([])
|
|
||||||
module = TestModule(config=cfg, widgets=[self.someWidget])
|
|
||||||
|
|
||||||
cmd = "sample-tool {instance} {name}"
|
|
||||||
module.set("left-click", cmd)
|
|
||||||
module.register_callbacks()
|
|
||||||
|
|
||||||
with unittest.mock.patch("core.input.util.cli") as cli:
|
|
||||||
cli.execute.return_value = ""
|
|
||||||
core.input.trigger(
|
|
||||||
{
|
|
||||||
"button": core.input.LEFT_MOUSE,
|
|
||||||
"instance": module.id,
|
|
||||||
"name": "sample-name",
|
|
||||||
}
|
|
||||||
)
|
|
||||||
|
|
||||||
cli.execute.assert_called_once_with(
|
|
||||||
cmd.format(instance=module.id, name="sample-name"),
|
|
||||||
wait=False,
|
|
||||||
shell=True,
|
|
||||||
)
|
|
||||||
|
|
||||||
|
|
||||||
# vim: tabstop=8 expandtab shiftwidth=4 softtabstop=4
|
# vim: tabstop=8 expandtab shiftwidth=4 softtabstop=4
|
||||||
|
|
|
@ -1,162 +1,173 @@
|
||||||
import json
|
import json
|
||||||
import unittest
|
import pytest
|
||||||
|
|
||||||
|
import core.event
|
||||||
import core.config
|
import core.config
|
||||||
import core.output
|
import core.output
|
||||||
import core.module
|
import core.module
|
||||||
|
|
||||||
|
|
||||||
class TestModule(core.module.Module):
|
class SampleModule(core.module.Module):
|
||||||
pass
|
pass
|
||||||
|
|
||||||
|
@pytest.fixture(autouse=True)
|
||||||
|
def clear_events():
|
||||||
|
core.event.clear()
|
||||||
|
|
||||||
class i3(unittest.TestCase):
|
@pytest.fixture
|
||||||
def setUp(self):
|
def i3():
|
||||||
self.i3 = core.output.i3()
|
return core.output.i3()
|
||||||
widget = unittest.mock.MagicMock()
|
|
||||||
widget.full_text.return_value = "test"
|
|
||||||
self.someModule = TestModule(
|
|
||||||
config=core.config.Config([]), widgets=[widget, widget, widget]
|
|
||||||
)
|
|
||||||
self.paddedTheme = core.theme.Theme(raw_data={"defaults": {"padding": " "}})
|
|
||||||
self.separator = "***"
|
|
||||||
self.separatorTheme = core.theme.Theme(
|
|
||||||
raw_data={
|
|
||||||
"defaults": {"separator": self.separator, "fg": "red", "bg": "blue"}
|
|
||||||
}
|
|
||||||
)
|
|
||||||
self.someBlock = core.output.block(
|
|
||||||
theme=self.separatorTheme,
|
|
||||||
module=self.someModule,
|
|
||||||
widget=self.someModule.widget(),
|
|
||||||
)
|
|
||||||
|
|
||||||
def test_start(self):
|
@pytest.fixture
|
||||||
core.event.clear()
|
def module_a(mocker):
|
||||||
|
widget = mocker.MagicMock()
|
||||||
|
widget.full_text.return_value = "test"
|
||||||
|
return SampleModule(
|
||||||
|
config=core.config.Config([]), widgets=[widget, widget, widget]
|
||||||
|
)
|
||||||
|
|
||||||
all_data = self.i3.start()
|
@pytest.fixture
|
||||||
data = all_data["blocks"]
|
def paddedTheme():
|
||||||
self.assertEqual(1, data["version"], "i3bar protocol version 1 expected")
|
return core.theme.Theme(raw_data={"defaults": {"padding": " "}})
|
||||||
self.assertTrue(data["click_events"], "click events should be enabled")
|
|
||||||
self.assertEqual("\n[", all_data["suffix"])
|
|
||||||
|
|
||||||
def test_stop(self):
|
@pytest.fixture
|
||||||
self.assertEqual(
|
def separatorTheme():
|
||||||
"\n]", self.i3.stop()["suffix"], "wrong i3bar protocol during stop"
|
return core.theme.Theme(
|
||||||
)
|
raw_data={
|
||||||
|
"defaults": {"separator": "***", "fg": "red", "bg": "blue"}
|
||||||
|
}
|
||||||
|
)
|
||||||
|
|
||||||
def test_no_modules_by_default(self):
|
@pytest.fixture
|
||||||
self.assertEqual(
|
def block_a(separatorTheme, module_a):
|
||||||
0, len(self.i3.modules()), "module list should be empty by default"
|
return core.output.block(
|
||||||
)
|
theme=separatorTheme,
|
||||||
|
module=module_a,
|
||||||
|
widget=module_a.widget(),
|
||||||
|
)
|
||||||
|
|
||||||
def test_register_single_module(self):
|
#def setUp(self):
|
||||||
self.i3.modules(self.someModule)
|
# self.i3 = core.output.i3()
|
||||||
self.assertEqual(
|
# widget = unittest.mock.MagicMock()
|
||||||
1, len(self.i3.modules()), "setting single module does not work"
|
# widget.full_text.return_value = "test"
|
||||||
)
|
# self.someModule = SampleModule(
|
||||||
|
# config=core.config.Config([]), widgets=[widget, widget, widget]
|
||||||
|
# )
|
||||||
|
# self.separator = "***"
|
||||||
|
#
|
||||||
|
def test_start(i3):
|
||||||
|
all_data = i3.start()
|
||||||
|
data = all_data["blocks"]
|
||||||
|
|
||||||
def test_register_multiple_modules(self):
|
assert data["version"] == 1
|
||||||
self.i3.modules([self.someModule, self.someModule, self.someModule])
|
assert data["click_events"] == True
|
||||||
self.assertEqual(3, len(self.i3.modules()), "setting module list does not work")
|
assert all_data["suffix"] == "\n["
|
||||||
|
|
||||||
def test_draw_existing_module(self):
|
def test_stop(i3):
|
||||||
self.i3.test_draw = unittest.mock.MagicMock(
|
assert i3.stop()["suffix"] == "\n]"
|
||||||
return_value={"blocks": {"test": True}, "suffix": "end"}
|
|
||||||
)
|
|
||||||
self.i3.draw("test_draw")
|
|
||||||
self.i3.test_draw.assert_called_once_with()
|
|
||||||
|
|
||||||
def test_empty_status_line(self):
|
def test_no_modules_by_default(i3):
|
||||||
data = self.i3.statusline()
|
assert i3.modules() == []
|
||||||
self.assertEqual(
|
|
||||||
[], data["blocks"], "expected empty list of status line entries"
|
|
||||||
)
|
|
||||||
self.assertEqual(",", data["suffix"], 'expected "," as suffix')
|
|
||||||
|
|
||||||
def test_statusline(self):
|
def test_register_single_module(i3, module_a):
|
||||||
self.i3.modules([self.someModule, self.someModule, self.someModule])
|
i3.modules(module_a)
|
||||||
self.i3.update()
|
|
||||||
data = self.i3.statusline()
|
|
||||||
self.assertEqual(
|
|
||||||
len(self.someModule.widgets()) * 3,
|
|
||||||
len(data["blocks"]),
|
|
||||||
"wrong number of widgets",
|
|
||||||
)
|
|
||||||
|
|
||||||
def test_padding(self):
|
assert i3.modules() == [module_a]
|
||||||
self.i3.theme(self.paddedTheme)
|
|
||||||
blk = core.output.block(
|
|
||||||
self.i3.theme(), self.someModule, self.someModule.widget()
|
|
||||||
)
|
|
||||||
blk.set("full_text", "abc")
|
|
||||||
result = blk.dict()["full_text"]
|
|
||||||
self.assertEqual(" abc ", result)
|
|
||||||
|
|
||||||
def test_no_separator(self):
|
def test_register_multiple_modules(i3, module_a):
|
||||||
result = self.i3.separator_block(self.someModule, self.someModule.widget())
|
i3.modules([module_a, module_a, module_a])
|
||||||
self.assertEqual([], result)
|
assert i3.modules() == [module_a, module_a, module_a]
|
||||||
|
|
||||||
def test_separator(self):
|
def test_draw_existing_module(mocker, i3):
|
||||||
self.i3.theme(self.separatorTheme)
|
i3.test_draw = mocker.MagicMock(
|
||||||
result = self.i3.separator_block(self.someModule, self.someModule.widget())
|
return_value={"blocks": {"test": True}, "suffix": "end"}
|
||||||
self.assertEqual(1, len(result))
|
)
|
||||||
self.assertEqual("***", result[0].dict()["full_text"])
|
i3.draw("test_draw")
|
||||||
self.assertTrue(result[0].dict().get("_decorator", False))
|
i3.test_draw.assert_called_once_with()
|
||||||
self.assertEqual(
|
|
||||||
self.separatorTheme.get("bg", self.someModule.widget()),
|
|
||||||
result[0].dict()["color"],
|
|
||||||
)
|
|
||||||
|
|
||||||
def test_dump_json(self):
|
def test_empty_status_line(i3):
|
||||||
obj = unittest.mock.MagicMock()
|
data = i3.statusline()
|
||||||
obj.dict = unittest.mock.MagicMock()
|
|
||||||
core.output.dump_json(obj)
|
|
||||||
obj.dict_assert_called_once_with()
|
|
||||||
|
|
||||||
def test_assign(self):
|
assert data["blocks"] == []
|
||||||
src = {"a": "x", "b": "y", "c": "z"}
|
assert data["suffix"] == ","
|
||||||
dst = {}
|
|
||||||
|
|
||||||
core.output.assign(src, dst, "a")
|
def test_statusline(i3, module_a):
|
||||||
self.assertEqual(dst["a"], src["a"])
|
i3.modules([module_a, module_a, module_a])
|
||||||
|
i3.update()
|
||||||
|
data = i3.statusline()
|
||||||
|
assert len(data["blocks"]) == len(module_a.widgets())*3
|
||||||
|
|
||||||
core.output.assign(src, dst, "123", "b")
|
def test_padding(i3, paddedTheme, module_a):
|
||||||
self.assertEqual(dst["123"], src["b"])
|
i3.theme(paddedTheme)
|
||||||
|
blk = core.output.block(
|
||||||
|
i3.theme(), module_a, module_a.widget()
|
||||||
|
)
|
||||||
|
blk.set("full_text", "abc")
|
||||||
|
result = blk.dict()["full_text"]
|
||||||
|
assert result == " abc "
|
||||||
|
|
||||||
core.output.assign(src, dst, "blub", default="def")
|
def test_no_separator(i3, module_a):
|
||||||
self.assertEqual("def", dst["blub"])
|
result = i3.separator_block(module_a, module_a.widget())
|
||||||
|
assert result == []
|
||||||
|
|
||||||
def test_pango_detection(self):
|
def test_separator(i3, separatorTheme, module_a):
|
||||||
self.assertFalse(self.someBlock.is_pango({}))
|
i3.theme(separatorTheme)
|
||||||
self.assertTrue(self.someBlock.is_pango({"pango": {}}))
|
result = i3.separator_block(module_a, module_a.widget())
|
||||||
|
|
||||||
def test_pangoize(self):
|
assert len(result) == 1
|
||||||
self.assertEqual("test", self.someBlock.pangoize("test"))
|
assert result[0].dict()["full_text"] == "***"
|
||||||
self.assertFalse("markup" in self.someBlock.dict())
|
assert result[0].dict().get("_decorator") == True
|
||||||
|
assert result[0].dict()["color"] == separatorTheme.get("bg", module_a.widget())
|
||||||
|
|
||||||
pango = self.someBlock.pangoize(
|
def test_dump_json(mocker):
|
||||||
{"pango": {"attr": "blub", "x": "y", "full_text": "test"}}
|
obj = mocker.MagicMock()
|
||||||
)
|
obj.dict = mocker.MagicMock()
|
||||||
self.assertTrue('attr="blub"' in pango)
|
core.output.dump_json(obj)
|
||||||
self.assertTrue('x="y"' in pango)
|
obj.dict_assert_called_once_with()
|
||||||
self.assertTrue("<span " in pango)
|
|
||||||
self.assertTrue(">test</span>" in pango)
|
|
||||||
self.assertEqual("pango", self.someBlock.dict()["markup"])
|
|
||||||
|
|
||||||
def test_padding(self):
|
def test_assign():
|
||||||
self.someBlock.set("padding", "***")
|
src = {"a": "x", "b": "y", "c": "z"}
|
||||||
self.someBlock.set("full_text", "test")
|
dst = {}
|
||||||
|
|
||||||
self.assertEqual("***test***", self.someBlock.dict()["full_text"])
|
core.output.assign(src, dst, "a")
|
||||||
|
assert src["a"] == dst["a"]
|
||||||
|
|
||||||
def test_pre_suffix(self):
|
core.output.assign(src, dst, "123", "b")
|
||||||
self.someBlock.set("padding", "*")
|
assert src["b"] == dst["123"]
|
||||||
self.someBlock.set("prefix", "pre")
|
|
||||||
self.someBlock.set("suffix", "suf")
|
|
||||||
self.someBlock.set("full_text", "test")
|
|
||||||
|
|
||||||
self.assertEqual("*pre*test*suf*", self.someBlock.dict()["full_text"])
|
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
|
||||||
|
|
||||||
|
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"}}
|
||||||
|
)
|
||||||
|
assert 'attr="blub"' in pango
|
||||||
|
assert 'x="y"' in pango
|
||||||
|
assert "<span " in pango
|
||||||
|
assert ">test</span>" 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")
|
||||||
|
block_a.set("suffix", "suf")
|
||||||
|
block_a.set("full_text", "test")
|
||||||
|
|
||||||
|
assert block_a.dict()["full_text"] == "*pre*test*suf*"
|
||||||
|
|
||||||
|
|
||||||
# vim: tabstop=8 expandtab shiftwidth=4 softtabstop=4
|
# vim: tabstop=8 expandtab shiftwidth=4 softtabstop=4
|
||||||
|
|
|
@ -1,4 +1,4 @@
|
||||||
import unittest
|
import pytest
|
||||||
import types
|
import types
|
||||||
|
|
||||||
import core.theme
|
import core.theme
|
||||||
|
@ -7,139 +7,169 @@ import core.widget
|
||||||
import core.module
|
import core.module
|
||||||
|
|
||||||
|
|
||||||
class TestModule(core.module.Module):
|
class SampleModule(core.module.Module):
|
||||||
def __init__(self, widgets, config=core.config.Config([]), theme=None):
|
def __init__(self, widgets, config=core.config.Config([]), theme=None):
|
||||||
super().__init__(config, theme, widgets)
|
super().__init__(config, theme, widgets)
|
||||||
self.name = "test"
|
self.name = "test"
|
||||||
|
|
||||||
|
@pytest.fixture(autouse=True)
|
||||||
|
def clear_events():
|
||||||
|
core.event.clear()
|
||||||
|
|
||||||
class theme(unittest.TestCase):
|
@pytest.fixture
|
||||||
def setUp(self):
|
def defaultsTheme():
|
||||||
core.event.clear()
|
return {"defaults": {"fg": "red", "bg": "black"}}
|
||||||
self.invalidThemeName = "this-theme-does-not-exist"
|
|
||||||
self.validThemeName = "default"
|
|
||||||
self.defaultsTheme = {"defaults": {"fg": "red", "bg": "black"}}
|
|
||||||
self.cycleTheme = {
|
|
||||||
"cycle": [
|
|
||||||
{"fg": "red", "bg": "black"},
|
|
||||||
{"fg": "black", "bg": "red"},
|
|
||||||
{"fg": "white", "bg": "blue"},
|
|
||||||
]
|
|
||||||
}
|
|
||||||
self.colorTheme = {"colors": [{"red": "#ff0000", "blue": "#0000ff"}]}
|
|
||||||
self.walTheme = {"colors": ["wal"]}
|
|
||||||
self.cycleValueTheme = {"defaults": {"fg": ["red", "green", "blue"]}}
|
|
||||||
self.stateTheme = {"warning": {"fg": "yellow"}, "critical": {"fg": "red"}}
|
|
||||||
self.overlayTheme = {
|
|
||||||
"load": {"prefix": "a"},
|
|
||||||
"test": {"load": {"prefix": "b"}, "prefix": "x"},
|
|
||||||
}
|
|
||||||
|
|
||||||
def test_invalid_theme(self):
|
@pytest.fixture
|
||||||
with self.assertRaises(RuntimeError):
|
def cycleTheme():
|
||||||
core.theme.Theme(self.invalidThemeName)
|
return {
|
||||||
|
"cycle": [
|
||||||
|
{"fg": "red", "bg": "black"},
|
||||||
|
{"fg": "black", "bg": "red"},
|
||||||
|
{"fg": "white", "bg": "blue"},
|
||||||
|
]
|
||||||
|
}
|
||||||
|
|
||||||
def test_valid_theme(self):
|
@pytest.fixture
|
||||||
theme = core.theme.Theme(self.validThemeName)
|
def colorTheme():
|
||||||
self.assertEqual(self.validThemeName, theme.name)
|
return {"colors": [{"red": "#ff0000", "blue": "#0000ff"}]}
|
||||||
|
|
||||||
def test_defaults(self):
|
@pytest.fixture
|
||||||
theme = core.theme.Theme(raw_data=self.defaultsTheme)
|
def walTheme():
|
||||||
self.assertEqual(self.defaultsTheme["defaults"]["fg"], theme.get("fg"))
|
return {"colors": ["wal"]}
|
||||||
self.assertEqual(self.defaultsTheme["defaults"]["bg"], theme.get("bg"))
|
|
||||||
|
|
||||||
def test_cycle(self):
|
@pytest.fixture
|
||||||
theme = core.theme.Theme(raw_data=self.cycleTheme)
|
def cycleValueTheme():
|
||||||
self.assertEqual(None, theme.get("prev-bg"))
|
return {"defaults": {"fg": ["red", "green", "blue"]}}
|
||||||
self.assertEqual(self.cycleTheme["cycle"][0]["fg"], theme.get("fg"))
|
|
||||||
self.assertEqual(self.cycleTheme["cycle"][0]["bg"], theme.get("bg"))
|
|
||||||
core.event.trigger("next-widget")
|
|
||||||
self.assertEqual(self.cycleTheme["cycle"][0]["bg"], theme.get("bg", "previous"))
|
|
||||||
core.event.trigger("next-widget")
|
|
||||||
self.assertEqual(self.cycleTheme["cycle"][2]["fg"], theme.get("fg"))
|
|
||||||
self.assertEqual(self.cycleTheme["cycle"][2]["bg"], theme.get("bg"))
|
|
||||||
|
|
||||||
with unittest.mock.patch("core.output.sys.stdout"):
|
@pytest.fixture
|
||||||
core.event.trigger("draw")
|
def stateTheme():
|
||||||
self.assertEqual(self.cycleTheme["cycle"][0]["fg"], theme.get("fg"))
|
return {"warning": {"fg": "yellow"}, "critical": {"fg": "red"}}
|
||||||
self.assertEqual(self.cycleTheme["cycle"][0]["bg"], theme.get("bg"))
|
|
||||||
|
|
||||||
def test_custom_iconset(self):
|
@pytest.fixture
|
||||||
theme = core.theme.Theme(raw_data=self.defaultsTheme)
|
def overlayTheme():
|
||||||
self.assertNotEqual("aaa", theme.get("padding"))
|
return {
|
||||||
theme = core.theme.Theme(
|
"load": {"prefix": "a"},
|
||||||
raw_data=self.defaultsTheme, iconset={"defaults": {"padding": "aaa"}}
|
"test": {"load": {"prefix": "b"}, "prefix": "x"},
|
||||||
)
|
}
|
||||||
self.assertEqual("aaa", theme.get("padding"))
|
|
||||||
|
|
||||||
def test_colors(self):
|
def test_invalid_theme():
|
||||||
theme = core.theme.Theme(raw_data=self.defaultsTheme)
|
with pytest.raises(RuntimeError):
|
||||||
self.assertEqual({}, theme.keywords())
|
core.theme.Theme("this-theme-does-not-exist")
|
||||||
theme = core.theme.Theme(raw_data=self.colorTheme)
|
|
||||||
self.assertEqual(self.colorTheme["colors"][0], theme.keywords())
|
|
||||||
|
|
||||||
def test_wal_colors(self):
|
def test_valid_theme():
|
||||||
with unittest.mock.patch("core.theme.io") as io:
|
theme = core.theme.Theme("default")
|
||||||
with unittest.mock.patch("core.theme.os") as os:
|
assert theme.name == "default"
|
||||||
os.path.isfile.return_value = True
|
|
||||||
io.open.return_value = unittest.mock.MagicMock()
|
|
||||||
io.open.return_value.__enter__.return_value.read.return_value = """
|
|
||||||
{ "colors": { "red": "#ff0000" } }
|
|
||||||
"""
|
|
||||||
|
|
||||||
theme = core.theme.Theme(raw_data=self.walTheme)
|
def test_defaults(defaultsTheme):
|
||||||
self.assertEqual({"red": "#ff0000"}, theme.keywords())
|
theme = core.theme.Theme(raw_data=defaultsTheme)
|
||||||
|
|
||||||
def test_wal_special(self):
|
assert theme.get("fg") == defaultsTheme["defaults"]["fg"]
|
||||||
with unittest.mock.patch("core.theme.io") as io:
|
assert theme.get("bg") == defaultsTheme["defaults"]["bg"]
|
||||||
with unittest.mock.patch("core.theme.os") as os:
|
|
||||||
os.path.isfile.return_value = True
|
|
||||||
io.open.return_value.__enter__.return_value.read.return_value = """
|
|
||||||
{ "special": { "background": "#ff0000" } }
|
|
||||||
"""
|
|
||||||
|
|
||||||
theme = core.theme.Theme(raw_data=self.walTheme)
|
def test_cycle(mocker, cycleTheme):
|
||||||
self.assertEqual({"background": "#ff0000"}, theme.keywords())
|
theme = core.theme.Theme(raw_data=cycleTheme)
|
||||||
|
|
||||||
def test_cycle_value(self):
|
assert theme.get("bg", "previous") == None
|
||||||
widget = core.widget.Widget()
|
assert theme.get("fg") == cycleTheme["cycle"][0]["fg"]
|
||||||
expected = self.cycleValueTheme["defaults"]["fg"]
|
assert theme.get("bg") == cycleTheme["cycle"][0]["bg"]
|
||||||
theme = core.theme.Theme(raw_data=self.cycleValueTheme)
|
|
||||||
|
|
||||||
for i in range(0, len(expected) * 3):
|
core.event.trigger("next-widget")
|
||||||
self.assertEqual(expected[i % len(expected)], theme.get("fg", widget))
|
|
||||||
self.assertEqual(
|
|
||||||
expected[i % len(expected)], theme.get("fg", widget)
|
|
||||||
) # ensure multiple invocations are OK
|
|
||||||
core.event.trigger("draw")
|
|
||||||
|
|
||||||
def test_state(self):
|
assert theme.get("bg", "previous") == cycleTheme["cycle"][0]["bg"]
|
||||||
widget = core.widget.Widget()
|
|
||||||
theme = core.theme.Theme(raw_data=self.stateTheme)
|
|
||||||
|
|
||||||
self.assertEqual(None, theme.get("fg", widget))
|
core.event.trigger("next-widget")
|
||||||
|
|
||||||
widget.state = types.MethodType(lambda self: ["warning"], widget)
|
assert theme.get("fg") == cycleTheme["cycle"][2]["fg"]
|
||||||
self.assertEqual(self.stateTheme["warning"]["fg"], theme.get("fg", widget))
|
assert theme.get("bg") == cycleTheme["cycle"][2]["bg"]
|
||||||
|
|
||||||
widget.state = types.MethodType(lambda self: ["critical"], widget)
|
stdout = mocker.patch("core.output.sys.stdout")
|
||||||
self.assertEqual(self.stateTheme["critical"]["fg"], theme.get("fg", widget))
|
|
||||||
|
|
||||||
def test_overlay(self):
|
core.event.trigger("draw")
|
||||||
widget = core.widget.Widget()
|
|
||||||
module = TestModule(widget)
|
|
||||||
theme = core.theme.Theme(raw_data=self.overlayTheme)
|
|
||||||
|
|
||||||
self.assertEqual(
|
assert theme.get("fg") == cycleTheme["cycle"][0]["fg"]
|
||||||
self.overlayTheme[module.name]["prefix"], theme.get("prefix", widget)
|
assert theme.get("bg") == cycleTheme["cycle"][0]["bg"]
|
||||||
)
|
|
||||||
|
|
||||||
widget.state = types.MethodType(lambda self: ["load"], widget)
|
def test_custom_iconset(defaultsTheme):
|
||||||
|
theme = core.theme.Theme(raw_data=defaultsTheme)
|
||||||
|
|
||||||
self.assertEqual(
|
assert theme.get("padding") != "aaa"
|
||||||
self.overlayTheme[module.name]["load"]["prefix"],
|
assert theme.get("fg") != "blue"
|
||||||
theme.get("prefix", widget),
|
|
||||||
)
|
theme = core.theme.Theme(
|
||||||
|
raw_data=defaultsTheme, iconset={"defaults": {"padding": "aaa", "fg": "blue"}}
|
||||||
|
)
|
||||||
|
|
||||||
|
assert theme.get("padding") == "aaa"
|
||||||
|
assert theme.get("fg") == "blue" # test override
|
||||||
|
|
||||||
|
def test_colors(defaultsTheme, colorTheme):
|
||||||
|
theme = core.theme.Theme(raw_data=defaultsTheme)
|
||||||
|
assert theme.keywords() == {}
|
||||||
|
|
||||||
|
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")
|
||||||
|
|
||||||
|
os.path.isfile.return_value = True
|
||||||
|
io.open.return_value = mocker.MagicMock()
|
||||||
|
io.open.return_value.__enter__.return_value.read.return_value = """
|
||||||
|
{ "colors": { "red": "#ff0000" } }
|
||||||
|
"""
|
||||||
|
|
||||||
|
theme = core.theme.Theme(raw_data=walTheme)
|
||||||
|
|
||||||
|
assert theme.keywords() == {"red": "#ff0000"}
|
||||||
|
|
||||||
|
def test_wal_special(mocker, walTheme):
|
||||||
|
io = mocker.patch("core.theme.io")
|
||||||
|
os = mocker.patch("core.theme.os")
|
||||||
|
|
||||||
|
os.path.isfile.return_value = True
|
||||||
|
io.open.return_value.__enter__.return_value.read.return_value = """
|
||||||
|
{ "special": { "background": "#ff0000" } }
|
||||||
|
"""
|
||||||
|
|
||||||
|
theme = core.theme.Theme(raw_data=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)]
|
||||||
|
# ensure multiple invocations are OK
|
||||||
|
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)
|
||||||
|
|
||||||
|
assert theme.get("fg", widget) == None
|
||||||
|
|
||||||
|
widget.state = types.MethodType(lambda self: ["warning"], widget)
|
||||||
|
assert theme.get("fg", widget) == stateTheme["warning"]["fg"]
|
||||||
|
|
||||||
|
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)
|
||||||
|
theme = core.theme.Theme(raw_data=overlayTheme)
|
||||||
|
|
||||||
|
assert theme.get("prefix", widget) == overlayTheme[module.name]["prefix"]
|
||||||
|
|
||||||
|
widget.state = types.MethodType(lambda self: ["load"], widget)
|
||||||
|
|
||||||
|
assert theme.get("prefix", widget) == overlayTheme[module.name]["load"]["prefix"]
|
||||||
|
|
||||||
|
|
||||||
# vim: tabstop=8 expandtab shiftwidth=4 softtabstop=4
|
# vim: tabstop=8 expandtab shiftwidth=4 softtabstop=4
|
||||||
|
|
|
@ -1,12 +1,11 @@
|
||||||
import unittest
|
import pytest
|
||||||
import unittest.mock
|
|
||||||
|
|
||||||
import core.widget
|
import core.widget
|
||||||
import core.module
|
import core.module
|
||||||
import core.config
|
import core.config
|
||||||
|
|
||||||
|
|
||||||
class TestModule(core.module.Module):
|
class SampleModule(core.module.Module):
|
||||||
def __init__(self, widgets, config=core.config.Config([]), theme=None):
|
def __init__(self, widgets, config=core.config.Config([]), theme=None):
|
||||||
super().__init__(config, theme, widgets)
|
super().__init__(config, theme, widgets)
|
||||||
self.states = []
|
self.states = []
|
||||||
|
@ -19,77 +18,80 @@ class TestModule(core.module.Module):
|
||||||
def state(self, widget):
|
def state(self, widget):
|
||||||
return self.states
|
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):
|
# def setUp(self):
|
||||||
self.someValue = "some random value"
|
# self.someValue = "some random value"
|
||||||
self.someOtherValue = "some different value"
|
# self.someOtherValue = "some different value"
|
||||||
self.callbackReturnValue = "callback return value"
|
# self.callbackReturnValue = "callback return value"
|
||||||
self.someWidget = core.widget.Widget(full_text=self.someValue)
|
# self.someWidget = core.widget.Widget(full_text=self.someValue)
|
||||||
self.someCallback = unittest.mock.MagicMock(
|
# self.someCallback = unittest.mock.MagicMock(
|
||||||
return_value=self.callbackReturnValue
|
# return_value=self.callbackReturnValue
|
||||||
)
|
# )
|
||||||
|
#
|
||||||
|
# self.assertNotEqual(self.someValue, self.someOtherValue)
|
||||||
|
|
||||||
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 tearDown(self):
|
def test_set_fulltext(widget_a):
|
||||||
pass
|
assert widget_a.full_text() != "new value"
|
||||||
|
widget_a.full_text("new value")
|
||||||
|
assert widget_a.full_text() == "new value"
|
||||||
|
|
||||||
def test_text_fulltext(self):
|
def test_callable_fulltext(mocker):
|
||||||
newWidget = core.widget.Widget(full_text=self.someValue)
|
callback = mocker.MagicMock(return_value="callback returns")
|
||||||
self.assertEqual(self.someValue, newWidget.full_text())
|
widget = core.widget.Widget(full_text=callback)
|
||||||
|
assert widget.full_text() == "callback returns"
|
||||||
|
callback.assert_called_once_with(widget)
|
||||||
|
|
||||||
def test_set_fulltext(self):
|
def test_set_callable_fulltext(mocker, widget_a):
|
||||||
self.assertNotEqual(self.someOtherValue, self.someWidget.full_text())
|
callback = mocker.MagicMock(return_value="this is a test")
|
||||||
self.someWidget.full_text(self.someOtherValue)
|
widget_a.full_text(callback)
|
||||||
self.assertEqual(self.someOtherValue, self.someWidget.full_text())
|
assert widget_a.full_text() == "this is a test"
|
||||||
|
callback.assert_called_once_with(widget_a)
|
||||||
|
|
||||||
def test_callable_fulltext(self):
|
def test_state_defaults_to_empty(widget_a):
|
||||||
newWidget = core.widget.Widget(full_text=self.someCallback)
|
assert widget_a.state() == []
|
||||||
self.assertEqual(newWidget.full_text(), self.callbackReturnValue)
|
|
||||||
self.someCallback.assert_called_once_with(newWidget)
|
|
||||||
|
|
||||||
def test_set_callable_fulltext(self):
|
def test_single_widget_state(widget_a):
|
||||||
self.someWidget.full_text(self.someCallback)
|
widget_a.set("state", "state1")
|
||||||
self.assertEqual(self.someWidget.full_text(), self.callbackReturnValue)
|
assert widget_a.state() == ["state1"]
|
||||||
self.someCallback.assert_called_once_with(self.someWidget)
|
|
||||||
|
|
||||||
def test_state_defaults_to_empty(self):
|
def test_multiple_widget_states(widget_a):
|
||||||
self.assertEqual([], self.someWidget.state())
|
widget_a.set("state", ["state1", "state2"])
|
||||||
|
assert widget_a.state() == ["state1", "state2"]
|
||||||
|
|
||||||
def test_single_widget_state(self):
|
def test_widget_module_state(widget_a):
|
||||||
self.someWidget.set("state", "state1")
|
module = SampleModule(widgets=widget_a)
|
||||||
self.assertEqual(["state1"], self.someWidget.state())
|
widget_a.set("state", ["state1", "state2"])
|
||||||
|
|
||||||
def test_multiple_widget_states(self):
|
module.states = "x"
|
||||||
self.someWidget.set("state", ["state1", "state2"])
|
assert widget_a.state() == ["state1", "state2", "x"]
|
||||||
self.assertEqual(["state1", "state2"], self.someWidget.state())
|
|
||||||
|
|
||||||
def test_widget_module_state(self):
|
module.states = ["a", "b"]
|
||||||
module = TestModule(widgets=self.someWidget)
|
assert widget_a.state() == ["state1", "state2", "a", "b"]
|
||||||
self.someWidget.set("state", ["state1", "state2"])
|
|
||||||
|
|
||||||
module.states = "x"
|
def test_multiple_widget_themes():
|
||||||
self.assertEqual(["state1", "state2", "x"], self.someWidget.state())
|
widget1 = core.widget.Widget(full_text="a")
|
||||||
module.states = ["a", "b"]
|
widget2 = core.widget.Widget(full_text="b")
|
||||||
self.assertEqual(["state1", "state2", "a", "b"], self.someWidget.state())
|
widget3 = core.widget.Widget(full_text="c")
|
||||||
|
|
||||||
def test_multiple_widget_themes(self):
|
module = SampleModule(widgets=[widget1, widget2, widget3])
|
||||||
widget1 = core.widget.Widget(full_text="a")
|
module.set("theme.test", "1,2,3")
|
||||||
widget2 = core.widget.Widget(full_text="b")
|
module.set("theme.test2", "x")
|
||||||
widget3 = core.widget.Widget(full_text="c")
|
|
||||||
|
|
||||||
module = TestModule(widgets=[widget1, widget2, widget3])
|
assert widget1.theme("test") == "1"
|
||||||
module.set("theme.test", "1,2,3")
|
assert widget2.theme("test") == "2"
|
||||||
module.set("theme.test2", "x")
|
assert widget3.theme("test") == "3"
|
||||||
|
|
||||||
self.assertEqual("1", widget1.theme("test"))
|
assert widget1.theme("test2") == "x"
|
||||||
self.assertEqual("2", widget2.theme("test"))
|
assert widget2.theme("test2") == None
|
||||||
self.assertEqual("3", widget3.theme("test"))
|
assert widget3.theme("test2") == None
|
||||||
|
|
||||||
self.assertEqual("x", widget1.theme("test2"))
|
|
||||||
self.assertEqual(None, widget2.theme("test2"))
|
|
||||||
self.assertEqual(None, widget3.theme("test2"))
|
|
||||||
|
|
||||||
|
|
||||||
# vim: tabstop=8 expandtab shiftwidth=4 softtabstop=4
|
# vim: tabstop=8 expandtab shiftwidth=4 softtabstop=4
|
||||||
|
|
|
@ -1,21 +0,0 @@
|
||||||
import unittest
|
|
||||||
|
|
||||||
import core.config
|
|
||||||
import modules.contrib.kernel
|
|
||||||
|
|
||||||
|
|
||||||
class kernel(unittest.TestCase):
|
|
||||||
def setUp(self):
|
|
||||||
self.someKernel = "this-is-my-kernel"
|
|
||||||
self.module = modules.contrib.kernel.Module(
|
|
||||||
config=core.config.Config([]), theme=None
|
|
||||||
)
|
|
||||||
|
|
||||||
def test_full_text(self):
|
|
||||||
with unittest.mock.patch("modules.contrib.kernel.platform") as platform:
|
|
||||||
platform.release.return_value = self.someKernel
|
|
||||||
self.assertEqual(1, len(self.module.widgets()))
|
|
||||||
self.assertEqual(self.someKernel, self.module.widget().full_text())
|
|
||||||
|
|
||||||
|
|
||||||
# vim: tabstop=8 expandtab shiftwidth=4 softtabstop=4
|
|
|
@ -1,42 +1,50 @@
|
||||||
import unittest
|
import pytest
|
||||||
|
|
||||||
from util.algorithm import *
|
from util.algorithm import *
|
||||||
|
|
||||||
|
@pytest.fixture
|
||||||
|
def someData():
|
||||||
|
return {"a": 100, "b": 200, "c": [1, 2, 3]}
|
||||||
|
|
||||||
class algorithm(unittest.TestCase):
|
@pytest.fixture
|
||||||
def setUp(self):
|
def differentData():
|
||||||
self.someData = {"a": 100, "b": 200, "c": [1, 2, 3]}
|
return {"x": 20, "y": "bla", "z": ["a", "b"]}
|
||||||
self.differentData = {"x": 20, "y": "bla", "z": ["a", "b"]}
|
|
||||||
self.moreData = {"n": 100}
|
|
||||||
self.overlapData = {"a": 200, "c": [1, 2, 4]}
|
|
||||||
|
|
||||||
def test_merge_with_empty(self):
|
@pytest.fixture
|
||||||
self.assertEqual(self.someData, merge(self.someData, {}))
|
def moreData():
|
||||||
self.assertEqual(None, merge(self.someData, None))
|
return {"n": 100}
|
||||||
|
|
||||||
def test_merge_no_overwrite(self):
|
@pytest.fixture
|
||||||
result = merge(self.someData, self.differentData)
|
def overlapData():
|
||||||
for k in self.someData:
|
return {"a": 200, "c": [1, 2, 4]}
|
||||||
self.assertEqual(result[k], self.someData[k])
|
|
||||||
|
def test_merge_with_empty(someData):
|
||||||
|
assert merge(someData, {}) == someData
|
||||||
|
assert merge(someData, None) == None
|
||||||
|
|
||||||
|
def test_merge_no_overwrite(someData, differentData):
|
||||||
|
result = merge(someData, differentData)
|
||||||
|
for k in someData:
|
||||||
|
assert someData[k] == result[k]
|
||||||
for k in self.differentData:
|
for k in self.differentData:
|
||||||
self.assertEqual(result[k], self.differentData[k])
|
assert differentData[k] == result[k]
|
||||||
|
|
||||||
def test_merge_multiple(self):
|
def test_merge_multiple(someData, differentData, moreData):
|
||||||
result = merge(self.someData, self.differentData, self.moreData)
|
result = merge(someData, differentData, moreData)
|
||||||
for k in self.someData:
|
for k in someData:
|
||||||
self.assertEqual(result[k], self.someData[k])
|
assert someData[k] == result[k]
|
||||||
for k in self.differentData:
|
for k in differentData:
|
||||||
self.assertEqual(result[k], self.differentData[k])
|
assert differentData[k] == result[k]
|
||||||
for k in self.moreData:
|
for k in moreData:
|
||||||
self.assertEqual(result[k], self.moreData[k])
|
assert moreData[k] == result[k]
|
||||||
|
|
||||||
def merge_overlap(self):
|
def merge_overlap(someData, overlapData):
|
||||||
result = merge(self.someData, self.overlapData)
|
result = merge(someData, overlapData)
|
||||||
for k in self.someData:
|
for k in someData:
|
||||||
if not k in self.overlapData:
|
if not k in self.overlapData:
|
||||||
self.assertEqual(result[k], self.someData[k])
|
assert someData[k] == result[k]
|
||||||
for k in self.overlapData:
|
for k in self.overlapData:
|
||||||
self.assertEqual(result[k], self.overlapData[k])
|
assert overlapData[k] == result[k]
|
||||||
|
|
||||||
|
|
||||||
# vim: tabstop=8 expandtab shiftwidth=4 softtabstop=4
|
# vim: tabstop=8 expandtab shiftwidth=4 softtabstop=4
|
||||||
|
|
|
@ -1,43 +1,27 @@
|
||||||
import unittest
|
import pytest
|
||||||
|
|
||||||
import util.cli
|
import util.cli
|
||||||
|
|
||||||
|
def test_valid_command():
|
||||||
|
assert util.cli.execute("echo test") == "test\n"
|
||||||
|
|
||||||
class cli(unittest.TestCase):
|
def test_utf_command():
|
||||||
def setUp(self):
|
rv = util.cli.execute("echo ÖPmŧß")
|
||||||
self.nonExistentCommand = "i-do-not-exist"
|
assert util.cli.execute("echo ÖPmŧß") == "ÖPmŧß\n"
|
||||||
self.validCommand = "echo test"
|
|
||||||
self.validCommandOutput = "test\n"
|
|
||||||
self.utfCommand = "echo ÖPmŧß"
|
|
||||||
self.utfCommandOutput = "ÖPmŧß\n"
|
|
||||||
|
|
||||||
def test_valid_command(self):
|
def test_invalid_command():
|
||||||
rv = util.cli.execute(self.validCommand)
|
with pytest.raises(RuntimeError):
|
||||||
self.assertEqual(self.validCommandOutput, rv)
|
util.cli.execute("i-do-not-exist")
|
||||||
|
|
||||||
def test_utf_command(self):
|
def test_command_exit_code():
|
||||||
rv = util.cli.execute(self.utfCommand)
|
with pytest.raises(RuntimeError):
|
||||||
self.assertEqual(self.utfCommandOutput, rv)
|
util.cli.execute("cat i-do-not-exist")
|
||||||
|
|
||||||
def test_invalid_command(self):
|
def test_command_exit_code_no_error():
|
||||||
with self.assertRaises(RuntimeError):
|
util.cli.execute("cat i-do-not-exist", ignore_errors=True)
|
||||||
util.cli.execute(self.nonExistentCommand)
|
|
||||||
|
|
||||||
def test_command_exit_code(self):
|
def test_async():
|
||||||
with self.assertRaises(RuntimeError):
|
assert util.cli.execute("echo test", wait=False) == ""
|
||||||
util.cli.execute("cat {}".format(self.nonExistentCommand))
|
|
||||||
|
|
||||||
def test_command_exit_code_no_error(self):
|
|
||||||
try:
|
|
||||||
util.cli.execute(
|
|
||||||
"cat {}".format(self.nonExistentCommand), ignore_errors=True
|
|
||||||
)
|
|
||||||
except Exception:
|
|
||||||
self.fail("exception was thrown")
|
|
||||||
|
|
||||||
def test_async(self):
|
|
||||||
rv = util.cli.execute(self.validCommand, wait=False)
|
|
||||||
self.assertEqual("", rv)
|
|
||||||
|
|
||||||
|
|
||||||
# vim: tabstop=8 expandtab shiftwidth=4 softtabstop=4
|
# vim: tabstop=8 expandtab shiftwidth=4 softtabstop=4
|
||||||
|
|
|
@ -1,103 +1,113 @@
|
||||||
import unittest
|
import pytest
|
||||||
|
|
||||||
from util.format import *
|
from util.format import *
|
||||||
|
|
||||||
|
|
||||||
class format(unittest.TestCase):
|
def test_int_from_string():
|
||||||
def test_int_from_string(self):
|
assert asint("100") == 100
|
||||||
self.assertEqual(100, asint("100"))
|
assert asint("-100") == -100
|
||||||
self.assertEqual(-100, asint("-100"))
|
assert asint("0") == 0
|
||||||
self.assertEqual(0, asint("0"))
|
|
||||||
|
|
||||||
def test_int_from_none(self):
|
|
||||||
self.assertEqual(0, asint(None))
|
|
||||||
|
|
||||||
def test_int_from_int(self):
|
def test_int_from_none():
|
||||||
self.assertEqual(100, asint(100))
|
assert asint(None) == 0
|
||||||
self.assertEqual(-100, asint(-100))
|
|
||||||
self.assertEqual(0, asint(0))
|
|
||||||
|
|
||||||
def test_int_minimum(self):
|
|
||||||
self.assertEqual(100, asint(100, minimum=10))
|
|
||||||
self.assertEqual(100, asint(100, minimum=100))
|
|
||||||
self.assertEqual(10, asint(5, minimum=10))
|
|
||||||
|
|
||||||
def test_int_maximum(self):
|
def test_int_from_int():
|
||||||
self.assertEqual(100, asint(100, maximum=200))
|
assert asint(100) == 100
|
||||||
self.assertEqual(100, asint(100, maximum=100))
|
assert asint(-100) == -100
|
||||||
self.assertEqual(100, asint(200, maximum=100))
|
assert asint(0) == 0
|
||||||
|
|
||||||
def test_true_from_str(self):
|
|
||||||
self.assertTrue(asbool("true"))
|
|
||||||
self.assertTrue(asbool(True))
|
|
||||||
self.assertTrue(asbool("t"))
|
|
||||||
self.assertTrue(asbool("1"))
|
|
||||||
self.assertTrue(asbool("yes"))
|
|
||||||
self.assertTrue(asbool("y"))
|
|
||||||
self.assertTrue(asbool("on"))
|
|
||||||
|
|
||||||
def test_false_from_str(self):
|
def test_int_minimum():
|
||||||
self.assertFalse(asbool("false"))
|
assert asint(100, minimum=10) == 100
|
||||||
self.assertFalse(asbool(False))
|
assert asint(100, minimum=100) == 100
|
||||||
self.assertFalse(asbool("f"))
|
assert asint(5, minimum=10) == 10
|
||||||
self.assertFalse(asbool("0"))
|
|
||||||
self.assertFalse(asbool("no"))
|
|
||||||
self.assertFalse(asbool("n"))
|
|
||||||
self.assertFalse(asbool("off"))
|
|
||||||
self.assertFalse(asbool(None))
|
|
||||||
|
|
||||||
def test_list_from_None(self):
|
|
||||||
self.assertEqual([], aslist(None))
|
|
||||||
|
|
||||||
def test_list_from_list(self):
|
def test_int_maximum():
|
||||||
self.assertEqual([1, 2, 3], aslist([1, 2, 3]))
|
assert asint(100, maximum=200) == 100
|
||||||
|
assert asint(100, maximum=100) == 100
|
||||||
|
assert asint(200, maximum=100) == 100
|
||||||
|
|
||||||
def test_list_from_str(self):
|
|
||||||
self.assertEqual(["12", "13", "14"], aslist("12,13,14"))
|
|
||||||
|
|
||||||
def test_byteformat(self):
|
def test_true_from_str():
|
||||||
self.assertEqual("500.00B", byte(500))
|
assert asbool("true") == True
|
||||||
self.assertEqual("1.00KiB", byte(1024))
|
assert asbool(True) == True
|
||||||
self.assertEqual("1KiB", byte(1024, "{:.0f}"))
|
assert asbool("t") == True
|
||||||
self.assertEqual("1.50KiB", byte(1024 + 512))
|
assert asbool("1") == True
|
||||||
self.assertEqual("2.50MiB", byte(1024 * 1024 * 2 + 1024 * 512))
|
assert asbool("yes") == True
|
||||||
self.assertEqual("4.50GiB", byte(1024 * 1024 * 1024 * 4 + 1024 * 1024 * 512))
|
assert asbool("y") == True
|
||||||
self.assertEqual("2048.00GiB", byte(1024 * 1024 * 1024 * 1024 * 2))
|
assert asbool("on") == True
|
||||||
|
|
||||||
def test_duration(self):
|
|
||||||
self.assertEqual("04:20:00", duration(4 * 60 * 60 + 20 * 60))
|
|
||||||
self.assertEqual("04:20:00h", duration(4 * 60 * 60 + 20 * 60, unit=True))
|
|
||||||
self.assertEqual(
|
|
||||||
"04:20h", duration(4 * 60 * 60 + 20 * 60, compact=True, unit=True)
|
|
||||||
)
|
|
||||||
|
|
||||||
self.assertEqual("20:00", duration(20 * 60))
|
def test_false_from_str():
|
||||||
self.assertEqual("20:00m", duration(20 * 60, unit=True))
|
assert asbool("false") == False
|
||||||
self.assertEqual("20:00m", duration(20 * 60, compact=True, unit=True))
|
assert asbool(False) == False
|
||||||
|
assert asbool("f") == False
|
||||||
|
assert asbool("0") == False
|
||||||
|
assert asbool("no") == False
|
||||||
|
assert asbool("n") == False
|
||||||
|
assert asbool("off") == False
|
||||||
|
assert asbool(None) == False
|
||||||
|
|
||||||
self.assertEqual("00:20", duration(20))
|
|
||||||
self.assertEqual("00:20m", duration(20, unit=True))
|
|
||||||
self.assertEqual("00:20m", duration(20, compact=True, unit=True))
|
|
||||||
|
|
||||||
self.assertEqual("n/a", duration(-1))
|
def test_list_from_None():
|
||||||
|
assert aslist(None) == []
|
||||||
|
|
||||||
def test_seconds(self):
|
|
||||||
self.assertEqual(10, seconds(10))
|
|
||||||
self.assertEqual(10, seconds("10"))
|
|
||||||
|
|
||||||
self.assertEqual(300, seconds("5m"))
|
def test_list_from_list():
|
||||||
self.assertEqual(320, seconds("5m20s"))
|
assert aslist([1, 2, 3] == [1, 2, 3])
|
||||||
|
|
||||||
self.assertEqual(4 * 3600, seconds("4h"))
|
|
||||||
self.assertEqual(4 * 3600 + 5 * 60 + 22, seconds("4h5m22s"))
|
|
||||||
|
|
||||||
self.assertEqual(4 * 3600 + 5 * 60, seconds("4h5m"))
|
def test_list_from_str():
|
||||||
|
assert aslist("12,13,14") == ["12", "13", "14"]
|
||||||
|
|
||||||
def test_temperature(self):
|
|
||||||
self.assertEqual("10°C", astemperature(10))
|
def test_byteformat():
|
||||||
self.assertEqual("10°C", astemperature(10, "metric"))
|
assert byte(500) == "500.00B"
|
||||||
self.assertEqual("-100°F", astemperature(-100, "imperial"))
|
assert byte(1024) == "1.00KiB"
|
||||||
self.assertEqual("-100°K", astemperature("-100", "kelvin"))
|
assert byte(1024, "{:.0f}") == "1KiB"
|
||||||
|
assert byte(1024 + 512) == "1.50KiB"
|
||||||
|
assert byte(1024 * 1024 * 2 + 1024 * 512) == "2.50MiB"
|
||||||
|
assert byte(1024 * 1024 * 1024 * 4 + 1024 * 1024 * 512) == "4.50GiB"
|
||||||
|
assert byte(1024 * 1024 * 1024 * 1024 * 2) == "2048.00GiB"
|
||||||
|
|
||||||
|
|
||||||
|
def test_duration():
|
||||||
|
assert duration(4 * 60 * 60 + 20 * 60) == "04:20:00"
|
||||||
|
assert duration(4 * 60 * 60 + 20 * 60, unit=True) == "04:20:00h"
|
||||||
|
assert duration(4 * 60 * 60 + 20 * 60, compact=True, unit=True) == "04:20h"
|
||||||
|
|
||||||
|
assert duration(20 * 60) == "20:00"
|
||||||
|
assert duration(20 * 60, unit=True) == "20:00m"
|
||||||
|
assert duration(20 * 60, compact=True, unit=True) == "20:00m"
|
||||||
|
|
||||||
|
assert duration(20) == "00:20"
|
||||||
|
assert duration(20, unit=True) == "00:20m"
|
||||||
|
assert duration(20, compact=True, unit=True) == "00:20m"
|
||||||
|
|
||||||
|
assert duration(-1) == "n/a"
|
||||||
|
|
||||||
|
|
||||||
|
def test_seconds():
|
||||||
|
assert seconds(10) == 10
|
||||||
|
assert seconds("10") == 10
|
||||||
|
|
||||||
|
assert seconds("5m") == 300
|
||||||
|
assert seconds("5m20s") == 320
|
||||||
|
|
||||||
|
assert seconds("4h") == 4 * 3600
|
||||||
|
assert seconds("4h5m22s") == 4 * 3600 + 5 * 60 + 22
|
||||||
|
|
||||||
|
assert seconds("4h5m") == 4 * 3600 + 5 * 60
|
||||||
|
|
||||||
|
|
||||||
|
def test_temperature():
|
||||||
|
assert astemperature(10) == "10°C"
|
||||||
|
assert astemperature(10, "metric") == "10°C"
|
||||||
|
assert astemperature(-100, "imperial") == "-100°F"
|
||||||
|
assert astemperature(-100, "kelvin") == "-100°K"
|
||||||
|
|
||||||
|
|
||||||
# vim: tabstop=8 expandtab shiftwidth=4 softtabstop=4
|
# vim: tabstop=8 expandtab shiftwidth=4 softtabstop=4
|
||||||
|
|
|
@ -1,51 +1,57 @@
|
||||||
import unittest
|
import pytest
|
||||||
import json
|
import json
|
||||||
import urllib.request
|
|
||||||
|
|
||||||
import util.location
|
import util.location
|
||||||
|
|
||||||
|
|
||||||
class location(unittest.TestCase):
|
@pytest.fixture
|
||||||
def setUp(self):
|
def urllib_req(mocker):
|
||||||
patcher = unittest.mock.patch("util.location.urllib.request")
|
util.location.reset()
|
||||||
self.addCleanup(patcher.stop)
|
return mocker.patch("util.location.urllib.request")
|
||||||
self.request = patcher.start()
|
|
||||||
util.location.reset()
|
|
||||||
|
|
||||||
self.primary = {
|
|
||||||
"country": "Middle Earth",
|
|
||||||
"longitude": "10.0",
|
|
||||||
"latitude": "20.5",
|
|
||||||
"ip": "127.0.0.1",
|
|
||||||
}
|
|
||||||
self.secondary = {
|
|
||||||
"country_name": "Rivia",
|
|
||||||
"longitude": "-10.0",
|
|
||||||
"latitude": "-23",
|
|
||||||
"ip": "127.0.0.6",
|
|
||||||
}
|
|
||||||
|
|
||||||
def test_primary_provider(self):
|
@pytest.fixture
|
||||||
self.request.urlopen.return_value.read.return_value = json.dumps(self.primary)
|
def primaryLocation():
|
||||||
util.location.country()
|
return {
|
||||||
self.assertEqual(self.primary["country"], util.location.country())
|
"country": "Middle Earth",
|
||||||
self.assertEqual(
|
"longitude": "10.0",
|
||||||
(self.primary["latitude"], self.primary["longitude"]),
|
"latitude": "20.5",
|
||||||
util.location.coordinates(),
|
"ip": "127.0.0.1",
|
||||||
)
|
}
|
||||||
self.assertEqual(self.primary["ip"], util.location.public_ip())
|
|
||||||
|
|
||||||
def test_secondary_provider(self):
|
|
||||||
urlopen = unittest.mock.MagicMock()
|
|
||||||
urlopen.read.return_value = json.dumps(self.secondary)
|
|
||||||
self.request.urlopen.side_effect = [RuntimeError(), urlopen]
|
|
||||||
|
|
||||||
self.assertEqual(self.secondary["country_name"], util.location.country())
|
@pytest.fixture
|
||||||
self.assertEqual(
|
def secondaryLocation():
|
||||||
(self.secondary["latitude"], self.secondary["longitude"]),
|
return {
|
||||||
util.location.coordinates(),
|
"country_name": "Rivia",
|
||||||
)
|
"longitude": "-10.0",
|
||||||
self.assertEqual(self.secondary["ip"], util.location.public_ip())
|
"latitude": "-23",
|
||||||
|
"ip": "127.0.0.6",
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
def test_primary_provider(urllib_req, primaryLocation):
|
||||||
|
urllib_req.urlopen.return_value.read.return_value = json.dumps(primaryLocation)
|
||||||
|
|
||||||
|
assert util.location.country() == primaryLocation["country"]
|
||||||
|
assert util.location.coordinates() == (
|
||||||
|
primaryLocation["latitude"],
|
||||||
|
primaryLocation["longitude"],
|
||||||
|
)
|
||||||
|
assert util.location.public_ip() == primaryLocation["ip"]
|
||||||
|
|
||||||
|
|
||||||
|
def test_secondary_provider(mocker, urllib_req, secondaryLocation):
|
||||||
|
urlopen = mocker.MagicMock()
|
||||||
|
urlopen.read.return_value = json.dumps(secondaryLocation)
|
||||||
|
urllib_req.urlopen.side_effect = [RuntimeError(), urlopen]
|
||||||
|
|
||||||
|
assert util.location.country() == secondaryLocation["country_name"]
|
||||||
|
assert util.location.coordinates() == (
|
||||||
|
secondaryLocation["latitude"],
|
||||||
|
secondaryLocation["longitude"],
|
||||||
|
)
|
||||||
|
assert util.location.public_ip() == secondaryLocation["ip"]
|
||||||
|
|
||||||
|
|
||||||
# vim: tabstop=8 expandtab shiftwidth=4 softtabstop=4
|
# vim: tabstop=8 expandtab shiftwidth=4 softtabstop=4
|
||||||
|
|
|
@ -1,62 +1,48 @@
|
||||||
import unittest
|
import pytest
|
||||||
|
|
||||||
import util.store
|
import util.store
|
||||||
|
|
||||||
|
|
||||||
class store(unittest.TestCase):
|
@pytest.fixture
|
||||||
def setUp(self):
|
def emptyStore():
|
||||||
self.store = util.store.Store()
|
return util.store.Store()
|
||||||
|
|
||||||
self.unusedKey = "someRandomUnusedKey"
|
|
||||||
self.someKey = "someRandomKey"
|
|
||||||
self.someOtherKey = "anotherRandomKey"
|
|
||||||
self.someValue = "someRandomValue"
|
|
||||||
self.someOtherValue = "anotherRandomValue"
|
|
||||||
|
|
||||||
def test_get_of_unset_key(self):
|
@pytest.fixture
|
||||||
self.assertEqual(
|
def store():
|
||||||
None, self.store.get(self.unusedKey), "default value expected to be None"
|
return util.store.Store()
|
||||||
)
|
|
||||||
self.assertEqual(
|
|
||||||
self.someValue,
|
|
||||||
self.store.get(self.unusedKey, self.someValue),
|
|
||||||
"wrong user-provided default value returned",
|
|
||||||
)
|
|
||||||
|
|
||||||
def test_get_of_set_key(self):
|
|
||||||
self.assertNotEqual(self.someValue, None)
|
|
||||||
|
|
||||||
self.store.set(self.someKey, self.someValue)
|
def test_get_of_unset_key(emptyStore):
|
||||||
self.assertEqual(
|
assert emptyStore.get("any-key") == None
|
||||||
self.someValue,
|
assert emptyStore.get("any-key", "default-value") == "default-value"
|
||||||
self.store.get(self.someKey),
|
|
||||||
"unexpected value for existing key",
|
|
||||||
)
|
|
||||||
|
|
||||||
def test_overwrite_set(self):
|
|
||||||
self.assertNotEqual(self.someValue, None)
|
|
||||||
self.assertNotEqual(self.someOtherValue, self.someValue)
|
|
||||||
|
|
||||||
self.store.set(self.someKey, self.someValue)
|
def test_get_of_set_key(store):
|
||||||
self.store.set(self.someKey, self.someOtherValue)
|
store.set("key", "value")
|
||||||
self.assertEqual(
|
assert store.get("key") == "value"
|
||||||
self.someOtherValue,
|
|
||||||
self.store.get(self.someKey),
|
|
||||||
"unexpected value for existing key",
|
|
||||||
)
|
|
||||||
|
|
||||||
def test_unused_keys(self):
|
|
||||||
self.assertNotEqual(self.someKey, self.someOtherKey)
|
|
||||||
|
|
||||||
self.store.set(self.someKey, self.someValue)
|
def test_overwrite_set(store):
|
||||||
self.store.set(self.someOtherKey, self.someOtherValue)
|
store.set("key", "value 1")
|
||||||
|
store.set("key", "value 2")
|
||||||
|
|
||||||
self.assertEqual(
|
assert store.get("key") == "value 2"
|
||||||
sorted(self.store.unused_keys()), sorted([self.someKey, self.someOtherKey])
|
|
||||||
)
|
|
||||||
|
|
||||||
self.store.get(self.someKey)
|
|
||||||
self.assertEqual(self.store.unused_keys(), [self.someOtherKey])
|
def test_unused_keys(store):
|
||||||
|
store.set("key 1", "value x")
|
||||||
|
store.set("key 2", "value y")
|
||||||
|
|
||||||
|
assert store.unused_keys() == sorted(["key 1", "key 2"])
|
||||||
|
|
||||||
|
store.get("key 2")
|
||||||
|
|
||||||
|
assert store.unused_keys() == ["key 1"]
|
||||||
|
|
||||||
|
store.get("key 1")
|
||||||
|
|
||||||
|
assert store.unused_keys() == []
|
||||||
|
|
||||||
|
|
||||||
# vim: tabstop=8 expandtab shiftwidth=4 softtabstop=4
|
# vim: tabstop=8 expandtab shiftwidth=4 softtabstop=4
|
||||||
|
|
Loading…
Reference in a new issue