1 // xestiacalendar_commonfunctions.h - Xestia Calendar Common Functions Unit Tests
3 // (c) 2016 Xestia Software Development.
5 // This file is part of Xestia Calendar.
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.
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.
16 // You should have received a copy of the GNU General Public License along
17 // with Xestia Calendar. If not, see <http://www.gnu.org/licenses/>
19 #include "../common/file.h"
20 #include "../common/text.h"
21 #include "../common/colour.h"
23 TEST(CommonFunctions, FileTests){
25 ASSERT_EQ(false, FileExists("iCalendarEvent-Missing.vcf"));
26 ASSERT_EQ(true, FileExists("iCalendarEvent-InvalidPermissions.vcf"));
30 TEST(CommonFunctions, ProcessTextVectorsTests){
32 // Setup the objects to be used for processing.
34 vector<string> TextPropertiesExample1;
35 vector<string> TextValueExample1;
36 multimap<string, string> TextExample1TestResult;
38 TextPropertiesExample1.push_back("ATTENDEE");
39 TextValueExample1.push_back("Example Attendee 1");
41 TextPropertiesExample1.push_back("ATTENDEE");
42 TextValueExample1.push_back("Example Attendee 2");
44 TextPropertiesExample1.push_back("ATTENDEE");
45 TextValueExample1.push_back("Example Attendee 3");
47 TextPropertiesExample1.push_back("CATEGORIES");
48 TextValueExample1.push_back("CATEGORY 1");
50 TextPropertiesExample1.push_back("CATEGORIES");
51 TextValueExample1.push_back("CATEGORY 2");
53 TextPropertiesExample1.push_back("CATEGORIES");
54 TextValueExample1.push_back("CATEGORY 3");
56 TextPropertiesExample1.push_back("COMMENT");
57 TextValueExample1.push_back("This is the first comment.");
59 TextPropertiesExample1.push_back("COMMENT");
60 TextValueExample1.push_back("This is the second comment.");
62 TextPropertiesExample1.push_back("COMMENT");
63 TextValueExample1.push_back("This is the third comment.");
65 TextPropertiesExample1.push_back("CONTACT;TEST=VALUE");
66 TextValueExample1.push_back("First Contact");
68 TextPropertiesExample1.push_back("CONTACT;LAZY=NOPE");
69 TextValueExample1.push_back("Second Contact");
71 TextPropertiesExample1.push_back("CONTACT;SETUP=NO");
72 TextValueExample1.push_back("Third Contact");
74 TextPropertiesExample1.push_back("RESOURCES;ROOM=YES");
75 TextValueExample1.push_back("First Resource Widget");
77 TextPropertiesExample1.push_back("RESOURCES;ROOM=NO");
78 TextValueExample1.push_back("Second Resource Widget");
80 TextPropertiesExample1.push_back("RESOURCES;ROOM=UNKNOWN");
81 TextValueExample1.push_back("Third Resource Widget");
83 TextPropertiesExample1.push_back("RELATED;RELATION=NO");
84 TextValueExample1.push_back("First Relation");
86 TextPropertiesExample1.push_back("RELATED;RELATION=YES");
87 TextValueExample1.push_back("Second Relation");
89 TextPropertiesExample1.push_back("RELATED;RELATION=MAYBE");
90 TextValueExample1.push_back("Third Relation");
92 // Examples 1 & 2: ATTENDEE
94 TextExample1TestResult.insert(make_pair("ATTENDEE", "Example Attendee 1"));
95 TextExample1TestResult.insert(make_pair("ATTENDEE", "Example Attendee 2"));
96 TextExample1TestResult.insert(make_pair("ATTENDEE", "Example Attendee 3"));
98 ASSERT_EQ(TextExample1TestResult, ProcessTextVectors(&TextPropertiesExample1, &TextValueExample1, true, "ATTENDEE"));
100 TextExample1TestResult.clear();
101 TextExample1TestResult.insert(make_pair("ATTENDEE", "Example Attendee 1"));
103 ASSERT_EQ(TextExample1TestResult, ProcessTextVectors(&TextPropertiesExample1, &TextValueExample1, false, "ATTENDEE"));
105 // Examples 3 & 4: CATEGORIES
107 TextExample1TestResult.clear();
108 TextExample1TestResult.insert(make_pair("CATEGORIES", "CATEGORY 1"));
109 TextExample1TestResult.insert(make_pair("CATEGORIES", "CATEGORY 2"));
110 TextExample1TestResult.insert(make_pair("CATEGORIES", "CATEGORY 3"));
112 ASSERT_EQ(TextExample1TestResult, ProcessTextVectors(&TextPropertiesExample1, &TextValueExample1, true, "CATEGORIES"));
114 TextExample1TestResult.clear();
115 TextExample1TestResult.insert(make_pair("CATEGORIES", "CATEGORY 1"));
117 // Examples 5 & 6: COMMENT
119 ASSERT_EQ(TextExample1TestResult, ProcessTextVectors(&TextPropertiesExample1, &TextValueExample1, false, "CATEGORIES"));
121 TextExample1TestResult.clear();
122 TextExample1TestResult.insert(make_pair("COMMENT", "This is the first comment."));
123 TextExample1TestResult.insert(make_pair("COMMENT", "This is the second comment."));
124 TextExample1TestResult.insert(make_pair("COMMENT", "This is the third comment."));
126 ASSERT_EQ(TextExample1TestResult, ProcessTextVectors(&TextPropertiesExample1, &TextValueExample1, true, "COMMENT"));
128 TextExample1TestResult.clear();
129 TextExample1TestResult.insert(make_pair("COMMENT", "This is the first comment."));
131 // Examples 7 & 8: CONTACT;(properties)
133 ASSERT_EQ(TextExample1TestResult, ProcessTextVectors(&TextPropertiesExample1, &TextValueExample1, false, "COMMENT"));
135 TextExample1TestResult.clear();
136 TextExample1TestResult.insert(make_pair("CONTACT;TEST=VALUE", "First Contact"));
137 TextExample1TestResult.insert(make_pair("CONTACT;LAZY=NOPE", "Second Contact"));
138 TextExample1TestResult.insert(make_pair("CONTACT;SETUP=NO", "Third Contact"));
140 ASSERT_EQ(TextExample1TestResult, ProcessTextVectors(&TextPropertiesExample1, &TextValueExample1, true, "CONTACT"));
142 TextExample1TestResult.clear();
143 TextExample1TestResult.insert(make_pair("CONTACT;TEST=VALUE", "First Contact"));
145 // Examples 9 & 10: RESOURCES;(properties)
147 ASSERT_EQ(TextExample1TestResult, ProcessTextVectors(&TextPropertiesExample1, &TextValueExample1, false, "CONTACT"));
149 TextExample1TestResult.clear();
150 TextExample1TestResult.insert(make_pair("RESOURCES;ROOM=YES", "First Resource Widget"));
151 TextExample1TestResult.insert(make_pair("RESOURCES;ROOM=NO", "Second Resource Widget"));
152 TextExample1TestResult.insert(make_pair("RESOURCES;ROOM=UNKNOWN", "Third Resource Widget"));
154 ASSERT_EQ(TextExample1TestResult, ProcessTextVectors(&TextPropertiesExample1, &TextValueExample1, true, "RESOURCES"));
156 TextExample1TestResult.clear();
157 TextExample1TestResult.insert(make_pair("RESOURCES;ROOM=YES", "First Resource Widget"));
159 ASSERT_EQ(TextExample1TestResult, ProcessTextVectors(&TextPropertiesExample1, &TextValueExample1, false, "RESOURCES"));
161 // Examples 11 & 12: RELATED;(properties)
163 TextExample1TestResult.clear();
164 TextExample1TestResult.insert(make_pair("RELATED;RELATION=NO", "First Relation"));
165 TextExample1TestResult.insert(make_pair("RELATED;RELATION=YES", "Second Relation"));
166 TextExample1TestResult.insert(make_pair("RELATED;RELATION=MAYBE", "Third Relation"));
168 ASSERT_EQ(TextExample1TestResult, ProcessTextVectors(&TextPropertiesExample1, &TextValueExample1, true, "RELATED"));
170 TextExample1TestResult.clear();
171 TextExample1TestResult.insert(make_pair("RELATED;RELATION=NO", "First Relation"));
173 ASSERT_EQ(TextExample1TestResult, ProcessTextVectors(&TextPropertiesExample1, &TextValueExample1, false, "RELATED"));
177 TEST(CommonFunctions, SplitValuesTests){
179 map<string, string> TextExample1TestResult;
181 TextExample1TestResult.insert(make_pair("RELATION", "NO"));
182 TextExample1TestResult.insert(make_pair("TEST", "YES"));
183 TextExample1TestResult.insert(make_pair("DATA", "SOMEDATA"));
185 ASSERT_EQ(TextExample1TestResult, SplitValues("TEST;RELATION=NO;TEST=YES;DATA=SOMEDATA"));
189 TEST(CommonFunctions, SplitNameValueTests){
191 PropertyNameValue NameValueResult;
193 NameValueResult = SplitNameValue("TEST=OK");
195 string PropertyName = NameValueResult.Name;
196 string PropertyValue = NameValueResult.Value;
198 ASSERT_EQ(PropertyName, "TEST");
199 ASSERT_EQ(PropertyValue, "OK");
203 TEST(CommonFunctions, HexToInt){
205 string Value1 = "10"; // 16
206 string Value2 = "50"; // 80
207 string Value3 = "4F"; // 79
208 string Value4 = "FF"; // 255
209 string Value5 = "FFF"; // 4095
210 string Value6 = "FFFF"; // 65535
211 string Value7 = "75AB"; // 30123
212 string Value8 = "2AC"; // 684
213 string Value9 = "!"; // Fail
214 string Value10 = "4BZ"; // Fail
215 string Value11 = "Z?!$"; // Fail
220 Result = HexToInt(&Value1, &OutputValue);
222 ASSERT_EQ(Result, true);
223 ASSERT_EQ(OutputValue, 16);
225 Result = HexToInt(&Value2, &OutputValue);
226 ASSERT_EQ(Result, true);
227 ASSERT_EQ(OutputValue, 80);
229 Result = HexToInt(&Value3, &OutputValue);
230 ASSERT_EQ(Result, true);
231 ASSERT_EQ(OutputValue, 79);
233 Result = HexToInt(&Value4, &OutputValue);
234 ASSERT_EQ(Result, true);
235 ASSERT_EQ(OutputValue, 255);
237 Result = HexToInt(&Value5, &OutputValue);
238 ASSERT_EQ(Result, true);
239 ASSERT_EQ(OutputValue, 4095);
241 Result = HexToInt(&Value6, &OutputValue);
242 ASSERT_EQ(Result, true);
243 ASSERT_EQ(OutputValue, 65535);
245 Result = HexToInt(&Value7, &OutputValue);
246 ASSERT_EQ(Result, true);
247 ASSERT_EQ(OutputValue, 30123);
249 Result = HexToInt(&Value8, &OutputValue);
250 ASSERT_EQ(Result, true);
251 ASSERT_EQ(OutputValue, 684);
253 Result = HexToInt(&Value9, &OutputValue);
254 ASSERT_EQ(Result, false);
256 Result = HexToInt(&Value10, &OutputValue);
257 ASSERT_EQ(Result, false);
259 Result = HexToInt(&Value11, &OutputValue);
260 ASSERT_EQ(Result, false);
264 TEST(CommonFunctions, SplitPathFilename){
266 // Setup the file split.
268 string PathFilenameOriginal = "/example/file/yay.txt";
272 SplitPathFilename(&PathFilenameOriginal, &Path, &File);
274 ASSERT_EQ("/example/file/", Path);
275 ASSERT_EQ("yay.txt", File);
277 PathFilenameOriginal = "/a/path/with/lots/of/bits/in/andthenthis.html";
281 SplitPathFilename(&PathFilenameOriginal, &Path, &File);
283 ASSERT_EQ("/a/path/with/lots/of/bits/in/", Path);
284 ASSERT_EQ("andthenthis.html", File);
286 PathFilenameOriginal = "/one/more/for/a/laugh/hahaha.zip";
290 SplitPathFilename(&PathFilenameOriginal, &Path, &File);
292 ASSERT_EQ("/one/more/for/a/laugh/", Path);
293 ASSERT_EQ("hahaha.zip", File);
297 TEST(CommonFunctions, ColourStruct){
324 ASSERT_EQ("#00000000", (string)Colour1);
325 ASSERT_EQ("#FFFFFFFF", (string)Colour2);
326 ASSERT_EQ("#10101010", (string)Colour3);
327 ASSERT_EQ("#50505050", (string)Colour4);
331 TEST(CommonFunctions, OutputText){
333 string InputText1 = "This is an example text.";
334 string OutputText1 = "This is an example text.";
335 string ResultText1 = "";
337 string InputText2 = "This is the first sentence. This is the second sentence"
338 ". This is the third sentence. This is the fourth sentence. This "
339 "is the fifth sentence.";
340 string OutputText2 = "This is the first sentence. This is the second sentence"
341 ". This is the third se\n ntence. This is the fourth sentence. This "
342 "is the fifth sentence.";
343 string ResultText2 = "";
345 string InputText3 = "This is the first sentence. This is the second sentence"
346 ". This is the third sentence. This is the fourth sentence. This "
347 "is the fifth sentence. This is the sixth sentence. This is the s"
348 "eventh sentence. This is the eighth sentence. This is the ninth "
349 "sentence. This is the tenth sentence.";
350 string OutputText3 = "This is the first sentence. This is the second sentence. This is the third se\n"
351 " ntence. This is the fourth sentence. This is the fifth sentence. This is the\n"
352 " sixth sentence. This is the seventh sentence. This is the eighth sentence. \n"
353 " This is the ninth sentence. This is the tenth sentence.";
354 string ResultText3 = "";
356 string InputText4 = "This is the first sentence. This is the second sentence"
357 ". This is the third sentence. This is the fourth sentence. This "
358 "is the fifth sentence. This is the sixth sentence. This is the s"
359 "eventh sentence. This is the eighth sentence. This is the ninth "
360 "sentence. This is the tenth sentence. This is the eleventh sente"
361 "nce. This is the twelfth sentence. This is the thirteenth senten"
362 "ce. This is the fourteenth sentence. This is the fifteenth sente"
364 string OutputText4 = "This is the first sentence. This is the second sentence. This is the third se\n"
365 " ntence. This is the fourth sentence. This is the fifth sentence. This is the\n"
366 " sixth sentence. This is the seventh sentence. This is the eighth sentence. \n"
367 " This is the ninth sentence. This is the tenth sentence. This is the eleventh\n"
368 " sentence. This is the twelfth sentence. This is the thirteenth sentence. Th\n"
369 " is is the fourteenth sentence. This is the fifteenth sentence.";
370 string ResultText4 = "";
372 string InputText5 = "This is the first sentence. This is the second sentence"
373 ". This is the third sentence. This is the fourth sentence. This "
374 "is the fifth sentence. This is the sixth sentence. This is the s"
375 "eventh sentence. This is the eighth sentence. This is the ninth "
376 "sentence. This is the tenth sentence. This is the eleventh sente"
377 "nce. This is the twelfth sentence. This is the thirteenth senten"
378 "ce. This is the fourteenth sentence. This is the fifteenth sente"
379 "nce. This is the sixteenth sentence. This is the seventeenth sen"
380 "tence. This is the eighteenth sentence. This is the ninteenth se"
381 "ntence. This is the twentieth sentence.";
382 string OutputText5 = "This is the first sentence. This is the second sentence. This is the third se\n"
383 " ntence. This is the fourth sentence. This is the fifth sentence. This is the\n"
384 " sixth sentence. This is the seventh sentence. This is the eighth sentence. \n"
385 " This is the ninth sentence. This is the tenth sentence. This is the eleventh\n"
386 " sentence. This is the twelfth sentence. This is the thirteenth sentence. Th\n"
387 " is is the fourteenth sentence. This is the fifteenth sentence. This is the s\n"
388 " ixteenth sentence. This is the seventeenth sentence. This is the eighteenth \n"
389 " sentence. This is the ninteenth sentence. This is the twentieth sentence.";
392 ResultText1 = OutputText(&InputText1);
393 ResultText2 = OutputText(&InputText2);
394 ResultText3 = OutputText(&InputText3);
395 ResultText4 = OutputText(&InputText4);
396 ResultText5 = OutputText(&InputText5);
398 ASSERT_EQ(ResultText1, OutputText1);
399 ASSERT_EQ(ResultText2, OutputText2);
400 ASSERT_EQ(ResultText3, OutputText3);
401 ASSERT_EQ(ResultText4, OutputText4);
402 ASSERT_EQ(ResultText5, OutputText5);