This commit adds a basic test to check callbacks work.

This can also serve as an exemple.
This commit is contained in:
Matthieu Coudron 2014-12-21 13:31:40 +01:00
parent 12546736a6
commit c5933a3b26
2 changed files with 71 additions and 2 deletions

View File

@ -40,8 +40,8 @@ class Module(SettingsBase):
It then triggers the callback depending on the nature (ie type) of
the callback variable:
1. if null callback, do nothing
2. if it's a python function
3. if it's a method of the current module
2. if it's a python function ()
3. if it's the name of a method of the current module (string)
To setup the callbacks, you can set the settings 'on_leftclick',
'on_rightclick', 'on_upscroll', 'on_downscroll'.
@ -58,6 +58,7 @@ class Module(SettingsBase):
],
on_leftclick= ["urxvtc"] , # launch urxvtc on left click
on_rightclick= ["scroll_format", 2] , # update format by steps of 2
on_upscroll= [print, "hello world"] , # call python function print
log_level=logging.DEBUG,
)
"""

68
tests/test_callbacks.py Normal file
View File

@ -0,0 +1,68 @@
import unittest
from i3pystatus.core.modules import Module
class CallbacksMetaTest(unittest.TestCase):
valid_msg_count = "hello world"
notmuch_config = "notmuch-config"
@staticmethod
def send_clicks(module):
# simulate click events for buttons 1 to 10
for button in range(1, 10):
module.on_click(button)
class NoDefaultCounter(Module):
counter = 0
def update_counter(self, step=1):
self.counter = self.counter + step
class WithDefaultsCounter(NoDefaultCounter):
on_leftclick = "update_counter"
on_rightclick = ["update_counter", 2]
on_upscroll = ["callable_callback", "i3 is", "awesome"]
on_downscroll = ["update_counter", -1]
def test_count_is_correct(self):
"""Checks module works in the intended way, ie increase correctly
a counter"""
counter = self.NoDefaultCounter()
self.assertEqual(counter.counter, 0)
counter.update_counter(1)
self.assertEqual(counter.counter, 1)
counter.update_counter(2)
self.assertEqual(counter.counter, 3)
counter.update_counter(-2)
self.assertEqual(counter.counter, 1)
CallbacksMetaTest.send_clicks(counter)
# retcode, out, err = run_through_shell("notmuch --config=%s new" % (self.notmuch_config), enable_shell=True)
# self.assertEqual(out.strip(), self.valid_output)
def test_callback_set_post_instanciation(self):
counter = self.NoDefaultCounter()
# set callbacks
counter.on_leftclick = "update_counter"
counter.on_rightclick = ["update_counter", 2]
counter.on_upscroll = [print, "i3 is", "awesome"]
counter.on_downscroll = ["update_counter", -1]
self.assertEqual(counter.counter, 0)
counter.on_click(1) # left_click
self.assertEqual(counter.counter, 1)
counter.on_click(2) # middle button
self.assertEqual(counter.counter, 1)
counter.on_click(3) # right click
self.assertEqual(counter.counter, 3)
counter.on_click(4) # upscroll
self.assertEqual(counter.counter, 3)
counter.on_click(5) # downscroll
self.assertEqual(counter.counter, 2)