2020-01-19 13:29:34 +01:00
|
|
|
import json
|
2020-02-02 14:18:13 +01:00
|
|
|
import unittest
|
2020-01-01 21:22:31 +01:00
|
|
|
|
2020-03-23 15:40:53 +01:00
|
|
|
import core.config
|
2020-01-19 13:29:34 +01:00
|
|
|
import core.output
|
2020-02-02 14:18:13 +01:00
|
|
|
import core.module
|
|
|
|
|
2020-05-03 11:15:52 +02:00
|
|
|
|
2020-02-02 14:18:13 +01:00
|
|
|
class TestModule(core.module.Module):
|
|
|
|
pass
|
2020-01-01 21:22:31 +01:00
|
|
|
|
2020-05-03 11:15:52 +02:00
|
|
|
|
2020-01-19 13:29:34 +01:00
|
|
|
class i3(unittest.TestCase):
|
2020-01-01 21:22:31 +01:00
|
|
|
def setUp(self):
|
2020-01-19 13:29:34 +01:00
|
|
|
self.i3 = core.output.i3()
|
2020-02-02 14:18:13 +01:00
|
|
|
widget = unittest.mock.MagicMock()
|
|
|
|
widget.full_text.return_value = "test"
|
2020-05-03 11:15:52 +02:00
|
|
|
self.someModule = TestModule(
|
|
|
|
config=core.config.Config([]), widgets=[widget, widget, widget]
|
|
|
|
)
|
|
|
|
self.paddedTheme = core.theme.Theme(raw_data={"defaults": {"padding": " "}})
|
|
|
|
self.separator = "***"
|
|
|
|
self.separatorTheme = core.theme.Theme(
|
|
|
|
raw_data={
|
|
|
|
"defaults": {"separator": self.separator, "fg": "red", "bg": "blue"}
|
|
|
|
}
|
|
|
|
)
|
2020-04-07 20:59:26 +02:00
|
|
|
self.someBlock = core.output.block(
|
|
|
|
theme=self.separatorTheme,
|
2020-05-03 11:15:52 +02:00
|
|
|
module=self.someModule,
|
|
|
|
widget=self.someModule.widget(),
|
2020-04-07 20:59:26 +02:00
|
|
|
)
|
2020-01-19 13:29:34 +01:00
|
|
|
|
|
|
|
def test_start(self):
|
2020-02-22 13:42:44 +01:00
|
|
|
core.event.clear()
|
|
|
|
|
2020-02-01 21:37:38 +01:00
|
|
|
all_data = self.i3.start()
|
2020-05-03 11:15:52 +02:00
|
|
|
data = all_data["blocks"]
|
|
|
|
self.assertEqual(1, data["version"], "i3bar protocol version 1 expected")
|
|
|
|
self.assertTrue(data["click_events"], "click events should be enabled")
|
|
|
|
self.assertEqual("\n[", all_data["suffix"])
|
2020-01-19 13:29:34 +01:00
|
|
|
|
|
|
|
def test_stop(self):
|
2020-05-03 11:15:52 +02:00
|
|
|
self.assertEqual(
|
|
|
|
"\n]", self.i3.stop()["suffix"], "wrong i3bar protocol during stop"
|
|
|
|
)
|
2020-02-02 14:18:13 +01:00
|
|
|
|
|
|
|
def test_no_modules_by_default(self):
|
2020-05-03 11:15:52 +02:00
|
|
|
self.assertEqual(
|
|
|
|
0, len(self.i3.modules()), "module list should be empty by default"
|
|
|
|
)
|
2020-02-02 14:18:13 +01:00
|
|
|
|
|
|
|
def test_register_single_module(self):
|
|
|
|
self.i3.modules(self.someModule)
|
2020-05-03 11:15:52 +02:00
|
|
|
self.assertEqual(
|
|
|
|
1, len(self.i3.modules()), "setting single module does not work"
|
|
|
|
)
|
2020-02-02 14:18:13 +01:00
|
|
|
|
|
|
|
def test_register_multiple_modules(self):
|
2020-05-03 11:15:52 +02:00
|
|
|
self.i3.modules([self.someModule, self.someModule, self.someModule])
|
|
|
|
self.assertEqual(3, len(self.i3.modules()), "setting module list does not work")
|
2020-02-02 14:18:13 +01:00
|
|
|
|
|
|
|
def test_draw_existing_module(self):
|
2020-05-03 11:15:52 +02:00
|
|
|
self.i3.test_draw = unittest.mock.MagicMock(
|
|
|
|
return_value={"blocks": {"test": True}, "suffix": "end"}
|
|
|
|
)
|
|
|
|
self.i3.draw("test_draw")
|
2020-02-02 14:18:13 +01:00
|
|
|
self.i3.test_draw.assert_called_once_with()
|
|
|
|
|
|
|
|
def test_empty_status_line(self):
|
|
|
|
data = self.i3.statusline()
|
2020-05-03 11:15:52 +02:00
|
|
|
self.assertEqual(
|
|
|
|
[], data["blocks"], "expected empty list of status line entries"
|
|
|
|
)
|
|
|
|
self.assertEqual(",", data["suffix"], 'expected "," as suffix')
|
2020-02-01 21:37:38 +01:00
|
|
|
|
2020-02-02 14:18:13 +01:00
|
|
|
def test_statusline(self):
|
2020-05-03 11:15:52 +02:00
|
|
|
self.i3.modules([self.someModule, self.someModule, self.someModule])
|
2020-02-09 13:25:34 +01:00
|
|
|
self.i3.update()
|
2020-02-02 14:18:13 +01:00
|
|
|
data = self.i3.statusline()
|
2020-05-03 11:15:52 +02:00
|
|
|
self.assertEqual(
|
|
|
|
len(self.someModule.widgets()) * 3,
|
|
|
|
len(data["blocks"]),
|
|
|
|
"wrong number of widgets",
|
|
|
|
)
|
2020-01-19 13:29:34 +01:00
|
|
|
|
2020-03-01 14:08:16 +01:00
|
|
|
def test_padding(self):
|
|
|
|
self.i3.theme(self.paddedTheme)
|
2020-05-03 11:15:52 +02:00
|
|
|
blk = core.output.block(
|
|
|
|
self.i3.theme(), self.someModule, self.someModule.widget()
|
|
|
|
)
|
|
|
|
blk.set("full_text", "abc")
|
|
|
|
result = blk.dict()["full_text"]
|
|
|
|
self.assertEqual(" abc ", result)
|
2020-03-01 14:08:16 +01:00
|
|
|
|
2020-03-31 20:57:09 +02:00
|
|
|
def test_no_separator(self):
|
2020-06-20 14:51:49 +02:00
|
|
|
result = self.i3.separator_block(self.someModule, self.someModule.widget())
|
2020-03-31 20:57:09 +02:00
|
|
|
self.assertEqual([], result)
|
|
|
|
|
|
|
|
def test_separator(self):
|
|
|
|
self.i3.theme(self.separatorTheme)
|
2020-06-20 14:51:49 +02:00
|
|
|
result = self.i3.separator_block(self.someModule, self.someModule.widget())
|
2020-03-31 20:57:09 +02:00
|
|
|
self.assertEqual(1, len(result))
|
2020-05-03 11:15:52 +02:00
|
|
|
self.assertEqual("***", result[0].dict()["full_text"])
|
|
|
|
self.assertTrue(result[0].dict().get("_decorator", False))
|
|
|
|
self.assertEqual(
|
|
|
|
self.separatorTheme.get("bg", self.someModule.widget()),
|
|
|
|
result[0].dict()["color"],
|
|
|
|
)
|
2020-04-07 20:41:09 +02:00
|
|
|
|
|
|
|
def test_dump_json(self):
|
|
|
|
obj = unittest.mock.MagicMock()
|
|
|
|
obj.dict = unittest.mock.MagicMock()
|
|
|
|
core.output.dump_json(obj)
|
|
|
|
obj.dict_assert_called_once_with()
|
2020-03-31 20:57:09 +02:00
|
|
|
|
2020-04-07 20:59:26 +02:00
|
|
|
def test_assign(self):
|
2020-05-03 11:15:52 +02:00
|
|
|
src = {"a": "x", "b": "y", "c": "z"}
|
2020-04-07 20:59:26 +02:00
|
|
|
dst = {}
|
|
|
|
|
2020-05-03 11:15:52 +02:00
|
|
|
core.output.assign(src, dst, "a")
|
|
|
|
self.assertEqual(dst["a"], src["a"])
|
2020-04-07 20:59:26 +02:00
|
|
|
|
2020-05-03 11:15:52 +02:00
|
|
|
core.output.assign(src, dst, "123", "b")
|
|
|
|
self.assertEqual(dst["123"], src["b"])
|
2020-04-07 20:59:26 +02:00
|
|
|
|
2020-05-03 11:15:52 +02:00
|
|
|
core.output.assign(src, dst, "blub", default="def")
|
|
|
|
self.assertEqual("def", dst["blub"])
|
2020-04-07 20:59:26 +02:00
|
|
|
|
|
|
|
def test_pango_detection(self):
|
|
|
|
self.assertFalse(self.someBlock.is_pango({}))
|
2020-05-03 11:15:52 +02:00
|
|
|
self.assertTrue(self.someBlock.is_pango({"pango": {}}))
|
2020-04-07 20:59:26 +02:00
|
|
|
|
|
|
|
def test_pangoize(self):
|
2020-05-03 11:15:52 +02:00
|
|
|
self.assertEqual("test", self.someBlock.pangoize("test"))
|
|
|
|
self.assertFalse("markup" in self.someBlock.dict())
|
2020-04-07 20:59:26 +02:00
|
|
|
|
2020-05-03 11:15:52 +02:00
|
|
|
pango = self.someBlock.pangoize(
|
|
|
|
{"pango": {"attr": "blub", "x": "y", "full_text": "test"}}
|
|
|
|
)
|
2020-04-13 13:43:07 +02:00
|
|
|
self.assertTrue('attr="blub"' in pango)
|
|
|
|
self.assertTrue('x="y"' in pango)
|
2020-05-03 11:15:52 +02:00
|
|
|
self.assertTrue("<span " in pango)
|
|
|
|
self.assertTrue(">test</span>" in pango)
|
|
|
|
self.assertEqual("pango", self.someBlock.dict()["markup"])
|
2020-04-07 20:59:26 +02:00
|
|
|
|
|
|
|
def test_padding(self):
|
2020-05-03 11:15:52 +02:00
|
|
|
self.someBlock.set("padding", "***")
|
|
|
|
self.someBlock.set("full_text", "test")
|
2020-04-07 20:59:26 +02:00
|
|
|
|
2020-05-03 11:15:52 +02:00
|
|
|
self.assertEqual("***test***", self.someBlock.dict()["full_text"])
|
2020-04-07 20:59:26 +02:00
|
|
|
|
|
|
|
def test_pre_suffix(self):
|
2020-05-03 11:15:52 +02:00
|
|
|
self.someBlock.set("padding", "*")
|
|
|
|
self.someBlock.set("prefix", "pre")
|
|
|
|
self.someBlock.set("suffix", "suf")
|
|
|
|
self.someBlock.set("full_text", "test")
|
|
|
|
|
|
|
|
self.assertEqual("*pre*test*suf*", self.someBlock.dict()["full_text"])
|
2020-04-07 20:59:26 +02:00
|
|
|
|
|
|
|
|
2020-01-19 13:29:34 +01:00
|
|
|
# vim: tabstop=8 expandtab shiftwidth=4 softtabstop=4
|