2016-12-04 12:26:20 +01:00
|
|
|
# pylint: disable=C0103,C0111
|
2016-12-04 12:53:18 +01:00
|
|
|
|
2016-12-04 12:26:20 +01:00
|
|
|
import json
|
|
|
|
import unittest
|
|
|
|
import mock
|
|
|
|
try:
|
|
|
|
from StringIO import StringIO
|
|
|
|
except ImportError:
|
|
|
|
from io import StringIO
|
|
|
|
|
|
|
|
from bumblebee.output import I3BarOutput
|
2016-12-04 12:53:18 +01:00
|
|
|
from tests.util import MockWidget
|
2016-12-08 11:31:20 +01:00
|
|
|
from tests.util import MockTheme
|
2016-12-04 12:26:20 +01:00
|
|
|
|
|
|
|
class TestI3BarOutput(unittest.TestCase):
|
|
|
|
def setUp(self):
|
2016-12-08 11:31:20 +01:00
|
|
|
self.theme = MockTheme()
|
|
|
|
self.output = I3BarOutput(self.theme)
|
2016-12-04 12:26:20 +01:00
|
|
|
self.expectedStart = json.dumps({"version": 1, "click_events": True}) + "[\n"
|
|
|
|
self.expectedStop = "]\n"
|
2016-12-04 12:53:18 +01:00
|
|
|
self.someWidget = MockWidget("foo bar baz")
|
2016-12-09 08:58:45 +01:00
|
|
|
self.anyColor = "#ababab"
|
|
|
|
self.anotherColor = "#cccccc"
|
2016-12-04 12:26:20 +01:00
|
|
|
|
|
|
|
@mock.patch("sys.stdout", new_callable=StringIO)
|
|
|
|
def test_start(self, stdout):
|
|
|
|
self.output.start()
|
|
|
|
self.assertEquals(self.expectedStart, stdout.getvalue())
|
|
|
|
|
|
|
|
@mock.patch("sys.stdout", new_callable=StringIO)
|
|
|
|
def test_stop(self, stdout):
|
|
|
|
self.output.stop()
|
|
|
|
self.assertEquals(self.expectedStop, stdout.getvalue())
|
|
|
|
|
2016-12-04 12:53:18 +01:00
|
|
|
@mock.patch("sys.stdout", new_callable=StringIO)
|
|
|
|
def test_draw_single_widget(self, stdout):
|
|
|
|
self.output.draw(self.someWidget)
|
2016-12-08 12:44:52 +01:00
|
|
|
self.output.flush()
|
2016-12-04 12:53:18 +01:00
|
|
|
result = json.loads(stdout.getvalue())[0]
|
2016-12-04 17:45:42 +01:00
|
|
|
self.assertEquals(result["full_text"], self.someWidget.full_text())
|
2016-12-04 12:53:18 +01:00
|
|
|
|
|
|
|
@mock.patch("sys.stdout", new_callable=StringIO)
|
|
|
|
def test_draw_multiple_widgets(self, stdout):
|
2016-12-08 12:44:52 +01:00
|
|
|
for widget in [self.someWidget, self.someWidget]:
|
|
|
|
self.output.draw(widget)
|
|
|
|
self.output.flush()
|
2016-12-04 12:53:18 +01:00
|
|
|
result = json.loads(stdout.getvalue())
|
|
|
|
for res in result:
|
2016-12-09 07:11:23 +01:00
|
|
|
self.assertEquals(res["full_text"], self.someWidget.full_text())
|
2016-12-04 12:53:18 +01:00
|
|
|
|
2016-12-04 16:14:43 +01:00
|
|
|
@mock.patch("sys.stdout", new_callable=StringIO)
|
2016-12-08 12:44:52 +01:00
|
|
|
def test_begin(self, stdout):
|
|
|
|
self.output.begin()
|
|
|
|
self.assertEquals("", stdout.getvalue())
|
|
|
|
|
|
|
|
@mock.patch("sys.stdout", new_callable=StringIO)
|
|
|
|
def test_end(self, stdout):
|
|
|
|
self.output.end()
|
2016-12-04 16:14:43 +01:00
|
|
|
self.assertEquals(",\n", stdout.getvalue())
|
|
|
|
|
2016-12-08 11:31:20 +01:00
|
|
|
@mock.patch("sys.stdout", new_callable=StringIO)
|
|
|
|
def test_prefix(self, stdout):
|
2016-12-09 11:42:02 +01:00
|
|
|
self.theme.attr_prefix = " - "
|
2016-12-08 11:31:20 +01:00
|
|
|
self.output.draw(self.someWidget)
|
2016-12-08 12:44:52 +01:00
|
|
|
self.output.flush()
|
2016-12-08 11:31:20 +01:00
|
|
|
result = json.loads(stdout.getvalue())[0]
|
|
|
|
self.assertEquals(result["full_text"], "{}{}".format(
|
2016-12-08 11:52:47 +01:00
|
|
|
self.theme.prefix(self.someWidget), self.someWidget.full_text())
|
2016-12-08 11:31:20 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
@mock.patch("sys.stdout", new_callable=StringIO)
|
|
|
|
def test_suffix(self, stdout):
|
2016-12-09 11:42:02 +01:00
|
|
|
self.theme.attr_suffix = " - "
|
2016-12-08 11:31:20 +01:00
|
|
|
self.output.draw(self.someWidget)
|
2016-12-08 12:44:52 +01:00
|
|
|
self.output.flush()
|
2016-12-08 11:31:20 +01:00
|
|
|
result = json.loads(stdout.getvalue())[0]
|
|
|
|
self.assertEquals(result["full_text"], "{}{}".format(
|
2016-12-08 11:52:47 +01:00
|
|
|
self.someWidget.full_text(), self.theme.suffix(self.someWidget))
|
2016-12-08 11:31:20 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
@mock.patch("sys.stdout", new_callable=StringIO)
|
|
|
|
def test_bothfix(self, stdout):
|
2016-12-09 11:42:02 +01:00
|
|
|
self.theme.attr_suffix = " - "
|
|
|
|
self.theme.attr_prefix = " * "
|
2016-12-08 11:31:20 +01:00
|
|
|
self.output.draw(self.someWidget)
|
2016-12-08 12:44:52 +01:00
|
|
|
self.output.flush()
|
2016-12-08 11:31:20 +01:00
|
|
|
result = json.loads(stdout.getvalue())[0]
|
|
|
|
self.assertEquals(result["full_text"], "{}{}{}".format(
|
2016-12-09 07:11:23 +01:00
|
|
|
self.theme.prefix(self.someWidget),
|
|
|
|
self.someWidget.full_text(),
|
|
|
|
self.theme.suffix(self.someWidget)
|
|
|
|
))
|
2016-12-04 16:14:43 +01:00
|
|
|
|
2016-12-09 08:58:45 +01:00
|
|
|
@mock.patch("sys.stdout", new_callable=StringIO)
|
|
|
|
def test_colors(self, stdout):
|
2016-12-09 11:42:02 +01:00
|
|
|
self.theme.attr_fg = self.anyColor
|
|
|
|
self.theme.attr_bg = self.anotherColor
|
2016-12-09 08:58:45 +01:00
|
|
|
self.output.draw(self.someWidget)
|
|
|
|
self.output.flush()
|
|
|
|
result = json.loads(stdout.getvalue())[0]
|
|
|
|
self.assertEquals(result["color"], self.anyColor)
|
|
|
|
self.assertEquals(result["background"], self.anotherColor)
|
|
|
|
|
2016-12-04 12:26:20 +01:00
|
|
|
# vim: tabstop=8 expandtab shiftwidth=4 softtabstop=4
|