From e488b75737f0d691b606b4ac00afcae2ea4ed7b0 Mon Sep 17 00:00:00 2001 From: mathieui Date: Fri, 22 May 2020 01:31:01 +0200 Subject: Add tests for text_buffer --- test/test_text_buffer.py | 157 +++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 157 insertions(+) create mode 100644 test/test_text_buffer.py (limited to 'test') diff --git a/test/test_text_buffer.py b/test/test_text_buffer.py new file mode 100644 index 00000000..8e9829f4 --- /dev/null +++ b/test/test_text_buffer.py @@ -0,0 +1,157 @@ +""" +Tests for the TextBuffer class +""" +from pytest import fixture + +from poezio.text_buffer import ( + TextBuffer, + HistoryGap, +) + +from poezio.ui.types import ( + Message, + BaseMessage, + MucOwnJoinMessage, + MucOwnLeaveMessage, +) + + +@fixture(scope='function') +def buf2048(): + return TextBuffer(2048) + +@fixture(scope='function') +def msgs_nojoin(): + msg1 = Message('1', 'q') + msg2 = Message('2', 's') + leave = MucOwnLeaveMessage('leave') + return [msg1, msg2, leave] + + +@fixture(scope='function') +def msgs_noleave(): + join = MucOwnJoinMessage('join') + msg3 = Message('3', 'd') + msg4 = Message('4', 'f') + return [join, msg3, msg4] + + +def test_last_message(buf2048): + msg = BaseMessage('toto') + buf2048.add_message(BaseMessage('titi')) + buf2048.add_message(msg) + assert buf2048.last_message is msg + + +def test_message_nb_limit(): + buf = TextBuffer(5) + for i in range(10): + buf.add_message(BaseMessage("%s" % i)) + assert len(buf.messages) == 5 + + +def test_find_gap(buf2048, msgs_noleave): + msg1 = Message('1', 'q') + msg2 = Message('2', 's') + leave = MucOwnLeaveMessage('leave') + join = MucOwnJoinMessage('join') + msg3 = Message('3', 'd') + msg4 = Message('4', 'f') + msgs = [msg1, msg2, leave, join, msg3, msg4] + for msg in msgs: + buf2048.add_message(msg) + gap = buf2048.find_last_gap_muc() + assert gap.leave_message == leave + assert gap.join_message == join + assert gap.last_timestamp_before_leave == msg2.time + assert gap.first_timestamp_after_join == msg3.time + + +def test_find_gap_already_filled(buf2048): + msg1 = Message('1', 'q') + msg2 = Message('2', 's') + leave = MucOwnLeaveMessage('leave') + msg5 = Message('5', 'g') + msg6 = Message('6', 'h') + join = MucOwnJoinMessage('join') + msg3 = Message('3', 'd') + msg4 = Message('4', 'f') + msgs = [msg1, msg2, leave, msg5, msg6, join, msg3, msg4] + for msg in msgs: + buf2048.add_message(msg) + assert buf2048.find_last_gap_muc() is None + + +def test_find_gap_noleave(buf2048, msgs_noleave): + for msg in msgs_noleave: + buf2048.add_message(msg) + gap = buf2048.find_last_gap_muc() + assert gap.leave_message is None + assert gap.last_timestamp_before_leave is None + assert gap.join_message == msgs_noleave[0] + assert gap.first_timestamp_after_join == msgs_noleave[1].time + + +def test_find_gap_nojoin(buf2048, msgs_nojoin): + for msg in msgs_nojoin: + buf2048.add_message(msg) + gap = buf2048.find_last_gap_muc() + assert gap.leave_message == msgs_nojoin[-1] + assert gap.join_message is None + assert gap.last_timestamp_before_leave == msgs_nojoin[1].time + + +def test_get_gap_index(buf2048): + msg1 = Message('1', 'q') + msg2 = Message('2', 's') + leave = MucOwnLeaveMessage('leave') + join = MucOwnJoinMessage('join') + msg3 = Message('3', 'd') + msg4 = Message('4', 'f') + msgs = [msg1, msg2, leave, join, msg3, msg4] + for msg in msgs: + buf2048.add_message(msg) + gap = buf2048.find_last_gap_muc() + assert buf2048.get_gap_index(gap) == 3 + + +def test_get_gap_index_nojoin(buf2048, msgs_nojoin): + for msg in msgs_nojoin: + buf2048.add_message(msg) + gap = buf2048.find_last_gap_muc() + assert buf2048.get_gap_index(gap) == 3 + + +def test_get_gap_index_noleave(buf2048, msgs_noleave): + for msg in msgs_noleave: + buf2048.add_message(msg) + gap = buf2048.find_last_gap_muc() + assert buf2048.get_gap_index(gap) == 0 + + +def test_add_history_messages(buf2048): + msg1 = Message('1', 'q') + msg2 = Message('2', 's') + leave = MucOwnLeaveMessage('leave') + join = MucOwnJoinMessage('join') + msg3 = Message('3', 'd') + msg4 = Message('4', 'f') + msgs = [msg1, msg2, leave, join, msg3, msg4] + for msg in msgs: + buf2048.add_message(msg) + msg5 = Message('5', 'g') + msg6 = Message('6', 'h') + gap = buf2048.find_last_gap_muc() + buf2048.add_history_messages([msg5, msg6], gap=gap) + assert buf2048.messages == [msg1, msg2, leave, msg5, msg6, join, msg3, msg4] + + +def test_add_history_empty(buf2048): + msg1 = Message('1', 'q') + msg2 = Message('2', 's') + msg3 = Message('3', 'd') + msg4 = Message('4', 'f') + buf2048.add_message(msg1) + buf2048.add_history_messages([msg2, msg3, msg4]) + assert buf2048.messages == [msg2, msg3, msg4, msg1] + -- cgit v1.2.3