From 163419063d3d709269e458453bb2b83feb4afb86 Mon Sep 17 00:00:00 2001 From: Tobi-wan Kenobi Date: Sat, 10 Dec 2016 14:50:49 +0100 Subject: [PATCH] [tests/battery] Add some tests for the battery module see #23 --- bumblebee/modules/battery.py | 6 ++-- tests/modules/test_battery.py | 64 +++++++++++++++++++++++++++++++++++ 2 files changed, 67 insertions(+), 3 deletions(-) create mode 100644 tests/modules/test_battery.py diff --git a/bumblebee/modules/battery.py b/bumblebee/modules/battery.py index 2e945de..9fa4c6e 100644 --- a/bumblebee/modules/battery.py +++ b/bumblebee/modules/battery.py @@ -24,7 +24,7 @@ class Module(bumblebee.engine.Module): self._capacity = 100 def capacity(self): - return "{:02d}%".format(self._capacity) + return "{:03d}%".format(self._capacity) def update(self, widgets): widget = widgets[0] @@ -38,9 +38,9 @@ class Module(bumblebee.engine.Module): def state(self, widget): state = [] - if self._capacity < self.parameter("critical", 10): + if self._capacity < int(self.parameter("critical", 10)): state.append("critical") - elif self._capacity < self.parameter("warning", 20): + elif self._capacity < int(self.parameter("warning", 20)): state.append("warning") if self._ac: diff --git a/tests/modules/test_battery.py b/tests/modules/test_battery.py new file mode 100644 index 0000000..da2e3cc --- /dev/null +++ b/tests/modules/test_battery.py @@ -0,0 +1,64 @@ +# pylint: disable=C0103,C0111 + +import sys +import json +import unittest +import mock + +from contextlib import contextmanager + +import bumblebee.input +from bumblebee.input import I3BarInput +from bumblebee.modules.battery import Module +from tests.util import MockEngine, MockConfig, assertPopen + +class MockOpen(object): + def __init__(self): + self._value = "" + + def returns(self, value): + self._value = value + + def __enter__(self): + return self + + def __exit__(self, a, b, c): + pass + + def read(self): + return self._value + +class TestBatteryModule(unittest.TestCase): + def setUp(self): + self.engine = MockEngine() + self.config = MockConfig() + self.module = Module(engine=self.engine, config={ "config": self.config }) + for widget in self.module.widgets(): + widget.link_module(self.module) + + @mock.patch("sys.stdout") + def test_format(self, mock_output): + for widget in self.module.widgets(): + self.assertEquals(len(widget.full_text()), len("100%")) + + @mock.patch("{}.open".format("__builtin__" if sys.version_info[0] < 3 else "builtins")) + @mock.patch("subprocess.Popen") + def test_critical(self, mock_output, mock_open): + mock_open.return_value = MockOpen() + mock_open.return_value.returns("19") + self.config.set("battery.critical", "20") + self.config.set("battery.warning", "25") + self.module.update(self.module.widgets()) + self.assertTrue("critical" in self.module.widgets()[0].state()) + + @mock.patch("{}.open".format("__builtin__" if sys.version_info[0] < 3 else "builtins")) + @mock.patch("subprocess.Popen") + def test_warning(self, mock_output, mock_open): + mock_open.return_value = MockOpen() + mock_open.return_value.returns("22") + self.config.set("battery.critical", "20") + self.config.set("battery.warning", "25") + self.module.update(self.module.widgets()) + self.assertTrue("warning" in self.module.widgets()[0].state()) + +# vim: tabstop=8 expandtab shiftwidth=4 softtabstop=4