Merge lp:~julien-spautz/cable/message-history-refactor into lp:cable

Proposed by Julien Spautz
Status: Merged
Merged at revision: 135
Proposed branch: lp:~julien-spautz/cable/message-history-refactor
Merge into: lp:cable
Diff against target: 398 lines (+187/-139)
5 files modified
CMakeLists.txt (+2/-2)
src/Utils/MessageHistory.vala (+78/-0)
src/Utils/RRStringList.vala (+0/-77)
src/Widgets/Room.vala (+14/-21)
tests/TestMessageHistory.vala (+93/-39)
To merge this branch: bzr merge lp:~julien-spautz/cable/message-history-refactor
Reviewer Review Type Date Requested Status
Cable Developers Pending
Review via email: mp+185290@code.launchpad.net

Description of the change

Refactored the MessageHistory class and added more tests.

To post a comment you must log in.
Revision history for this message
Julien Spautz (julien-spautz) wrote :

Forgot to change CMakeLists.txt. Don't merge yet.

Revision history for this message
982c80311320c1b (alexander-wilms) wrote :

One detail that caught my attention is that when you browser the history and then repeatedly press <down>, you won't end up with an empty text entry, but the last message is still shown.

Revision history for this message
Julien Spautz (julien-spautz) wrote :

You mean in trunk? Because it should be fixed in this branch.

Revision history for this message
982c80311320c1b (alexander-wilms) wrote :

Yes, trunk. I thought you only refactored it.

132. By Julien Spautz

fixed CMakeLists.txt

Revision history for this message
982c80311320c1b (alexander-wilms) wrote :

Works as expected and all tests pass

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
1=== modified file 'CMakeLists.txt'
2--- CMakeLists.txt 2013-09-05 19:26:00 +0000
3+++ CMakeLists.txt 2013-09-13 14:24:42 +0000
4@@ -55,7 +55,7 @@
5 src/Utils/EmbeddedAlert.vala
6 src/Utils/Identity.vala
7 src/Utils/Network.vala
8- src/Utils/RRStringList.vala
9+ src/Utils/MessageHistory.vala
10
11 src/Irc/Maki.vala
12 src/Irc/Irc.vala
13@@ -118,7 +118,7 @@
14 tests/TestMessageHistory.vala
15
16 src/Utils/Utils.vala
17- src/Utils/RRStringList.vala
18+ src/Utils/MessageHistory.vala
19 PACKAGES
20 gtk+-3.0
21 granite
22
23=== added file 'src/Utils/MessageHistory.vala'
24--- src/Utils/MessageHistory.vala 1970-01-01 00:00:00 +0000
25+++ src/Utils/MessageHistory.vala 2013-09-13 14:24:42 +0000
26@@ -0,0 +1,78 @@
27+/***
28+ Copyright (C) 2013 Cable Developers
29+
30+ This program or library is free software; you can redistribute it
31+ and/or modify it under the terms of the GNU Lesser General Public
32+ License as published by the Free Software Foundation; either
33+ version 3 of the License, or (at your option) any later version.
34+
35+ This library is distributed in the hope that it will be useful,
36+ but WITHOUT ANY WARRANTY; without even the implied warranty of
37+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
38+ Lesser General Public License for more details.
39+
40+ You should have received a copy of the GNU Lesser General
41+ Public License along with this library; if not, write to the
42+ Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
43+ Boston, MA 02110-1301 USA.
44+***/
45+
46+public class Cable.Utils.MessageHistory {
47+
48+ Gee.List <string> data;
49+ int current;
50+
51+ public MessageHistory () {
52+ data = new Gee.ArrayList <string> ();
53+ current = 0;
54+ }
55+
56+ // data[data.size] is an imaginary, empty string, which is
57+ // always the most recent message
58+ public void append (string message) {
59+ data.add (message);
60+ current = data.size;
61+ }
62+
63+ public string get_current ()
64+ requires (0 <= current <= data.size) {
65+ if (current == data.size)
66+ return "";
67+ return data[current];
68+ }
69+
70+ public string get_older ()
71+ requires (has_older ()) {
72+ set_current_to_older ();
73+ return get_current ();
74+ }
75+
76+ public string get_newer ()
77+ requires (has_newer ()) {
78+ set_current_to_newer ();
79+ return get_current ();
80+ }
81+
82+ public bool has_older () {
83+ return current > 0;
84+ }
85+
86+ public bool has_newer () {
87+ return current < data.size;
88+ }
89+
90+ void set_current_to_older ()
91+ requires (has_older ()) {
92+ current--;
93+ }
94+
95+ void set_current_to_newer ()
96+ requires (has_newer ()) {
97+ current++;
98+ }
99+
100+ public void modify_current (string value)
101+ requires (0 <= current < data.size) {
102+ data[current] = value;
103+ }
104+}
105
106=== removed file 'src/Utils/RRStringList.vala'
107--- src/Utils/RRStringList.vala 2013-08-31 15:19:12 +0000
108+++ src/Utils/RRStringList.vala 1970-01-01 00:00:00 +0000
109@@ -1,77 +0,0 @@
110-/***
111- Copyright (C) 2013 Cable Developers
112-
113- This program or library is free software; you can redistribute it
114- and/or modify it under the terms of the GNU Lesser General Public
115- License as published by the Free Software Foundation; either
116- version 3 of the License, or (at your option) any later version.
117-
118- This library is distributed in the hope that it will be useful,
119- but WITHOUT ANY WARRANTY; without even the implied warranty of
120- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
121- Lesser General Public License for more details.
122-
123- You should have received a copy of the GNU Lesser General
124- Public License along with this library; if not, write to the
125- Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
126- Boston, MA 02110-1301 USA.
127-***/
128-
129-
130-public class Cable.Collections.RRStringList {
131-
132- internal int? current_;
133- internal Gee.List <string> data;
134-
135- public int capacity { get; set; }
136- public int? current_index { get { return current_; } }
137-
138- public RRStringList.with_capacity (int capacity_value)
139- requires (capacity_value > 0) {
140- capacity = capacity_value;
141- data = new Gee.ArrayList <string>();
142- }
143-
144- public RRStringList () {
145- this.with_capacity(50); // default capacity
146- }
147-
148- public void add(string value) {
149- if ( data.size >= this.capacity )
150- data.remove_at( 0 );
151- data.add(value);
152- current_ = data.size/*-1*/;
153- }
154-
155- public bool contains(string value) {
156- return data.contains(value);
157- }
158-
159- public string current() {
160- if ( current_ == null ) return "";
161- if ( current_ >= data.size ) current_ = data.size-1;
162- if ( current_ < 0 ) current_ = 0;
163- return data[current_];
164- }
165-
166- public bool previous() {
167- if ( current_ == null ) return false;
168- if ( current_ <= 0 ) return false;
169- current_ = current_ - 1;
170- return true;
171- }
172-
173- public bool next() {
174- if ( current_ == null ) return false;
175- if ( current_+1 >= data.size ) return false;
176- current_ = current_ + 1;
177- return true;
178- }
179-
180- public bool set_current_value(string value) {
181- if ( !(value in data) ) return false;
182- current_ = data.index_of(value)+1;
183- return true;
184- }
185-
186-}
187
188=== modified file 'src/Widgets/Room.vala'
189--- src/Widgets/Room.vala 2013-08-31 15:19:12 +0000
190+++ src/Widgets/Room.vala 2013-09-13 14:24:42 +0000
191@@ -51,8 +51,6 @@
192 internal Gtk.Entry text_entry;
193 Utils.AutoScrolled left_scrolled;
194
195- internal Cable.Collections.RRStringList history_set;
196-
197 Gtk.Box user_box;
198
199 public Widgets.Topic topic_bar;
200@@ -75,9 +73,7 @@
201 set { text_entry.text = value; }
202 }
203
204- public Cable.Collections.RRStringList history {
205- get { return history_set; }
206- }
207+ public Utils.MessageHistory history { get; private set; }
208
209 State _state;
210 public State state {
211@@ -125,7 +121,7 @@
212 public Room (string channel, string nick="") {
213 this.channel = channel;
214
215- history_set = new Cable.Collections.RRStringList();
216+ history = new Utils.MessageHistory ();
217
218 init_layout ();
219 connect_callbacks ();
220@@ -243,26 +239,23 @@
221 }
222 return true;
223 }
224- }
225- else if ( ev.keyval == Gdk.Key.Up ) {
226- if ( history.previous() )
227- text_entry.text = history.current();
228- return true;
229- }
230- else if ( ev.keyval == Gdk.Key.Down ) {
231- if ( history.next() )
232- text_entry.text = history.current();
233- return true;
234- }
235+ } else if (ev.keyval == Gdk.Key.Up) {
236+ if (history.has_older ())
237+ text_entry.text = history.get_older ();
238+ return true;
239+ } else if (ev.keyval == Gdk.Key.Down) {
240+ if (history.has_newer ())
241+ text_entry.text = history.get_newer ();
242+ return true;
243+ }
244+
245 return false;
246 });
247
248 text_entry.activate.connect (() => {
249 if (text_entry.text != "") {
250-
251- // permit duplicates
252- history.add(text_entry.text);
253- send_message (text_entry.text); // signal
254+ history.append (text_entry.text);
255+ send_message (text_entry.text);
256 }
257 });
258
259
260=== modified file 'tests/TestMessageHistory.vala'
261--- tests/TestMessageHistory.vala 2013-09-03 20:19:11 +0000
262+++ tests/TestMessageHistory.vala 2013-09-13 14:24:42 +0000
263@@ -1,41 +1,95 @@
264 class TestMessageHistory : Gee.TestCase {
265
266- public TestMessageHistory() {
267- // assign a name for this class
268- base("TestMessageHistory");
269- // add test methods
270- add_test("[History] Navigate history", test_navigate);
271- add_test("[History] Limited capacity", test_capacity);
272- }
273-
274- public override void set_up () {
275- // setup your test
276- }
277-
278- public void test_navigate() {
279- var history = new Cable.Collections.RRStringList();
280- history.add("one");
281- history.add("two");
282- history.add("three");
283- assert(history.current() == "three");
284- assert(history.next() == false);
285- assert(history.current() == "three");
286- assert(history.previous() == true);
287- assert(history.current() == "two");
288- }
289-
290- public void test_capacity() {
291- var history = new Cable.Collections.RRStringList.with_capacity(2);
292- history.add("one");
293- history.add("two");
294- history.add("three");
295- assert(history.current() == "three");
296- assert(history.previous() == true);
297- assert(history.previous() == false);
298- assert(history.current() == "two");
299- }
300-
301- public override void tear_down () {
302- // tear down your test
303- }
304-}
305\ No newline at end of file
306+ public TestMessageHistory () {
307+ base("Message History");
308+
309+ add_test("Initial State", test_initial_state);
310+ add_test("First Insert", test_first_insert);
311+ add_test("Second Insert", test_second_insert);
312+ add_test("Navigate Back", test_navigate_back);
313+ add_test("Navigate Forth", test_navigate_forth);
314+ add_test("Reset After Insert", test_reset_after_insert);
315+ add_test("Modify Current", test_modify_current);
316+ }
317+
318+ public override void set_up () {
319+ // setup your test
320+ }
321+
322+ public void test_initial_state () {
323+ var history = new Cable.Utils.MessageHistory ();
324+
325+ assert (history.has_older () == false);
326+ assert (history.has_newer () == false);
327+ assert (history.get_current () == "");
328+ }
329+
330+ public void test_first_insert () {
331+ var history = new Cable.Utils.MessageHistory ();
332+ history.append ("one");
333+
334+ assert (history.get_current () == "");
335+ assert (history.has_newer () == false);
336+ assert (history.has_older () == true);
337+ }
338+
339+ public void test_second_insert () {
340+ var history = new Cable.Utils.MessageHistory ();
341+ history.append ("one");
342+ history.append ("two");
343+
344+ assert (history.get_current () == "");
345+ assert (history.has_newer () == false);
346+ assert (history.has_older () == true);
347+ }
348+
349+ public void test_navigate_back () {
350+ var history = new Cable.Utils.MessageHistory ();
351+ history.append ("one");
352+
353+ assert (history.get_older () == "one");
354+ assert (history.get_current () == "one");
355+ assert (history.has_newer () == true);
356+ }
357+
358+ public void test_navigate_forth () {
359+ var history = new Cable.Utils.MessageHistory ();
360+ history.append ("one");
361+ history.get_older ();
362+
363+ assert (history.get_current () == "one");
364+ assert (history.get_newer () == "");
365+ assert (history.has_older () == true);
366+ assert (history.has_newer () == false);
367+ }
368+
369+ public void test_reset_after_insert () {
370+ var history = new Cable.Utils.MessageHistory ();
371+ history.append ("one");
372+ history.get_older ();
373+ history.append ("two");
374+
375+ assert (history.get_current () == "");
376+ assert (history.has_newer () == false);
377+ assert (history.has_older () == true);
378+ }
379+
380+ public void test_modify_current () {
381+ var history = new Cable.Utils.MessageHistory ();
382+ history.append ("one");
383+ history.get_older ();
384+
385+ assert (history.get_current () == "one");
386+ assert (history.has_newer () == true);
387+ assert (history.has_older () == false);
388+
389+ history.modify_current ("modified");
390+ assert (history.get_current () == "modified");
391+ assert (history.has_newer () == true);
392+ assert (history.has_older () == false);
393+ }
394+
395+ public override void tear_down () {
396+ // tear down your test
397+ }
398+}

Subscribers

People subscribed via source and target branches

to all changes: