diff options
author | mathieui <mathieui@mathieui.net> | 2019-09-28 18:35:50 +0200 |
---|---|---|
committer | mathieui <mathieui@mathieui.net> | 2020-05-09 19:46:17 +0200 |
commit | 32beeca48018422499b9c0af55a5c5b1eaa67d75 (patch) | |
tree | f5978ed173c38d295cfc142000bdec9cf5bc0826 /test/test_ui | |
parent | 80ce8453f50ccaad4d71fda8811ee33f5ffa3624 (diff) | |
download | poezio-32beeca48018422499b9c0af55a5c5b1eaa67d75.tar.gz poezio-32beeca48018422499b9c0af55a5c5b1eaa67d75.tar.bz2 poezio-32beeca48018422499b9c0af55a5c5b1eaa67d75.tar.xz poezio-32beeca48018422499b9c0af55a5c5b1eaa67d75.zip |
Add tests for the new modules
Diffstat (limited to 'test/test_ui')
-rw-r--r-- | test/test_ui/test_render.py | 124 | ||||
-rw-r--r-- | test/test_ui/test_types.py | 126 |
2 files changed, 250 insertions, 0 deletions
diff --git a/test/test_ui/test_render.py b/test/test_ui/test_render.py new file mode 100644 index 00000000..ae32059e --- /dev/null +++ b/test/test_ui/test_render.py @@ -0,0 +1,124 @@ +import pytest +from contextlib import contextmanager +from datetime import datetime +from poezio.theming import get_theme +from poezio.ui.render import build_lines, Line, write_pre +from poezio.ui.consts import SHORT_FORMAT +from poezio.ui.types import BaseMessage, Message, XMLLog + +def test_simple_build_basemsg(): + msg = BaseMessage(txt='coucou') + line = build_lines(msg, 100, True, 10)[0] + assert (line.start_pos, line.end_pos) == (0, 6) + + +def test_simple_render_message(): + msg = Message(txt='coucou', nickname='toto') + line = build_lines(msg, 100, True, 10)[0] + assert (line.start_pos, line.end_pos) == (0, 8) + + +def test_simple_render_xmllog(): + msg = XMLLog(txt='coucou', incoming=True) + line = build_lines(msg, 100, True, 10)[0] + assert (line.start_pos, line.end_pos) == (0, 6) + + +def test_simple_render_separator(): + line = build_lines(None, 100, True, 10)[0] + assert line is None + +class FakeBuffer: + def __init__(self): + self.text = '' + + @contextmanager + def colored_text(self, *args, **kwargs): + yield None + + def addstr(self, txt): + self.text += txt + +@pytest.fixture(scope='function') +def buffer(): + return FakeBuffer() + +@pytest.fixture +def time(): + return datetime.strptime('2019-09-27 10:11:12', '%Y-%m-%d %H:%M:%S') + +def test_write_pre_basemsg(buffer): + str_time = '10:11:12' + time = datetime.strptime(str_time, '%H:%M:%S') + msg = BaseMessage(txt='coucou', time=time) + + write_pre(msg, buffer, True, 10) + assert buffer.text == '10:11:12 ' + +def test_write_pre_message_simple(buffer, time): + msg = Message(txt='coucou', nickname='toto', time=time) + write_pre(msg, buffer, True, 10) + assert buffer.text == '10:11:12 toto> ' + + +def test_write_pre_message_simple_history(buffer, time): + msg = Message(txt='coucou', nickname='toto', time=time, history=True) + write_pre(msg, buffer, True, 10) + assert buffer.text == '2019-09-27 10:11:12 toto> ' + + +def test_write_pre_message_highlight(buffer, time): + msg = Message(txt='coucou', nickname='toto', time=time, highlight=True) + write_pre(msg, buffer, True, 10) + assert buffer.text == '10:11:12 toto> ' + +def test_write_pre_message_no_timestamp(buffer): + msg = Message(txt='coucou', nickname='toto') + write_pre(msg, buffer, False, 10) + assert buffer.text == 'toto> ' + + +def test_write_pre_message_me(buffer, time): + msg = Message(txt='/me coucou', nickname='toto', time=time) + write_pre(msg, buffer, True, 10) + assert buffer.text == '10:11:12 * toto ' + + +def test_write_pre_message_revisions(buffer, time): + msg = Message(txt='coucou', nickname='toto', time=time, revisions=5) + write_pre(msg, buffer, True, 10) + assert buffer.text == '10:11:12 toto5> ' + + +def test_write_pre_message_revisions_me(buffer, time): + msg = Message(txt='/me coucou', nickname='toto', time=time, revisions=5) + write_pre(msg, buffer, True, 10) + assert buffer.text == '10:11:12 * toto5 ' + + +def test_write_pre_message_ack(buffer, time): + ack = get_theme().CHAR_ACK_RECEIVED + expected = '10:11:12 %s toto> ' % ack + msg = Message(txt='coucou', nickname='toto', time=time, ack=1) + write_pre(msg, buffer, True, 10) + assert buffer.text == expected + + +def test_write_pre_message_nack(buffer, time): + nack = get_theme().CHAR_NACK + expected = '10:11:12 %s toto> ' % nack + msg = Message(txt='coucou', nickname='toto', time=time, ack=-1) + write_pre(msg, buffer, True, 10) + assert buffer.text == expected + + +def test_write_pre_xmllog_in(buffer): + msg = XMLLog(txt="coucou", incoming=True) + write_pre(msg, buffer, True, 10) + assert buffer.text == '%s IN ' % msg.time.strftime('%H:%M:%S') + + +def test_write_pre_xmllog_out(buffer): + msg = XMLLog(txt="coucou", incoming=False) + write_pre(msg, buffer, True, 10) + assert buffer.text == '%s OUT ' % msg.time.strftime('%H:%M:%S') diff --git a/test/test_ui/test_types.py b/test/test_ui/test_types.py new file mode 100644 index 00000000..e4c6c010 --- /dev/null +++ b/test/test_ui/test_types.py @@ -0,0 +1,126 @@ +import pytest +from datetime import datetime + +from poezio.ui.types import BaseMessage, Message, XMLLog + + +def test_create_message(): + now = datetime.now() + msg = Message( + txt="coucou", + nickname="toto", + ) + assert now < msg.time < datetime.now() + + msg = Message( + txt="coucou", + nickname="toto", + time=now, + ) + assert msg.time == now + + +def test_message_offset_simple(): + msg = Message( + txt="coucou", + nickname="toto", + ) + example = "10:10:10 toto> " + assert msg.compute_offset(True, 10) == len(example) + + msg = Message( + txt="coucou", + nickname="toto", + history=True, + ) + example = "2019:09:01 10:10:10 toto> " + assert msg.compute_offset(True, 10) == len(example) + +def test_message_offset_no_nick(): + msg = Message( + txt="coucou", + nickname="", + ) + example = "10:10:10 " + assert msg.compute_offset(True, 10) == len(example) + +def test_message_offset_ack(): + msg = Message( + txt="coucou", + nickname="toto", + ack=1, + ) + example = "10:10:10 V toto> " + assert msg.compute_offset(True, 10) == len(example) + + msg = Message( + txt="coucou", + nickname="toto", + ack=-1, + ) + example = "10:10:10 X toto> " + assert msg.compute_offset(True, 10) == len(example) + + +def test_message_offset_me(): + msg = Message( + txt="/me coucou", + nickname="toto", + ) + example = "10:10:10 * toto " + assert msg.compute_offset(True, 10) == len(example) + + +def test_message_offset_revisions(): + msg = Message( + txt="coucou", + nickname="toto", + revisions=3, + ) + example = "10:10:10 toto3> " + assert msg.compute_offset(True, 10) == len(example) + + msg = Message( + txt="coucou", + nickname="toto", + revisions=250, + ) + example = "10:10:10 toto250> " + assert msg.compute_offset(True, 10) == len(example) + + +def test_message_repr_works(): + msg1 = Message( + txt="coucou", + nickname="toto", + revisions=250, + ) + msg2 = Message( + txt="coucou", + nickname="toto", + old_message=msg1 + ) + + assert repr(msg2) is not None + +def test_xmllog_offset(): + msg = XMLLog( + txt='toto', + incoming=True, + ) + example = '10:10:10 IN ' + assert msg.compute_offset(True, 10) == len(example) + + msg = XMLLog( + txt='toto', + incoming=False, + ) + example = '10:10:10 OUT ' + assert msg.compute_offset(True, 10) == len(example) + +def test_basemessage_offset(): + msg = BaseMessage( + txt='coucou', + ) + example = '10:10:10 ' + assert msg.compute_offset(True, 10) == len(example) |