1 // Copyright 2008, Google Inc. 2 // All rights reserved. 3 // 4 // Redistribution and use in source and binary forms, with or without 5 // modification, are permitted provided that the following conditions are 6 // met: 7 // 8 // * Redistributions of source code must retain the above copyright 9 // notice, this list of conditions and the following disclaimer. 10 // * Redistributions in binary form must reproduce the above 11 // copyright notice, this list of conditions and the following disclaimer 12 // in the documentation and/or other materials provided with the 13 // distribution. 14 // * Neither the name of Google Inc. nor the names of its 15 // contributors may be used to endorse or promote products derived from 16 // this software without specific prior written permission. 17 // 18 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 19 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 20 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 21 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 22 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 23 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 24 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 25 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 26 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 27 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 28 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 29 // 30 // Authors: vladl (at) google.com (Vlad Losev), wan (at) google.com (Zhanyong Wan) 31 // 32 // This file tests the internal cross-platform support utilities. 33 34 #include <gtest/internal/gtest-port.h> 35 #include <gtest/gtest.h> 36 #include <gtest/gtest-spi.h> 37 38 // Indicates that this translation unit is part of Google Test's 39 // implementation. It must come before gtest-internal-inl.h is 40 // included, or there will be a compiler error. This trick is to 41 // prevent a user from accidentally including gtest-internal-inl.h in 42 // his code. 43 #define GTEST_IMPLEMENTATION_ 1 44 #include "src/gtest-internal-inl.h" 45 #undef GTEST_IMPLEMENTATION_ 46 47 namespace testing { 48 namespace internal { 49 50 TEST(GtestCheckSyntaxTest, BehavesLikeASingleStatement) { 51 if (false) 52 GTEST_CHECK_(false) << "This should never be executed; " 53 "It's a compilation test only."; 54 55 if (true) 56 GTEST_CHECK_(true); 57 else 58 ; // NOLINT 59 60 if (false) 61 ; // NOLINT 62 else 63 GTEST_CHECK_(true) << ""; 64 } 65 66 TEST(GtestCheckSyntaxTest, WorksWithSwitch) { 67 switch (0) { 68 case 1: 69 break; 70 default: 71 GTEST_CHECK_(true); 72 } 73 74 switch(0) 75 case 0: 76 GTEST_CHECK_(true) << "Check failed in switch case"; 77 } 78 79 #if GTEST_HAS_DEATH_TEST 80 81 TEST(GtestCheckDeathTest, DiesWithCorrectOutputOnFailure) { 82 const bool a_false_condition = false; 83 const char regex[] = 84 #ifdef _MSC_VER 85 "gtest-port_test\\.cc\\(\\d+\\):" 86 #else 87 "gtest-port_test\\.cc:[0-9]+" 88 #endif // _MSC_VER 89 ".*a_false_condition.*Extra info.*"; 90 91 EXPECT_DEATH(GTEST_CHECK_(a_false_condition) << "Extra info", regex); 92 } 93 94 TEST(GtestCheckDeathTest, LivesSilentlyOnSuccess) { 95 EXPECT_EXIT({ 96 GTEST_CHECK_(true) << "Extra info"; 97 ::std::cerr << "Success\n"; 98 exit(0); }, 99 ::testing::ExitedWithCode(0), "Success"); 100 } 101 102 #endif // GTEST_HAS_DEATH_TEST 103 104 #if GTEST_USES_POSIX_RE 105 106 template <typename Str> 107 class RETest : public ::testing::Test {}; 108 109 // Defines StringTypes as the list of all string types that class RE 110 // supports. 111 typedef testing::Types< 112 #if GTEST_HAS_STD_STRING 113 ::std::string, 114 #endif // GTEST_HAS_STD_STRING 115 #if GTEST_HAS_GLOBAL_STRING 116 ::string, 117 #endif // GTEST_HAS_GLOBAL_STRING 118 const char*> StringTypes; 119 120 TYPED_TEST_CASE(RETest, StringTypes); 121 122 // Tests RE's implicit constructors. 123 TYPED_TEST(RETest, ImplicitConstructorWorks) { 124 const RE empty(TypeParam("")); 125 EXPECT_STREQ("", empty.pattern()); 126 127 const RE simple(TypeParam("hello")); 128 EXPECT_STREQ("hello", simple.pattern()); 129 130 const RE normal(TypeParam(".*(\\w+)")); 131 EXPECT_STREQ(".*(\\w+)", normal.pattern()); 132 } 133 134 // Tests that RE's constructors reject invalid regular expressions. 135 TYPED_TEST(RETest, RejectsInvalidRegex) { 136 EXPECT_NONFATAL_FAILURE({ 137 const RE invalid(TypeParam("?")); 138 }, "\"?\" is not a valid POSIX Extended regular expression."); 139 } 140 141 // Tests RE::FullMatch(). 142 TYPED_TEST(RETest, FullMatchWorks) { 143 const RE empty(TypeParam("")); 144 EXPECT_TRUE(RE::FullMatch(TypeParam(""), empty)); 145 EXPECT_FALSE(RE::FullMatch(TypeParam("a"), empty)); 146 147 const RE re(TypeParam("a.*z")); 148 EXPECT_TRUE(RE::FullMatch(TypeParam("az"), re)); 149 EXPECT_TRUE(RE::FullMatch(TypeParam("axyz"), re)); 150 EXPECT_FALSE(RE::FullMatch(TypeParam("baz"), re)); 151 EXPECT_FALSE(RE::FullMatch(TypeParam("azy"), re)); 152 } 153 154 // Tests RE::PartialMatch(). 155 TYPED_TEST(RETest, PartialMatchWorks) { 156 const RE empty(TypeParam("")); 157 EXPECT_TRUE(RE::PartialMatch(TypeParam(""), empty)); 158 EXPECT_TRUE(RE::PartialMatch(TypeParam("a"), empty)); 159 160 const RE re(TypeParam("a.*z")); 161 EXPECT_TRUE(RE::PartialMatch(TypeParam("az"), re)); 162 EXPECT_TRUE(RE::PartialMatch(TypeParam("axyz"), re)); 163 EXPECT_TRUE(RE::PartialMatch(TypeParam("baz"), re)); 164 EXPECT_TRUE(RE::PartialMatch(TypeParam("azy"), re)); 165 EXPECT_FALSE(RE::PartialMatch(TypeParam("zza"), re)); 166 } 167 168 #elif GTEST_USES_SIMPLE_RE 169 170 TEST(IsInSetTest, NulCharIsNotInAnySet) { 171 EXPECT_FALSE(IsInSet('\0', "")); 172 EXPECT_FALSE(IsInSet('\0', "\0")); 173 EXPECT_FALSE(IsInSet('\0', "a")); 174 } 175 176 TEST(IsInSetTest, WorksForNonNulChars) { 177 EXPECT_FALSE(IsInSet('a', "Ab")); 178 EXPECT_FALSE(IsInSet('c', "")); 179 180 EXPECT_TRUE(IsInSet('b', "bcd")); 181 EXPECT_TRUE(IsInSet('b', "ab")); 182 } 183 184 TEST(IsDigitTest, IsFalseForNonDigit) { 185 EXPECT_FALSE(IsDigit('\0')); 186 EXPECT_FALSE(IsDigit(' ')); 187 EXPECT_FALSE(IsDigit('+')); 188 EXPECT_FALSE(IsDigit('-')); 189 EXPECT_FALSE(IsDigit('.')); 190 EXPECT_FALSE(IsDigit('a')); 191 } 192 193 TEST(IsDigitTest, IsTrueForDigit) { 194 EXPECT_TRUE(IsDigit('0')); 195 EXPECT_TRUE(IsDigit('1')); 196 EXPECT_TRUE(IsDigit('5')); 197 EXPECT_TRUE(IsDigit('9')); 198 } 199 200 TEST(IsPunctTest, IsFalseForNonPunct) { 201 EXPECT_FALSE(IsPunct('\0')); 202 EXPECT_FALSE(IsPunct(' ')); 203 EXPECT_FALSE(IsPunct('\n')); 204 EXPECT_FALSE(IsPunct('a')); 205 EXPECT_FALSE(IsPunct('0')); 206 } 207 208 TEST(IsPunctTest, IsTrueForPunct) { 209 for (const char* p = "^-!\"#$%&'()*+,./:;<=>?@[\\]_`{|}~"; *p; p++) { 210 EXPECT_PRED1(IsPunct, *p); 211 } 212 } 213 214 TEST(IsRepeatTest, IsFalseForNonRepeatChar) { 215 EXPECT_FALSE(IsRepeat('\0')); 216 EXPECT_FALSE(IsRepeat(' ')); 217 EXPECT_FALSE(IsRepeat('a')); 218 EXPECT_FALSE(IsRepeat('1')); 219 EXPECT_FALSE(IsRepeat('-')); 220 } 221 222 TEST(IsRepeatTest, IsTrueForRepeatChar) { 223 EXPECT_TRUE(IsRepeat('?')); 224 EXPECT_TRUE(IsRepeat('*')); 225 EXPECT_TRUE(IsRepeat('+')); 226 } 227 228 TEST(IsWhiteSpaceTest, IsFalseForNonWhiteSpace) { 229 EXPECT_FALSE(IsWhiteSpace('\0')); 230 EXPECT_FALSE(IsWhiteSpace('a')); 231 EXPECT_FALSE(IsWhiteSpace('1')); 232 EXPECT_FALSE(IsWhiteSpace('+')); 233 EXPECT_FALSE(IsWhiteSpace('_')); 234 } 235 236 TEST(IsWhiteSpaceTest, IsTrueForWhiteSpace) { 237 EXPECT_TRUE(IsWhiteSpace(' ')); 238 EXPECT_TRUE(IsWhiteSpace('\n')); 239 EXPECT_TRUE(IsWhiteSpace('\r')); 240 EXPECT_TRUE(IsWhiteSpace('\t')); 241 EXPECT_TRUE(IsWhiteSpace('\v')); 242 EXPECT_TRUE(IsWhiteSpace('\f')); 243 } 244 245 TEST(IsWordCharTest, IsFalseForNonWordChar) { 246 EXPECT_FALSE(IsWordChar('\0')); 247 EXPECT_FALSE(IsWordChar('+')); 248 EXPECT_FALSE(IsWordChar('.')); 249 EXPECT_FALSE(IsWordChar(' ')); 250 EXPECT_FALSE(IsWordChar('\n')); 251 } 252 253 TEST(IsWordCharTest, IsTrueForLetter) { 254 EXPECT_TRUE(IsWordChar('a')); 255 EXPECT_TRUE(IsWordChar('b')); 256 EXPECT_TRUE(IsWordChar('A')); 257 EXPECT_TRUE(IsWordChar('Z')); 258 } 259 260 TEST(IsWordCharTest, IsTrueForDigit) { 261 EXPECT_TRUE(IsWordChar('0')); 262 EXPECT_TRUE(IsWordChar('1')); 263 EXPECT_TRUE(IsWordChar('7')); 264 EXPECT_TRUE(IsWordChar('9')); 265 } 266 267 TEST(IsWordCharTest, IsTrueForUnderscore) { 268 EXPECT_TRUE(IsWordChar('_')); 269 } 270 271 TEST(IsValidEscapeTest, IsFalseForNonPrintable) { 272 EXPECT_FALSE(IsValidEscape('\0')); 273 EXPECT_FALSE(IsValidEscape('\007')); 274 } 275 276 TEST(IsValidEscapeTest, IsFalseForDigit) { 277 EXPECT_FALSE(IsValidEscape('0')); 278 EXPECT_FALSE(IsValidEscape('9')); 279 } 280 281 TEST(IsValidEscapeTest, IsFalseForWhiteSpace) { 282 EXPECT_FALSE(IsValidEscape(' ')); 283 EXPECT_FALSE(IsValidEscape('\n')); 284 } 285 286 TEST(IsValidEscapeTest, IsFalseForSomeLetter) { 287 EXPECT_FALSE(IsValidEscape('a')); 288 EXPECT_FALSE(IsValidEscape('Z')); 289 } 290 291 TEST(IsValidEscapeTest, IsTrueForPunct) { 292 EXPECT_TRUE(IsValidEscape('.')); 293 EXPECT_TRUE(IsValidEscape('-')); 294 EXPECT_TRUE(IsValidEscape('^')); 295 EXPECT_TRUE(IsValidEscape('$')); 296 EXPECT_TRUE(IsValidEscape('(')); 297 EXPECT_TRUE(IsValidEscape(']')); 298 EXPECT_TRUE(IsValidEscape('{')); 299 EXPECT_TRUE(IsValidEscape('|')); 300 } 301 302 TEST(IsValidEscapeTest, IsTrueForSomeLetter) { 303 EXPECT_TRUE(IsValidEscape('d')); 304 EXPECT_TRUE(IsValidEscape('D')); 305 EXPECT_TRUE(IsValidEscape('s')); 306 EXPECT_TRUE(IsValidEscape('S')); 307 EXPECT_TRUE(IsValidEscape('w')); 308 EXPECT_TRUE(IsValidEscape('W')); 309 } 310 311 TEST(AtomMatchesCharTest, EscapedPunct) { 312 EXPECT_FALSE(AtomMatchesChar(true, '\\', '\0')); 313 EXPECT_FALSE(AtomMatchesChar(true, '\\', ' ')); 314 EXPECT_FALSE(AtomMatchesChar(true, '_', '.')); 315 EXPECT_FALSE(AtomMatchesChar(true, '.', 'a')); 316 317 EXPECT_TRUE(AtomMatchesChar(true, '\\', '\\')); 318 EXPECT_TRUE(AtomMatchesChar(true, '_', '_')); 319 EXPECT_TRUE(AtomMatchesChar(true, '+', '+')); 320 EXPECT_TRUE(AtomMatchesChar(true, '.', '.')); 321 } 322 323 TEST(AtomMatchesCharTest, Escaped_d) { 324 EXPECT_FALSE(AtomMatchesChar(true, 'd', '\0')); 325 EXPECT_FALSE(AtomMatchesChar(true, 'd', 'a')); 326 EXPECT_FALSE(AtomMatchesChar(true, 'd', '.')); 327 328 EXPECT_TRUE(AtomMatchesChar(true, 'd', '0')); 329 EXPECT_TRUE(AtomMatchesChar(true, 'd', '9')); 330 } 331 332 TEST(AtomMatchesCharTest, Escaped_D) { 333 EXPECT_FALSE(AtomMatchesChar(true, 'D', '0')); 334 EXPECT_FALSE(AtomMatchesChar(true, 'D', '9')); 335 336 EXPECT_TRUE(AtomMatchesChar(true, 'D', '\0')); 337 EXPECT_TRUE(AtomMatchesChar(true, 'D', 'a')); 338 EXPECT_TRUE(AtomMatchesChar(true, 'D', '-')); 339 } 340 341 TEST(AtomMatchesCharTest, Escaped_s) { 342 EXPECT_FALSE(AtomMatchesChar(true, 's', '\0')); 343 EXPECT_FALSE(AtomMatchesChar(true, 's', 'a')); 344 EXPECT_FALSE(AtomMatchesChar(true, 's', '.')); 345 EXPECT_FALSE(AtomMatchesChar(true, 's', '9')); 346 347 EXPECT_TRUE(AtomMatchesChar(true, 's', ' ')); 348 EXPECT_TRUE(AtomMatchesChar(true, 's', '\n')); 349 EXPECT_TRUE(AtomMatchesChar(true, 's', '\t')); 350 } 351 352 TEST(AtomMatchesCharTest, Escaped_S) { 353 EXPECT_FALSE(AtomMatchesChar(true, 'S', ' ')); 354 EXPECT_FALSE(AtomMatchesChar(true, 'S', '\r')); 355 356 EXPECT_TRUE(AtomMatchesChar(true, 'S', '\0')); 357 EXPECT_TRUE(AtomMatchesChar(true, 'S', 'a')); 358 EXPECT_TRUE(AtomMatchesChar(true, 'S', '9')); 359 } 360 361 TEST(AtomMatchesCharTest, Escaped_w) { 362 EXPECT_FALSE(AtomMatchesChar(true, 'w', '\0')); 363 EXPECT_FALSE(AtomMatchesChar(true, 'w', '+')); 364 EXPECT_FALSE(AtomMatchesChar(true, 'w', ' ')); 365 EXPECT_FALSE(AtomMatchesChar(true, 'w', '\n')); 366 367 EXPECT_TRUE(AtomMatchesChar(true, 'w', '0')); 368 EXPECT_TRUE(AtomMatchesChar(true, 'w', 'b')); 369 EXPECT_TRUE(AtomMatchesChar(true, 'w', 'C')); 370 EXPECT_TRUE(AtomMatchesChar(true, 'w', '_')); 371 } 372 373 TEST(AtomMatchesCharTest, Escaped_W) { 374 EXPECT_FALSE(AtomMatchesChar(true, 'W', 'A')); 375 EXPECT_FALSE(AtomMatchesChar(true, 'W', 'b')); 376 EXPECT_FALSE(AtomMatchesChar(true, 'W', '9')); 377 EXPECT_FALSE(AtomMatchesChar(true, 'W', '_')); 378 379 EXPECT_TRUE(AtomMatchesChar(true, 'W', '\0')); 380 EXPECT_TRUE(AtomMatchesChar(true, 'W', '*')); 381 EXPECT_TRUE(AtomMatchesChar(true, 'W', '\n')); 382 } 383 384 TEST(AtomMatchesCharTest, EscapedWhiteSpace) { 385 EXPECT_FALSE(AtomMatchesChar(true, 'f', '\0')); 386 EXPECT_FALSE(AtomMatchesChar(true, 'f', '\n')); 387 EXPECT_FALSE(AtomMatchesChar(true, 'n', '\0')); 388 EXPECT_FALSE(AtomMatchesChar(true, 'n', '\r')); 389 EXPECT_FALSE(AtomMatchesChar(true, 'r', '\0')); 390 EXPECT_FALSE(AtomMatchesChar(true, 'r', 'a')); 391 EXPECT_FALSE(AtomMatchesChar(true, 't', '\0')); 392 EXPECT_FALSE(AtomMatchesChar(true, 't', 't')); 393 EXPECT_FALSE(AtomMatchesChar(true, 'v', '\0')); 394 EXPECT_FALSE(AtomMatchesChar(true, 'v', '\f')); 395 396 EXPECT_TRUE(AtomMatchesChar(true, 'f', '\f')); 397 EXPECT_TRUE(AtomMatchesChar(true, 'n', '\n')); 398 EXPECT_TRUE(AtomMatchesChar(true, 'r', '\r')); 399 EXPECT_TRUE(AtomMatchesChar(true, 't', '\t')); 400 EXPECT_TRUE(AtomMatchesChar(true, 'v', '\v')); 401 } 402 403 TEST(AtomMatchesCharTest, UnescapedDot) { 404 EXPECT_FALSE(AtomMatchesChar(false, '.', '\n')); 405 406 EXPECT_TRUE(AtomMatchesChar(false, '.', '\0')); 407 EXPECT_TRUE(AtomMatchesChar(false, '.', '.')); 408 EXPECT_TRUE(AtomMatchesChar(false, '.', 'a')); 409 EXPECT_TRUE(AtomMatchesChar(false, '.', ' ')); 410 } 411 412 TEST(AtomMatchesCharTest, UnescapedChar) { 413 EXPECT_FALSE(AtomMatchesChar(false, 'a', '\0')); 414 EXPECT_FALSE(AtomMatchesChar(false, 'a', 'b')); 415 EXPECT_FALSE(AtomMatchesChar(false, '$', 'a')); 416 417 EXPECT_TRUE(AtomMatchesChar(false, '$', '$')); 418 EXPECT_TRUE(AtomMatchesChar(false, '5', '5')); 419 EXPECT_TRUE(AtomMatchesChar(false, 'Z', 'Z')); 420 } 421 422 TEST(ValidateRegexTest, GeneratesFailureAndReturnsFalseForInvalid) { 423 EXPECT_NONFATAL_FAILURE(ASSERT_FALSE(ValidateRegex(NULL)), 424 "NULL is not a valid simple regular expression"); 425 EXPECT_NONFATAL_FAILURE( 426 ASSERT_FALSE(ValidateRegex("a\\")), 427 "Syntax error at index 1 in simple regular expression \"a\\\": "); 428 EXPECT_NONFATAL_FAILURE(ASSERT_FALSE(ValidateRegex("a\\")), 429 "'\\' cannot appear at the end"); 430 EXPECT_NONFATAL_FAILURE(ASSERT_FALSE(ValidateRegex("\\n\\")), 431 "'\\' cannot appear at the end"); 432 EXPECT_NONFATAL_FAILURE(ASSERT_FALSE(ValidateRegex("\\s\\hb")), 433 "invalid escape sequence \"\\h\""); 434 EXPECT_NONFATAL_FAILURE(ASSERT_FALSE(ValidateRegex("^^")), 435 "'^' can only appear at the beginning"); 436 EXPECT_NONFATAL_FAILURE(ASSERT_FALSE(ValidateRegex(".*^b")), 437 "'^' can only appear at the beginning"); 438 EXPECT_NONFATAL_FAILURE(ASSERT_FALSE(ValidateRegex("$$")), 439 "'$' can only appear at the end"); 440 EXPECT_NONFATAL_FAILURE(ASSERT_FALSE(ValidateRegex("^$a")), 441 "'$' can only appear at the end"); 442 EXPECT_NONFATAL_FAILURE(ASSERT_FALSE(ValidateRegex("a(b")), 443 "'(' is unsupported"); 444 EXPECT_NONFATAL_FAILURE(ASSERT_FALSE(ValidateRegex("ab)")), 445 "')' is unsupported"); 446 EXPECT_NONFATAL_FAILURE(ASSERT_FALSE(ValidateRegex("[ab")), 447 "'[' is unsupported"); 448 EXPECT_NONFATAL_FAILURE(ASSERT_FALSE(ValidateRegex("a{2")), 449 "'{' is unsupported"); 450 EXPECT_NONFATAL_FAILURE(ASSERT_FALSE(ValidateRegex("?")), 451 "'?' can only follow a repeatable token"); 452 EXPECT_NONFATAL_FAILURE(ASSERT_FALSE(ValidateRegex("^*")), 453 "'*' can only follow a repeatable token"); 454 EXPECT_NONFATAL_FAILURE(ASSERT_FALSE(ValidateRegex("5*+")), 455 "'+' can only follow a repeatable token"); 456 } 457 458 TEST(ValidateRegexTest, ReturnsTrueForValid) { 459 EXPECT_TRUE(ValidateRegex("")); 460 EXPECT_TRUE(ValidateRegex("a")); 461 EXPECT_TRUE(ValidateRegex(".*")); 462 EXPECT_TRUE(ValidateRegex("^a_+")); 463 EXPECT_TRUE(ValidateRegex("^a\\t\\&?")); 464 EXPECT_TRUE(ValidateRegex("09*$")); 465 EXPECT_TRUE(ValidateRegex("^Z$")); 466 EXPECT_TRUE(ValidateRegex("a\\^Z\\$\\(\\)\\|\\[\\]\\{\\}")); 467 } 468 469 TEST(MatchRepetitionAndRegexAtHeadTest, WorksForZeroOrOne) { 470 EXPECT_FALSE(MatchRepetitionAndRegexAtHead(false, 'a', '?', "a", "ba")); 471 // Repeating more than once. 472 EXPECT_FALSE(MatchRepetitionAndRegexAtHead(false, 'a', '?', "b", "aab")); 473 474 // Repeating zero times. 475 EXPECT_TRUE(MatchRepetitionAndRegexAtHead(false, 'a', '?', "b", "ba")); 476 // Repeating once. 477 EXPECT_TRUE(MatchRepetitionAndRegexAtHead(false, 'a', '?', "b", "ab")); 478 EXPECT_TRUE(MatchRepetitionAndRegexAtHead(false, '#', '?', ".", "##")); 479 } 480 481 TEST(MatchRepetitionAndRegexAtHeadTest, WorksForZeroOrMany) { 482 EXPECT_FALSE(MatchRepetitionAndRegexAtHead(false, '.', '*', "a$", "baab")); 483 484 // Repeating zero times. 485 EXPECT_TRUE(MatchRepetitionAndRegexAtHead(false, '.', '*', "b", "bc")); 486 // Repeating once. 487 EXPECT_TRUE(MatchRepetitionAndRegexAtHead(false, '.', '*', "b", "abc")); 488 // Repeating more than once. 489 EXPECT_TRUE(MatchRepetitionAndRegexAtHead(true, 'w', '*', "-", "ab_1-g")); 490 } 491 492 TEST(MatchRepetitionAndRegexAtHeadTest, WorksForOneOrMany) { 493 EXPECT_FALSE(MatchRepetitionAndRegexAtHead(false, '.', '+', "a$", "baab")); 494 // Repeating zero times. 495 EXPECT_FALSE(MatchRepetitionAndRegexAtHead(false, '.', '+', "b", "bc")); 496 497 // Repeating once. 498 EXPECT_TRUE(MatchRepetitionAndRegexAtHead(false, '.', '+', "b", "abc")); 499 // Repeating more than once. 500 EXPECT_TRUE(MatchRepetitionAndRegexAtHead(true, 'w', '+', "-", "ab_1-g")); 501 } 502 503 TEST(MatchRegexAtHeadTest, ReturnsTrueForEmptyRegex) { 504 EXPECT_TRUE(MatchRegexAtHead("", "")); 505 EXPECT_TRUE(MatchRegexAtHead("", "ab")); 506 } 507 508 TEST(MatchRegexAtHeadTest, WorksWhenDollarIsInRegex) { 509 EXPECT_FALSE(MatchRegexAtHead("$", "a")); 510 511 EXPECT_TRUE(MatchRegexAtHead("$", "")); 512 EXPECT_TRUE(MatchRegexAtHead("a$", "a")); 513 } 514 515 TEST(MatchRegexAtHeadTest, WorksWhenRegexStartsWithEscapeSequence) { 516 EXPECT_FALSE(MatchRegexAtHead("\\w", "+")); 517 EXPECT_FALSE(MatchRegexAtHead("\\W", "ab")); 518 519 EXPECT_TRUE(MatchRegexAtHead("\\sa", "\nab")); 520 EXPECT_TRUE(MatchRegexAtHead("\\d", "1a")); 521 } 522 523 TEST(MatchRegexAtHeadTest, WorksWhenRegexStartsWithRepetition) { 524 EXPECT_FALSE(MatchRegexAtHead(".+a", "abc")); 525 EXPECT_FALSE(MatchRegexAtHead("a?b", "aab")); 526 527 EXPECT_TRUE(MatchRegexAtHead(".*a", "bc12-ab")); 528 EXPECT_TRUE(MatchRegexAtHead("a?b", "b")); 529 EXPECT_TRUE(MatchRegexAtHead("a?b", "ab")); 530 } 531 532 TEST(MatchRegexAtHeadTest, 533 WorksWhenRegexStartsWithRepetionOfEscapeSequence) { 534 EXPECT_FALSE(MatchRegexAtHead("\\.+a", "abc")); 535 EXPECT_FALSE(MatchRegexAtHead("\\s?b", " b")); 536 537 EXPECT_TRUE(MatchRegexAtHead("\\(*a", "((((ab")); 538 EXPECT_TRUE(MatchRegexAtHead("\\^?b", "^b")); 539 EXPECT_TRUE(MatchRegexAtHead("\\\\?b", "b")); 540 EXPECT_TRUE(MatchRegexAtHead("\\\\?b", "\\b")); 541 } 542 543 TEST(MatchRegexAtHeadTest, MatchesSequentially) { 544 EXPECT_FALSE(MatchRegexAtHead("ab.*c", "acabc")); 545 546 EXPECT_TRUE(MatchRegexAtHead("ab.*c", "ab-fsc")); 547 } 548 549 TEST(MatchRegexAnywhereTest, ReturnsFalseWhenStringIsNull) { 550 EXPECT_FALSE(MatchRegexAnywhere("", NULL)); 551 } 552 553 TEST(MatchRegexAnywhereTest, WorksWhenRegexStartsWithCaret) { 554 EXPECT_FALSE(MatchRegexAnywhere("^a", "ba")); 555 EXPECT_FALSE(MatchRegexAnywhere("^$", "a")); 556 557 EXPECT_TRUE(MatchRegexAnywhere("^a", "ab")); 558 EXPECT_TRUE(MatchRegexAnywhere("^", "ab")); 559 EXPECT_TRUE(MatchRegexAnywhere("^$", "")); 560 } 561 562 TEST(MatchRegexAnywhereTest, ReturnsFalseWhenNoMatch) { 563 EXPECT_FALSE(MatchRegexAnywhere("a", "bcde123")); 564 EXPECT_FALSE(MatchRegexAnywhere("a.+a", "--aa88888888")); 565 } 566 567 TEST(MatchRegexAnywhereTest, ReturnsTrueWhenMatchingPrefix) { 568 EXPECT_TRUE(MatchRegexAnywhere("\\w+", "ab1_ - 5")); 569 EXPECT_TRUE(MatchRegexAnywhere(".*=", "=")); 570 EXPECT_TRUE(MatchRegexAnywhere("x.*ab?.*bc", "xaaabc")); 571 } 572 573 TEST(MatchRegexAnywhereTest, ReturnsTrueWhenMatchingNonPrefix) { 574 EXPECT_TRUE(MatchRegexAnywhere("\\w+", "$$$ ab1_ - 5")); 575 EXPECT_TRUE(MatchRegexAnywhere("\\.+=", "= ...=")); 576 } 577 578 // Tests RE's implicit constructors. 579 TEST(RETest, ImplicitConstructorWorks) { 580 const RE empty(""); 581 EXPECT_STREQ("", empty.pattern()); 582 583 const RE simple("hello"); 584 EXPECT_STREQ("hello", simple.pattern()); 585 } 586 587 // Tests that RE's constructors reject invalid regular expressions. 588 TEST(RETest, RejectsInvalidRegex) { 589 EXPECT_NONFATAL_FAILURE({ 590 const RE normal(NULL); 591 }, "NULL is not a valid simple regular expression"); 592 593 EXPECT_NONFATAL_FAILURE({ 594 const RE normal(".*(\\w+"); 595 }, "'(' is unsupported"); 596 597 EXPECT_NONFATAL_FAILURE({ 598 const RE invalid("^?"); 599 }, "'?' can only follow a repeatable token"); 600 } 601 602 // Tests RE::FullMatch(). 603 TEST(RETest, FullMatchWorks) { 604 const RE empty(""); 605 EXPECT_TRUE(RE::FullMatch("", empty)); 606 EXPECT_FALSE(RE::FullMatch("a", empty)); 607 608 const RE re1("a"); 609 EXPECT_TRUE(RE::FullMatch("a", re1)); 610 611 const RE re("a.*z"); 612 EXPECT_TRUE(RE::FullMatch("az", re)); 613 EXPECT_TRUE(RE::FullMatch("axyz", re)); 614 EXPECT_FALSE(RE::FullMatch("baz", re)); 615 EXPECT_FALSE(RE::FullMatch("azy", re)); 616 } 617 618 // Tests RE::PartialMatch(). 619 TEST(RETest, PartialMatchWorks) { 620 const RE empty(""); 621 EXPECT_TRUE(RE::PartialMatch("", empty)); 622 EXPECT_TRUE(RE::PartialMatch("a", empty)); 623 624 const RE re("a.*z"); 625 EXPECT_TRUE(RE::PartialMatch("az", re)); 626 EXPECT_TRUE(RE::PartialMatch("axyz", re)); 627 EXPECT_TRUE(RE::PartialMatch("baz", re)); 628 EXPECT_TRUE(RE::PartialMatch("azy", re)); 629 EXPECT_FALSE(RE::PartialMatch("zza", re)); 630 } 631 632 #endif // GTEST_USES_POSIX_RE 633 634 #if GTEST_HAS_STD_STRING 635 636 TEST(CaptureStderrTest, CapturesStdErr) { 637 CaptureStderr(); 638 fprintf(stderr, "abc"); 639 ASSERT_EQ("abc", GetCapturedStderr()); 640 } 641 642 #endif // GTEST_HAS_STD_STRING 643 644 } // namespace internal 645 } // namespace testing 646