Publishes the even listener API (by Vlad Losev); adds OS-indicating macros to simplify gtest code (by Zhanyong Wan).

This commit is contained in:
zhanyong.wan
2009-09-24 21:15:59 +00:00
parent 7fba282ce7
commit b50ef44a35
16 changed files with 250 additions and 394 deletions

View File

@@ -50,17 +50,17 @@
#include "src/gtest-internal-inl.h"
#undef GTEST_IMPLEMENTATION_
#ifdef _WIN32_WCE
#if GTEST_OS_WINDOWS_MOBILE
#include <windows.h> // NOLINT
#elif GTEST_OS_WINDOWS
#include <direct.h> // NOLINT
#endif // _WIN32_WCE
#endif // GTEST_OS_WINDOWS_MOBILE
namespace testing {
namespace internal {
namespace {
#ifdef _WIN32_WCE
#if GTEST_OS_WINDOWS_MOBILE
// TODO(wan@google.com): Move these to the POSIX adapter section in
// gtest-port.h.
@@ -81,9 +81,7 @@ int _rmdir(const char* path) {
return ret;
}
#endif // _WIN32_WCE
#ifndef _WIN32_WCE
#else
TEST(GetCurrentDirTest, ReturnsCurrentDir) {
const FilePath original_dir = FilePath::GetCurrentDir();
@@ -103,7 +101,7 @@ TEST(GetCurrentDirTest, ReturnsCurrentDir) {
#endif
}
#endif // _WIN32_WCE
#endif // GTEST_OS_WINDOWS_MOBILE
TEST(IsEmptyTest, ReturnsTrueForEmptyPath) {
EXPECT_TRUE(FilePath("").IsEmpty());
@@ -156,7 +154,7 @@ TEST(RemoveDirectoryNameTest, ShouldAlsoGiveFileName) {
// RemoveFileName "" -> "./"
TEST(RemoveFileNameTest, EmptyName) {
#ifdef _WIN32_WCE
#if GTEST_OS_WINDOWS_MOBILE
// On Windows CE, we use the root as the current directory.
EXPECT_STREQ(GTEST_PATH_SEP_,
FilePath("").RemoveFileName().c_str());
@@ -344,12 +342,12 @@ TEST(DirectoryTest, RootOfWrongDriveDoesNotExists) {
}
#endif // GTEST_OS_WINDOWS
#ifndef _WIN32_WCE
#if !GTEST_OS_WINDOWS_MOBILE
// Windows CE _does_ consider an empty directory to exist.
TEST(DirectoryTest, EmptyPathDirectoryDoesNotExist) {
EXPECT_FALSE(FilePath("").DirectoryExists());
}
#endif // ! _WIN32_WCE
#endif // !GTEST_OS_WINDOWS_MOBILE
TEST(DirectoryTest, CurrentDirectoryExists) {
#if GTEST_OS_WINDOWS // We are on Windows.
@@ -449,9 +447,8 @@ class DirectoryCreationTest : public Test {
}
String TempDir() const {
#ifdef _WIN32_WCE
#if GTEST_OS_WINDOWS_MOBILE
return String("\\temp\\");
#elif GTEST_OS_WINDOWS
const char* temp_dir = posix::GetEnv("TEMP");
if (temp_dir == NULL || temp_dir[0] == '\0')
@@ -462,7 +459,7 @@ class DirectoryCreationTest : public Test {
return String::Format("%s\\", temp_dir);
#else
return String("/tmp/");
#endif
#endif // GTEST_OS_WINDOWS_MOBILE
}
void CreateTextFile(const char* filename) {

View File

@@ -48,12 +48,12 @@ using ::testing::AddGlobalTestEnvironment;
using ::testing::Environment;
using ::testing::InitGoogleTest;
using ::testing::Test;
using ::testing::TestCase;
using ::testing::TestEventListener;
using ::testing::TestInfo;
using ::testing::TestPartResult;
using ::testing::UnitTest;
using ::testing::internal::String;
using ::testing::internal::TestCase;
using ::testing::internal::UnitTestEventListenerInterface;
using ::testing::internal::Vector;
// Used by tests to register their events.
@@ -62,17 +62,7 @@ Vector<String>* g_events = NULL;
namespace testing {
namespace internal {
// TODO(vladl@google.com): Remove this and use UnitTest::listeners()
// directly after it is published.
class UnitTestAccessor {
public:
static EventListeners& GetEventListeners() {
return UnitTest::GetInstance()->listeners();
}
static bool UnitTestFailed() { return UnitTest::GetInstance()->Failed(); }
};
class EventRecordingListener : public UnitTestEventListenerInterface {
class EventRecordingListener : public TestEventListener {
public:
EventRecordingListener(const char* name) : name_(name) {}
@@ -195,7 +185,6 @@ TEST_F(ListenerTest, DoesBar) {
using ::testing::internal::EnvironmentInvocationCatcher;
using ::testing::internal::EventRecordingListener;
using ::testing::internal::UnitTestAccessor;
void VerifyResults(const Vector<String>& data,
const char* const* expected_data,
@@ -225,9 +214,9 @@ int main(int argc, char **argv) {
g_events = &events;
InitGoogleTest(&argc, argv);
UnitTestAccessor::GetEventListeners().Append(
UnitTest::GetInstance()->listeners().Append(
new EventRecordingListener("1st"));
UnitTestAccessor::GetEventListeners().Append(
UnitTest::GetInstance()->listeners().Append(
new EventRecordingListener("2nd"));
AddGlobalTestEnvironment(new EnvironmentInvocationCatcher);
@@ -326,7 +315,7 @@ int main(int argc, char **argv) {
// We need to check manually for ad hoc test failures that happen after
// RUN_ALL_TESTS finishes.
if (UnitTestAccessor::UnitTestFailed())
if (UnitTest::GetInstance()->Failed())
ret_val = 1;
return ret_val;

View File

@@ -40,11 +40,11 @@
#include <gtest/gtest.h>
#ifdef _WIN32_WCE
#if GTEST_OS_WINDOWS_MOBILE
#include <windows.h>
#elif GTEST_OS_WINDOWS
#include <direct.h>
#endif // _WIN32_WCE
#endif // GTEST_OS_WINDOWS_MOBILE
// Indicates that this translation unit is part of Google Test's
// implementation. It must come before gtest-internal-inl.h is
@@ -130,7 +130,7 @@ TEST(XmlOutputTest, GetOutputFileFromDirectoryPath) {
TEST(OutputFileHelpersTest, GetCurrentExecutableName) {
const FilePath executable = GetCurrentExecutableName();
const char* const exe_str = executable.c_str();
#if defined(_WIN32_WCE) || GTEST_OS_WINDOWS
#if GTEST_OS_WINDOWS
ASSERT_TRUE(_strcmpi("gtest-options_test", exe_str) == 0 ||
_strcmpi("gtest-options-ex_test", exe_str) == 0 ||
_strcmpi("gtest_all_test", exe_str) == 0)
@@ -143,7 +143,7 @@ TEST(OutputFileHelpersTest, GetCurrentExecutableName) {
String(exe_str) == "gtest_all_test" ||
String(exe_str) == "lt-gtest_all_test")
<< "GetCurrentExecutableName() returns " << exe_str;
#endif
#endif // GTEST_OS_WINDOWS
}
class XmlOutputChangeDirTest : public Test {

View File

@@ -38,21 +38,7 @@
#include <string.h> // For strcmp.
#include <algorithm>
using ::testing::AddGlobalTestEnvironment;
using ::testing::Environment;
using ::testing::InitGoogleTest;
using ::testing::Test;
using ::testing::TestInfo;
using ::testing::TestPartResult;
using ::testing::UnitTest;
using ::testing::internal::TestCase;
using ::testing::internal::TestProperty;
#if GTEST_HAS_TYPED_TEST
using ::testing::Types;
using ::testing::internal::GetTypeName;
using ::testing::internal::String;
#endif // GTEST_HAS_TYPED_TEST
namespace testing {
namespace internal {
@@ -64,20 +50,20 @@ struct LessByName {
}
};
class UnitTestAccessor {
class UnitTestHelper {
public:
// Returns the array of pointers to all test cases sorted by the test case
// name. The caller is responsible for deleting the array.
static TestCase const** const GetSortedTestCases() {
UnitTest* unit_test = UnitTest::GetInstance();
UnitTest& unit_test = *UnitTest::GetInstance();
TestCase const** const test_cases =
new const TestCase*[unit_test->total_test_case_count()];
new const TestCase*[unit_test.total_test_case_count()];
for (int i = 0; i < unit_test->total_test_case_count(); ++i)
test_cases[i] = unit_test->GetTestCase(i);
for (int i = 0; i < unit_test.total_test_case_count(); ++i)
test_cases[i] = unit_test.GetTestCase(i);
std::sort(test_cases,
test_cases + unit_test->total_test_case_count(),
test_cases + unit_test.total_test_case_count(),
LessByName<TestCase>());
return test_cases;
}
@@ -85,9 +71,9 @@ class UnitTestAccessor {
// Returns the test case by its name. The caller doesn't own the returned
// pointer.
static const TestCase* FindTestCase(const char* name) {
UnitTest* unit_test = UnitTest::GetInstance();
for (int i = 0; i < unit_test->total_test_case_count(); ++i) {
const TestCase* test_case = unit_test->GetTestCase(i);
UnitTest& unit_test = *UnitTest::GetInstance();
for (int i = 0; i < unit_test.total_test_case_count(); ++i) {
const TestCase* test_case = unit_test.GetTestCase(i);
if (0 == strcmp(test_case->name(), name))
return test_case;
}
@@ -104,19 +90,12 @@ class UnitTestAccessor {
for (int i = 0; i < test_case->total_test_count(); ++i)
tests[i] = test_case->GetTestInfo(i);
std::sort(tests,
tests + test_case->total_test_count(),
std::sort(tests, tests + test_case->total_test_count(),
LessByName<TestInfo>());
return tests;
}
};
// TODO(vladl@google.com): Put tests into the internal namespace after
// UnitTest methods are published.
} // namespace internal
using internal::UnitTestAccessor;
#if GTEST_HAS_TYPED_TEST
template <typename T> class TestCaseWithCommentTest : public Test {};
TYPED_TEST_CASE(TestCaseWithCommentTest, Types<int>);
@@ -147,7 +126,7 @@ TEST(ApiTest, UnitTestImmutableAccessorsWork) {
EXPECT_EQ(5 + kTypedTests, unit_test->total_test_count());
EXPECT_EQ(3 + kTypedTests, unit_test->test_to_run_count());
const TestCase** const test_cases = UnitTestAccessor::GetSortedTestCases();
const TestCase** const test_cases = UnitTestHelper::GetSortedTestCases();
EXPECT_STREQ("ApiTest", test_cases[0]->name());
EXPECT_STREQ("DISABLED_Test", test_cases[1]->name());
@@ -165,7 +144,7 @@ TEST(ApiTest, UnitTestImmutableAccessorsWork) {
}
TEST(ApiTest, TestCaseImmutableAccessorsWork) {
const TestCase* test_case = UnitTestAccessor::FindTestCase("ApiTest");
const TestCase* test_case = UnitTestHelper::FindTestCase("ApiTest");
ASSERT_TRUE(test_case != NULL);
EXPECT_STREQ("ApiTest", test_case->name());
@@ -175,7 +154,7 @@ TEST(ApiTest, TestCaseImmutableAccessorsWork) {
EXPECT_EQ(3, test_case->test_to_run_count());
ASSERT_EQ(4, test_case->total_test_count());
const TestInfo** tests = UnitTestAccessor::GetSortedTests(test_case);
const TestInfo** tests = UnitTestHelper::GetSortedTests(test_case);
EXPECT_STREQ("DISABLED_Dummy1", tests[0]->name());
EXPECT_STREQ("ApiTest", tests[0]->test_case_name());
@@ -205,7 +184,7 @@ TEST(ApiTest, TestCaseImmutableAccessorsWork) {
tests = NULL;
#if GTEST_HAS_TYPED_TEST
test_case = UnitTestAccessor::FindTestCase("TestCaseWithCommentTest/0");
test_case = UnitTestHelper::FindTestCase("TestCaseWithCommentTest/0");
ASSERT_TRUE(test_case != NULL);
EXPECT_STREQ("TestCaseWithCommentTest/0", test_case->name());
@@ -215,7 +194,7 @@ TEST(ApiTest, TestCaseImmutableAccessorsWork) {
EXPECT_EQ(1, test_case->test_to_run_count());
ASSERT_EQ(1, test_case->total_test_count());
tests = UnitTestAccessor::GetSortedTests(test_case);
tests = UnitTestHelper::GetSortedTests(test_case);
EXPECT_STREQ("Dummy", tests[0]->name());
EXPECT_STREQ("TestCaseWithCommentTest/0", tests[0]->test_case_name());
@@ -229,7 +208,7 @@ TEST(ApiTest, TestCaseImmutableAccessorsWork) {
}
TEST(ApiTest, TestCaseDisabledAccessorsWork) {
const TestCase* test_case = UnitTestAccessor::FindTestCase("DISABLED_Test");
const TestCase* test_case = UnitTestHelper::FindTestCase("DISABLED_Test");
ASSERT_TRUE(test_case != NULL);
EXPECT_STREQ("DISABLED_Test", test_case->name());
@@ -265,7 +244,7 @@ class FinalSuccessChecker : public Environment {
EXPECT_FALSE(unit_test->Failed());
ASSERT_EQ(2 + kTypedTestCases, unit_test->total_test_case_count());
const TestCase** const test_cases = UnitTestAccessor::GetSortedTestCases();
const TestCase** const test_cases = UnitTestHelper::GetSortedTestCases();
EXPECT_STREQ("ApiTest", test_cases[0]->name());
EXPECT_STREQ("", test_cases[0]->comment());
@@ -298,8 +277,8 @@ class FinalSuccessChecker : public Environment {
EXPECT_FALSE(test_cases[2]->Failed());
#endif // GTEST_HAS_TYPED_TEST
const TestCase* test_case = UnitTestAccessor::FindTestCase("ApiTest");
const TestInfo** tests = UnitTestAccessor::GetSortedTests(test_case);
const TestCase* test_case = UnitTestHelper::FindTestCase("ApiTest");
const TestInfo** tests = UnitTestHelper::GetSortedTests(test_case);
EXPECT_STREQ("DISABLED_Dummy1", tests[0]->name());
EXPECT_STREQ("ApiTest", tests[0]->test_case_name());
EXPECT_FALSE(tests[0]->should_run());
@@ -334,8 +313,8 @@ class FinalSuccessChecker : public Environment {
delete[] tests;
#if GTEST_HAS_TYPED_TEST
test_case = UnitTestAccessor::FindTestCase("TestCaseWithCommentTest/0");
tests = UnitTestAccessor::GetSortedTests(test_case);
test_case = UnitTestHelper::FindTestCase("TestCaseWithCommentTest/0");
tests = UnitTestHelper::GetSortedTests(test_case);
EXPECT_STREQ("Dummy", tests[0]->name());
EXPECT_STREQ("TestCaseWithCommentTest/0", tests[0]->test_case_name());
@@ -352,12 +331,13 @@ class FinalSuccessChecker : public Environment {
}
};
} // namespace internal
} // namespace testing
int main(int argc, char **argv) {
InitGoogleTest(&argc, argv);
AddGlobalTestEnvironment(new testing::FinalSuccessChecker());
AddGlobalTestEnvironment(new testing::internal::FinalSuccessChecker());
return RUN_ALL_TESTS();
}

View File

@@ -46,7 +46,6 @@ namespace testing {
namespace {
using internal::String;
using internal::TestProperty;
using internal::TestPropertyKeyIs;
using internal::Vector;

View File

@@ -88,16 +88,16 @@ bool ParseInt32Flag(const char* str, const char* flag, Int32* value);
// that are needed to test it.
class EventListenersAccessor {
public:
static UnitTestEventListenerInterface* GetRepeater(
EventListeners* listeners) { return listeners->repeater(); }
static TestEventListener* GetRepeater(EventListeners* listeners) {
return listeners->repeater();
}
static void SetDefaultResultPrinter(
EventListeners* listeners,
UnitTestEventListenerInterface* listener) {
static void SetDefaultResultPrinter(EventListeners* listeners,
TestEventListener* listener) {
listeners->SetDefaultResultPrinter(listener);
}
static void SetDefaultXmlGenerator(EventListeners* listeners,
UnitTestEventListenerInterface* listener) {
TestEventListener* listener) {
listeners->SetDefaultXmlGenerator(listener);
}
@@ -131,6 +131,8 @@ using testing::AssertionFailure;
using testing::AssertionResult;
using testing::AssertionSuccess;
using testing::DoubleLE;
using testing::EmptyTestEventListener;
using testing::EventListeners;
using testing::FloatLE;
using testing::GTEST_FLAG(also_run_disabled_tests);
using testing::GTEST_FLAG(break_on_failure);
@@ -153,16 +155,15 @@ using testing::Message;
using testing::ScopedFakeTestPartResultReporter;
using testing::StaticAssertTypeEq;
using testing::Test;
using testing::TestCase;
using testing::TestPartResult;
using testing::TestPartResultArray;
using testing::TestProperty;
using testing::TestResult;
using testing::UnitTest;
using testing::internal::kMaxRandomSeed;
using testing::internal::kTestTypeIdInGoogleTest;
using testing::internal::AppendUserMessage;
using testing::internal::CodePointToUtf8;
using testing::internal::EmptyTestEventListener;
using testing::internal::EqFailure;
using testing::internal::EventListeners;
using testing::internal::FloatingPoint;
using testing::internal::GTestFlagSaver;
using testing::internal::GetCurrentOsStackTraceExceptTop;
@@ -178,14 +179,12 @@ using testing::internal::ShouldShard;
using testing::internal::ShouldUseColor;
using testing::internal::StreamableToString;
using testing::internal::String;
using testing::internal::TestCase;
using testing::internal::TestProperty;
using testing::internal::TestResult;
using testing::internal::TestResultAccessor;
using testing::internal::ThreadLocal;
using testing::internal::UInt32;
using testing::internal::Vector;
using testing::internal::WideStringToUtf8;
using testing::internal::kMaxRandomSeed;
using testing::internal::kTestTypeIdInGoogleTest;
using testing::internal::scoped_ptr;
@@ -1157,7 +1156,7 @@ TEST(StringTest, ShowWideCStringQuoted) {
String::ShowWideCStringQuoted(L"foo").c_str());
}
#ifdef _WIN32_WCE
#if GTEST_OS_WINDOWS_MOBILE
TEST(StringTest, AnsiAndUtf16Null) {
EXPECT_EQ(NULL, String::AnsiToUtf16(NULL));
EXPECT_EQ(NULL, String::Utf16ToAnsi(NULL));
@@ -1180,7 +1179,7 @@ TEST(StringTest, AnsiAndUtf16ConvertPathChars) {
EXPECT_EQ(0, wcsncmp(L".:\\ \"*?", utf16, 3));
delete [] utf16;
}
#endif // _WIN32_WCE
#endif // GTEST_OS_WINDOWS_MOBILE
#endif // GTEST_OS_WINDOWS
@@ -1808,7 +1807,7 @@ TEST_F(GTestFlagSaverTest, VerifyGTestFlags) {
// value. If the value argument is "", unsets the environment
// variable. The caller must ensure that both arguments are not NULL.
static void SetEnv(const char* name, const char* value) {
#ifdef _WIN32_WCE
#if GTEST_OS_WINDOWS_MOBILE
// Environment variables are not supported on Windows CE.
return;
#elif defined(__BORLANDC__)
@@ -1826,7 +1825,6 @@ static void SetEnv(const char* name, const char* value) {
added_env[name] = new String((Message() << name << "=" << value).GetString());
putenv(added_env[name]->c_str());
delete prev_env;
#elif GTEST_OS_WINDOWS // If we are on Windows proper.
_putenv((Message() << name << "=" << value).GetString().c_str());
#else
@@ -1835,10 +1833,10 @@ static void SetEnv(const char* name, const char* value) {
} else {
setenv(name, value, 1);
}
#endif
#endif // GTEST_OS_WINDOWS_MOBILE
}
#ifndef _WIN32_WCE
#if !GTEST_OS_WINDOWS_MOBILE
// Environment variables are not supported on Windows CE.
using testing::internal::Int32FromGTestEnv;
@@ -1886,7 +1884,7 @@ TEST(Int32FromGTestEnvTest, ParsesAndReturnsValidValue) {
SetEnv(GTEST_FLAG_PREFIX_UPPER_ "TEMP", "-321");
EXPECT_EQ(-321, Int32FromGTestEnv("temp", 0));
}
#endif // !defined(_WIN32_WCE)
#endif // !GTEST_OS_WINDOWS_MOBILE
// Tests ParseInt32Flag().
@@ -1944,7 +1942,7 @@ TEST(ParseInt32FlagTest, ParsesAndReturnsValidValue) {
// Tests that Int32FromEnvOrDie() parses the value of the var or
// returns the correct default.
// Environment variables are not supported on Windows CE.
#ifndef _WIN32_WCE
#if !GTEST_OS_WINDOWS_MOBILE
TEST(Int32FromEnvOrDieTest, ParsesAndReturnsValidValue) {
EXPECT_EQ(333, Int32FromEnvOrDie(GTEST_FLAG_PREFIX_UPPER_ "UnsetVar", 333));
SetEnv(GTEST_FLAG_PREFIX_UPPER_ "UnsetVar", "123");
@@ -1952,7 +1950,7 @@ TEST(Int32FromEnvOrDieTest, ParsesAndReturnsValidValue) {
SetEnv(GTEST_FLAG_PREFIX_UPPER_ "UnsetVar", "-123");
EXPECT_EQ(-123, Int32FromEnvOrDie(GTEST_FLAG_PREFIX_UPPER_ "UnsetVar", 333));
}
#endif // _WIN32_WCE
#endif // !GTEST_OS_WINDOWS_MOBILE
// Tests that Int32FromEnvOrDie() aborts with an error message
// if the variable is not an Int32.
@@ -2019,7 +2017,7 @@ TEST_F(ShouldShardTest, ReturnsFalseWhenTotalShardIsOne) {
// Tests that sharding is enabled if total_shards > 1 and
// we are not in a death test subprocess.
// Environment variables are not supported on Windows CE.
#ifndef _WIN32_WCE
#if !GTEST_OS_WINDOWS_MOBILE
TEST_F(ShouldShardTest, WorksWhenShardEnvVarsAreValid) {
SetEnv(index_var_, "4");
SetEnv(total_var_, "22");
@@ -2036,7 +2034,7 @@ TEST_F(ShouldShardTest, WorksWhenShardEnvVarsAreValid) {
EXPECT_TRUE(ShouldShard(total_var_, index_var_, false));
EXPECT_FALSE(ShouldShard(total_var_, index_var_, true));
}
#endif // _WIN32_WCE
#endif // !GTEST_OS_WINDOWS_MOBILE
// Tests that we exit in error if the sharding values are not valid.

View File

@@ -40,19 +40,9 @@
#include <gtest/gtest.h>
// TODO(vladl@google.com): Remove this include when the event listener API is
// published and GetUnitTestImpl is no longer needed.
//
// Indicates that this translation unit is part of Google Test's
// implementation. It must come before gtest-internal-inl.h is
// included, or there will be a compiler error. This trick is to
// prevent a user from accidentally including gtest-internal-inl.h in
// his code.
#define GTEST_IMPLEMENTATION_ 1
#include "src/gtest-internal-inl.h"
#undef GTEST_IMPLEMENTATION_
using ::testing::EventListeners;
using ::testing::InitGoogleTest;
using ::testing::UnitTest;
class SuccessfulTest : public testing::Test {
};
@@ -137,11 +127,7 @@ int main(int argc, char** argv) {
InitGoogleTest(&argc, argv);
if (argc > 1 && strcmp(argv[1], "--shut_down_xml") == 0) {
// TODO(vladl@google.com): Replace GetUnitTestImpl()->listeners() with
// UnitTest::GetInstance()->listeners() when the event listener API is
// published.
::testing::internal::EventListeners& listeners =
*::testing::internal::GetUnitTestImpl()->listeners();
EventListeners& listeners = UnitTest::GetInstance()->listeners();
delete listeners.Release(listeners.default_xml_generator());
}
return RUN_ALL_TESTS();