Home | News | Projects | Releases
Bugs | RFE | Repositories | Help
CommonFunctions Unit Tests: WIP changes (ConvertToHTML & MapDataExists left)
[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, GivenExampleStringWhenEscapeStringIsCalledThenReturnedStringIsExample){
340         // Test the escape string function.
342         // Given
344         wxString EscapeStringData = "Example";
345         
346         // When
347         
348         EscapeString(&EscapeStringData, false);
349         
350         // Then
351         
352         ASSERT_EQ("Example", EscapeStringData.ToStdString());
353         
356 TEST(CommonFunctions, GivenStringWithCommaWhenEscapeStringIsCalledThenCommaIsEscaped){
358         // Test the escape string function.
360         // Given
362         EscapeStringData = "Here we go, an \"example\" of escaping string.";
363         
364         // When
365         
366         EscapeString(&EscapeStringData, false);
367         
368         // Then
369         
370         ASSERT_EQ("Here we go\\, an \"example\" of escaping string.", EscapeStringData.ToStdString());
374 TEST(CommonFunctions, GivenStringWithNewlineCharactersWhenEscapeStringIsCalledThenNewlinesAreEscaped){
376         // Test the escape string function.
378         // Given
380         EscapeStringData = "Lets put some \nnew \nlines \nin \nhere.";
381         
382         // When
383         
384         EscapeString(&EscapeStringData, false);
385         
386         // Then
387         
388         ASSERT_EQ("Lets put some \\nnew \\nlines \\nin \\nhere.", EscapeStringData.ToStdString());
392 TEST(CommonFunctions, GivenAStringWithReturnCharactersWhenEscapeStringIsCalledThenReturnsAreRemoved){
394         // Test the escape string function.
396         // Given
398         EscapeStringData = "Lets put some \r\nnew \r\nlines \r\nin \r\nhere.";
399         
400         // When
401         
402         EscapeString(&EscapeStringData, false);
403         
404         // Then
405         
406         ASSERT_EQ("Lets put some \\nnew \\nlines \\nin \\nhere.", EscapeStringData.ToStdString());
410 TEST(CommonFunctions, GivenStringWithBackslashesWhenEscapeStringIsCalledThenBackslashesAreEscaped){
412         // Test the escape string function.
414         // Given
416         EscapeStringData = "A:\\path\\example\\lets\\have\\some\\fun";
417         
418         // When
419         
420         EscapeString(&EscapeStringData, false);
421         
422         // Then
423         
424         ASSERT_EQ("A:\\\\path\\\\example\\\\lets\\\\have\\\\some\\\\fun", EscapeStringData.ToStdString());
428 TEST(CommonFunctions, GivenExampleStringWhenCaptureStringIsCalledThenReturnedStringIsExample){
430         // Test the capture string function.
431         
432         // Given
433         
434         wxString CaptureStringData = "Example";
435         
436         // When
437         
438         CaptureString(&CaptureStringData, false);
439         
440         // Then
441         
442         ASSERT_EQ("Example", CaptureStringData.ToStdString());
446 TEST(CommonFunctions, GivenStringWithEscapedCommasWhenCaptureStringIsCalledThenCommasAreCaptured){
448         // Test the capture string function.
449         
450         // Given
451         
452         CaptureStringData = "Here we go\\, an \"example\" of escaping string.";
453         
454         // When
455         
456         CaptureString(&CaptureStringData, false);
457         
458         // Then
459         
460         ASSERT_EQ("Here we go, an \"example\" of escaping string.", CaptureStringData.ToStdString());
464 TEST(CommonFunctions, GivenStringWithEscapedNewlinesWhenCaptureStringIsCalledThenNewlinesAreCaptured){
466         // Test the capture string function.
467         
468         // Given
469         
470         CaptureStringData = "Lets put some \\nnew \\nlines \\nin \\nhere.";
471         
472         // When
473         
474         CaptureString(&CaptureStringData, false);
475         
476         // Then
477         
478         ASSERT_EQ("Lets put some \nnew \nlines \nin \nhere.", CaptureStringData.ToStdString());
482 TEST(CommonFunctions, GivenStringWithEscapedBackslashesWhenCaptureStringIsCalledThenBackslashesAreCaptured){
484         // Test the capture string function.
485         
486         // Given
487         
488         CaptureStringData = "A:\\\\path\\\\example\\\\lets\\\\have\\\\some\\\\fun";
489         
490         // When
491         
492         CaptureString(&CaptureStringData, false);
493         
494         // Then
495         
496         ASSERT_EQ("A:\\path\\example\\lets\\have\\some\\fun", CaptureStringData.ToStdString());
500 TEST(CommonFunctions, GivenStringWithSeveralBackslashesWhenResetUnusedStringIsCalledThenStringIsReset){
502         // Tests for resetting an unused string.
504         // Given
506         wxString ResetUnusedStringData = "Random string with a \\\\\\\\serverpath\\\\location";
507         
508         // When
509         
510         ResetUnusedString(&ResetUnusedStringData);
511         
512         // Then
513         
514         ASSERT_EQ("Random string with a \\\\serverpath\\location", ResetUnusedStringData.ToStdString());        
518 TEST(CommonFunctions, GivenStringWithNewlinesWhenResetUnusedStringIsCalledThenStringIsFormatted){
520         // Tests for resetting an unused string.
522         // Given
524         ResetUnusedStringData = "Some text \\nwith new \\nlines!";
525         
526         // When
527         
528         ResetUnusedString(&ResetUnusedStringData);
529         
530         // Then
531         
532         ASSERT_EQ("Some text \nwith new \nlines!", ResetUnusedStringData.ToStdString());
536 TEST(CommonFunctions, GivenStringWithSeveralCharactersWhenResetUnusedStringThenStringIsFormatted){
538         // Tests for resetting an unused string.
540         // Given
541         
542         ResetUnusedStringData = "And now the list of characters: \\\\ \\n \\; \\,";
544         // When
546         ResetUnusedString(&ResetUnusedStringData);
548         // Then
550         ASSERT_EQ("And now the list of characters: \\ \n ; ,", ResetUnusedStringData.ToStdString());
554 TEST(CommonFunctions, ConvertToHTML){
556         // Tests for converting data for inserting into a HTML
557         // formatted document.
559         wxString ConvertToHTMLData = "The test line where < is probably better than >!";
560         ConvertToHTML(&ConvertToHTMLData);
561         ASSERT_EQ("The test line where &lt; is probably better than &gt;!", ConvertToHTMLData.ToStdString());
563         ConvertToHTMLData = "More testing &<>\n";
564         ConvertToHTML(&ConvertToHTMLData);
565         ASSERT_EQ("More testing &amp;&lt;&gt;<br>", ConvertToHTMLData.ToStdString());
567         ConvertToHTMLData = "This is the first line.\nThis is the second line.\nThis is the third line.";
568         ConvertToHTML(&ConvertToHTMLData);
569         ASSERT_EQ("This is the first line.<br>This is the second line.<br>This is the third line.", ConvertToHTMLData.ToStdString());
573 TEST(CommonFunctions, MapDataExists){
575         // Tests for checking that map data exists.
577         std::map<int,int> MapExample;
578         int ItemIndex = 0;
580         MapExample.insert(std::make_pair(0,1));
581         MapExample.insert(std::make_pair(1,1));
582         MapExample.insert(std::make_pair(2,1));
583         MapExample.insert(std::make_pair(5,1));
584         MapExample.insert(std::make_pair(500,1));
585         MapExample.insert(std::make_pair(2415,1));
587         ASSERT_EQ(true, MapDataExists(&ItemIndex, &MapExample));
589         ItemIndex = 1;
590         ASSERT_EQ(true, MapDataExists(&ItemIndex, &MapExample));
592         ItemIndex = 2;
593         ASSERT_EQ(true, MapDataExists(&ItemIndex, &MapExample));
595         ItemIndex = 3;
596         ASSERT_EQ(false, MapDataExists(&ItemIndex, &MapExample));
598         ItemIndex = 5;
599         ASSERT_EQ(true, MapDataExists(&ItemIndex, &MapExample));
601         ItemIndex = 6;
602         ASSERT_EQ(false, MapDataExists(&ItemIndex, &MapExample));
604         ItemIndex = 499;
605         ASSERT_EQ(false, MapDataExists(&ItemIndex, &MapExample));
607         ItemIndex = 500;
608         ASSERT_EQ(true, MapDataExists(&ItemIndex, &MapExample));
610         ItemIndex = 501;
611         ASSERT_EQ(false, MapDataExists(&ItemIndex, &MapExample));
613         ItemIndex = 2414;
614         ASSERT_EQ(false, MapDataExists(&ItemIndex, &MapExample));
616         ItemIndex = 2415;
617         ASSERT_EQ(true, MapDataExists(&ItemIndex, &MapExample));
619         ItemIndex = 2416;
620         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