[tests/battery] Add some tests for the battery module

see #23
This commit is contained in:
Tobi-wan Kenobi 2016-12-10 14:50:49 +01:00
parent 1b8385b33f
commit 163419063d
2 changed files with 67 additions and 3 deletions

View file

@ -24,7 +24,7 @@ class Module(bumblebee.engine.Module):
self._capacity = 100 self._capacity = 100
def capacity(self): def capacity(self):
return "{:02d}%".format(self._capacity) return "{:03d}%".format(self._capacity)
def update(self, widgets): def update(self, widgets):
widget = widgets[0] widget = widgets[0]
@ -38,9 +38,9 @@ class Module(bumblebee.engine.Module):
def state(self, widget): def state(self, widget):
state = [] state = []
if self._capacity < self.parameter("critical", 10): if self._capacity < int(self.parameter("critical", 10)):
state.append("critical") state.append("critical")
elif self._capacity < self.parameter("warning", 20): elif self._capacity < int(self.parameter("warning", 20)):
state.append("warning") state.append("warning")
if self._ac: if self._ac:

View file

@ -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