1
// TortoiseGit - a Windows shell extension for easy version control
3 // Copyright (C) 2015-2017, 2021 - TortoiseGit
4 // Copyright (C) 2003-2011 - TortoiseSVN
6 // This program is free software; you can redistribute it and/or
7 // modify it under the terms of the GNU General Public License
8 // as published by the Free Software Foundation; either version 2
9 // of the License, or (at your option) any later version.
11 // This program 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
17 // along with this program; if not, write to the Free Software Foundation,
18 // 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
22 #include "StringUtils.h"
24 TEST(CStringUtils
, WordWrap
)
26 CString longline
= L
"this is a test of how a string can be splitted into several lines";
27 CString splittedline
= CStringUtils::WordWrap(longline
, 10, true, false, 4);
28 EXPECT_STREQ(L
"this is a\n test of\n how a\n string\n can be\n splitted\n into\n several\n lines", splittedline
);
29 longline
= L
"c:\\this_is_a_very_long\\path_on_windows and of course some other words added to make the line longer";
30 splittedline
= CStringUtils::WordWrap(longline
, 10, true, false, 4);
31 EXPECT_STREQ(L
"...\\pat...\n and of\n course\n some\n other\n words\n added to\n make the\n line\n longer", splittedline
);
32 longline
= L
"Forced failure in https://myserver.com/a_long_url_to_split PROPFIND error";
33 splittedline
= CStringUtils::WordWrap(longline
, 20, true, false, 4);
34 EXPECT_STREQ(L
"Forced failure in\n https://myserver.com/a_long_url_to_split\n PROPFIND error", splittedline
);
35 longline
= L
"Forced\nfailure in https://myserver.com/a_long_url_to_split PROPFIND\nerror";
36 splittedline
= CStringUtils::WordWrap(longline
, 40, true, false, 4);
37 EXPECT_STREQ(L
"Forced\nfailure in\n https://myserver.com/a_long_url_to_split\n PROPFIND\nerror", splittedline
);
38 longline
= L
"Failed to add file\nc:\\export\\spare\\Devl-JBoss\\development\\head\\src\\something\\CoreApplication\\somethingelse\\src\\com\\yetsomthingelse\\shipper\\DAO\\ShipmentInfoDAO1.java\nc:\\export\\spare\\Devl-JBoss\\development\\head\\src\\something\\CoreApplication\\somethingelse\\src\\com\\yetsomthingelse\\shipper\\DAO\\ShipmentInfoDAO2.java";
39 splittedline
= CStringUtils::WordWrap(longline
, 80, true, false, 4);
40 EXPECT_STREQ(L
"Failed to add\n file\nc:\\export\\spare\\Devl-JBoss\\development\\head\\src\\something\\CoreApplication\\somethingelse\\src\\com\\yetsomthingelse\\shipper\\DAO\\ShipmentInfoDAO1.java\nc:\\export\\spare\\Devl-JBoss\\development\\head\\src\\something\\CoreApplication\\somethingelse\\src\\com\\yetsomthing\n", splittedline
);
43 TEST(CStringUtils
, LinesWrap
)
45 CString longline
= L
"this is a test of how a string can be splitted into several lines";
46 CString splittedline
= CStringUtils::LinesWrap(longline
, 10, true);
47 EXPECT_STREQ(L
"this is a\n test of\n how a\n string\n can be\n splitted\n into\n several\n lines", splittedline
);
48 longline
= L
"c:\\this_is_a_very_long\\path_on_windows and of course some other words added to make the line longer";
49 splittedline
= CStringUtils::LinesWrap(longline
, 10);
50 EXPECT_STREQ(L
"c:\\this_is_a_very_long\\path_on_windows\n and of\n course\n some\n other\n words\n added to\n make the\n line\n longer", splittedline
);
51 longline
= L
"Forced failure in https://myserver.com/a_long_url_to_split PROPFIND error";
52 splittedline
= CStringUtils::LinesWrap(longline
, 20, true);
53 EXPECT_STREQ(L
"Forced failure in\n https://myserver.com/a_long_url_to_split\n PROPFIND error", splittedline
);
54 longline
= L
"Forced\nfailure in https://myserver.com/a_long_url_to_split PROPFIND\nerror";
55 splittedline
= CStringUtils::LinesWrap(longline
, 40);
56 EXPECT_STREQ(L
"Forced\nfailure in\n https://myserver.com/a_long_url_to_split\n PROPFIND\nerror", splittedline
);
57 longline
= L
"Failed to add file\nc:\\export\\spare\\Devl-JBoss\\development\\head\\src\\something\\CoreApplication\\somethingelse\\src\\com\\yetsomthingelse\\shipper\\DAO\\ShipmentInfoDAO1.java\nc:\\export\\spare\\Devl-JBoss\\development\\head\\src\\something\\CoreApplication\\somethingelse\\src\\com\\yetsomthingelse\\shipper\\DAO\\ShipmentInfoDAO2.java";
58 splittedline
= CStringUtils::LinesWrap(longline
);
59 EXPECT_STREQ(L
"Failed to add file\nc:\\export\\spare\\Devl-JBoss\\development\\head\\src\\something\\CoreApplication\\somethingelse\\src\\com\\yetsomthingelse\\shipper\\DAO\\ShipmentInfoDAO1.java\n\nc:\\export\\spare\\Devl-JBoss\\development\\head\\src\\something\\CoreApplication\\somethingelse\\src\\com\\yetsomthingelse\\shipper\\DAO\\ShipmentInfoDAO2.java", splittedline
);
60 longline
= L
"The commit comment is not properly formatted.\nFormat:\n Field 1 : Field 2 : Field 3\nWhere:\nField 1 - Team Name|Triage|Merge|Goal\nField 2 - V1 Backlog Item ID|Triage Number|SVNBranch|Goal Name\nField 3 - Description of change\nExamples:\n\nTeam Gamma : B-12345 : Changed some code\n Triage : 123 : Fixed production release bug\n Merge : sprint0812 : Merged sprint0812 into prod\n Goal : Implement Pre-Commit Hook : Commit message hook impl";
61 splittedline
= CStringUtils::LinesWrap(longline
, 80);
62 EXPECT_STREQ(L
"The commit comment is not properly formatted.\nFormat:\n Field 1 : Field 2 : Field 3\nWhere:\nField 1 - Team Name|Triage|Merge|Goal\nField 2 - V1 Backlog Item ID|Triage Number|SVNBranch|Goal Name\nField 3 - Description of change\nExamples:\n\nTeam Gamma : B-12345 : Changed some code\n Triage : 123 : Fixed production release bug\n Merge : sprint0812 : Merged sprint0812 into prod\n Goal : Implement Pre-Commit Hook : Commit message hook impl", splittedline
);
65 TEST(CStringUtils
, RemoveAccelerators
)
68 CStringUtils::RemoveAccelerators(empty
);
69 EXPECT_STREQ(L
"", empty
);
71 CString text1
= L
"&Accellerator";
72 CStringUtils::RemoveAccelerators(text1
);
73 EXPECT_STREQ(L
"Accellerator", text1
);
75 CString text1a
= L
"Ac&cellerator";
76 CStringUtils::RemoveAccelerators(text1a
);
77 EXPECT_STREQ(L
"Accellerator", text1a
);
79 CString text2
= L
"Accellerator&";
80 CStringUtils::RemoveAccelerators(text2
);
81 EXPECT_STREQ(L
"Accellerator", text2
);
83 CString text3
= L
"Some & text";
84 CStringUtils::RemoveAccelerators(text3
);
85 EXPECT_STREQ(L
"Some & text", text3
);
87 CString text4
= L
"&&Accellerator";
88 CStringUtils::RemoveAccelerators(text4
);
89 EXPECT_STREQ(L
"&Accellerator", text4
);
91 CString text5
= L
"Acce&&&llerator";
92 CStringUtils::RemoveAccelerators(text5
);
93 EXPECT_STREQ(L
"Acce&llerator", text5
);
95 CString text6
= L
"Some & te&xt";
96 CStringUtils::RemoveAccelerators(text6
);
97 EXPECT_STREQ(L
"Some & text", text6
);
100 TEST(CStringUtils
, ParseEmailAddress
)
103 CStringUtils::ParseEmailAddress(L
"", mail
, &name
);
104 EXPECT_STREQ(L
"", mail
);
105 EXPECT_STREQ(L
"", name
);
107 CStringUtils::ParseEmailAddress(L
" ", mail
, &name
);
108 EXPECT_STREQ(L
"", mail
);
109 EXPECT_STREQ(L
"", name
);
112 CStringUtils::ParseEmailAddress(L
"test@example.com ", mail
);
113 EXPECT_STREQ(L
"test@example.com", mail
);
116 CStringUtils::ParseEmailAddress(L
" test@example.com", mail
);
117 EXPECT_STREQ(L
"test@example.com", mail
);
120 CStringUtils::ParseEmailAddress(L
"test@example.com", mail
);
121 EXPECT_STREQ(L
"test@example.com", mail
);
124 CStringUtils::ParseEmailAddress(L
"John Doe <johndoe>", mail
);
125 EXPECT_STREQ(L
"johndoe", mail
);
129 CStringUtils::ParseEmailAddress(L
"test@example.com", mail
, &name
);
130 EXPECT_STREQ(L
"test@example.com", mail
);
131 EXPECT_STREQ(L
"test@example.com", name
);
135 CStringUtils::ParseEmailAddress(L
"<test@example.com>", mail
, &name
);
136 EXPECT_STREQ(L
"test@example.com", mail
);
137 EXPECT_STREQ(L
"test@example.com", name
);
141 CStringUtils::ParseEmailAddress(L
"John Doe <test@example.com>", mail
, &name
);
142 EXPECT_STREQ(L
"test@example.com", mail
);
143 EXPECT_STREQ(L
"John Doe", name
);
147 CStringUtils::ParseEmailAddress(L
"\"John Doe\" <test@example.com>", mail
, &name
);
148 EXPECT_STREQ(L
"test@example.com", mail
);
149 EXPECT_STREQ(L
"John Doe", name
);
153 CStringUtils::ParseEmailAddress(L
"<test@example.com", mail
, &name
);
154 EXPECT_STREQ(L
"test@example.com", mail
);
155 EXPECT_STREQ(L
"test@example.com", name
);
159 CStringUtils::ParseEmailAddress(L
"test@example.com>", mail
, &name
);
160 EXPECT_STREQ(L
"test@example.com", mail
);
161 EXPECT_STREQ(L
"test@example.com", name
);
165 CStringUtils::ParseEmailAddress(L
"John Doe <johndoe>", mail
, &name
);
166 EXPECT_STREQ(L
"johndoe", mail
);
167 EXPECT_STREQ(L
"John Doe", name
);
171 CStringUtils::ParseEmailAddress(L
"john.doe@example.com (John Doe)", mail
, &name
);
172 EXPECT_STREQ(L
"john.doe@example.com", mail
);
173 EXPECT_STREQ(L
"John Doe", name
);
177 CStringUtils::ParseEmailAddress(L
"John (zzz) Doe <john.doe@example.com> (Comment)", mail
, &name
);
178 EXPECT_STREQ(L
"john.doe@example.com", mail
);
179 EXPECT_STREQ(L
"John (zzz) Doe (Comment)", name
);
183 CStringUtils::ParseEmailAddress(L
"\"john.doe@example.com >> John Doe\" <john.doe@example.com>", mail
, &name
);
184 EXPECT_STREQ(L
"john.doe@example.com", mail
);
185 EXPECT_STREQ(L
"john.doe@example.com >> John Doe", name
);
189 CStringUtils::ParseEmailAddress(L
"\"John<something> Doe\" <john.doe@example.com>", mail
, &name
);
190 EXPECT_STREQ(L
"john.doe@example.com", mail
);
191 EXPECT_STREQ(L
"John<something> Doe", name
);
195 CStringUtils::ParseEmailAddress(L
"\"John<something@example.com> Doe\" <john.doe@example.com>", mail
, &name
);
196 EXPECT_STREQ(L
"john.doe@example.com", mail
);
197 EXPECT_STREQ(L
"John<something@example.com> Doe", name
);
201 CStringUtils::ParseEmailAddress(L
"\"something@example.com\" <john.doe@example.com>", mail
, &name
);
202 EXPECT_STREQ(L
"john.doe@example.com", mail
);
203 EXPECT_STREQ(L
"something@example.com", name
);
207 CStringUtils::ParseEmailAddress(L
"\"John D\\\"o\\\"e\" <john.doe@example.com>", mail
, &name
);
208 EXPECT_STREQ(L
"john.doe@example.com", mail
);
209 EXPECT_STREQ(L
"John D\"o\"e", name
);
213 CStringUtils::ParseEmailAddress(L
"<test@example.com> \"John Doe\"", mail
, &name
);
214 EXPECT_STREQ(L
"test@example.com", mail
);
215 EXPECT_STREQ(L
"John Doe", name
);
218 TEST(CStringUtils
, IsPlainReadableASCII
)
220 EXPECT_TRUE(CStringUtils::IsPlainReadableASCII(L
""));
221 EXPECT_TRUE(CStringUtils::IsPlainReadableASCII(L
" 1234,#+*.:-&!\\<>|\"$%&/()=567890abcdefSBDUDB[](!}~"));
222 EXPECT_FALSE(CStringUtils::IsPlainReadableASCII(L
"\n"));
223 EXPECT_FALSE(CStringUtils::IsPlainReadableASCII(L
"\u2302"));
224 EXPECT_FALSE(CStringUtils::IsPlainReadableASCII(L
"é"));
225 EXPECT_FALSE(CStringUtils::IsPlainReadableASCII(L
"\u2550"));
226 EXPECT_FALSE(CStringUtils::IsPlainReadableASCII(L
"ä"));
227 EXPECT_FALSE(CStringUtils::IsPlainReadableASCII(L
"€"));
228 EXPECT_FALSE(CStringUtils::IsPlainReadableASCII(L
"\u570B"));
229 EXPECT_FALSE(CStringUtils::IsPlainReadableASCII(L
"\u7ACB"));
232 TEST(CStringUtils
, StartsWith
)
234 EXPECT_TRUE(CStringUtils::StartsWith(L
"", L
""));
235 EXPECT_FALSE(CStringUtils::StartsWith(L
"", L
"sometest"));
237 CString heystack
= L
"sometest";
238 EXPECT_TRUE(CStringUtils::StartsWith(heystack
, L
"sometest"));
239 EXPECT_TRUE(CStringUtils::StartsWith(heystack
, L
""));
240 EXPECT_TRUE(CStringUtils::StartsWith(heystack
, L
"sometes"));
241 EXPECT_FALSE(CStringUtils::StartsWith(heystack
, L
"sometEs"));
242 EXPECT_FALSE(CStringUtils::StartsWith(heystack
, L
"someteste"));
243 EXPECT_FALSE(CStringUtils::StartsWith(heystack
, L
"sometess"));
246 CString sometest
= L
"sometest";
247 CString sometes
= L
"sometes";
248 CString sometEs
= L
"sometEs";
249 CString someteste
= L
"someteste";
250 CString sometess
= L
"sometess";
251 EXPECT_TRUE(CStringUtils::StartsWith(heystack
, sometest
));
252 EXPECT_TRUE(CStringUtils::StartsWith(heystack
, empty
));
253 EXPECT_TRUE(CStringUtils::StartsWith(heystack
, sometes
));
254 EXPECT_FALSE(CStringUtils::StartsWith(heystack
, sometEs
));
255 EXPECT_FALSE(CStringUtils::StartsWith(heystack
, someteste
));
256 EXPECT_FALSE(CStringUtils::StartsWith(heystack
, sometess
));
257 EXPECT_FALSE(CStringUtils::StartsWith(empty
, sometess
));
260 TEST(CStringUtils
, StartsWithI
)
262 EXPECT_TRUE(CStringUtils::StartsWithI(L
"", L
""));
263 EXPECT_FALSE(CStringUtils::StartsWithI(L
"", L
"sometest"));
265 CString heystack
= L
"someTest";
266 EXPECT_TRUE(CStringUtils::StartsWithI(heystack
, L
"sometest"));
267 EXPECT_TRUE(CStringUtils::StartsWithI(heystack
, L
"someTest"));
268 EXPECT_TRUE(CStringUtils::StartsWithI(heystack
, L
"sOmetest"));
269 EXPECT_TRUE(CStringUtils::StartsWithI(heystack
, L
""));
270 EXPECT_TRUE(CStringUtils::StartsWithI(heystack
, L
"sometEs"));
271 EXPECT_FALSE(CStringUtils::StartsWithI(heystack
, L
"sometesTe"));
272 EXPECT_FALSE(CStringUtils::StartsWithI(heystack
, L
"someteSs"));
275 CString sometest
= L
"someteSt";
276 CString sometes
= L
"soMetes";
277 CString someteste
= L
"sOmeteste";
278 CString sometess
= L
"sometesS";
279 EXPECT_TRUE(CStringUtils::StartsWithI(heystack
, sometest
));
280 EXPECT_TRUE(CStringUtils::StartsWithI(heystack
, empty
));
281 EXPECT_TRUE(CStringUtils::StartsWithI(heystack
, sometes
));
282 EXPECT_FALSE(CStringUtils::StartsWithI(heystack
, someteste
));
283 EXPECT_FALSE(CStringUtils::StartsWithI(heystack
, sometess
));
284 EXPECT_FALSE(CStringUtils::StartsWithI(empty
, sometess
));
287 TEST(CStringUtils
, StartsWithA
)
289 EXPECT_TRUE(CStringUtils::StartsWith("", ""));
290 EXPECT_FALSE(CStringUtils::StartsWith("", "sometest"));
292 CStringA heystack
= L
"sometest";
293 EXPECT_TRUE(CStringUtils::StartsWith(heystack
, "sometest"));
294 EXPECT_TRUE(CStringUtils::StartsWith(heystack
, ""));
295 EXPECT_TRUE(CStringUtils::StartsWith(heystack
, "sometes"));
296 EXPECT_FALSE(CStringUtils::StartsWith(heystack
, "someTe"));
297 EXPECT_FALSE(CStringUtils::StartsWith(heystack
, "someteste"));
298 EXPECT_FALSE(CStringUtils::StartsWith(heystack
, "sometess"));
301 TEST(CStringUtils
, EndsWith
)
303 EXPECT_TRUE(CStringUtils::EndsWith(L
"", L
""));
304 EXPECT_FALSE(CStringUtils::EndsWith(L
"", L
"sometest"));
306 CString heystack
= L
"sometest";
307 EXPECT_TRUE(CStringUtils::EndsWith(heystack
, L
"sometest"));
308 EXPECT_TRUE(CStringUtils::EndsWith(heystack
, L
"test"));
309 EXPECT_TRUE(CStringUtils::EndsWith(heystack
, L
"st"));
310 EXPECT_TRUE(CStringUtils::EndsWith(heystack
, L
"t"));
311 EXPECT_TRUE(CStringUtils::EndsWith(heystack
, L
""));
312 EXPECT_FALSE(CStringUtils::EndsWith(heystack
, L
"teSt"));
313 EXPECT_FALSE(CStringUtils::EndsWith(heystack
, L
"esometest"));
314 EXPECT_FALSE(CStringUtils::EndsWith(heystack
, L
"someteste"));
315 EXPECT_FALSE(CStringUtils::EndsWith(heystack
, L
"text"));
316 EXPECT_FALSE(CStringUtils::EndsWith(heystack
, L
"xt"));
318 EXPECT_TRUE(CStringUtils::EndsWith(heystack
, L
't'));
319 EXPECT_FALSE(CStringUtils::EndsWith(heystack
, L
'T'));
320 EXPECT_FALSE(CStringUtils::EndsWith(heystack
, L
'x'));
323 TEST(CStringUtils
, EndsWithI
)
325 EXPECT_TRUE(CStringUtils::EndsWithI(L
"", L
""));
326 EXPECT_FALSE(CStringUtils::EndsWithI(L
"", L
"sometest"));
328 CString heystack
= L
"sometest";
329 EXPECT_TRUE(CStringUtils::EndsWithI(heystack
, L
"someteSt"));
330 EXPECT_TRUE(CStringUtils::EndsWithI(heystack
, L
"tEst"));
331 EXPECT_TRUE(CStringUtils::EndsWithI(heystack
, L
"sT"));
332 EXPECT_TRUE(CStringUtils::EndsWithI(heystack
, L
"T"));
333 EXPECT_TRUE(CStringUtils::EndsWithI(heystack
, L
""));
334 EXPECT_FALSE(CStringUtils::EndsWithI(heystack
, L
"esometEst"));
335 EXPECT_FALSE(CStringUtils::EndsWithI(heystack
, L
"someteSte"));
336 EXPECT_FALSE(CStringUtils::EndsWithI(heystack
, L
"text"));
337 EXPECT_FALSE(CStringUtils::EndsWithI(heystack
, L
"xt"));
340 TEST(CStringUtils
, UnescapeGitQuotePath
)
342 EXPECT_STREQ(L
"", CStringUtils::UnescapeGitQuotePath(L
""));
344 EXPECT_STREQ(L
"ascii.txt", CStringUtils::UnescapeGitQuotePath(L
"ascii.txt"));
345 EXPECT_STREQ(L
"ümlauts.txt", CStringUtils::UnescapeGitQuotePath(L
"\\303\\274mlauts.txt"));
346 EXPECT_STREQ(L
"umläütß", CStringUtils::UnescapeGitQuotePath(L
"uml\\303\\244\\303\\274t\\303\\237"));
348 // taken from Git tests:
349 EXPECT_STREQ(L
"\u6FF1\u91CE/file", CStringUtils::UnescapeGitQuotePath(L
"\\346\\277\\261\\351\\207\\216/file"));
350 EXPECT_STREQ(L
"\u6FF1\u91CE\u7D14", CStringUtils::UnescapeGitQuotePath(L
"\\346\\277\\261\\351\\207\\216\\347\\264\\224"));