Home | News | Projects | Releases
Bugs | RFE | Repositories | Help
CommonFunctions Unit Tests: WIP changes
[xestiaab/.git] / source / tests / xestiaab_common.h
1  // xestiaab_common.h - Xestia Address Book Common Functions Unit Tests.
2 //
3 // (c) 2012-2015 Xestia Software Development.
4 //
5 // This file is part of Xestia Address Book.
6 //
7 // Xestia Address Book is free software: you can redistribute it and/or modify
8 // it under the terms of the GNU General Public License as published by the
9 // Free Software Foundation, version 3 of the license.
10 //
11 // Xestia Address Book is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 // GNU General Public License for more details.
15 //
16 // You should have received a copy of the GNU General Public License along
17 // with Xestia Address Book. If not, see <http://www.gnu.org/licenses/>
19 #include <gtest/gtest.h>
20 #include "../common/dirs.h"
21 #include "../common/filename.h"
22 #include "../common/mime.h"
23 #include "../common/svrblist.h"
24 #include "../common/textprocessing.h"
26 static wxString AccountInput1 = "Example1";
27 static wxString AccountInput2 = "Example2";
28 static wxString AccountInput3 = "Example3";
30 TEST(CommonFunctions, GivenAPredefinedPathWhenGetUserDirIsCalledThenReceivedPathMatches){
31         
32         // Run the unit tests depending on the operating system being used.
33         
34 #if defined(__WIN32__)
35         
36 #elif defined(__APPLE__)
37         
38 #else
39         
40         // Setup for comparison purposes. Should be /home/<user>/.xestiaab
41         
42         // Given
43         
44         wxString UserDirCheck = "";
45         UserDirCheck.Append(wxString::FromUTF8(getenv("HOME")));
46         UserDirCheck.Append(wxT("/.xestiaab/"));
47         
48         // When
49         
50         wxString UserDir = GetUserDir();
51         
52         // Then
53         
54         ASSERT_EQ(UserDir, UserDirCheck);
55         
56 #endif
57         
58 }
60 TEST(CommonFunctions, GivenAPredefinedPathWhenGetUserPrefDirIsCalledThenReceivedPathMatches){
61         
62         // Run the unit tests depending on the operating system being used.
63         
64 #if defined(__WIN32__)
65         
66 #elif defined(__APPLE__)
67         
68 #else
69         
70         // Setup for comparison purposes. Should be /home/<user>/.xestiaab/preferences
71         
72         // Given
73         
74         wxString UserPrefDirCheck = "";
75         UserPrefDirCheck.Append(wxString::FromUTF8(getenv("HOME")));
76         UserPrefDirCheck.Append(wxT("/.xestiaab/preferences/"));
77         
78         // When
79         
80         wxString UserPrefDir = GetUserPrefDir();
81         
82         // Then
83         
84         ASSERT_EQ(UserPrefDir, UserPrefDirCheck);
85         
86 #endif
88 }
90 TEST(CommonFunctions, GivenThreePredefinedPathsWhenGetAccountDirIsCalledForEachThenReceivedPathsMatches){
91         
92         // Run the unit tests depending on the operating system being used.
93         
94 #if defined(__WIN32__)
95         
96 #elif defined(__APPLE__)
97         
98 #else
99         
100         // Setup for comparison purposes. Should be /home/<user>/.xestiaab/preferences
101         
102         wxString AccountDirCheck1 = "";
103         wxString AccountDirCheck2 = "";
104         wxString AccountDirCheck3 = "";
105         
106         // Given
107         
108         AccountDirCheck1.Append(wxString::FromUTF8(getenv("HOME")));
109         AccountDirCheck1.Append(wxT("/.xestiaab/accounts/"));
110         AccountDirCheck1.Append("Example1");
111         AccountDirCheck1.Append(wxT("/"));
113         AccountDirCheck2.Append(wxString::FromUTF8(getenv("HOME")));
114         AccountDirCheck2.Append(wxT("/.xestiaab/accounts/"));
115         AccountDirCheck2.Append("Example2");
116         AccountDirCheck2.Append(wxT("/"));      
117         
118         AccountDirCheck3.Append(wxString::FromUTF8(getenv("HOME")));
119         AccountDirCheck3.Append(wxT("/.xestiaab/accounts/"));
120         AccountDirCheck3.Append("Example3");
121         AccountDirCheck3.Append(wxT("/"));
122         
123         // When
124         
125         wxString AccountDir1 = GetAccountDir(AccountInput1, false);
126         wxString AccountDir2 = GetAccountDir(AccountInput2, false);
127         wxString AccountDir3 = GetAccountDir(AccountInput3, false);
129         // Then
131         ASSERT_EQ(AccountDir1, AccountDirCheck1);
132         ASSERT_EQ(AccountDir2, AccountDirCheck2);
133         ASSERT_EQ(AccountDir3, AccountDirCheck3);
134         
135 #endif
136         
139 TEST(CommonFunctions, GivenThreePredefinedFilePathsWhenGetAccountDirIsCalledWithAppendServerCertificateSetToTrueForEachThenReceivedPathsMatches){
140         
141         // Run the unit tests depending on the operating system being used.
142         
143 #if defined(__WIN32__)
144         
145 #elif defined(__APPLE__)
146         
147 #else
148         
149         // Setup for comparison purposes. Should be /home/<user>/.xestiaab/preferences
150         
151         wxString AccountDirCheck1 = "";
152         wxString AccountDirCheck2 = "";
153         wxString AccountDirCheck3 = "";
154         
155         // Given
156         
157         AccountDirCheck1.Append(wxString::FromUTF8(getenv("HOME")));
158         AccountDirCheck1.Append(wxT("/.xestiaab/accounts/"));
159         AccountDirCheck1.Append("Example1");
160         AccountDirCheck1.Append(wxT("/server.crt"));
162         AccountDirCheck2.Append(wxString::FromUTF8(getenv("HOME")));
163         AccountDirCheck2.Append(wxT("/.xestiaab/accounts/"));
164         AccountDirCheck2.Append("Example2");
165         AccountDirCheck2.Append(wxT("/server.crt"));
166         
167         AccountDirCheck3.Append(wxString::FromUTF8(getenv("HOME")));
168         AccountDirCheck3.Append(wxT("/.xestiaab/accounts/"));
169         AccountDirCheck3.Append("Example3");
170         AccountDirCheck3.Append(wxT("/server.crt"));
171         
172         // When
173         
174         wxString AccountDir1 = GetAccountDir(AccountInput1, true);
175         wxString AccountDir2 = GetAccountDir(AccountInput2, true);
176         wxString AccountDir3 = GetAccountDir(AccountInput3, true);
178         // Then
180         ASSERT_EQ(AccountDir1, AccountDirCheck1);
181         ASSERT_EQ(AccountDir2, AccountDirCheck2);
182         ASSERT_EQ(AccountDir3, AccountDirCheck3);
183         
184 #endif
185         
188 TEST(CommonFunctions, GivenPredefinedFilePathWhenGetAccountsFileIsCalledThenReceivedFilePathMatches){
189         
190         // Run the unit tests depending on the operating system being used.
191         
192 #if defined(__WIN32__)
193         
194 #elif defined(__APPLE__)
195         
196 #else
197         
198         // Setup for comparison purposes. Should be 
199         // /home/<user>/.xestiaab/preferences/accounts
200         
201         // Given
202         
203         wxString AccountsFileCheck = "";
204         AccountsFileCheck.Clear();
205         AccountsFileCheck.Append(wxString::FromUTF8(getenv("HOME")));
206         AccountsFileCheck.Append(wxT("/.xestiaab/preferences/accounts"));
207         
208         // When
209         
210         wxString AccountsFile = GetAccountsFile();
211         
212         // Then
213         
214         ASSERT_EQ(AccountsFile, AccountsFileCheck);
215         
216 #endif
217         
220 TEST(CommonFunctions, GivenPredefinedFilePathWhenGetSettingsFileIsCalledThenReceivedFilePathMatches){
221         
222         // Run the unit tests depending on the operating system being used.
223         
224 #if defined(__WIN32__)
225         
226 #elif defined(__APPLE__)
227         
228 #else
229         
230         // Setup for comparison purposes. Should be 
231         // /home/<user>/.xestiaab/preferences/accounts
232         
233         // Given
234         
235         wxString SettingsFileCheck = "";
236         SettingsFileCheck.Clear();
237         SettingsFileCheck.Append(wxString::FromUTF8(getenv("HOME")));
238         SettingsFileCheck.Append(wxT("/.xestiaab/preferences/settings"));
239         
240         // When
241         
242         wxString SettingsFile = GetSettingsFile();
243         
244         // Then
245         
246         ASSERT_EQ(SettingsFile, SettingsFileCheck);
247         
248 #endif
249         
252 TEST(CommonFunctions, GivenFivePredefinedFilenamePathsWhenCreateFilenamePathIsCalledThenReceivedPathMatches){
253         
254         // Run the unit tests depending on the operating system being used.
255         
256 #if defined(__WIN32__)
257         
258 #elif defined(__APPLE__)
259         
260 #else
262         // Given
264         wxString FilenamePath1 = "/test";
265         wxString FilenamePath2 = "/example/test";
266         wxString FilenamePath3 = "/home/meep/test";
267         wxString FilenamePath4 = "/home/meep/moo/test";
268         wxString FilenamePath5 = "/home/meep/moo/yargh/test";
270         // When
272         wxString GeneratedFilenamePath1 = CreateFilenamePath("", "test");
273         wxString GeneratedFilenamePath2 = CreateFilenamePath("/example", "test");
274         wxString GeneratedFilenamePath3 = CreateFilenamePath("/home/meep", "test");
275         wxString GeneratedFilenamePath4 = CreateFilenamePath("/home/meep/moo", "test"); 
276         wxString GeneratedFilenamePath5 = CreateFilenamePath("/home/meep/moo/yargh", "test");   
278         // Then
280         ASSERT_EQ(FilenamePath1, GeneratedFilenamePath1);
281         ASSERT_EQ(FilenamePath2, GeneratedFilenamePath2);
282         ASSERT_EQ(FilenamePath3, GeneratedFilenamePath3);
283         ASSERT_EQ(FilenamePath4, GeneratedFilenamePath4);
284         ASSERT_EQ(FilenamePath5, GeneratedFilenamePath5);
285         
286 #endif
287         
290 TEST(CommonFunctions, GivenTwoFilesWhenGetMIMEIsCalledThenBothFilesMIMETypesAreGiven){
291         
292         // Get the MIME type based on the files given.
293         
294         // Given
295         
296         wxString File1MIMEFormat = "";
297         wxString File2MIMEFormat = "";
298         
299         // When
300         
301         File1MIMEFormat = GetMIME("TextMIMEExample.txt");
302         File2MIMEFormat = GetMIME("PictureMIMEExample.png");
303         
304         // Then
305         
306         ASSERT_EQ("text/plain", GetMIME("TextMIMEExample.txt"));
307         ASSERT_EQ("image/png", GetMIME("PictureMIMEExample.png"));
308         
311 TEST(CommonFunctions, GivenTwoDomainNamesWhenCheckBlacklistIsCalledThenReturnsCorrectStatus){
312         
313         // Test that the blacklist is actually working.
315         // Given
316         
317         bool domainStatus = true;
318         
319         // When
320         
321         domainStatus = CheckBlacklist("example.com");
322         
323         // Then
324         
325         ASSERT_EQ(false, domainStatus);
326         
327         // When
328         
329         domainStatus = false;
330         domainStatus = CheckBlacklist("google.com");
331         
332         // Then
333         
334         ASSERT_EQ(true, domainStatus);
335         
338 TEST(CommonFunctions, EscapeString){
340         // Test the escape string function.
342         wxString EscapeStringData = "Example";
343         EscapeString(&EscapeStringData, false);
344         ASSERT_EQ("Example", EscapeStringData.ToStdString());
346         EscapeStringData = "Here we go, an \"example\" of escaping string.";
347         EscapeString(&EscapeStringData, false);
348         ASSERT_EQ("Here we go\\, an \"example\" of escaping string.", EscapeStringData.ToStdString());
350         EscapeStringData = "Lets put some \nnew \nlines \nin \nhere.";
351         EscapeString(&EscapeStringData, false);
352         ASSERT_EQ("Lets put some \\nnew \\nlines \\nin \\nhere.", EscapeStringData.ToStdString());
354         EscapeStringData = "Lets put some \r\nnew \r\nlines \r\nin \r\nhere.";
355         EscapeString(&EscapeStringData, false);
356         ASSERT_EQ("Lets put some \\nnew \\nlines \\nin \\nhere.", EscapeStringData.ToStdString());
358         EscapeStringData = "A:\\path\\example\\lets\\have\\some\\fun";
359         EscapeString(&EscapeStringData, false);
360         ASSERT_EQ("A:\\\\path\\\\example\\\\lets\\\\have\\\\some\\\\fun", EscapeStringData.ToStdString());
364 TEST(CommonFunctions, CaptureString){
366         // Test the capture string function.
367         
368         wxString CaptureStringData = "Example";
369         CaptureString(&CaptureStringData, false);
370         ASSERT_EQ("Example", CaptureStringData.ToStdString());
372         CaptureStringData = "Here we go\\, an \"example\" of escaping string.";
373         CaptureString(&CaptureStringData, false);
374         ASSERT_EQ("Here we go, an \"example\" of escaping string.", CaptureStringData.ToStdString());
376         CaptureStringData = "Lets put some \\nnew \\nlines \\nin \\nhere.";
377         CaptureString(&CaptureStringData, false);
378         ASSERT_EQ("Lets put some \nnew \nlines \nin \nhere.", CaptureStringData.ToStdString());
380         CaptureStringData = "A:\\\\path\\\\example\\\\lets\\\\have\\\\some\\\\fun";
381         CaptureString(&CaptureStringData, false);
382         ASSERT_EQ("A:\\path\\example\\lets\\have\\some\\fun", CaptureStringData.ToStdString());
386 TEST(CommonFunctions, ResetUnusedString){
388         // Tests for resetting an unused string.
390         wxString ResetUnusedStringData = "Random string with a \\\\\\\\serverpath\\\\location";
391         ResetUnusedString(&ResetUnusedStringData);
392         ASSERT_EQ("Random string with a \\\\serverpath\\location", ResetUnusedStringData.ToStdString());        
394         ResetUnusedStringData = "Some text \\nwith new \\nlines!";
395         ResetUnusedString(&ResetUnusedStringData);
396         ASSERT_EQ("Some text \nwith new \nlines!", ResetUnusedStringData.ToStdString());
398         ResetUnusedStringData = "Some text \\nwith new \\nlines!";
399         ResetUnusedString(&ResetUnusedStringData);
400         ASSERT_EQ("Some text \nwith new \nlines!", ResetUnusedStringData.ToStdString());
401         
402         ResetUnusedStringData = "And now the list of characters: \\\\ \\n \\; \\,";
403         ResetUnusedString(&ResetUnusedStringData);
404         ASSERT_EQ("And now the list of characters: \\ \n ; ,", ResetUnusedStringData.ToStdString());
408 TEST(CommonFunctions, ConvertToHTML){
410         // Tests for converting data for inserting into a HTML
411         // formatted document.
413         wxString ConvertToHTMLData = "The test line where < is probably better than >!";
414         ConvertToHTML(&ConvertToHTMLData);
415         ASSERT_EQ("The test line where &lt; is probably better than &gt;!", ConvertToHTMLData.ToStdString());
417         ConvertToHTMLData = "More testing &<>\n";
418         ConvertToHTML(&ConvertToHTMLData);
419         ASSERT_EQ("More testing &amp;&lt;&gt;<br>", ConvertToHTMLData.ToStdString());
421         ConvertToHTMLData = "This is the first line.\nThis is the second line.\nThis is the third line.";
422         ConvertToHTML(&ConvertToHTMLData);
423         ASSERT_EQ("This is the first line.<br>This is the second line.<br>This is the third line.", ConvertToHTMLData.ToStdString());
427 TEST(CommonFunctions, MapDataExists){
429         // Tests for checking that map data exists.
431         std::map<int,int> MapExample;
432         int ItemIndex = 0;
434         MapExample.insert(std::make_pair(0,1));
435         MapExample.insert(std::make_pair(1,1));
436         MapExample.insert(std::make_pair(2,1));
437         MapExample.insert(std::make_pair(5,1));
438         MapExample.insert(std::make_pair(500,1));
439         MapExample.insert(std::make_pair(2415,1));
441         ASSERT_EQ(true, MapDataExists(&ItemIndex, &MapExample));
443         ItemIndex = 1;
444         ASSERT_EQ(true, MapDataExists(&ItemIndex, &MapExample));
446         ItemIndex = 2;
447         ASSERT_EQ(true, MapDataExists(&ItemIndex, &MapExample));
449         ItemIndex = 3;
450         ASSERT_EQ(false, MapDataExists(&ItemIndex, &MapExample));
452         ItemIndex = 5;
453         ASSERT_EQ(true, MapDataExists(&ItemIndex, &MapExample));
455         ItemIndex = 6;
456         ASSERT_EQ(false, MapDataExists(&ItemIndex, &MapExample));
458         ItemIndex = 499;
459         ASSERT_EQ(false, MapDataExists(&ItemIndex, &MapExample));
461         ItemIndex = 500;
462         ASSERT_EQ(true, MapDataExists(&ItemIndex, &MapExample));
464         ItemIndex = 501;
465         ASSERT_EQ(false, MapDataExists(&ItemIndex, &MapExample));
467         ItemIndex = 2414;
468         ASSERT_EQ(false, MapDataExists(&ItemIndex, &MapExample));
470         ItemIndex = 2415;
471         ASSERT_EQ(true, MapDataExists(&ItemIndex, &MapExample));
473         ItemIndex = 2416;
474         ASSERT_EQ(false, MapDataExists(&ItemIndex, &MapExample));
Xestia Software Development
Yn Maystri
© 2006 - 2019 Xestia Software Development
Software

Xestia Address Book
Xestia Calendar
Development

Xestia Gelforn
Everything else

About
News
Privacy Policy