gtest-typed-test.cc revision 41d0579e8de9ef4ff178fc4991043c61a19943f7
15821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Copyright 2008 Google Inc. 25821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// All Rights Reserved. 35821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// 45821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Redistribution and use in source and binary forms, with or without 55821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// modification, are permitted provided that the following conditions are 65821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// met: 75821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// 890dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)// * Redistributions of source code must retain the above copyright 9f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)// notice, this list of conditions and the following disclaimer. 10f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)// * Redistributions in binary form must reproduce the above 115821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// copyright notice, this list of conditions and the following disclaimer 125821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// in the documentation and/or other materials provided with the 135821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// distribution. 145821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// * Neither the name of Google Inc. nor the names of its 155821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// contributors may be used to endorse or promote products derived from 165821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// this software without specific prior written permission. 175821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// 185821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 195821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 205821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 2190dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 2290dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 2390dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 2490dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 255821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 265821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 275821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 285821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 2958537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)// 305821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Author: wan@google.com (Zhanyong Wan) 315821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 325821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "gtest/gtest-typed-test.h" 335821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "gtest/gtest.h" 345821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 355821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)namespace testing { 365821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)namespace internal { 3790dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) 3890dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)#if GTEST_HAS_TYPED_TEST_P 395821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 405821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Skips to the first non-space char in str. Returns an empty string if str 4190dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)// contains only whitespace characters. 425821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)static const char* SkipSpaces(const char* str) { 435821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) while (IsSpace(*str)) 445821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) str++; 455821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) return str; 465821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)} 4790dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) 4890dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)// Verifies that registered_tests match the test names in 4990dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)// defined_test_names_; returns registered_tests if successful, or 5090dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)// aborts the program otherwise. 515d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)const char* TypedTestCasePState::VerifyRegisteredTestNames( 525d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) const char* file, int line, const char* registered_tests) { 535d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) typedef ::std::set<const char*>::const_iterator DefinedTestIter; 547d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles) registered_ = true; 555821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 565821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // Skip initial whitespace in registered_tests since some 5790dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) // preprocessors prefix stringizied literals with whitespace. 585821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) registered_tests = SkipSpaces(registered_tests); 5990dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) 605821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) Message errors; 6190dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) ::std::set<String> tests; 625821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) for (const char* names = registered_tests; names != NULL; 635821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) names = SkipComma(names)) { 645821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) const String name = GetPrefixUntilComma(names); 655821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) if (tests.count(name) != 0) { 665821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) errors << "Test " << name << " is listed more than once.\n"; 675821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) continue; 685821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) } 695821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 705821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) bool found = false; 715821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) for (DefinedTestIter it = defined_test_names_.begin(); 7258537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles) it != defined_test_names_.end(); 7358537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles) ++it) { 7458537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles) if (name == *it) { 7558537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles) found = true; 7658537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles) break; 7758537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles) } 7858537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles) } 7958537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles) 8058537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles) if (found) { 8158537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles) tests.insert(name); 825821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) } else { 8390dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) errors << "No test named " << name 8490dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) << " can be found in this test case.\n"; 855821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) } 865821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) } 875821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 885821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) for (DefinedTestIter it = defined_test_names_.begin(); 895821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) it != defined_test_names_.end(); 905821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) ++it) { 915821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) if (tests.count(*it) == 0) { 9290dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) errors << "You forgot to list test " << *it << ".\n"; 9390dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) } 9490dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) } 9590dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) 965821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) const String& errors_str = errors.GetString(); 9790dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) if (errors_str != "") { 9890dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) fprintf(stderr, "%s %s", FormatFileLocation(file, line).c_str(), 9990dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) errors_str.c_str()); 10090dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) fflush(stderr); 10190dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) posix::Abort(); 102010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles) } 103f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles) 104f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles) return registered_tests; 105f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)} 10690dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) 10790dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)#endif // GTEST_HAS_TYPED_TEST_P 10890dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) 10990dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)} // namespace internal 110} // namespace testing 111