Merge lp:~julien-spautz/cable/identity-network-refactor into lp:cable

Proposed by Julien Spautz
Status: Work in progress
Proposed branch: lp:~julien-spautz/cable/identity-network-refactor
Merge into: lp:cable
Diff against target: 382 lines (+282/-7)
9 files modified
CMakeLists.txt (+33/-5)
tests/Identity/Identity.vala (+23/-0)
tests/Identity/IdentityFactory.vala (+93/-0)
tests/Identity/TestIdentity.vala (+31/-0)
tests/Identity/TestIdentityFactory.vala (+86/-0)
tests/Identity/main.vala (+8/-0)
tests/TestMessageHistory.vala (+3/-0)
tests/TestUpMarker.vala (+3/-0)
unit/TestCase.vala (+2/-2)
To merge this branch: bzr merge lp:~julien-spautz/cable/identity-network-refactor
Reviewer Review Type Date Requested Status
elementary Apps team Pending
Review via email: mp+187102@code.launchpad.net

Description of the change

Work in progress.

Refactoring Identity and Network classes, also testing TDD approach.

Wrote tests for Identity and IdentityFactory and implemented them.

To post a comment you must log in.
144. By Julien Spautz

added import export functions to IdentityFactory

145. By Julien Spautz

implemented remove method

Unmerged revisions

145. By Julien Spautz

implemented remove method

144. By Julien Spautz

added import export functions to IdentityFactory

143. By Julien Spautz

added files for previous commit

142. By Julien Spautz

added new Identity and IdentityFActory classes with unit tests

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-23 16:18:15 +0000
3+++ CMakeLists.txt 2013-09-25 18:53:06 +0000
4@@ -115,15 +115,16 @@
5 )
6
7 vala_precompile(VALA_C_TESTS
8- unit/Assert.vala
9- unit/Test.vala
10- unit/TestCase.vala
11- unit/TestSuite.vala
12- unit/Unit.vala
13
14 tests/TestMain.vala
15 tests/TestUpMarker.vala
16 tests/TestMessageHistory.vala
17+
18+ unit/Assert.vala
19+ unit/Test.vala
20+ unit/TestCase.vala
21+ unit/TestSuite.vala
22+ unit/Unit.vala
23
24 src/Utils/Utils.vala
25 src/Utils/MessageHistory.vala
26@@ -145,10 +146,37 @@
27 --verbose
28 )
29
30+vala_precompile(VALA_C_TESTS_2
31+
32+ tests/Identity/main.vala
33+ tests/Identity/Identity.vala
34+ tests/Identity/TestIdentity.vala
35+ tests/Identity/IdentityFactory.vala
36+ tests/Identity/TestIdentityFactory.vala
37+
38+ unit/Assert.vala
39+ unit/Test.vala
40+ unit/TestCase.vala
41+ unit/TestSuite.vala
42+ unit/Unit.vala
43+PACKAGES
44+ gtk+-3.0
45+ granite
46+ libnotify
47+ unity
48+OPTIONS
49+ --target-glib=2.32
50+ --vapidir=${CMAKE_CURRENT_SOURCE_DIR}/vapi/
51+ --enable-experimental
52+ #--fatal-warnings
53+ --verbose
54+)
55+
56 add_subdirectory (po)
57 add_subdirectory (data)
58
59 add_executable(cable ${VALA_C})
60 add_executable(cable_tests ${VALA_C_TESTS})
61+add_executable(cable_tests_2 ${VALA_C_TESTS_2})
62
63 install(TARGETS cable RUNTIME DESTINATION bin)
64
65=== added directory 'tests/Identity'
66=== added file 'tests/Identity/Identity.vala'
67--- tests/Identity/Identity.vala 1970-01-01 00:00:00 +0000
68+++ tests/Identity/Identity.vala 2013-09-25 18:53:06 +0000
69@@ -0,0 +1,23 @@
70+public class Cable.Utils.Identity : GLib.Object {
71+
72+ public string nick_name { get; construct; }
73+ public string real_name { get; construct; }
74+ //public string user_name { get; construct; }
75+
76+ public string part_message { get; construct set; }
77+ public string away_message { get; construct set; }
78+ public string quit_message { get; construct set; }
79+
80+ public Identity (string nick_name,
81+ string? real_name = null,
82+ string? part_message = null,
83+ string? quit_message = null,
84+ string? away_message = null) {
85+
86+ GLib.Object (nick_name: nick_name,
87+ real_name: real_name ?? nick_name,
88+ part_message: part_message ?? _("Good night everyone!"),
89+ quit_message: quit_message ?? _("Good night everyone!"),
90+ away_message: away_message ?? _("Try again later!"));
91+ }
92+}
93
94=== added file 'tests/Identity/IdentityFactory.vala'
95--- tests/Identity/IdentityFactory.vala 1970-01-01 00:00:00 +0000
96+++ tests/Identity/IdentityFactory.vala 2013-09-25 18:53:06 +0000
97@@ -0,0 +1,93 @@
98+namespace Cable.Utils.IdentityFactory {
99+
100+ Gee.List <Identity> id_list;
101+
102+ public void init () {
103+ id_list = new Gee.ArrayList <Identity> () as Gee.List;
104+ }
105+
106+ public void uninit () {
107+ id_list = null;
108+ }
109+
110+ public Identity get_or_create (string nick) {
111+ if (has (nick) == false)
112+ add (new Identity (nick));
113+ return search (nick);
114+ }
115+
116+ void add (Identity id) requires (has (id.nick_name) == false) {
117+ id_list.add (id);
118+ }
119+
120+ Identity search (string nick) requires (has (nick)) {
121+ foreach (var id in id_list)
122+ if (id.nick_name == nick)
123+ return id;
124+ return (Identity) null;
125+ }
126+
127+ public bool has (string nick) {
128+ foreach (var id in id_list)
129+ if (id.nick_name == nick)
130+ return true;
131+ return false;
132+ }
133+
134+ public void remove (string nick) requires (has (nick)) {
135+ foreach (var id in id_list)
136+ if (id.nick_name == nick)
137+ id_list.remove (id);
138+ }
139+
140+ public void import (string[] data) {
141+ foreach (var identity in data) {
142+ var values = identity.split (":");
143+ Identity id = null;
144+
145+ switch (values.length) {
146+ case 0:
147+ warn_if_reached ();
148+ break;
149+ case 1:
150+ id = new Identity (values[0]);
151+ break;
152+ case 2:
153+ id = new Identity (values[0], values[1]);
154+ break;
155+ case 3:
156+ id = new Identity (values[0], values[1], values[2]);
157+ break;
158+ case 4:
159+ id = new Identity (values[0], values[1], values[2],
160+ values[3]);
161+ break;
162+ case 5:
163+ id = new Identity (values[0], values[1], values[2],
164+ values[3], values[4]);
165+ break;
166+ default:
167+ warn_if_reached ();
168+ break;
169+ }
170+
171+ if (id != null && ! has (id.nick_name))
172+ add (id);
173+ }
174+ }
175+
176+ public string[] export () {
177+ string[] result = {};
178+
179+ foreach (var identity in id_list) {
180+ result += string.join (":",
181+ identity.nick_name,
182+ identity.real_name,
183+ identity.part_message,
184+ identity.quit_message,
185+ identity.away_message);
186+ }
187+
188+ return result;
189+ }
190+}
191
192=== added file 'tests/Identity/TestIdentity.vala'
193--- tests/Identity/TestIdentity.vala 1970-01-01 00:00:00 +0000
194+++ tests/Identity/TestIdentity.vala 2013-09-25 18:53:06 +0000
195@@ -0,0 +1,31 @@
196+using Cable.Utils;
197+
198+public class TestIdentity : Unit.TestCase {
199+
200+ const string PART_MESSAGE = "Good night everyone!";
201+ const string QUIT_MESSAGE = "Good night everyone!";
202+ const string AWAY_MESSAGE = "Try again later!";
203+
204+ public TestIdentity () {
205+ base ("Identity");
206+
207+ add_test_method ("Creation", test_creation);
208+ }
209+
210+ public override void set_up () {
211+ }
212+
213+ public override void tear_down () {
214+ }
215+
216+ void test_creation () throws Assert.Error {
217+ var id = new Identity ("Test");
218+
219+ Assert.equals ("Test", id.nick_name);
220+ Assert.equals ("Test", id.real_name);
221+ Assert.equals (PART_MESSAGE, id.part_message);
222+ Assert.equals (QUIT_MESSAGE, id.quit_message);
223+ Assert.equals (AWAY_MESSAGE, id.away_message);
224+ }
225+
226+}
227\ No newline at end of file
228
229=== added file 'tests/Identity/TestIdentityFactory.vala'
230--- tests/Identity/TestIdentityFactory.vala 1970-01-01 00:00:00 +0000
231+++ tests/Identity/TestIdentityFactory.vala 2013-09-25 18:53:06 +0000
232@@ -0,0 +1,86 @@
233+using Cable.Utils;
234+
235+public class TestIdentityFactory : Unit.TestCase {
236+
237+ const string PART_MESSAGE = "Good night everyone!";
238+ const string QUIT_MESSAGE = "Good night everyone!";
239+ const string AWAY_MESSAGE = "Try again later!";
240+
241+ string[] import_data = {
242+ "johndoe:John Doe:Parting:Quitting:I'm away"
243+ };
244+
245+ public TestIdentityFactory () {
246+ base ("Identity Factory");
247+
248+ add_test_method ("Initial State", test_initial_state);
249+ add_test_method ("First Insert", test_first_insert);
250+ add_test_method ("Get or Create", test_insert_same);
251+ add_test_method ("Remove", test_remove);
252+ add_test_method ("Import", test_import);
253+ add_test_method ("Export", test_export);
254+ }
255+
256+ public override void set_up () {
257+ IdentityFactory.init ();
258+ }
259+
260+ void test_initial_state () throws Assert.Error {
261+ Assert.is_false (IdentityFactory.has ("Test"));
262+ }
263+
264+ void test_first_insert () throws Assert.Error {
265+ IdentityFactory.get_or_create ("Test");
266+
267+ Assert.is_true (IdentityFactory.has ("Test"));
268+ Assert.is_false (IdentityFactory.has ("Test not there"));
269+ }
270+
271+ void test_insert_same () throws Assert.Error {
272+ var id_1 = IdentityFactory.get_or_create ("Test");
273+ var id_2 = IdentityFactory.get_or_create ("Test");
274+
275+ Assert.is_true (IdentityFactory.has ("Test"));
276+ Assert.equals (id_1, id_2);
277+ }
278+
279+ void test_remove () throws Assert.Error {
280+ IdentityFactory.get_or_create ("Test");
281+ IdentityFactory.get_or_create ("Removed");
282+ IdentityFactory.remove ("Removed");
283+
284+ Assert.is_false (IdentityFactory.has ("Removed"));
285+ }
286+
287+ void test_import () throws Assert.Error {
288+ IdentityFactory.import (import_data);
289+ var id = IdentityFactory.get_or_create ("johndoe");
290+
291+ Assert.equals ("johndoe", id.nick_name);
292+ Assert.equals ("John Doe", id.real_name);
293+ Assert.equals ("Parting", id.part_message);
294+ Assert.equals ("Quitting", id.quit_message);
295+ Assert.equals ("I'm away", id.away_message);
296+ }
297+
298+ void test_export () throws Assert.Error {
299+ var id_1 = IdentityFactory.get_or_create ("First");
300+ var id_2 = IdentityFactory.get_or_create ("Second");
301+
302+ id_2.part_message = "New Part Message";
303+
304+ var export_data = IdentityFactory.export ();
305+
306+ Assert.equals (2, export_data.length);
307+ Assert.equals (@"First:First:$PART_MESSAGE:"+
308+ @"$QUIT_MESSAGE:$AWAY_MESSAGE",
309+ export_data[0]);
310+ Assert.equals (@"Second:Second:New Part Message:"+
311+ @"$QUIT_MESSAGE:$AWAY_MESSAGE",
312+ export_data[1]);
313+ }
314+
315+ public override void tear_down () {
316+ IdentityFactory.uninit ();
317+ }
318+}
319
320=== added file 'tests/Identity/main.vala'
321--- tests/Identity/main.vala 1970-01-01 00:00:00 +0000
322+++ tests/Identity/main.vala 2013-09-25 18:53:06 +0000
323@@ -0,0 +1,8 @@
324+void main () {
325+ var suite = new Unit.TestSuite ("Identity and Identity Factory");
326+
327+ suite.add_test (new TestIdentity ());
328+ suite.add_test (new TestIdentityFactory ());
329+
330+ suite.run ();
331+}
332\ No newline at end of file
333
334=== modified file 'tests/TestMessageHistory.vala'
335--- tests/TestMessageHistory.vala 2013-09-21 09:14:29 +0000
336+++ tests/TestMessageHistory.vala 2013-09-25 18:53:06 +0000
337@@ -18,6 +18,9 @@
338 history = new Cable.Utils.MessageHistory ();
339 }
340
341+ public override void tear_down () {
342+ }
343+
344 public void test_initial_state () throws Assert.Error {
345 Assert.is_false (history.has_older ());
346 Assert.is_false (history.has_newer ());
347
348=== modified file 'tests/TestUpMarker.vala'
349--- tests/TestUpMarker.vala 2013-09-21 13:19:14 +0000
350+++ tests/TestUpMarker.vala 2013-09-25 18:53:06 +0000
351@@ -18,6 +18,9 @@
352 public override void set_up () {
353 up_marker = new UpMarker ();
354 }
355+
356+ public override void tear_down () {
357+ }
358
359 string create_token (int foreground_color = -1, int background_color = -1) {
360 var token = "&#x3;";
361
362=== modified file 'unit/TestCase.vala'
363--- unit/TestCase.vala 2013-09-21 13:19:14 +0000
364+++ unit/TestCase.vala 2013-09-25 18:53:06 +0000
365@@ -88,7 +88,7 @@
366 * Sets up the fixture, for example, open a network connection.
367 * This method is called before a test is executed.
368 */
369- protected virtual void set_up () {
370+ public virtual void set_up () {
371 }
372
373
374@@ -96,7 +96,7 @@
375 * Tears down the fixture, for example, close a network connection.
376 * This method is called after a test is executed.
377 */
378- protected virtual void tear_down () {
379+ public virtual void tear_down () {
380 }
381
382 /**

Subscribers

People subscribed via source and target branches

to all changes: