From 70f138b97bb10380b6b6f041fbb36fe43020d13a Mon Sep 17 00:00:00 2001 From: Tobi-wan Kenobi Date: Sun, 5 Mar 2017 08:35:15 +0100 Subject: [PATCH] [tests/i3bar-input] Add input tests for i3bar protocol Also, replaced the MockModule with a generic mock object. --- tests/mocks.py | 33 ++++++++++++++++++++++++--------- tests/test_i3barinput.py | 6 ++++-- tests/test_theme.py | 13 +++++++++---- 3 files changed, 37 insertions(+), 15 deletions(-) diff --git a/tests/mocks.py b/tests/mocks.py index 3a5aac3..62a5700 100644 --- a/tests/mocks.py +++ b/tests/mocks.py @@ -1,14 +1,31 @@ # pylint: disable=C0103,C0111 import mock +import json import shlex import subprocess from bumblebee.output import Widget +import random, string + +def rand(cnt): + return "".join(random.choice(string.lowercase) for i in range(cnt)) + +def mouseEvent(stdin, button, inp, module=None, instance=None): + stdin.readline.return_value = json.dumps({ + "name": module.id if module else rand(10), + "button": button, + "instance": instance + }) + inp.start() + inp.stop() + stdin.readline.assert_any_call() + class MockPopen(object): - def __init__(self, module): - self._patch = mock.patch("{}.subprocess.Popen".format(module)) + def __init__(self, module=""): + if len(module) > 0: module = "{}.".format(module) + self._patch = mock.patch("{}subprocess.Popen".format(module)) self._popen = self._patch.start() self.mock = mock.Mock() # for a nicer, more uniform interface @@ -72,18 +89,16 @@ class MockEngine(object): class MockWidget(Widget): def __init__(self, text): super(MockWidget, self).__init__(text) - self._text = text self.module = None self.attr_state = ["state-default"] - self.id = "none" + self.id = rand(10) - def state(self): - return self.attr_state + self.full_text(text) + +# def state(self): +# return self.attr_state def update(self, widgets): pass - def full_text(self): - return self._text - # vim: tabstop=8 expandtab shiftwidth=4 softtabstop=4 diff --git a/tests/test_i3barinput.py b/tests/test_i3barinput.py index fe41753..5b4bfb1 100644 --- a/tests/test_i3barinput.py +++ b/tests/test_i3barinput.py @@ -25,8 +25,10 @@ class TestI3BarInput(unittest.TestCase): epoll.poll.return_value = [(self.stdin.fileno.return_value, 2)] - self.anyModule = mocks.MockModule() - self.anotherModule = mocks.MockModule() + self.anyModule = mock.Mock() + self.anyModule.id = mocks.rand(10) + self.anotherModule = mock.Mock() + self.anotherModule.id = mocks.rand(10) self.anyWidget = mocks.MockWidget("some-widget") self.anotherWidget = mocks.MockWidget("another-widget") self.anyData = self.invalidData = "any data" diff --git a/tests/test_theme.py b/tests/test_theme.py index 708fa08..474b66a 100644 --- a/tests/test_theme.py +++ b/tests/test_theme.py @@ -1,5 +1,6 @@ # pylint: disable=C0103,C0111,W0703 +import mock import unittest from bumblebee.theme import Theme from bumblebee.error import ThemeLoadError @@ -14,9 +15,15 @@ class TestTheme(unittest.TestCase): self.themedWidget = MockWidget("bla") self.theme = Theme(self.validThemeName) self.cycleTheme = Theme("test_cycle") + self.anyModule = mock.Mock() self.anyWidget = MockWidget("bla") self.anotherWidget = MockWidget("blub") + self.anyModule.state.return_value = "state-default" + + self.anyWidget.link_module(self.anyModule) + self.themedWidget.link_module(self.anyModule) + data = self.theme.data() self.widgetTheme = "test-widget" self.themedWidget.module = self.widgetTheme @@ -103,11 +110,9 @@ class TestTheme(unittest.TestCase): self.assertEquals(theme.fg(self.anyWidget), data["defaults"]["fg"]) self.assertEquals(theme.bg(self.anyWidget), data["defaults"]["bg"]) - self.anyWidget.attr_state = ["critical"] + self.anyModule.state.return_value = "critical" self.assertEquals(theme.fg(self.anyWidget), data["defaults"]["critical"]["fg"]) self.assertEquals(theme.bg(self.anyWidget), data["defaults"]["critical"]["bg"]) - - self.themedWidget.attr_state = ["critical"] self.assertEquals(theme.fg(self.themedWidget), data[self.widgetTheme]["critical"]["fg"]) # if elements are missing in the state theme, they are taken from the # widget theme instead (i.e. no fallback to a more general state theme) @@ -119,7 +124,7 @@ class TestTheme(unittest.TestCase): def test_list(self): theme = self.theme data = theme.data()[self.widgetTheme]["cycle-test"]["fg"] - self.themedWidget.attr_state = ["cycle-test"] + self.anyModule.state.return_value = "cycle-test" self.assertTrue(len(data) > 1) for idx in range(0, len(data)):