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
=== modified file 'CMakeLists.txt'
--- CMakeLists.txt 2013-09-23 16:18:15 +0000
+++ CMakeLists.txt 2013-09-25 18:53:06 +0000
@@ -115,15 +115,16 @@
115 )115 )
116116
117vala_precompile(VALA_C_TESTS117vala_precompile(VALA_C_TESTS
118 unit/Assert.vala
119 unit/Test.vala
120 unit/TestCase.vala
121 unit/TestSuite.vala
122 unit/Unit.vala
123 118
124 tests/TestMain.vala119 tests/TestMain.vala
125 tests/TestUpMarker.vala120 tests/TestUpMarker.vala
126 tests/TestMessageHistory.vala121 tests/TestMessageHistory.vala
122
123 unit/Assert.vala
124 unit/Test.vala
125 unit/TestCase.vala
126 unit/TestSuite.vala
127 unit/Unit.vala
127128
128 src/Utils/Utils.vala129 src/Utils/Utils.vala
129 src/Utils/MessageHistory.vala130 src/Utils/MessageHistory.vala
@@ -145,10 +146,37 @@
145 --verbose146 --verbose
146)147)
147148
149vala_precompile(VALA_C_TESTS_2
150
151 tests/Identity/main.vala
152 tests/Identity/Identity.vala
153 tests/Identity/TestIdentity.vala
154 tests/Identity/IdentityFactory.vala
155 tests/Identity/TestIdentityFactory.vala
156
157 unit/Assert.vala
158 unit/Test.vala
159 unit/TestCase.vala
160 unit/TestSuite.vala
161 unit/Unit.vala
162PACKAGES
163 gtk+-3.0
164 granite
165 libnotify
166 unity
167OPTIONS
168 --target-glib=2.32
169 --vapidir=${CMAKE_CURRENT_SOURCE_DIR}/vapi/
170 --enable-experimental
171 #--fatal-warnings
172 --verbose
173)
174
148add_subdirectory (po)175add_subdirectory (po)
149add_subdirectory (data)176add_subdirectory (data)
150177
151add_executable(cable ${VALA_C})178add_executable(cable ${VALA_C})
152add_executable(cable_tests ${VALA_C_TESTS})179add_executable(cable_tests ${VALA_C_TESTS})
180add_executable(cable_tests_2 ${VALA_C_TESTS_2})
153181
154install(TARGETS cable RUNTIME DESTINATION bin)182install(TARGETS cable RUNTIME DESTINATION bin)
155183
=== added directory 'tests/Identity'
=== added file 'tests/Identity/Identity.vala'
--- tests/Identity/Identity.vala 1970-01-01 00:00:00 +0000
+++ tests/Identity/Identity.vala 2013-09-25 18:53:06 +0000
@@ -0,0 +1,23 @@
1public class Cable.Utils.Identity : GLib.Object {
2
3 public string nick_name { get; construct; }
4 public string real_name { get; construct; }
5 //public string user_name { get; construct; }
6
7 public string part_message { get; construct set; }
8 public string away_message { get; construct set; }
9 public string quit_message { get; construct set; }
10
11 public Identity (string nick_name,
12 string? real_name = null,
13 string? part_message = null,
14 string? quit_message = null,
15 string? away_message = null) {
16
17 GLib.Object (nick_name: nick_name,
18 real_name: real_name ?? nick_name,
19 part_message: part_message ?? _("Good night everyone!"),
20 quit_message: quit_message ?? _("Good night everyone!"),
21 away_message: away_message ?? _("Try again later!"));
22 }
23}
024
=== added file 'tests/Identity/IdentityFactory.vala'
--- tests/Identity/IdentityFactory.vala 1970-01-01 00:00:00 +0000
+++ tests/Identity/IdentityFactory.vala 2013-09-25 18:53:06 +0000
@@ -0,0 +1,93 @@
1namespace Cable.Utils.IdentityFactory {
2
3 Gee.List <Identity> id_list;
4
5 public void init () {
6 id_list = new Gee.ArrayList <Identity> () as Gee.List;
7 }
8
9 public void uninit () {
10 id_list = null;
11 }
12
13 public Identity get_or_create (string nick) {
14 if (has (nick) == false)
15 add (new Identity (nick));
16 return search (nick);
17 }
18
19 void add (Identity id) requires (has (id.nick_name) == false) {
20 id_list.add (id);
21 }
22
23 Identity search (string nick) requires (has (nick)) {
24 foreach (var id in id_list)
25 if (id.nick_name == nick)
26 return id;
27 return (Identity) null;
28 }
29
30 public bool has (string nick) {
31 foreach (var id in id_list)
32 if (id.nick_name == nick)
33 return true;
34 return false;
35 }
36
37 public void remove (string nick) requires (has (nick)) {
38 foreach (var id in id_list)
39 if (id.nick_name == nick)
40 id_list.remove (id);
41 }
42
43 public void import (string[] data) {
44 foreach (var identity in data) {
45 var values = identity.split (":");
46 Identity id = null;
47
48 switch (values.length) {
49 case 0:
50 warn_if_reached ();
51 break;
52 case 1:
53 id = new Identity (values[0]);
54 break;
55 case 2:
56 id = new Identity (values[0], values[1]);
57 break;
58 case 3:
59 id = new Identity (values[0], values[1], values[2]);
60 break;
61 case 4:
62 id = new Identity (values[0], values[1], values[2],
63 values[3]);
64 break;
65 case 5:
66 id = new Identity (values[0], values[1], values[2],
67 values[3], values[4]);
68 break;
69 default:
70 warn_if_reached ();
71 break;
72 }
73
74 if (id != null && ! has (id.nick_name))
75 add (id);
76 }
77 }
78
79 public string[] export () {
80 string[] result = {};
81
82 foreach (var identity in id_list) {
83 result += string.join (":",
84 identity.nick_name,
85 identity.real_name,
86 identity.part_message,
87 identity.quit_message,
88 identity.away_message);
89 }
90
91 return result;
92 }
93}
094
=== added file 'tests/Identity/TestIdentity.vala'
--- tests/Identity/TestIdentity.vala 1970-01-01 00:00:00 +0000
+++ tests/Identity/TestIdentity.vala 2013-09-25 18:53:06 +0000
@@ -0,0 +1,31 @@
1using Cable.Utils;
2
3public class TestIdentity : Unit.TestCase {
4
5 const string PART_MESSAGE = "Good night everyone!";
6 const string QUIT_MESSAGE = "Good night everyone!";
7 const string AWAY_MESSAGE = "Try again later!";
8
9 public TestIdentity () {
10 base ("Identity");
11
12 add_test_method ("Creation", test_creation);
13 }
14
15 public override void set_up () {
16 }
17
18 public override void tear_down () {
19 }
20
21 void test_creation () throws Assert.Error {
22 var id = new Identity ("Test");
23
24 Assert.equals ("Test", id.nick_name);
25 Assert.equals ("Test", id.real_name);
26 Assert.equals (PART_MESSAGE, id.part_message);
27 Assert.equals (QUIT_MESSAGE, id.quit_message);
28 Assert.equals (AWAY_MESSAGE, id.away_message);
29 }
30
31}
0\ No newline at end of file32\ No newline at end of file
133
=== added file 'tests/Identity/TestIdentityFactory.vala'
--- tests/Identity/TestIdentityFactory.vala 1970-01-01 00:00:00 +0000
+++ tests/Identity/TestIdentityFactory.vala 2013-09-25 18:53:06 +0000
@@ -0,0 +1,86 @@
1using Cable.Utils;
2
3public class TestIdentityFactory : Unit.TestCase {
4
5 const string PART_MESSAGE = "Good night everyone!";
6 const string QUIT_MESSAGE = "Good night everyone!";
7 const string AWAY_MESSAGE = "Try again later!";
8
9 string[] import_data = {
10 "johndoe:John Doe:Parting:Quitting:I'm away"
11 };
12
13 public TestIdentityFactory () {
14 base ("Identity Factory");
15
16 add_test_method ("Initial State", test_initial_state);
17 add_test_method ("First Insert", test_first_insert);
18 add_test_method ("Get or Create", test_insert_same);
19 add_test_method ("Remove", test_remove);
20 add_test_method ("Import", test_import);
21 add_test_method ("Export", test_export);
22 }
23
24 public override void set_up () {
25 IdentityFactory.init ();
26 }
27
28 void test_initial_state () throws Assert.Error {
29 Assert.is_false (IdentityFactory.has ("Test"));
30 }
31
32 void test_first_insert () throws Assert.Error {
33 IdentityFactory.get_or_create ("Test");
34
35 Assert.is_true (IdentityFactory.has ("Test"));
36 Assert.is_false (IdentityFactory.has ("Test not there"));
37 }
38
39 void test_insert_same () throws Assert.Error {
40 var id_1 = IdentityFactory.get_or_create ("Test");
41 var id_2 = IdentityFactory.get_or_create ("Test");
42
43 Assert.is_true (IdentityFactory.has ("Test"));
44 Assert.equals (id_1, id_2);
45 }
46
47 void test_remove () throws Assert.Error {
48 IdentityFactory.get_or_create ("Test");
49 IdentityFactory.get_or_create ("Removed");
50 IdentityFactory.remove ("Removed");
51
52 Assert.is_false (IdentityFactory.has ("Removed"));
53 }
54
55 void test_import () throws Assert.Error {
56 IdentityFactory.import (import_data);
57 var id = IdentityFactory.get_or_create ("johndoe");
58
59 Assert.equals ("johndoe", id.nick_name);
60 Assert.equals ("John Doe", id.real_name);
61 Assert.equals ("Parting", id.part_message);
62 Assert.equals ("Quitting", id.quit_message);
63 Assert.equals ("I'm away", id.away_message);
64 }
65
66 void test_export () throws Assert.Error {
67 var id_1 = IdentityFactory.get_or_create ("First");
68 var id_2 = IdentityFactory.get_or_create ("Second");
69
70 id_2.part_message = "New Part Message";
71
72 var export_data = IdentityFactory.export ();
73
74 Assert.equals (2, export_data.length);
75 Assert.equals (@"First:First:$PART_MESSAGE:"+
76 @"$QUIT_MESSAGE:$AWAY_MESSAGE",
77 export_data[0]);
78 Assert.equals (@"Second:Second:New Part Message:"+
79 @"$QUIT_MESSAGE:$AWAY_MESSAGE",
80 export_data[1]);
81 }
82
83 public override void tear_down () {
84 IdentityFactory.uninit ();
85 }
86}
087
=== added file 'tests/Identity/main.vala'
--- tests/Identity/main.vala 1970-01-01 00:00:00 +0000
+++ tests/Identity/main.vala 2013-09-25 18:53:06 +0000
@@ -0,0 +1,8 @@
1void main () {
2 var suite = new Unit.TestSuite ("Identity and Identity Factory");
3
4 suite.add_test (new TestIdentity ());
5 suite.add_test (new TestIdentityFactory ());
6
7 suite.run ();
8}
0\ No newline at end of file9\ No newline at end of file
110
=== modified file 'tests/TestMessageHistory.vala'
--- tests/TestMessageHistory.vala 2013-09-21 09:14:29 +0000
+++ tests/TestMessageHistory.vala 2013-09-25 18:53:06 +0000
@@ -18,6 +18,9 @@
18 history = new Cable.Utils.MessageHistory ();18 history = new Cable.Utils.MessageHistory ();
19 }19 }
2020
21 public override void tear_down () {
22 }
23
21 public void test_initial_state () throws Assert.Error {24 public void test_initial_state () throws Assert.Error {
22 Assert.is_false (history.has_older ());25 Assert.is_false (history.has_older ());
23 Assert.is_false (history.has_newer ());26 Assert.is_false (history.has_newer ());
2427
=== modified file 'tests/TestUpMarker.vala'
--- tests/TestUpMarker.vala 2013-09-21 13:19:14 +0000
+++ tests/TestUpMarker.vala 2013-09-25 18:53:06 +0000
@@ -18,6 +18,9 @@
18 public override void set_up () {18 public override void set_up () {
19 up_marker = new UpMarker ();19 up_marker = new UpMarker ();
20 }20 }
21
22 public override void tear_down () {
23 }
2124
22 string create_token (int foreground_color = -1, int background_color = -1) {25 string create_token (int foreground_color = -1, int background_color = -1) {
23 var token = "&#x3;";26 var token = "&#x3;";
2427
=== modified file 'unit/TestCase.vala'
--- unit/TestCase.vala 2013-09-21 13:19:14 +0000
+++ unit/TestCase.vala 2013-09-25 18:53:06 +0000
@@ -88,7 +88,7 @@
88 * Sets up the fixture, for example, open a network connection.88 * Sets up the fixture, for example, open a network connection.
89 * This method is called before a test is executed.89 * This method is called before a test is executed.
90 */90 */
91 protected virtual void set_up () {91 public virtual void set_up () {
92 }92 }
93 93
9494
@@ -96,7 +96,7 @@
96 * Tears down the fixture, for example, close a network connection.96 * Tears down the fixture, for example, close a network connection.
97 * This method is called after a test is executed.97 * This method is called after a test is executed.
98 */98 */
99 protected virtual void tear_down () {99 public virtual void tear_down () {
100 }100 }
101 101
102 /**102 /**

Subscribers

People subscribed via source and target branches

to all changes: