Commit 87fdda2c authored by jgm's avatar jgm
Browse files

Unfortunately, the svn repo is a bit out of date. This commit contains 8

changes that haven't made it to svn. The descriptions of each change are listed
below.

- Fixes some python shebang lines.

- Add ElementsAreArray overloads to gmock. ElementsAreArray now makes a copy of
  its input elements before the conversion to a Matcher. ElementsAreArray can
  now take a vector as input. ElementsAreArray can now take an iterator pair as
  input.

- Templatize MatchAndExplain to allow independent string types for the matcher
  and matchee. I also templatized the ConstCharPointer version of
  MatchAndExplain to avoid calls with "char*" from using the new templated
  MatchAndExplain.

- Fixes the bug where the constructor of the return type of ElementsAre() saves
  a reference instead of a copy of the arguments.

- Extends ElementsAre() to accept arrays whose sizes aren't known.

- Switches gTest's internal FilePath class from testing::internal::String to
  std::string. testing::internal::String was introduced when gTest couldn't
  depend on std::string.  It's now deprecated.

- Switches gTest & gMock from using testing::internal::String objects to
  std::string. Some static methods of String are still in use.  We may be able
  to remove some but not all of them.  In particular, String::Format() should
  eventually be removed as it truncates the result at 4096 characters, often
  causing problems.
parent 78bf6d57
...@@ -39,79 +39,72 @@ namespace { ...@@ -39,79 +39,72 @@ namespace {
using ::testing::Message; using ::testing::Message;
// A helper function that turns a Message into a C string.
const char* ToCString(const Message& msg) {
static testing::internal::String result;
result = msg.GetString();
return result.c_str();
}
// Tests the testing::Message class // Tests the testing::Message class
// Tests the default constructor. // Tests the default constructor.
TEST(MessageTest, DefaultConstructor) { TEST(MessageTest, DefaultConstructor) {
const Message msg; const Message msg;
EXPECT_STREQ("", ToCString(msg)); EXPECT_EQ("", msg.GetString());
} }
// Tests the copy constructor. // Tests the copy constructor.
TEST(MessageTest, CopyConstructor) { TEST(MessageTest, CopyConstructor) {
const Message msg1("Hello"); const Message msg1("Hello");
const Message msg2(msg1); const Message msg2(msg1);
EXPECT_STREQ("Hello", ToCString(msg2)); EXPECT_EQ("Hello", msg2.GetString());
} }
// Tests constructing a Message from a C-string. // Tests constructing a Message from a C-string.
TEST(MessageTest, ConstructsFromCString) { TEST(MessageTest, ConstructsFromCString) {
Message msg("Hello"); Message msg("Hello");
EXPECT_STREQ("Hello", ToCString(msg)); EXPECT_EQ("Hello", msg.GetString());
} }
// Tests streaming a float. // Tests streaming a float.
TEST(MessageTest, StreamsFloat) { TEST(MessageTest, StreamsFloat) {
const char* const s = ToCString(Message() << 1.23456F << " " << 2.34567F); const std::string s = (Message() << 1.23456F << " " << 2.34567F).GetString();
// Both numbers should be printed with enough precision. // Both numbers should be printed with enough precision.
EXPECT_PRED_FORMAT2(testing::IsSubstring, "1.234560", s); EXPECT_PRED_FORMAT2(testing::IsSubstring, "1.234560", s.c_str());
EXPECT_PRED_FORMAT2(testing::IsSubstring, " 2.345669", s); EXPECT_PRED_FORMAT2(testing::IsSubstring, " 2.345669", s.c_str());
} }
// Tests streaming a double. // Tests streaming a double.
TEST(MessageTest, StreamsDouble) { TEST(MessageTest, StreamsDouble) {
const char* const s = ToCString(Message() << 1260570880.4555497 << " " const std::string s = (Message() << 1260570880.4555497 << " "
<< 1260572265.1954534); << 1260572265.1954534).GetString();
// Both numbers should be printed with enough precision. // Both numbers should be printed with enough precision.
EXPECT_PRED_FORMAT2(testing::IsSubstring, "1260570880.45", s); EXPECT_PRED_FORMAT2(testing::IsSubstring, "1260570880.45", s.c_str());
EXPECT_PRED_FORMAT2(testing::IsSubstring, " 1260572265.19", s); EXPECT_PRED_FORMAT2(testing::IsSubstring, " 1260572265.19", s.c_str());
} }
// Tests streaming a non-char pointer. // Tests streaming a non-char pointer.
TEST(MessageTest, StreamsPointer) { TEST(MessageTest, StreamsPointer) {
int n = 0; int n = 0;
int* p = &n; int* p = &n;
EXPECT_STRNE("(null)", ToCString(Message() << p)); EXPECT_NE("(null)", (Message() << p).GetString());
} }
// Tests streaming a NULL non-char pointer. // Tests streaming a NULL non-char pointer.
TEST(MessageTest, StreamsNullPointer) { TEST(MessageTest, StreamsNullPointer) {
int* p = NULL; int* p = NULL;
EXPECT_STREQ("(null)", ToCString(Message() << p)); EXPECT_EQ("(null)", (Message() << p).GetString());
} }
// Tests streaming a C string. // Tests streaming a C string.
TEST(MessageTest, StreamsCString) { TEST(MessageTest, StreamsCString) {
EXPECT_STREQ("Foo", ToCString(Message() << "Foo")); EXPECT_EQ("Foo", (Message() << "Foo").GetString());
} }
// Tests streaming a NULL C string. // Tests streaming a NULL C string.
TEST(MessageTest, StreamsNullCString) { TEST(MessageTest, StreamsNullCString) {
char* p = NULL; char* p = NULL;
EXPECT_STREQ("(null)", ToCString(Message() << p)); EXPECT_EQ("(null)", (Message() << p).GetString());
} }
// Tests streaming std::string. // Tests streaming std::string.
TEST(MessageTest, StreamsString) { TEST(MessageTest, StreamsString) {
const ::std::string str("Hello"); const ::std::string str("Hello");
EXPECT_STREQ("Hello", ToCString(Message() << str)); EXPECT_EQ("Hello", (Message() << str).GetString());
} }
// Tests that we can output strings containing embedded NULs. // Tests that we can output strings containing embedded NULs.
...@@ -120,34 +113,34 @@ TEST(MessageTest, StreamsStringWithEmbeddedNUL) { ...@@ -120,34 +113,34 @@ TEST(MessageTest, StreamsStringWithEmbeddedNUL) {
"Here's a NUL\0 and some more string"; "Here's a NUL\0 and some more string";
const ::std::string string_with_nul(char_array_with_nul, const ::std::string string_with_nul(char_array_with_nul,
sizeof(char_array_with_nul) - 1); sizeof(char_array_with_nul) - 1);
EXPECT_STREQ("Here's a NUL\\0 and some more string", EXPECT_EQ("Here's a NUL\\0 and some more string",
ToCString(Message() << string_with_nul)); (Message() << string_with_nul).GetString());
} }
// Tests streaming a NUL char. // Tests streaming a NUL char.
TEST(MessageTest, StreamsNULChar) { TEST(MessageTest, StreamsNULChar) {
EXPECT_STREQ("\\0", ToCString(Message() << '\0')); EXPECT_EQ("\\0", (Message() << '\0').GetString());
} }
// Tests streaming int. // Tests streaming int.
TEST(MessageTest, StreamsInt) { TEST(MessageTest, StreamsInt) {
EXPECT_STREQ("123", ToCString(Message() << 123)); EXPECT_EQ("123", (Message() << 123).GetString());
} }
// Tests that basic IO manipulators (endl, ends, and flush) can be // Tests that basic IO manipulators (endl, ends, and flush) can be
// streamed to Message. // streamed to Message.
TEST(MessageTest, StreamsBasicIoManip) { TEST(MessageTest, StreamsBasicIoManip) {
EXPECT_STREQ("Line 1.\nA NUL char \\0 in line 2.", EXPECT_EQ("Line 1.\nA NUL char \\0 in line 2.",
ToCString(Message() << "Line 1." << std::endl (Message() << "Line 1." << std::endl
<< "A NUL char " << std::ends << std::flush << "A NUL char " << std::ends << std::flush
<< " in line 2.")); << " in line 2.").GetString());
} }
// Tests Message::GetString() // Tests Message::GetString()
TEST(MessageTest, GetString) { TEST(MessageTest, GetString) {
Message msg; Message msg;
msg << 1 << " lamb"; msg << 1 << " lamb";
EXPECT_STREQ("1 lamb", msg.GetString().c_str()); EXPECT_EQ("1 lamb", msg.GetString());
} }
// Tests streaming a Message object to an ostream. // Tests streaming a Message object to an ostream.
...@@ -155,7 +148,7 @@ TEST(MessageTest, StreamsToOStream) { ...@@ -155,7 +148,7 @@ TEST(MessageTest, StreamsToOStream) {
Message msg("Hello"); Message msg("Hello");
::std::stringstream ss; ::std::stringstream ss;
ss << msg; ss << msg;
EXPECT_STREQ("Hello", testing::internal::StringStreamToString(&ss).c_str()); EXPECT_EQ("Hello", testing::internal::StringStreamToString(&ss));
} }
// Tests that a Message object doesn't take up too much stack space. // Tests that a Message object doesn't take up too much stack space.
......
...@@ -78,14 +78,14 @@ TEST(XmlOutputTest, GetOutputFormat) { ...@@ -78,14 +78,14 @@ TEST(XmlOutputTest, GetOutputFormat) {
TEST(XmlOutputTest, GetOutputFileDefault) { TEST(XmlOutputTest, GetOutputFileDefault) {
GTEST_FLAG(output) = ""; GTEST_FLAG(output) = "";
EXPECT_STREQ(GetAbsolutePathOf(FilePath("test_detail.xml")).c_str(), EXPECT_EQ(GetAbsolutePathOf(FilePath("test_detail.xml")).string(),
UnitTestOptions::GetAbsolutePathToOutputFile().c_str()); UnitTestOptions::GetAbsolutePathToOutputFile());
} }
TEST(XmlOutputTest, GetOutputFileSingleFile) { TEST(XmlOutputTest, GetOutputFileSingleFile) {
GTEST_FLAG(output) = "xml:filename.abc"; GTEST_FLAG(output) = "xml:filename.abc";
EXPECT_STREQ(GetAbsolutePathOf(FilePath("filename.abc")).c_str(), EXPECT_EQ(GetAbsolutePathOf(FilePath("filename.abc")).string(),
UnitTestOptions::GetAbsolutePathToOutputFile().c_str()); UnitTestOptions::GetAbsolutePathToOutputFile());
} }
TEST(XmlOutputTest, GetOutputFileFromDirectoryPath) { TEST(XmlOutputTest, GetOutputFileFromDirectoryPath) {
...@@ -93,8 +93,9 @@ TEST(XmlOutputTest, GetOutputFileFromDirectoryPath) { ...@@ -93,8 +93,9 @@ TEST(XmlOutputTest, GetOutputFileFromDirectoryPath) {
const std::string expected_output_file = const std::string expected_output_file =
GetAbsolutePathOf( GetAbsolutePathOf(
FilePath(std::string("path") + GTEST_PATH_SEP_ + FilePath(std::string("path") + GTEST_PATH_SEP_ +
GetCurrentExecutableName().c_str() + ".xml")).c_str(); GetCurrentExecutableName().string() + ".xml")).string();
const String& output_file = UnitTestOptions::GetAbsolutePathToOutputFile(); const std::string& output_file =
UnitTestOptions::GetAbsolutePathToOutputFile();
#if GTEST_OS_WINDOWS #if GTEST_OS_WINDOWS
EXPECT_STRCASEEQ(expected_output_file.c_str(), output_file.c_str()); EXPECT_STRCASEEQ(expected_output_file.c_str(), output_file.c_str());
#else #else
...@@ -103,7 +104,7 @@ TEST(XmlOutputTest, GetOutputFileFromDirectoryPath) { ...@@ -103,7 +104,7 @@ TEST(XmlOutputTest, GetOutputFileFromDirectoryPath) {
} }
TEST(OutputFileHelpersTest, GetCurrentExecutableName) { TEST(OutputFileHelpersTest, GetCurrentExecutableName) {
const std::string exe_str = GetCurrentExecutableName().c_str(); const std::string exe_str = GetCurrentExecutableName().string();
#if GTEST_OS_WINDOWS #if GTEST_OS_WINDOWS
const bool success = const bool success =
_strcmpi("gtest-options_test", exe_str.c_str()) == 0 || _strcmpi("gtest-options_test", exe_str.c_str()) == 0 ||
...@@ -129,12 +130,12 @@ class XmlOutputChangeDirTest : public Test { ...@@ -129,12 +130,12 @@ class XmlOutputChangeDirTest : public Test {
original_working_dir_ = FilePath::GetCurrentDir(); original_working_dir_ = FilePath::GetCurrentDir();
posix::ChDir(".."); posix::ChDir("..");
// This will make the test fail if run from the root directory. // This will make the test fail if run from the root directory.
EXPECT_STRNE(original_working_dir_.c_str(), EXPECT_NE(original_working_dir_.string(),
FilePath::GetCurrentDir().c_str()); FilePath::GetCurrentDir().string());
} }
virtual void TearDown() { virtual void TearDown() {
posix::ChDir(original_working_dir_.c_str()); posix::ChDir(original_working_dir_.string().c_str());
} }
FilePath original_working_dir_; FilePath original_working_dir_;
...@@ -142,23 +143,23 @@ class XmlOutputChangeDirTest : public Test { ...@@ -142,23 +143,23 @@ class XmlOutputChangeDirTest : public Test {
TEST_F(XmlOutputChangeDirTest, PreserveOriginalWorkingDirWithDefault) { TEST_F(XmlOutputChangeDirTest, PreserveOriginalWorkingDirWithDefault) {
GTEST_FLAG(output) = ""; GTEST_FLAG(output) = "";
EXPECT_STREQ(FilePath::ConcatPaths(original_working_dir_, EXPECT_EQ(FilePath::ConcatPaths(original_working_dir_,
FilePath("test_detail.xml")).c_str(), FilePath("test_detail.xml")).string(),
UnitTestOptions::GetAbsolutePathToOutputFile().c_str()); UnitTestOptions::GetAbsolutePathToOutputFile());
} }
TEST_F(XmlOutputChangeDirTest, PreserveOriginalWorkingDirWithDefaultXML) { TEST_F(XmlOutputChangeDirTest, PreserveOriginalWorkingDirWithDefaultXML) {
GTEST_FLAG(output) = "xml"; GTEST_FLAG(output) = "xml";
EXPECT_STREQ(FilePath::ConcatPaths(original_working_dir_, EXPECT_EQ(FilePath::ConcatPaths(original_working_dir_,
FilePath("test_detail.xml")).c_str(), FilePath("test_detail.xml")).string(),
UnitTestOptions::GetAbsolutePathToOutputFile().c_str()); UnitTestOptions::GetAbsolutePathToOutputFile());
} }
TEST_F(XmlOutputChangeDirTest, PreserveOriginalWorkingDirWithRelativeFile) { TEST_F(XmlOutputChangeDirTest, PreserveOriginalWorkingDirWithRelativeFile) {
GTEST_FLAG(output) = "xml:filename.abc"; GTEST_FLAG(output) = "xml:filename.abc";
EXPECT_STREQ(FilePath::ConcatPaths(original_working_dir_, EXPECT_EQ(FilePath::ConcatPaths(original_working_dir_,
FilePath("filename.abc")).c_str(), FilePath("filename.abc")).string(),
UnitTestOptions::GetAbsolutePathToOutputFile().c_str()); UnitTestOptions::GetAbsolutePathToOutputFile());
} }
TEST_F(XmlOutputChangeDirTest, PreserveOriginalWorkingDirWithRelativePath) { TEST_F(XmlOutputChangeDirTest, PreserveOriginalWorkingDirWithRelativePath) {
...@@ -167,8 +168,9 @@ TEST_F(XmlOutputChangeDirTest, PreserveOriginalWorkingDirWithRelativePath) { ...@@ -167,8 +168,9 @@ TEST_F(XmlOutputChangeDirTest, PreserveOriginalWorkingDirWithRelativePath) {
FilePath::ConcatPaths( FilePath::ConcatPaths(
original_working_dir_, original_working_dir_,
FilePath(std::string("path") + GTEST_PATH_SEP_ + FilePath(std::string("path") + GTEST_PATH_SEP_ +
GetCurrentExecutableName().c_str() + ".xml")).c_str(); GetCurrentExecutableName().string() + ".xml")).string();
const String& output_file = UnitTestOptions::GetAbsolutePathToOutputFile(); const std::string& output_file =
UnitTestOptions::GetAbsolutePathToOutputFile();
#if GTEST_OS_WINDOWS #if GTEST_OS_WINDOWS
EXPECT_STRCASEEQ(expected_output_file.c_str(), output_file.c_str()); EXPECT_STRCASEEQ(expected_output_file.c_str(), output_file.c_str());
#else #else
...@@ -179,12 +181,12 @@ TEST_F(XmlOutputChangeDirTest, PreserveOriginalWorkingDirWithRelativePath) { ...@@ -179,12 +181,12 @@ TEST_F(XmlOutputChangeDirTest, PreserveOriginalWorkingDirWithRelativePath) {
TEST_F(XmlOutputChangeDirTest, PreserveOriginalWorkingDirWithAbsoluteFile) { TEST_F(XmlOutputChangeDirTest, PreserveOriginalWorkingDirWithAbsoluteFile) {
#if GTEST_OS_WINDOWS #if GTEST_OS_WINDOWS
GTEST_FLAG(output) = "xml:c:\\tmp\\filename.abc"; GTEST_FLAG(output) = "xml:c:\\tmp\\filename.abc";
EXPECT_STREQ(FilePath("c:\\tmp\\filename.abc").c_str(), EXPECT_EQ(FilePath("c:\\tmp\\filename.abc").string(),
UnitTestOptions::GetAbsolutePathToOutputFile().c_str()); UnitTestOptions::GetAbsolutePathToOutputFile());
#else #else
GTEST_FLAG(output) ="xml:/tmp/filename.abc"; GTEST_FLAG(output) ="xml:/tmp/filename.abc";
EXPECT_STREQ(FilePath("/tmp/filename.abc").c_str(), EXPECT_EQ(FilePath("/tmp/filename.abc").string(),
UnitTestOptions::GetAbsolutePathToOutputFile().c_str()); UnitTestOptions::GetAbsolutePathToOutputFile());
#endif #endif
} }
...@@ -197,8 +199,9 @@ TEST_F(XmlOutputChangeDirTest, PreserveOriginalWorkingDirWithAbsolutePath) { ...@@ -197,8 +199,9 @@ TEST_F(XmlOutputChangeDirTest, PreserveOriginalWorkingDirWithAbsolutePath) {
GTEST_FLAG(output) = "xml:" + path; GTEST_FLAG(output) = "xml:" + path;
const std::string expected_output_file = const std::string expected_output_file =
path + GetCurrentExecutableName().c_str() + ".xml"; path + GetCurrentExecutableName().string() + ".xml";
const String& output_file = UnitTestOptions::GetAbsolutePathToOutputFile(); const std::string& output_file =
UnitTestOptions::GetAbsolutePathToOutputFile();
#if GTEST_OS_WINDOWS #if GTEST_OS_WINDOWS
EXPECT_STRCASEEQ(expected_output_file.c_str(), output_file.c_str()); EXPECT_STRCASEEQ(expected_output_file.c_str(), output_file.c_str());
......
...@@ -280,10 +280,10 @@ class DogAdder { ...@@ -280,10 +280,10 @@ class DogAdder {
bool operator<(const DogAdder& other) const { bool operator<(const DogAdder& other) const {
return value_ < other.value_; return value_ < other.value_;
} }
const ::testing::internal::String& value() const { return value_; } const std::string& value() const { return value_; }
private: private:
::testing::internal::String value_; std::string value_;
}; };
TEST(RangeTest, WorksWithACustomType) { TEST(RangeTest, WorksWithACustomType) {
......
...@@ -1005,7 +1005,7 @@ TEST(ThreadLocalTest, ValueDefaultContructorIsNotRequiredForParamVersion) { ...@@ -1005,7 +1005,7 @@ TEST(ThreadLocalTest, ValueDefaultContructorIsNotRequiredForParamVersion) {
} }
TEST(ThreadLocalTest, GetAndPointerReturnSameValue) { TEST(ThreadLocalTest, GetAndPointerReturnSameValue) {
ThreadLocal<String> thread_local_string; ThreadLocal<std::string> thread_local_string;
EXPECT_EQ(thread_local_string.pointer(), &(thread_local_string.get())); EXPECT_EQ(thread_local_string.pointer(), &(thread_local_string.get()));
...@@ -1015,8 +1015,9 @@ TEST(ThreadLocalTest, GetAndPointerReturnSameValue) { ...@@ -1015,8 +1015,9 @@ TEST(ThreadLocalTest, GetAndPointerReturnSameValue) {
} }
TEST(ThreadLocalTest, PointerAndConstPointerReturnSameValue) { TEST(ThreadLocalTest, PointerAndConstPointerReturnSameValue) {
ThreadLocal<String> thread_local_string; ThreadLocal<std::string> thread_local_string;
const ThreadLocal<String>& const_thread_local_string = thread_local_string; const ThreadLocal<std::string>& const_thread_local_string =
thread_local_string;
EXPECT_EQ(thread_local_string.pointer(), const_thread_local_string.pointer()); EXPECT_EQ(thread_local_string.pointer(), const_thread_local_string.pointer());
...@@ -1126,18 +1127,19 @@ void RunFromThread(void (func)(T), T param) { ...@@ -1126,18 +1127,19 @@ void RunFromThread(void (func)(T), T param) {
thread.Join(); thread.Join();
} }
void RetrieveThreadLocalValue(pair<ThreadLocal<String>*, String*> param) { void RetrieveThreadLocalValue(
pair<ThreadLocal<std::string>*, std::string*> param) {
*param.second = param.first->get(); *param.second = param.first->get();
} }
TEST(ThreadLocalTest, ParameterizedConstructorSetsDefault) { TEST(ThreadLocalTest, ParameterizedConstructorSetsDefault) {
ThreadLocal<String> thread_local_string("foo"); ThreadLocal<std::string> thread_local_string("foo");
EXPECT_STREQ("foo", thread_local_string.get().c_str()); EXPECT_STREQ("foo", thread_local_string.get().c_str());
thread_local_string.set("bar"); thread_local_string.set("bar");
EXPECT_STREQ("bar", thread_local_string.get().c_str()); EXPECT_STREQ("bar", thread_local_string.get().c_str());
String result; std::string result;
RunFromThread(&RetrieveThreadLocalValue, RunFromThread(&RetrieveThreadLocalValue,
make_pair(&thread_local_string, &result)); make_pair(&thread_local_string, &result));
EXPECT_STREQ("foo", result.c_str()); EXPECT_STREQ("foo", result.c_str());
...@@ -1235,14 +1237,14 @@ TEST(ThreadLocalTest, DestroysManagedObjectAtThreadExit) { ...@@ -1235,14 +1237,14 @@ TEST(ThreadLocalTest, DestroysManagedObjectAtThreadExit) {
} }
TEST(ThreadLocalTest, ThreadLocalMutationsAffectOnlyCurrentThread) { TEST(ThreadLocalTest, ThreadLocalMutationsAffectOnlyCurrentThread) {
ThreadLocal<String> thread_local_string; ThreadLocal<std::string> thread_local_string;
thread_local_string.set("Foo"); thread_local_string.set("Foo");
EXPECT_STREQ("Foo", thread_local_string.get().c_str()); EXPECT_STREQ("Foo", thread_local_string.get().c_str());
String result; std::string result;
RunFromThread(&RetrieveThreadLocalValue, RunFromThread(&RetrieveThreadLocalValue,
make_pair(&thread_local_string, &result)); make_pair(&thread_local_string, &result));
EXPECT_TRUE(result.c_str() == NULL); EXPECT_TRUE(result.empty());
} }
#endif // GTEST_IS_THREADSAFE #endif // GTEST_IS_THREADSAFE
......
...@@ -58,7 +58,6 @@ using testing::internal::ThreadWithParam; ...@@ -58,7 +58,6 @@ using testing::internal::ThreadWithParam;
#endif #endif
namespace posix = ::testing::internal::posix; namespace posix = ::testing::internal::posix;
using testing::internal::String;
using testing::internal::scoped_ptr; using testing::internal::scoped_ptr;
// Tests catching fatal failures. // Tests catching fatal failures.
...@@ -1005,7 +1004,8 @@ int main(int argc, char **argv) { ...@@ -1005,7 +1004,8 @@ int main(int argc, char **argv) {
// for it. // for it.
testing::InitGoogleTest(&argc, argv); testing::InitGoogleTest(&argc, argv);
if (argc >= 2 && if (argc >= 2 &&
String(argv[1]) == "--gtest_internal_skip_environment_and_ad_hoc_tests") (std::string(argv[1]) ==
"--gtest_internal_skip_environment_and_ad_hoc_tests"))
GTEST_FLAG(internal_skip_environment_and_ad_hoc_tests) = true; GTEST_FLAG(internal_skip_environment_and_ad_hoc_tests) = true;
#if GTEST_HAS_DEATH_TEST #if GTEST_HAS_DEATH_TEST
......
...@@ -42,7 +42,6 @@ using ::testing::Test; ...@@ -42,7 +42,6 @@ using ::testing::Test;
using ::testing::TestEventListeners; using ::testing::TestEventListeners;
using ::testing::TestInfo; using ::testing::TestInfo;
using ::testing::UnitTest; using ::testing::UnitTest;
using ::testing::internal::String;
using ::testing::internal::scoped_ptr; using ::testing::internal::scoped_ptr;
// The test methods are empty, as the sole purpose of this program is // The test methods are empty, as the sole purpose of this program is
......
...@@ -50,7 +50,6 @@ namespace testing { ...@@ -50,7 +50,6 @@ namespace testing {
namespace { namespace {
using internal::Notification; using internal::Notification;
using internal::String;
using internal::TestPropertyKeyIs; using internal::TestPropertyKeyIs;
using internal::ThreadWithParam; using internal::ThreadWithParam;
using internal::scoped_ptr; using internal::scoped_ptr;
...@@ -62,13 +61,13 @@ using internal::scoped_ptr; ...@@ -62,13 +61,13 @@ using internal::scoped_ptr;
// How many threads to create? // How many threads to create?
const int kThreadCount = 50; const int kThreadCount = 50;
String IdToKey(int id, const char* suffix) { std::string IdToKey(int id, const char* suffix) {
Message key; Message key;
key << "key_" << id << "_" << suffix; key << "key_" << id << "_" << suffix;
return key.GetString(); return key.GetString();
} }
String IdToString(int id) { std::string IdToString(int id) {
Message id_message; Message id_message;
id_message << id; id_message << id;
return id_message.GetString(); return id_message.GetString();
......
...@@ -931,259 +931,16 @@ TEST(AssertHelperTest, AssertHelperIsSmall) { ...@@ -931,259 +931,16 @@ TEST(AssertHelperTest, AssertHelperIsSmall) {
EXPECT_LE(sizeof(testing::internal::AssertHelper), sizeof(void*)); EXPECT_LE(sizeof(testing::internal::AssertHelper), sizeof(void*));
} }
// Tests the String class.
// Tests String's constructors.
TEST(StringTest, Constructors) {
// Default ctor.
String s1;
// We aren't using EXPECT_EQ(NULL, s1.c_str()) because comparing
// pointers with NULL isn't supported on all platforms.
EXPECT_EQ(0U, s1.length());
EXPECT_TRUE(NULL == s1.c_str());
// Implicitly constructs from a C-string.
String s2 = "Hi";
EXPECT_EQ(2U, s2.length());
EXPECT_STREQ("Hi", s2.c_str());
// Constructs from a C-string and a length.
String s3("hello", 3);
EXPECT_EQ(3U, s3.length());
EXPECT_STREQ("hel", s3.c_str());
// The empty String should be created when String is constructed with
// a NULL pointer and length 0.
EXPECT_EQ(0U, String(NULL, 0).length());
EXPECT_FALSE(String(NULL, 0).c_str() == NULL);
// Constructs a String that contains '\0'.
String s4("a\0bcd", 4);
EXPECT_EQ(4U, s4.length());
EXPECT_EQ('a', s4.c_str()[0]);
EXPECT_EQ('\0', s4.c_str()[1]);
EXPECT_EQ('b', s4.c_str()[2]);
EXPECT_EQ('c', s4.c_str()[3]);
// Copy ctor where the source is NULL.
const String null_str;
String s5 = null_str;
EXPECT_TRUE(s5.c_str() == NULL);
// Copy ctor where the source isn't NULL.
String s6 = s3;
EXPECT_EQ(3U, s6.length());
EXPECT_STREQ("hel", s6.c_str());
// Copy ctor where the source contains '\0'.
String s7 = s4;
EXPECT_EQ(4U, s7.length());
EXPECT_EQ('a', s7.c_str()[0]);
EXPECT_EQ('\0', s7.c_str()[1]);
EXPECT_EQ('b', s7.c_str()[2]);
EXPECT_EQ('c', s7.c_str()[3]);
}
TEST(StringTest, ConvertsFromStdString) {
// An empty std::string.
const std::string src1("");
const String dest1 = src1;
EXPECT_EQ(0U, dest1.length());
EXPECT_STREQ("", dest1.c_str());
// A normal std::string.
const std::string src2("Hi");
const String dest2 = src2;
EXPECT_EQ(2U, dest2.length());
EXPECT_STREQ("Hi", dest2.c_str());
// An std::string with an embedded NUL character.
const char src3[] = "a\0b";
const String dest3 = std::string(src3, sizeof(src3));
EXPECT_EQ(sizeof(src3), dest3.length());
EXPECT_EQ('a', dest3.c_str()[0]);
EXPECT_EQ('\0', dest3.c_str()[1]);
EXPECT_EQ('b', dest3.c_str()[2]);
}
TEST(StringTest, ConvertsToStdString) {
// An empty String.
const String src1("");
const std::string dest1 = src1;
EXPECT_EQ("", dest1);
// A normal String.
const String src2("Hi");
const std::string dest2 = src2;
EXPECT_EQ("Hi", dest2);
// A String containing a '\0'.
const String src3("x\0y", 3);
const std::string dest3 = src3;
EXPECT_EQ(std::string("x\0y", 3), dest3);
}
#if GTEST_HAS_GLOBAL_STRING
TEST(StringTest, ConvertsFromGlobalString) {
// An empty ::string.
const ::string src1("");
const String dest1 = src1;
EXPECT_EQ(0U, dest1.length());
EXPECT_STREQ("", dest1.c_str());
// A normal ::string.
const ::string src2("Hi");
const String dest2 = src2;
EXPECT_EQ(2U, dest2.length());
EXPECT_STREQ("Hi", dest2.c_str());
// An ::string with an embedded NUL character.
const char src3[] = "x\0y";
const String dest3 = ::string(src3, sizeof(src3));
EXPECT_EQ(sizeof(src3), dest3.length());
EXPECT_EQ('x', dest3.c_str()[0]);
EXPECT_EQ('\0', dest3.c_str()[1]);
EXPECT_EQ('y', dest3.c_str()[2]);
}
TEST(StringTest, ConvertsToGlobalString) {
// An empty String.
const String src1("");
const ::string dest1 = src1;
EXPECT_EQ("", dest1);
// A normal String.
const String src2("Hi");
const ::string dest2 = src2;
EXPECT_EQ("Hi", dest2);
const String src3("x\0y", 3);
const ::string dest3 = src3;
EXPECT_EQ(::string("x\0y", 3), dest3);
}
#endif // GTEST_HAS_GLOBAL_STRING
// Tests String::empty().
TEST(StringTest, Empty) {
EXPECT_TRUE(String("").empty());
EXPECT_FALSE(String().empty());
EXPECT_FALSE(String(NULL).empty());
EXPECT_FALSE(String("a").empty());
EXPECT_FALSE(String("\0", 1).empty());
}
// Tests String::Compare().
TEST(StringTest, Compare) {
// NULL vs NULL.
EXPECT_EQ(0, String().Compare(String()));
// NULL vs non-NULL.
EXPECT_EQ(-1, String().Compare(String("")));
// Non-NULL vs NULL.
EXPECT_EQ(1, String("").Compare(String()));
// The following covers non-NULL vs non-NULL.
// "" vs "".
EXPECT_EQ(0, String("").Compare(String("")));
// "" vs non-"".
EXPECT_EQ(-1, String("").Compare(String("\0", 1)));
EXPECT_EQ(-1, String("").Compare(" "));
// Non-"" vs "".
EXPECT_EQ(1, String("a").Compare(String("")));
// The following covers non-"" vs non-"".
// Same length and equal.
EXPECT_EQ(0, String("a").Compare(String("a")));
// Same length and different.
EXPECT_EQ(-1, String("a\0b", 3).Compare(String("a\0c", 3)));
EXPECT_EQ(1, String("b").Compare(String("a")));
// Different lengths.
EXPECT_EQ(-1, String("a").Compare(String("ab")));
EXPECT_EQ(-1, String("a").Compare(String("a\0", 2)));
EXPECT_EQ(1, String("abc").Compare(String("aacd")));
}
// Tests String::operator==().
TEST(StringTest, Equals) {
const String null(NULL);
EXPECT_TRUE(null == NULL); // NOLINT
EXPECT_FALSE(null == ""); // NOLINT
EXPECT_FALSE(null == "bar"); // NOLINT
const String empty("");
EXPECT_FALSE(empty == NULL); // NOLINT
EXPECT_TRUE(empty == ""); // NOLINT
EXPECT_FALSE(empty == "bar"); // NOLINT
const String foo("foo");
EXPECT_FALSE(foo == NULL); // NOLINT
EXPECT_FALSE(foo == ""); // NOLINT
EXPECT_FALSE(foo == "bar"); // NOLINT
EXPECT_TRUE(foo == "foo"); // NOLINT
const String bar("x\0y", 3);
EXPECT_NE(bar, "x");
}
// Tests String::operator!=().
TEST(StringTest, NotEquals) {
const String null(NULL);
EXPECT_FALSE(null != NULL); // NOLINT
EXPECT_TRUE(null != ""); // NOLINT
EXPECT_TRUE(null != "bar"); // NOLINT
const String empty("");
EXPECT_TRUE(empty != NULL); // NOLINT
EXPECT_FALSE(empty != ""); // NOLINT
EXPECT_TRUE(empty != "bar"); // NOLINT
const String foo("foo");
EXPECT_TRUE(foo != NULL); // NOLINT
EXPECT_TRUE(foo != ""); // NOLINT
EXPECT_TRUE(foo != "bar"); // NOLINT
EXPECT_FALSE(foo != "foo"); // NOLINT
const String bar("x\0y", 3);
EXPECT_NE(bar, "x");
}
// Tests String::length().
TEST(StringTest, Length) {
EXPECT_EQ(0U, String().length());
EXPECT_EQ(0U, String("").length());
EXPECT_EQ(2U, String("ab").length());
EXPECT_EQ(3U, String("a\0b", 3).length());
}
// Tests String::EndsWith().
TEST(StringTest, EndsWith) {
EXPECT_TRUE(String("foobar").EndsWith("bar"));
EXPECT_TRUE(String("foobar").EndsWith(""));
EXPECT_TRUE(String("").EndsWith(""));
EXPECT_FALSE(String("foobar").EndsWith("foo"));
EXPECT_FALSE(String("").EndsWith("foo"));
}
// Tests String::EndsWithCaseInsensitive(). // Tests String::EndsWithCaseInsensitive().
TEST(StringTest, EndsWithCaseInsensitive) { TEST(StringTest, EndsWithCaseInsensitive) {
EXPECT_TRUE(String("foobar").EndsWithCaseInsensitive("BAR")); EXPECT_TRUE(String::EndsWithCaseInsensitive("foobar", "BAR"));
EXPECT_TRUE(String("foobaR").EndsWithCaseInsensitive("bar")); EXPECT_TRUE(String::EndsWithCaseInsensitive("foobaR", "bar"));
EXPECT_TRUE(String("foobar").EndsWithCaseInsensitive("")); EXPECT_TRUE(String::EndsWithCaseInsensitive("foobar", ""));
EXPECT_TRUE(String("").EndsWithCaseInsensitive("")); EXPECT_TRUE(String::EndsWithCaseInsensitive("", ""));
EXPECT_FALSE(String("Foobar").EndsWithCaseInsensitive("foo")); EXPECT_FALSE(String::EndsWithCaseInsensitive("Foobar", "foo"));
EXPECT_FALSE(String("foobar").EndsWithCaseInsensitive("Foo")); EXPECT_FALSE(String::EndsWithCaseInsensitive("foobar", "Foo"));
EXPECT_FALSE(String("").EndsWithCaseInsensitive("foo")); EXPECT_FALSE(String::EndsWithCaseInsensitive("", "foo"));
} }
// C++Builder's preprocessor is buggy; it fails to expand macros that // C++Builder's preprocessor is buggy; it fails to expand macros that
...@@ -1203,61 +960,6 @@ TEST(StringTest, CaseInsensitiveWideCStringEquals) { ...@@ -1203,61 +960,6 @@ TEST(StringTest, CaseInsensitiveWideCStringEquals) {
EXPECT_TRUE(String::CaseInsensitiveWideCStringEquals(L"FOOBAR", L"foobar")); EXPECT_TRUE(String::CaseInsensitiveWideCStringEquals(L"FOOBAR", L"foobar"));
} }
// Tests that NULL can be assigned to a String.
TEST(StringTest, CanBeAssignedNULL) {
const String src(NULL);
String dest;
dest = src;
EXPECT_STREQ(NULL, dest.c_str());
}
// Tests that the empty string "" can be assigned to a String.
TEST(StringTest, CanBeAssignedEmpty) {
const String src("");
String dest;
dest = src;
EXPECT_STREQ("", dest.c_str());
}
// Tests that a non-empty string can be assigned to a String.
TEST(StringTest, CanBeAssignedNonEmpty) {
const String src("hello");
String dest;
dest = src;
EXPECT_EQ(5U, dest.length());
EXPECT_STREQ("hello", dest.c_str());
const String src2("x\0y", 3);
String dest2;
dest2 = src2;
EXPECT_EQ(3U, dest2.length());
EXPECT_EQ('x', dest2.c_str()[0]);
EXPECT_EQ('\0', dest2.c_str()[1]);
EXPECT_EQ('y', dest2.c_str()[2]);
}
// Tests that a String can be assigned to itself.
TEST(StringTest, CanBeAssignedSelf) {
String dest("hello");
// Use explicit function call notation here to suppress self-assign warning.
dest.operator=(dest);
EXPECT_STREQ("hello", dest.c_str());
}
// Sun Studio < 12 incorrectly rejects this code due to an overloading
// ambiguity.
#if !(defined(__SUNPRO_CC) && __SUNPRO_CC < 0x590)
// Tests streaming a String.
TEST(StringTest, Streams) {
EXPECT_EQ(StreamableToString(String()), "(null)");
EXPECT_EQ(StreamableToString(String("")), "");
EXPECT_EQ(StreamableToString(String("a\0b", 3)), "a\\0b");
}
#endif
// Tests that String::Format() works. // Tests that String::Format() works.
TEST(StringTest, FormatWorks) { TEST(StringTest, FormatWorks) {
// Normal case: the format spec is valid, the arguments match the // Normal case: the format spec is valid, the arguments match the
...@@ -1269,19 +971,19 @@ TEST(StringTest, FormatWorks) { ...@@ -1269,19 +971,19 @@ TEST(StringTest, FormatWorks) {
const size_t kSize = sizeof(buffer); const size_t kSize = sizeof(buffer);
memset(buffer, 'a', kSize - 1); memset(buffer, 'a', kSize - 1);
buffer[kSize - 1] = '\0'; buffer[kSize - 1] = '\0';
EXPECT_STREQ(buffer, String::Format("%s", buffer).c_str()); EXPECT_EQ(buffer, String::Format("%s", buffer));
// The result needs to be 4096 characters, exceeding Format()'s limit. // The result needs to be 4096 characters, exceeding Format()'s limit.
EXPECT_STREQ("<formatting error or buffer exceeded>", EXPECT_EQ("<formatting error or buffer exceeded>",
String::Format("x%s", buffer).c_str()); String::Format("x%s", buffer));
#if GTEST_OS_LINUX && !GTEST_OS_LINUX_ANDROID #if GTEST_OS_LINUX && !GTEST_OS_LINUX_ANDROID
// On Linux, invalid format spec should lead to an error message. // On Linux, invalid format spec should lead to an error message.
// In other environment (e.g. MSVC on Windows), String::Format() may // In other environment (e.g. MSVC on Windows), String::Format() may
// simply ignore a bad format spec, so this assertion is run on // simply ignore a bad format spec, so this assertion is run on
// Linux only. // Linux only.
EXPECT_STREQ("<formatting error or buffer exceeded>", EXPECT_EQ("<formatting error or buffer exceeded>",
String::Format("%").c_str()); String::Format("%"));
#endif #endif
} }
...@@ -1915,15 +1617,16 @@ static void SetEnv(const char* name, const char* value) { ...@@ -1915,15 +1617,16 @@ static void SetEnv(const char* name, const char* value) {
// C++Builder's putenv only stores a pointer to its parameter; we have to // C++Builder's putenv only stores a pointer to its parameter; we have to
// ensure that the string remains valid as long as it might be needed. // ensure that the string remains valid as long as it might be needed.
// We use an std::map to do so. // We use an std::map to do so.
static std::map<String, String*> added_env; static std::map<std::string, std::string*> added_env;
// Because putenv stores a pointer to the string buffer, we can't delete the // Because putenv stores a pointer to the string buffer, we can't delete the
// previous string (if present) until after it's replaced. // previous string (if present) until after it's replaced.
String *prev_env = NULL; std::string *prev_env = NULL;
if (added_env.find(name) != added_env.end()) { if (added_env.find(name) != added_env.end()) {
prev_env = added_env[name]; prev_env = added_env[name];
} }
added_env[name] = new String((Message() << name << "=" << value).GetString()); added_env[name] = new std::string(
(Message() << name << "=" << value).GetString());
// The standard signature of putenv accepts a 'char*' argument. Other // The standard signature of putenv accepts a 'char*' argument. Other
// implementations, like C++Builder's, accept a 'const char*'. // implementations, like C++Builder's, accept a 'const char*'.
...@@ -3568,8 +3271,8 @@ TEST_F(NoFatalFailureTest, MessageIsStreamable) { ...@@ -3568,8 +3271,8 @@ TEST_F(NoFatalFailureTest, MessageIsStreamable) {
// Tests EqFailure(), used for implementing *EQ* assertions. // Tests EqFailure(), used for implementing *EQ* assertions.
TEST(AssertionTest, EqFailure) { TEST(AssertionTest, EqFailure) {
const String foo_val("5"), bar_val("6"); const std::string foo_val("5"), bar_val("6");
const String msg1( const std::string msg1(
EqFailure("foo", "bar", foo_val, bar_val, false) EqFailure("foo", "bar", foo_val, bar_val, false)
.failure_message()); .failure_message());
EXPECT_STREQ( EXPECT_STREQ(
...@@ -3579,7 +3282,7 @@ TEST(AssertionTest, EqFailure) { ...@@ -3579,7 +3282,7 @@ TEST(AssertionTest, EqFailure) {
"Which is: 5", "Which is: 5",
msg1.c_str()); msg1.c_str());
const String msg2( const std::string msg2(
EqFailure("foo", "6", foo_val, bar_val, false) EqFailure("foo", "6", foo_val, bar_val, false)
.failure_message()); .failure_message());
EXPECT_STREQ( EXPECT_STREQ(
...@@ -3588,7 +3291,7 @@ TEST(AssertionTest, EqFailure) { ...@@ -3588,7 +3291,7 @@ TEST(AssertionTest, EqFailure) {
"Which is: 5", "Which is: 5",
msg2.c_str()); msg2.c_str());
const String msg3( const std::string msg3(
EqFailure("5", "bar", foo_val, bar_val, false) EqFailure("5", "bar", foo_val, bar_val, false)
.failure_message()); .failure_message());
EXPECT_STREQ( EXPECT_STREQ(
...@@ -3597,16 +3300,16 @@ TEST(AssertionTest, EqFailure) { ...@@ -3597,16 +3300,16 @@ TEST(AssertionTest, EqFailure) {
"Expected: 5", "Expected: 5",
msg3.c_str()); msg3.c_str());
const String msg4( const std::string msg4(
EqFailure("5", "6", foo_val, bar_val, false).failure_message()); EqFailure("5", "6", foo_val, bar_val, false).failure_message());
EXPECT_STREQ( EXPECT_STREQ(
"Value of: 6\n" "Value of: 6\n"
"Expected: 5", "Expected: 5",
msg4.c_str()); msg4.c_str());
const String msg5( const std::string msg5(
EqFailure("foo", "bar", EqFailure("foo", "bar",
String("\"x\""), String("\"y\""), std::string("\"x\""), std::string("\"y\""),
true).failure_message()); true).failure_message());
EXPECT_STREQ( EXPECT_STREQ(
"Value of: bar\n" "Value of: bar\n"
...@@ -3618,7 +3321,7 @@ TEST(AssertionTest, EqFailure) { ...@@ -3618,7 +3321,7 @@ TEST(AssertionTest, EqFailure) {
// Tests AppendUserMessage(), used for implementing the *EQ* macros. // Tests AppendUserMessage(), used for implementing the *EQ* macros.
TEST(AssertionTest, AppendUserMessage) { TEST(AssertionTest, AppendUserMessage) {
const String foo("foo"); const std::string foo("foo");
Message msg; Message msg;
EXPECT_STREQ("foo", EXPECT_STREQ("foo",
...@@ -4199,7 +3902,7 @@ TEST(AssertionSyntaxTest, WorksWithSwitch) { ...@@ -4199,7 +3902,7 @@ TEST(AssertionSyntaxTest, WorksWithSwitch) {
#if GTEST_HAS_EXCEPTIONS #if GTEST_HAS_EXCEPTIONS
void ThrowAString() { void ThrowAString() {
throw "String"; throw "std::string";
} }
// Test that the exception assertion macros compile and work with const // Test that the exception assertion macros compile and work with const
...@@ -5619,7 +5322,7 @@ class InitGoogleTestTest : public Test { ...@@ -5619,7 +5322,7 @@ class InitGoogleTestTest : public Test {
internal::ParseGoogleTestFlagsOnly(&argc1, const_cast<CharType**>(argv1)); internal::ParseGoogleTestFlagsOnly(&argc1, const_cast<CharType**>(argv1));
#if GTEST_HAS_STREAM_REDIRECTION #if GTEST_HAS_STREAM_REDIRECTION
const String captured_stdout = GetCapturedStdout(); const std::string captured_stdout = GetCapturedStdout();
#endif #endif
// Verifies the flag values. // Verifies the flag values.
...@@ -6891,7 +6594,7 @@ TEST(TestEventListenersTest, Append) { ...@@ -6891,7 +6594,7 @@ TEST(TestEventListenersTest, Append) {
// order. // order.
class SequenceTestingListener : public EmptyTestEventListener { class SequenceTestingListener : public EmptyTestEventListener {
public: public:
SequenceTestingListener(std::vector<String>* vector, const char* id) SequenceTestingListener(std::vector<std::string>* vector, const char* id)
: vector_(vector), id_(id) {} : vector_(vector), id_(id) {}
protected: protected:
...@@ -6914,20 +6617,20 @@ class SequenceTestingListener : public EmptyTestEventListener { ...@@ -6914,20 +6617,20 @@ class SequenceTestingListener : public EmptyTestEventListener {
} }
private: private:
String GetEventDescription(const char* method) { std::string GetEventDescription(const char* method) {
Message message; Message message;
message << id_ << "." << method; message << id_ << "." << method;
return message.GetString(); return message.GetString();
} }
std::vector<String>* vector_; std::vector<std::string>* vector_;
const char* const id_; const char* const id_;
GTEST_DISALLOW_COPY_AND_ASSIGN_(SequenceTestingListener); GTEST_DISALLOW_COPY_AND_ASSIGN_(SequenceTestingListener);
}; };
TEST(EventListenerTest, AppendKeepsOrder) { TEST(EventListenerTest, AppendKeepsOrder) {
std::vector<String> vec; std::vector<std::string> vec;
TestEventListeners listeners; TestEventListeners listeners;
listeners.Append(new SequenceTestingListener(&vec, "1st")); listeners.Append(new SequenceTestingListener(&vec, "1st"));
listeners.Append(new SequenceTestingListener(&vec, "2nd")); listeners.Append(new SequenceTestingListener(&vec, "2nd"));
...@@ -7313,7 +7016,7 @@ TEST(GTestReferenceToConstTest, Works) { ...@@ -7313,7 +7016,7 @@ TEST(GTestReferenceToConstTest, Works) {
TestGTestReferenceToConst<const char&, char>(); TestGTestReferenceToConst<const char&, char>();
TestGTestReferenceToConst<const int&, const int>(); TestGTestReferenceToConst<const int&, const int>();
TestGTestReferenceToConst<const double&, double>(); TestGTestReferenceToConst<const double&, double>();
TestGTestReferenceToConst<const String&, const String&>(); TestGTestReferenceToConst<const std::string&, const std::string&>();
} }
// Tests that ImplicitlyConvertible<T1, T2>::value is a compile-time constant. // Tests that ImplicitlyConvertible<T1, T2>::value is a compile-time constant.
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment