summaryrefslogtreecommitdiff
path: root/test/test_text_buffer.py
blob: 65c6d9bf723b1516bab600b87159f9ccdf01ef9e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
"""
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]

@fixture(scope='function')
def msgs_doublejoin():
    join = MucOwnJoinMessage('join')
    msg1 = Message('1', 'd')
    msg2 = Message('2', 'f')
    join2 = MucOwnJoinMessage('join')
    return [join, msg1, msg2, join2]

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_doublejoin(buf2048, msgs_doublejoin):
    for msg in msgs_doublejoin:
        buf2048.add_message(msg)
    gap = buf2048.find_last_gap_muc()
    assert gap.leave_message == msgs_doublejoin[2]
    assert gap.join_message == msgs_doublejoin[3]


def test_find_gap_doublejoin_no_msg(buf2048):
    join1 = MucOwnJoinMessage('join')
    join2 = MucOwnJoinMessage('join')
    for msg in [join1, join2]:
        buf2048.add_message(msg)
    gap = buf2048.find_last_gap_muc()
    assert gap.leave_message is join1
    assert gap.join_message is join2


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_doublejoin(buf2048, msgs_doublejoin):
    for msg in msgs_doublejoin:
        buf2048.add_message(msg)
    gap = buf2048.find_last_gap_muc()
    assert buf2048.get_gap_index(gap) == 3


def test_get_gap_index_doublejoin_no_msg(buf2048):
    join1 = MucOwnJoinMessage('join')
    join2 = MucOwnJoinMessage('join')
    for msg in [join1, join2]:
        buf2048.add_message(msg)
    gap = buf2048.find_last_gap_muc()
    assert buf2048.get_gap_index(gap) == 1


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]