From a7e756e015cc37e1fbe2ef6b4e5258fa198a9060 Mon Sep 17 00:00:00 2001 From: Tobi-wan Kenobi Date: Fri, 9 Dec 2016 07:27:01 +0100 Subject: [PATCH] [tests] Generic module tests Add a helper function that lists all existing modules and modify the CPU module test so that it now generically iterates all available modules and tests their widgets. see #23 --- bumblebee/engine.py | 13 +++++++++++++ tests/modules/test_cpu.py | 23 ----------------------- tests/modules/test_modules.py | 31 +++++++++++++++++++++++++++++++ tests/util.py | 3 +++ 4 files changed, 47 insertions(+), 23 deletions(-) delete mode 100644 tests/modules/test_cpu.py create mode 100644 tests/modules/test_modules.py diff --git a/bumblebee/engine.py b/bumblebee/engine.py index c5b6266..42a02e5 100644 --- a/bumblebee/engine.py +++ b/bumblebee/engine.py @@ -1,8 +1,21 @@ """Core application engine""" +import os import time +import pkgutil import importlib import bumblebee.error +import bumblebee.modules + +def modules(): + """Return a list of available modules""" + result = [] + path = os.path.dirname(bumblebee.modules.__file__) + for mod in [name for _, name, _ in pkgutil.iter_modules([path])]: + result.append({ + "name": mod + }) + return result class Module(object): """Module instance base class diff --git a/tests/modules/test_cpu.py b/tests/modules/test_cpu.py deleted file mode 100644 index 9ec7ecb..0000000 --- a/tests/modules/test_cpu.py +++ /dev/null @@ -1,23 +0,0 @@ -# pylint: disable=C0103,C0111 - -import unittest - -from bumblebee.modules.cpu import Module -from tests.util import assertWidgetAttributes - -class TestCPUModule(unittest.TestCase): - def setUp(self): - self.module = Module(None) - - def test_widgets(self): - widgets = self.module.widgets() - for widget in widgets: - assertWidgetAttributes(self, widget) - - def test_update(self): - widgets = self.module.widgets() - self.module.update(widgets) - self.test_widgets() - self.assertEquals(widgets, self.module.widgets()) - -# vim: tabstop=8 expandtab shiftwidth=4 softtabstop=4 diff --git a/tests/modules/test_modules.py b/tests/modules/test_modules.py new file mode 100644 index 0000000..7cdb48b --- /dev/null +++ b/tests/modules/test_modules.py @@ -0,0 +1,31 @@ +# pylint: disable=C0103,C0111 + +import unittest +import importlib + +from bumblebee.modules.cpu import Module +from bumblebee.engine import modules +from tests.util import assertWidgetAttributes, MockEngine + +class TestGenericModules(unittest.TestCase): + def setUp(self): + engine = MockEngine() + self.objects = {} + for mod in modules(): + cls = importlib.import_module("bumblebee.modules.{}".format(mod["name"])) + self.objects[mod["name"]] = getattr(cls, "Module")(engine) + + def test_widgets(self): + for mod in self.objects: + widgets = self.objects[mod].widgets() + for widget in widgets: + assertWidgetAttributes(self, widget) + + def test_update(self): + for mod in self.objects: + widgets = self.objects[mod].widgets() + self.objects[mod].update(widgets) + self.test_widgets() + self.assertEquals(widgets, self.objects[mod].widgets()) + +# vim: tabstop=8 expandtab shiftwidth=4 softtabstop=4 diff --git a/tests/util.py b/tests/util.py index 334664b..3779c1c 100644 --- a/tests/util.py +++ b/tests/util.py @@ -6,6 +6,9 @@ def assertWidgetAttributes(test, widget): test.assertTrue(isinstance(widget, Widget)) test.assertTrue(hasattr(widget, "full_text")) +class MockEngine(object): + pass + class MockOutput(object): def start(self): pass