From ef29ce3576240e51f289e49b2c4e156b414d6685 Mon Sep 17 00:00:00 2001 From: "zhanyong.wan" Date: Mon, 22 Jun 2009 23:29:24 +0000 Subject: Turns on exceptions when compiling gtest_output_test (by Vlad Losev); moves TestCase to gtest.h to prepare for the event listener API (by Vlad Losev). --- include/gtest/gtest.h | 129 +++++++++++++++++++++++++++++- include/gtest/internal/gtest-internal.h | 1 - scons/SConscript | 6 +- src/gtest-internal-inl.h | 134 -------------------------------- src/gtest.cc | 27 ++++++- test/gtest_output_test_golden_win.txt | 42 ++++++++-- 6 files changed, 191 insertions(+), 148 deletions(-) diff --git a/include/gtest/gtest.h b/include/gtest/gtest.h index 1c504f8..66653d1 100644 --- a/include/gtest/gtest.h +++ b/include/gtest/gtest.h @@ -142,6 +142,7 @@ const int kMaxStackTraceDepth = 100; namespace internal { class GTestFlagSaver; +class TestCase; // A collection of related tests. // Converts a streamable value to a String. A NULL pointer is // converted to "(null)". When the input value is a ::string, @@ -540,7 +541,7 @@ class TestInfo { friend class internal::TestInfoImpl; friend class internal::UnitTestImpl; friend class Test; - friend class TestCase; + friend class internal::TestCase; friend TestInfo* internal::MakeAndRegisterTestInfo( const char* test_case_name, const char* name, const char* test_case_comment, const char* comment, @@ -570,6 +571,130 @@ class TestInfo { GTEST_DISALLOW_COPY_AND_ASSIGN_(TestInfo); }; +namespace internal { + +// A test case, which consists of a list of TestInfos. +// +// TestCase is not copyable. +class TestCase { + public: + // Creates a TestCase with the given name. + // + // TestCase does NOT have a default constructor. Always use this + // constructor to create a TestCase object. + // + // Arguments: + // + // name: name of the test case + // set_up_tc: pointer to the function that sets up the test case + // tear_down_tc: pointer to the function that tears down the test case + TestCase(const char* name, const char* comment, + Test::SetUpTestCaseFunc set_up_tc, + Test::TearDownTestCaseFunc tear_down_tc); + + // Destructor of TestCase. + virtual ~TestCase(); + + // Gets the name of the TestCase. + const char* name() const { return name_.c_str(); } + + // Returns the test case comment. + const char* comment() const { return comment_.c_str(); } + + // Returns true if any test in this test case should run. + bool should_run() const { return should_run_; } + + // Sets the should_run member. + void set_should_run(bool should) { should_run_ = should; } + + // Gets the (mutable) list of TestInfos in this TestCase. + internal::List& test_info_list() { return *test_info_list_; } + + // Gets the (immutable) list of TestInfos in this TestCase. + const internal::List & test_info_list() const { + return *test_info_list_; + } + + // Gets the number of successful tests in this test case. + int successful_test_count() const; + + // Gets the number of failed tests in this test case. + int failed_test_count() const; + + // Gets the number of disabled tests in this test case. + int disabled_test_count() const; + + // Get the number of tests in this test case that should run. + int test_to_run_count() const; + + // Gets the number of all tests in this test case. + int total_test_count() const; + + // Returns true iff the test case passed. + bool Passed() const { return !Failed(); } + + // Returns true iff the test case failed. + bool Failed() const { return failed_test_count() > 0; } + + // Returns the elapsed time, in milliseconds. + internal::TimeInMillis elapsed_time() const { return elapsed_time_; } + + // Adds a TestInfo to this test case. Will delete the TestInfo upon + // destruction of the TestCase object. + void AddTestInfo(TestInfo * test_info); + + // Finds and returns a TestInfo with the given name. If one doesn't + // exist, returns NULL. + TestInfo* GetTestInfo(const char* test_name); + + // Clears the results of all tests in this test case. + void ClearResult(); + + // Clears the results of all tests in the given test case. + static void ClearTestCaseResult(TestCase* test_case) { + test_case->ClearResult(); + } + + // Runs every test in this TestCase. + void Run(); + + // Runs every test in the given TestCase. + static void RunTestCase(TestCase * test_case) { test_case->Run(); } + + // Returns true iff test passed. + static bool TestPassed(const TestInfo * test_info); + + // Returns true iff test failed. + static bool TestFailed(const TestInfo * test_info); + + // Returns true iff test is disabled. + static bool TestDisabled(const TestInfo * test_info); + + // Returns true if the given test should run. + static bool ShouldRunTest(const TestInfo *test_info); + + private: + // Name of the test case. + internal::String name_; + // Comment on the test case. + internal::String comment_; + // List of TestInfos. + internal::List* test_info_list_; + // Pointer to the function that sets up the test case. + Test::SetUpTestCaseFunc set_up_tc_; + // Pointer to the function that tears down the test case. + Test::TearDownTestCaseFunc tear_down_tc_; + // True iff any test in this test case should run. + bool should_run_; + // Elapsed time, in milliseconds. + internal::TimeInMillis elapsed_time_; + + // We disallow copying TestCases. + GTEST_DISALLOW_COPY_AND_ASSIGN_(TestCase); +}; + +} // namespace internal + // An Environment object is capable of setting up and tearing down an // environment. The user should subclass this to define his own // environment(s). @@ -659,7 +784,7 @@ class UnitTest { // Returns the TestCase object for the test that's currently running, // or NULL if no test is running. - const TestCase* current_test_case() const; + const internal::TestCase* current_test_case() const; // Returns the TestInfo object for the test that's currently running, // or NULL if no test is running. diff --git a/include/gtest/internal/gtest-internal.h b/include/gtest/internal/gtest-internal.h index 6e605e0..a4c3736 100644 --- a/include/gtest/internal/gtest-internal.h +++ b/include/gtest/internal/gtest-internal.h @@ -103,7 +103,6 @@ namespace testing { class Message; // Represents a failure message. class Test; // Represents a test. -class TestCase; // A collection of related tests. class TestPartResult; // Result of a test part. class TestInfo; // Information about a test. class UnitTest; // A collection of test cases. diff --git a/scons/SConscript b/scons/SConscript index d591810..2fa519b 100644 --- a/scons/SConscript +++ b/scons/SConscript @@ -113,11 +113,13 @@ env_with_exceptions = env.Clone() if env_with_exceptions['PLATFORM'] == 'win32': env_with_exceptions.Append(CCFLAGS=['/EHsc']) env_with_exceptions.Append(CPPDEFINES='_HAS_EXCEPTIONS=1') + cppdefines = env_with_exceptions['CPPDEFINES'] # Undoes the _TYPEINFO_ hack, which is unnecessary and only creates # trouble when exceptions are enabled. - cppdefines = env_with_exceptions['CPPDEFINES'] if '_TYPEINFO_' in cppdefines: cppdefines.remove('_TYPEINFO_') + if '_HAS_EXCEPTIONS=0' in cppdefines: + cppdefines.remove('_HAS_EXCEPTIONS=0') else: env_with_exceptions.Append(CCFLAGS='-fexceptions') ccflags = env_with_exceptions['CCFLAGS'] @@ -287,7 +289,6 @@ GtestTest(env, 'gtest-typed-test_test', gtest_main, additional_sources=['../test/gtest-typed-test2_test.cc']) GtestTest(env, 'gtest-param-test_test', gtest, additional_sources=['../test/gtest-param-test2_test.cc']) -GtestTest(env, 'gtest_output_test_', gtest) GtestTest(env, 'gtest_color_test_', gtest) GtestTest(env, 'gtest-linked_ptr_test', gtest_main) GtestTest(env, 'gtest-port_test', gtest_main) @@ -305,6 +306,7 @@ GtestBinary(env, 'gtest_unittest', gtest_main, [gtest_unittest_obj]) ############################################################ # Tests targets using custom environments. +GtestTest(env_with_exceptions, 'gtest_output_test_', gtest_ex) GtestTest(env_with_exceptions, 'gtest_throw_on_failure_ex_test', gtest_ex) GtestTest(env_with_threads, 'gtest-death-test_test', gtest_main) GtestTest(env_with_less_optimization, 'gtest_env_var_test_', gtest) diff --git a/src/gtest-internal-inl.h b/src/gtest-internal-inl.h index cbe3dbf..589be02 100644 --- a/src/gtest-internal-inl.h +++ b/src/gtest-internal-inl.h @@ -556,140 +556,6 @@ class TestInfoImpl { GTEST_DISALLOW_COPY_AND_ASSIGN_(TestInfoImpl); }; -} // namespace internal - -// A test case, which consists of a list of TestInfos. -// -// TestCase is not copyable. -class TestCase { - public: - // Creates a TestCase with the given name. - // - // TestCase does NOT have a default constructor. Always use this - // constructor to create a TestCase object. - // - // Arguments: - // - // name: name of the test case - // set_up_tc: pointer to the function that sets up the test case - // tear_down_tc: pointer to the function that tears down the test case - TestCase(const char* name, const char* comment, - Test::SetUpTestCaseFunc set_up_tc, - Test::TearDownTestCaseFunc tear_down_tc); - - // Destructor of TestCase. - virtual ~TestCase(); - - // Gets the name of the TestCase. - const char* name() const { return name_.c_str(); } - - // Returns the test case comment. - const char* comment() const { return comment_.c_str(); } - - // Returns true if any test in this test case should run. - bool should_run() const { return should_run_; } - - // Sets the should_run member. - void set_should_run(bool should) { should_run_ = should; } - - // Gets the (mutable) list of TestInfos in this TestCase. - internal::List& test_info_list() { return *test_info_list_; } - - // Gets the (immutable) list of TestInfos in this TestCase. - const internal::List & test_info_list() const { - return *test_info_list_; - } - - // Gets the number of successful tests in this test case. - int successful_test_count() const; - - // Gets the number of failed tests in this test case. - int failed_test_count() const; - - // Gets the number of disabled tests in this test case. - int disabled_test_count() const; - - // Get the number of tests in this test case that should run. - int test_to_run_count() const; - - // Gets the number of all tests in this test case. - int total_test_count() const; - - // Returns true iff the test case passed. - bool Passed() const { return !Failed(); } - - // Returns true iff the test case failed. - bool Failed() const { return failed_test_count() > 0; } - - // Returns the elapsed time, in milliseconds. - internal::TimeInMillis elapsed_time() const { return elapsed_time_; } - - // Adds a TestInfo to this test case. Will delete the TestInfo upon - // destruction of the TestCase object. - void AddTestInfo(TestInfo * test_info); - - // Finds and returns a TestInfo with the given name. If one doesn't - // exist, returns NULL. - TestInfo* GetTestInfo(const char* test_name); - - // Clears the results of all tests in this test case. - void ClearResult(); - - // Clears the results of all tests in the given test case. - static void ClearTestCaseResult(TestCase* test_case) { - test_case->ClearResult(); - } - - // Runs every test in this TestCase. - void Run(); - - // Runs every test in the given TestCase. - static void RunTestCase(TestCase * test_case) { test_case->Run(); } - - // Returns true iff test passed. - static bool TestPassed(const TestInfo * test_info) { - const internal::TestInfoImpl* const impl = test_info->impl(); - return impl->should_run() && impl->result()->Passed(); - } - - // Returns true iff test failed. - static bool TestFailed(const TestInfo * test_info) { - const internal::TestInfoImpl* const impl = test_info->impl(); - return impl->should_run() && impl->result()->Failed(); - } - - // Returns true iff test is disabled. - static bool TestDisabled(const TestInfo * test_info) { - return test_info->impl()->is_disabled(); - } - - // Returns true if the given test should run. - static bool ShouldRunTest(const TestInfo *test_info) { - return test_info->impl()->should_run(); - } - - private: - // Name of the test case. - internal::String name_; - // Comment on the test case. - internal::String comment_; - // List of TestInfos. - internal::List* test_info_list_; - // Pointer to the function that sets up the test case. - Test::SetUpTestCaseFunc set_up_tc_; - // Pointer to the function that tears down the test case. - Test::TearDownTestCaseFunc tear_down_tc_; - // True iff any test in this test case should run. - bool should_run_; - // Elapsed time, in milliseconds. - internal::TimeInMillis elapsed_time_; - - // We disallow copying TestCases. - GTEST_DISALLOW_COPY_AND_ASSIGN_(TestCase); -}; - -namespace internal { - // Class UnitTestOptions. // // This class contains functions for processing options the user diff --git a/src/gtest.cc b/src/gtest.cc index cec4503..f5b05b2 100644 --- a/src/gtest.cc +++ b/src/gtest.cc @@ -129,6 +129,8 @@ namespace testing { +using internal::TestCase; + // Constants. // A test whose test case name or test name matches this filter is @@ -2309,8 +2311,6 @@ void TestInfoImpl::Run() { impl->set_current_test_info(NULL); } -} // namespace internal - // class TestCase // Gets the number of successful tests in this test case. @@ -2401,6 +2401,29 @@ void TestCase::ClearResult() { test_info_list_->ForEach(internal::TestInfoImpl::ClearTestResult); } +// Returns true iff test passed. +bool TestCase::TestPassed(const TestInfo * test_info) { + const internal::TestInfoImpl* const impl = test_info->impl(); + return impl->should_run() && impl->result()->Passed(); +} + +// Returns true iff test failed. +bool TestCase::TestFailed(const TestInfo * test_info) { + const internal::TestInfoImpl* const impl = test_info->impl(); + return impl->should_run() && impl->result()->Failed(); +} + +// Returns true iff test is disabled. +bool TestCase::TestDisabled(const TestInfo * test_info) { + return test_info->impl()->is_disabled(); +} + +// Returns true if the given test should run. +bool TestCase::ShouldRunTest(const TestInfo *test_info) { + return test_info->impl()->should_run(); +} + +} // namespace internal // class UnitTestEventListenerInterface diff --git a/test/gtest_output_test_golden_win.txt b/test/gtest_output_test_golden_win.txt index d8bb622..92fe7f4 100644 --- a/test/gtest_output_test_golden_win.txt +++ b/test/gtest_output_test_golden_win.txt @@ -5,7 +5,7 @@ gtest_output_test_.cc:#: error: Value of: false Expected: true gtest_output_test_.cc:#: error: Value of: 3 Expected: 2 -[==========] Running 57 tests from 26 test cases. +[==========] Running 61 tests from 27 test cases. [----------] Global test environment set-up. FooEnvironment::SetUp() called. BarEnvironment::SetUp() called. @@ -186,7 +186,7 @@ Expected failure #2, in TearDown(). gtest_output_test_.cc:#: error: Failed Expected failure #3, in the test fixture d'tor. [ FAILED ] ExceptionInSetUpTest.ExceptionInSetUp -[----------] 1 test from ExceptionInTestFunctionTest +[----------] 2 tests from ExceptionInTestFunctionTest [ RUN ] ExceptionInTestFunctionTest.SEH (expecting 3 failures) unknown file: error: Exception thrown with code 0xc0000005 in the test body. @@ -195,6 +195,20 @@ Expected failure #2, in TearDown(). gtest_output_test_.cc:#: error: Failed Expected failure #3, in the test fixture d'tor. [ FAILED ] ExceptionInTestFunctionTest.SEH +[ RUN ] ExceptionInTestFunctionTest.CppException +unknown file: error: Exception thrown with code 0xe06d7363 in the test body. +gtest_output_test_.cc:#: error: Failed +Expected failure #2, in TearDown(). +gtest_output_test_.cc:#: error: Failed +Expected failure #3, in the test fixture d'tor. +[ FAILED ] ExceptionInTestFunctionTest.CppException +[----------] 1 test from ExceptionInTearDownTest +[ RUN ] ExceptionInTearDownTest.ExceptionInTearDown +(expecting 2 failures) +unknown file: error: Exception thrown with code 0xe06d7363 in TearDown(). +gtest_output_test_.cc:#: error: Failed +Expected failure #2, in the test fixture d'tor. +[ FAILED ] ExceptionInTearDownTest.ExceptionInTearDown [----------] 4 tests from MixedUpTestCaseTest [ RUN ] MixedUpTestCaseTest.FirstTestFromNamespaceFoo [ OK ] MixedUpTestCaseTest.FirstTestFromNamespaceFoo @@ -259,7 +273,7 @@ test DefinedUsingTEST is defined using TEST. You probably want to change the TEST to TEST_F or move it to another test case. [ FAILED ] TEST_before_TEST_F_in_same_test_case.DefinedUsingTEST_FAndShouldFail -[----------] 7 tests from ExpectNonfatalFailureTest +[----------] 8 tests from ExpectNonfatalFailureTest [ RUN ] ExpectNonfatalFailureTest.CanReferenceGlobalVariables [ OK ] ExpectNonfatalFailureTest.CanReferenceGlobalVariables [ RUN ] ExpectNonfatalFailureTest.CanReferenceLocalVariables @@ -298,7 +312,12 @@ Expected fatal failure. gtest.cc:#: error: Expected: 1 non-fatal failure Actual: 0 failures [ FAILED ] ExpectNonfatalFailureTest.FailsWhenStatementReturns -[----------] 7 tests from ExpectFatalFailureTest +[ RUN ] ExpectNonfatalFailureTest.FailsWhenStatementThrows +(expecting a failure) +gtest.cc:#: error: Expected: 1 non-fatal failure + Actual: 0 failures +[ FAILED ] ExpectNonfatalFailureTest.FailsWhenStatementThrows +[----------] 8 tests from ExpectFatalFailureTest [ RUN ] ExpectFatalFailureTest.CanReferenceGlobalVariables [ OK ] ExpectFatalFailureTest.CanReferenceGlobalVariables [ RUN ] ExpectFatalFailureTest.CanReferenceLocalStaticVariables @@ -337,6 +356,11 @@ Expected non-fatal failure. gtest.cc:#: error: Expected: 1 fatal failure Actual: 0 failures [ FAILED ] ExpectFatalFailureTest.FailsWhenStatementReturns +[ RUN ] ExpectFatalFailureTest.FailsWhenStatementThrows +(expecting a failure) +gtest.cc:#: error: Expected: 1 fatal failure + Actual: 0 failures +[ FAILED ] ExpectFatalFailureTest.FailsWhenStatementThrows [----------] 2 tests from TypedTest/0, where TypeParam = int [ RUN ] TypedTest/0.Success [ OK ] TypedTest/0.Success @@ -460,9 +484,9 @@ Expected non-fatal failure. FooEnvironment::TearDown() called. gtest_output_test_.cc:#: error: Failed Expected fatal failure. -[==========] 57 tests from 26 test cases ran. +[==========] 61 tests from 27 test cases ran. [ PASSED ] 21 tests. -[ FAILED ] 36 tests, listed below: +[ FAILED ] 40 tests, listed below: [ FAILED ] FatalFailureTest.FatalFailureInSubroutine [ FAILED ] FatalFailureTest.FatalFailureInNestedSubroutine [ FAILED ] FatalFailureTest.NonfatalFailureInSubroutine @@ -479,6 +503,8 @@ Expected fatal failure. [ FAILED ] ExceptionInFixtureCtorTest.ExceptionInFixtureCtor [ FAILED ] ExceptionInSetUpTest.ExceptionInSetUp [ FAILED ] ExceptionInTestFunctionTest.SEH +[ FAILED ] ExceptionInTestFunctionTest.CppException +[ FAILED ] ExceptionInTearDownTest.ExceptionInTearDown [ FAILED ] MixedUpTestCaseTest.ThisShouldFail [ FAILED ] MixedUpTestCaseTest.ThisShouldFailToo [ FAILED ] MixedUpTestCaseWithSameTestNameTest.TheSecondTestWithThisNameShouldFail @@ -488,10 +514,12 @@ Expected fatal failure. [ FAILED ] ExpectNonfatalFailureTest.FailsWhenThereAreTwoNonfatalFailures [ FAILED ] ExpectNonfatalFailureTest.FailsWhenThereIsOneFatalFailure [ FAILED ] ExpectNonfatalFailureTest.FailsWhenStatementReturns +[ FAILED ] ExpectNonfatalFailureTest.FailsWhenStatementThrows [ FAILED ] ExpectFatalFailureTest.FailsWhenThereIsNoFatalFailure [ FAILED ] ExpectFatalFailureTest.FailsWhenThereAreTwoFatalFailures [ FAILED ] ExpectFatalFailureTest.FailsWhenThereIsOneNonfatalFailure [ FAILED ] ExpectFatalFailureTest.FailsWhenStatementReturns +[ FAILED ] ExpectFatalFailureTest.FailsWhenStatementThrows [ FAILED ] TypedTest/0.Failure, where TypeParam = int [ FAILED ] Unsigned/TypedTestP/0.Failure, where TypeParam = unsigned char [ FAILED ] Unsigned/TypedTestP/1.Failure, where TypeParam = unsigned int @@ -500,7 +528,7 @@ Expected fatal failure. [ FAILED ] ExpectFailureTest.ExpectFatalFailureOnAllThreads [ FAILED ] ExpectFailureTest.ExpectNonFatalFailureOnAllThreads -36 FAILED TESTS +40 FAILED TESTS YOU HAVE 1 DISABLED TEST Note: Google Test filter = FatalFailureTest.*:LoggingTest.* -- cgit v0.12