From 4bd13c2f63a8566cb32874749564e7aa12748b6c Mon Sep 17 00:00:00 2001 From: Tobi-wan Kenobi Date: Sun, 11 Dec 2016 08:01:16 +0100 Subject: [PATCH] [tests] Refactor i3barinput Use the assertMouseEvent helper in i3barinput. see #23 --- tests/test_i3barinput.py | 82 ++++++++-------------------------------- tests/util.py | 11 +++--- 2 files changed, 22 insertions(+), 71 deletions(-) diff --git a/tests/test_i3barinput.py b/tests/test_i3barinput.py index 57aa161..e2758d6 100644 --- a/tests/test_i3barinput.py +++ b/tests/test_i3barinput.py @@ -7,7 +7,7 @@ import mock import bumblebee.input from bumblebee.input import I3BarInput -from tests.util import MockWidget, MockModule, assertPopen +from tests.util import MockWidget, MockModule, assertPopen, assertMouseEvent class TestI3BarInput(unittest.TestCase): def setUp(self): @@ -57,109 +57,59 @@ class TestI3BarInput(unittest.TestCase): @mock.patch("select.select") @mock.patch("sys.stdin") def test_global_callback(self, mock_input, mock_select): - mock_select.return_value = (1,2,3) - mock_input.readline.return_value = json.dumps({ - "name": "somename", - "instance": "someinstance", - "button": bumblebee.input.LEFT_MOUSE, - }) self.input.register_callback(None, button=1, cmd=self.callback) - self.input.start() - self.assertEquals(self.input.stop(), True) - mock_input.readline.assert_any_call() + assertMouseEvent(mock_input, None, mock_select, self, None, + bumblebee.input.LEFT_MOUSE, None, "someinstance") self.assertTrue(self._called > 0) @mock.patch("select.select") @mock.patch("sys.stdin") def test_remove_global_callback(self, mock_input, mock_select): - mock_select.return_value = (1,2,3) - mock_input.readline.return_value = json.dumps({ - "name": "somename", - "instance": "someinstance", - "button": bumblebee.input.LEFT_MOUSE, - }) self.input.register_callback(None, button=1, cmd=self.callback) self.input.deregister_callbacks(None) - self.input.start() - self.assertEquals(self.input.stop(), True) - mock_input.readline.assert_any_call() + assertMouseEvent(mock_input, None, mock_select, self, None, + bumblebee.input.LEFT_MOUSE, None, "someinstance") self.assertTrue(self._called == 0) @mock.patch("select.select") @mock.patch("sys.stdin") def test_global_callback_button_missmatch(self, mock_input, mock_select): - mock_select.return_value = (1,2,3) - mock_input.readline.return_value = json.dumps({ - "name": "somename", - "instance": "someinstance", - "button": bumblebee.input.RIGHT_MOUSE, - }) - self.input.register_callback(None, button=1, cmd=self.callback) - self.input.start() - self.assertEquals(self.input.stop(), True) - mock_input.readline.assert_any_call() + self.input.register_callback(self.anyModule, button=1, cmd=self.callback) + assertMouseEvent(mock_input, None, mock_select, self, None, + bumblebee.input.RIGHT_MOUSE, None, "someinstance") self.assertTrue(self._called == 0) @mock.patch("select.select") @mock.patch("sys.stdin") def test_module_callback(self, mock_input, mock_select): - mock_select.return_value = (1,2,3) - mock_input.readline.return_value = json.dumps({ - "name": self.anyModule.id, - "instance": None, - "button": bumblebee.input.LEFT_MOUSE, - }) self.input.register_callback(self.anyModule, button=1, cmd=self.callback) - self.input.start() - self.assertEquals(self.input.stop(), True) - mock_input.readline.assert_any_call() + assertMouseEvent(mock_input, None, mock_select, self, self.anyModule, + bumblebee.input.LEFT_MOUSE, None) self.assertTrue(self._called > 0) @mock.patch("select.select") @mock.patch("sys.stdin") def test_remove_module_callback(self, mock_input, mock_select): - mock_select.return_value = (1,2,3) - mock_input.readline.return_value = json.dumps({ - "name": self.anyModule.id, - "instance": None, - "button": bumblebee.input.LEFT_MOUSE, - }) self.input.register_callback(self.anyModule, button=1, cmd=self.callback) self.input.deregister_callbacks(self.anyModule) - self.input.start() - self.assertEquals(self.input.stop(), True) - mock_input.readline.assert_any_call() + assertMouseEvent(mock_input, None, mock_select, self, None, + bumblebee.input.LEFT_MOUSE, None, self.anyWidget.id) self.assertTrue(self._called == 0) @mock.patch("select.select") @mock.patch("sys.stdin") def test_widget_callback(self, mock_input, mock_select): - mock_select.return_value = (1,2,3) - mock_input.readline.return_value = json.dumps({ - "name": "test", - "instance": self.anyWidget.id, - "button": bumblebee.input.LEFT_MOUSE, - }) self.input.register_callback(self.anyWidget, button=1, cmd=self.callback) - self.input.start() - self.assertEquals(self.input.stop(), True) - mock_input.readline.assert_any_call() + assertMouseEvent(mock_input, None, mock_select, self, None, + bumblebee.input.LEFT_MOUSE, None, self.anyWidget.id) self.assertTrue(self._called > 0) @mock.patch("select.select") @mock.patch("subprocess.Popen") @mock.patch("sys.stdin") def test_widget_cmd_callback(self, mock_input, mock_output, mock_select): - mock_select.return_value = (1,2,3) - mock_input.readline.return_value = json.dumps({ - "name": "test", - "instance": self.anyWidget.id, - "button": bumblebee.input.LEFT_MOUSE, - }) self.input.register_callback(self.anyWidget, button=1, cmd="echo") - self.input.start() - self.assertEquals(self.input.stop(), True) - mock_input.readline.assert_any_call() - assertPopen(mock_output, "echo") + assertMouseEvent(mock_input, mock_output, mock_select, self, None, + bumblebee.input.LEFT_MOUSE, "echo", self.anyWidget.id) # vim: tabstop=8 expandtab shiftwidth=4 softtabstop=4 diff --git a/tests/util.py b/tests/util.py index 4a0e16f..0b46d62 100644 --- a/tests/util.py +++ b/tests/util.py @@ -23,17 +23,18 @@ def assertStateContains(test, module, state): module.update(module.widgets()) test.assertTrue(state in module.widgets()[0].state()) -def assertMouseEvent(mock_input, mock_output, mock_select, engine, module, button, cmd): +def assertMouseEvent(mock_input, mock_output, mock_select, engine, module, button, cmd, instance_id=None): mock_input.readline.return_value = json.dumps({ - "name": module.id, + "name": module.id if module else "test", "button": button, - "instance": None + "instance": instance_id }) - mock_select.return_value = (1,2,3) + mock_select.return_value = (1, 2, 3) engine.input.start() engine.input.stop() mock_input.readline.assert_any_call() - assertPopen(mock_output, cmd) + if cmd: + assertPopen(mock_output, cmd) class MockInput(object): def start(self):