Adds support for type-parameterized tests (by Zhanyong Wan); also adds case-insensitive wide string comparison to the String class (by Vlad Losev).

This commit is contained in:
shiqian
2008-09-08 17:55:52 +00:00
parent 0c5a66245b
commit a2b1a8556e
17 changed files with 930 additions and 137 deletions

View File

@@ -103,6 +103,84 @@ class MyEnvironment : public testing::Environment {
}
};
#elif defined(TEST_CATCHES_WRONG_CASE_IN_TYPED_TEST_P)
// Tests that the compiler catches using the wrong test case name in
// TYPED_TEST_P.
#include <gtest/gtest.h>
template <typename T>
class FooTest : public testing::Test {
};
template <typename T>
class BarTest : public testing::Test {
};
TYPED_TEST_CASE_P(FooTest);
TYPED_TEST_P(BarTest, A) {} // Wrong test case name.
REGISTER_TYPED_TEST_CASE_P(FooTest, A);
INSTANTIATE_TYPED_TEST_CASE_P(My, FooTest, testing::Types<int>);
#elif defined(TEST_CATCHES_WRONG_CASE_IN_REGISTER_TYPED_TEST_CASE_P)
// Tests that the compiler catches using the wrong test case name in
// REGISTER_TYPED_TEST_CASE_P.
#include <gtest/gtest.h>
template <typename T>
class FooTest : public testing::Test {
};
template <typename T>
class BarTest : public testing::Test {
};
TYPED_TEST_CASE_P(FooTest);
TYPED_TEST_P(FooTest, A) {}
REGISTER_TYPED_TEST_CASE_P(BarTest, A); // Wrong test case name.
INSTANTIATE_TYPED_TEST_CASE_P(My, FooTest, testing::Types<int>);
#elif defined(TEST_CATCHES_WRONG_CASE_IN_INSTANTIATE_TYPED_TEST_CASE_P)
// Tests that the compiler catches using the wrong test case name in
// INSTANTIATE_TYPED_TEST_CASE_P.
#include <gtest/gtest.h>
template <typename T>
class FooTest : public testing::Test {
};
template <typename T>
class BarTest : public testing::Test {
};
TYPED_TEST_CASE_P(FooTest);
TYPED_TEST_P(FooTest, A) {}
REGISTER_TYPED_TEST_CASE_P(FooTest, A);
// Wrong test case name.
INSTANTIATE_TYPED_TEST_CASE_P(My, BarTest, testing::Types<int>);
#elif defined(TEST_CATCHES_INSTANTIATE_TYPED_TESET_CASE_P_WITH_SAME_NAME_PREFIX)
// Tests that the compiler catches instantiating TYPED_TEST_CASE_P
// twice with the same name prefix.
#include <gtest/gtest.h>
template <typename T>
class FooTest : public testing::Test {
};
TYPED_TEST_CASE_P(FooTest);
TYPED_TEST_P(FooTest, A) {}
REGISTER_TYPED_TEST_CASE_P(FooTest, A);
INSTANTIATE_TYPED_TEST_CASE_P(My, FooTest, testing::Types<int>);
// Wrong name prefix: "My" has been used.
INSTANTIATE_TYPED_TEST_CASE_P(My, FooTest, testing::Types<double>);
#else
// A sanity test. This should compile.

View File

@@ -66,6 +66,18 @@ class GTestNCTest(unittest.TestCase):
('CATCHES_CALLING_SETUP_IN_ENVIRONMENT_WITH_TYPO',
[r'Setup_should_be_spelled_SetUp']),
('CATCHES_WRONG_CASE_IN_TYPED_TEST_P',
[r'BarTest.*was not declared']),
('CATCHES_WRONG_CASE_IN_REGISTER_TYPED_TEST_CASE_P',
[r'BarTest.*was not declared']),
('CATCHES_WRONG_CASE_IN_INSTANTIATE_TYPED_TEST_CASE_P',
[r'BarTest.*not declared']),
('CATCHES_INSTANTIATE_TYPED_TESET_CASE_P_WITH_SAME_NAME_PREFIX',
[r'redefinition of.*My.*FooTest']),
('SANITY',
None)
]

View File

@@ -32,6 +32,8 @@
"""Tests the text output of Google C++ Testing Framework.
SYNOPSIS
gtest_output_test.py --gtest_build_dir=BUILD/DIR --gengolden
# where BUILD/DIR contains the built gtest_output_test_ file.
gtest_output_test.py --gengolden
gtest_output_test.py
"""

View File

@@ -699,6 +699,97 @@ TEST(ExpectFatalFailureTest, FailsWhenStatementThrows) {
#endif // GTEST_HAS_EXCEPTIONS
// This #ifdef block tests the output of typed tests.
#ifdef GTEST_HAS_TYPED_TEST
template <typename T>
class TypedTest : public testing::Test {
};
TYPED_TEST_CASE(TypedTest, testing::Types<int>);
TYPED_TEST(TypedTest, Success) {
EXPECT_EQ(0, TypeParam());
}
TYPED_TEST(TypedTest, Failure) {
EXPECT_EQ(1, TypeParam()) << "Expected failure";
}
#endif // GTEST_HAS_TYPED_TEST
// This #ifdef block tests the output of type-parameterized tests.
#ifdef GTEST_HAS_TYPED_TEST_P
template <typename T>
class TypedTestP : public testing::Test {
};
TYPED_TEST_CASE_P(TypedTestP);
TYPED_TEST_P(TypedTestP, Success) {
EXPECT_EQ(0, TypeParam());
}
TYPED_TEST_P(TypedTestP, Failure) {
EXPECT_EQ(1, TypeParam()) << "Expected failure";
}
REGISTER_TYPED_TEST_CASE_P(TypedTestP, Success, Failure);
typedef testing::Types<unsigned char, unsigned int> UnsignedTypes;
INSTANTIATE_TYPED_TEST_CASE_P(Unsigned, TypedTestP, UnsignedTypes);
#endif // GTEST_HAS_TYPED_TEST_P
#ifdef GTEST_HAS_DEATH_TEST
// We rely on the golden file to verify that tests whose test case
// name ends with DeathTest are run first.
TEST(ADeathTest, ShouldRunFirst) {
}
#ifdef GTEST_HAS_TYPED_TEST
// We rely on the golden file to verify that typed tests whose test
// case name ends with DeathTest are run first.
template <typename T>
class ATypedDeathTest : public testing::Test {
};
typedef testing::Types<int, double> NumericTypes;
TYPED_TEST_CASE(ATypedDeathTest, NumericTypes);
TYPED_TEST(ATypedDeathTest, ShouldRunFirst) {
}
#endif // GTEST_HAS_TYPED_TEST
#ifdef GTEST_HAS_TYPED_TEST_P
// We rely on the golden file to verify that type-parameterized tests
// whose test case name ends with DeathTest are run first.
template <typename T>
class ATypeParamDeathTest : public testing::Test {
};
TYPED_TEST_CASE_P(ATypeParamDeathTest);
TYPED_TEST_P(ATypeParamDeathTest, ShouldRunFirst) {
}
REGISTER_TYPED_TEST_CASE_P(ATypeParamDeathTest, ShouldRunFirst);
INSTANTIATE_TYPED_TEST_CASE_P(My, ATypeParamDeathTest, NumericTypes);
#endif // GTEST_HAS_TYPED_TEST_P
#endif // GTEST_HAS_DEATH_TEST
// Two test environments for testing testing::AddGlobalTestEnvironment().
class FooEnvironment : public testing::Environment {

View File

@@ -7,10 +7,25 @@ Expected: true
gtest_output_test_.cc:#: Failure
Value of: 3
Expected: 2
[==========] Running 37 tests from 13 test cases.
[==========] Running 48 tests from 21 test cases.
[----------] Global test environment set-up.
FooEnvironment::SetUp() called.
BarEnvironment::SetUp() called.
[----------] 1 test from ADeathTest
[ RUN ] ADeathTest.ShouldRunFirst
[ OK ] ADeathTest.ShouldRunFirst
[----------] 1 test from ATypedDeathTest/0, where TypeParam = int
[ RUN ] ATypedDeathTest/0.ShouldRunFirst
[ OK ] ATypedDeathTest/0.ShouldRunFirst
[----------] 1 test from ATypedDeathTest/1, where TypeParam = double
[ RUN ] ATypedDeathTest/1.ShouldRunFirst
[ OK ] ATypedDeathTest/1.ShouldRunFirst
[----------] 1 test from My/ATypeParamDeathTest/0, where TypeParam = int
[ RUN ] My/ATypeParamDeathTest/0.ShouldRunFirst
[ OK ] My/ATypeParamDeathTest/0.ShouldRunFirst
[----------] 1 test from My/ATypeParamDeathTest/1, where TypeParam = double
[ RUN ] My/ATypeParamDeathTest/1.ShouldRunFirst
[ OK ] My/ATypeParamDeathTest/1.ShouldRunFirst
[----------] 3 tests from FatalFailureTest
[ RUN ] FatalFailureTest.FatalFailureInSubroutine
(expecting a failure that x should be 1)
@@ -341,6 +356,36 @@ gtest.cc:#: Failure
Expected: 1 fatal failure
Actual: 0 failures
[ FAILED ] ExpectFatalFailureTest.FailsWhenStatementReturns
[----------] 2 tests from TypedTest/0, where TypeParam = int
[ RUN ] TypedTest/0.Success
[ OK ] TypedTest/0.Success
[ RUN ] TypedTest/0.Failure
gtest_output_test_.cc:#: Failure
Value of: TypeParam()
Actual: 0
Expected: 1
Expected failure
[ FAILED ] TypedTest/0.Failure
[----------] 2 tests from Unsigned/TypedTestP/0, where TypeParam = unsigned char
[ RUN ] Unsigned/TypedTestP/0.Success
[ OK ] Unsigned/TypedTestP/0.Success
[ RUN ] Unsigned/TypedTestP/0.Failure
gtest_output_test_.cc:#: Failure
Value of: TypeParam()
Actual: \0
Expected: 1
Expected failure
[ FAILED ] Unsigned/TypedTestP/0.Failure
[----------] 2 tests from Unsigned/TypedTestP/1, where TypeParam = unsigned int
[ RUN ] Unsigned/TypedTestP/1.Success
[ OK ] Unsigned/TypedTestP/1.Success
[ RUN ] Unsigned/TypedTestP/1.Failure
gtest_output_test_.cc:#: Failure
Value of: TypeParam()
Actual: 0
Expected: 1
Expected failure
[ FAILED ] Unsigned/TypedTestP/1.Failure
[----------] Global test environment tear-down
BarEnvironment::TearDown() called.
gtest_output_test_.cc:#: Failure
@@ -350,9 +395,9 @@ FooEnvironment::TearDown() called.
gtest_output_test_.cc:#: Failure
Failed
Expected fatal failure.
[==========] 37 tests from 13 test cases ran.
[ PASSED ] 11 tests.
[ FAILED ] 26 tests, listed below:
[==========] 48 tests from 21 test cases ran.
[ PASSED ] 19 tests.
[ FAILED ] 29 tests, listed below:
[ FAILED ] FatalFailureTest.FatalFailureInSubroutine
[ FAILED ] FatalFailureTest.FatalFailureInNestedSubroutine
[ FAILED ] FatalFailureTest.NonfatalFailureInSubroutine
@@ -379,8 +424,11 @@ Expected fatal failure.
[ FAILED ] ExpectFatalFailureTest.FailsWhenThereAreTwoFatalFailures
[ FAILED ] ExpectFatalFailureTest.FailsWhenThereIsOneNonfatalFailure
[ FAILED ] ExpectFatalFailureTest.FailsWhenStatementReturns
[ 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
26 FAILED TESTS
29 FAILED TESTS
The non-test part of the code is expected to have 2 failures.
gtest_output_test_.cc:#: Failure

View File

@@ -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 40 tests from 16 test cases.
[==========] Running 46 tests from 19 test cases.
[----------] Global test environment set-up.
FooEnvironment::SetUp() called.
BarEnvironment::SetUp() called.
@@ -317,6 +317,33 @@ Expected non-fatal failure.
gtest.cc:#: error: Expected: 1 fatal failure
Actual: 0 failures
[ FAILED ] ExpectFatalFailureTest.FailsWhenStatementReturns
[----------] 2 tests from TypedTest/0, where TypeParam = int
[ RUN ] TypedTest/0.Success
[ OK ] TypedTest/0.Success
[ RUN ] TypedTest/0.Failure
gtest_output_test_.cc:#: error: Value of: TypeParam()
Actual: 0
Expected: 1
Expected failure
[ FAILED ] TypedTest/0.Failure
[----------] 2 tests from Unsigned/TypedTestP/0, where TypeParam = unsigned char
[ RUN ] Unsigned/TypedTestP/0.Success
[ OK ] Unsigned/TypedTestP/0.Success
[ RUN ] Unsigned/TypedTestP/0.Failure
gtest_output_test_.cc:#: error: Value of: TypeParam()
Actual: \0
Expected: 1
Expected failure
[ FAILED ] Unsigned/TypedTestP/0.Failure
[----------] 2 tests from Unsigned/TypedTestP/1, where TypeParam = unsigned int
[ RUN ] Unsigned/TypedTestP/1.Success
[ OK ] Unsigned/TypedTestP/1.Success
[ RUN ] Unsigned/TypedTestP/1.Failure
gtest_output_test_.cc:#: error: Value of: TypeParam()
Actual: 0
Expected: 1
Expected failure
[ FAILED ] Unsigned/TypedTestP/1.Failure
[----------] Global test environment tear-down
BarEnvironment::TearDown() called.
gtest_output_test_.cc:#: error: Failed
@@ -324,9 +351,9 @@ Expected non-fatal failure.
FooEnvironment::TearDown() called.
gtest_output_test_.cc:#: error: Failed
Expected fatal failure.
[==========] 40 tests from 16 test cases ran.
[ PASSED ] 11 tests.
[ FAILED ] 29 tests, listed below:
[==========] 46 tests from 19 test cases ran.
[ PASSED ] 14 tests.
[ FAILED ] 32 tests, listed below:
[ FAILED ] FatalFailureTest.FatalFailureInSubroutine
[ FAILED ] FatalFailureTest.FatalFailureInNestedSubroutine
[ FAILED ] FatalFailureTest.NonfatalFailureInSubroutine
@@ -356,8 +383,11 @@ Expected fatal failure.
[ FAILED ] ExpectFatalFailureTest.FailsWhenThereAreTwoFatalFailures
[ FAILED ] ExpectFatalFailureTest.FailsWhenThereIsOneNonfatalFailure
[ FAILED ] ExpectFatalFailureTest.FailsWhenStatementReturns
[ 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
29 FAILED TESTS
32 FAILED TESTS
The non-test part of the code is expected to have 2 failures.
gtest_output_test_.cc:#: error: Value of: false

View File

@@ -529,6 +529,18 @@ TEST(StringTest, EndsWithCaseInsensitive) {
EXPECT_FALSE(String("").EndsWithCaseInsensitive("foo"));
}
// Tests String::CaseInsensitiveWideCStringEquals
TEST(StringTest, CaseInsensitiveWideCStringEquals) {
EXPECT_TRUE(String::CaseInsensitiveWideCStringEquals(NULL, NULL));
EXPECT_FALSE(String::CaseInsensitiveWideCStringEquals(NULL, L""));
EXPECT_FALSE(String::CaseInsensitiveWideCStringEquals(L"", NULL));
EXPECT_FALSE(String::CaseInsensitiveWideCStringEquals(NULL, L"foobar"));
EXPECT_FALSE(String::CaseInsensitiveWideCStringEquals(L"foobar", NULL));
EXPECT_TRUE(String::CaseInsensitiveWideCStringEquals(L"foobar", L"foobar"));
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);
@@ -2134,6 +2146,68 @@ TEST_F(DisabledTestsTest, DISABLED_TestShouldNotRun_2) {
FAIL() << "Unexpected failure: Disabled test should not be run.";
}
// Tests that disabled typed tests aren't run.
#ifdef GTEST_HAS_TYPED_TEST
template <typename T>
class TypedTest : public Test {
};
typedef testing::Types<int, double> NumericTypes;
TYPED_TEST_CASE(TypedTest, NumericTypes);
TYPED_TEST(TypedTest, DISABLED_ShouldNotRun) {
FAIL() << "Unexpected failure: Disabled typed test should not run.";
}
template <typename T>
class DISABLED_TypedTest : public Test {
};
TYPED_TEST_CASE(DISABLED_TypedTest, NumericTypes);
TYPED_TEST(DISABLED_TypedTest, ShouldNotRun) {
FAIL() << "Unexpected failure: Disabled typed test should not run.";
}
#endif // GTEST_HAS_TYPED_TEST
// Tests that disabled type-parameterized tests aren't run.
#ifdef GTEST_HAS_TYPED_TEST_P
template <typename T>
class TypedTestP : public Test {
};
TYPED_TEST_CASE_P(TypedTestP);
TYPED_TEST_P(TypedTestP, DISABLED_ShouldNotRun) {
FAIL() << "Unexpected failure: "
<< "Disabled type-parameterized test should not run.";
}
REGISTER_TYPED_TEST_CASE_P(TypedTestP, DISABLED_ShouldNotRun);
INSTANTIATE_TYPED_TEST_CASE_P(My, TypedTestP, NumericTypes);
template <typename T>
class DISABLED_TypedTestP : public Test {
};
TYPED_TEST_CASE_P(DISABLED_TypedTestP);
TYPED_TEST_P(DISABLED_TypedTestP, ShouldNotRun) {
FAIL() << "Unexpected failure: "
<< "Disabled type-parameterized test should not run.";
}
REGISTER_TYPED_TEST_CASE_P(DISABLED_TypedTestP, ShouldNotRun);
INSTANTIATE_TYPED_TEST_CASE_P(My, DISABLED_TypedTestP, NumericTypes);
#endif // GTEST_HAS_TYPED_TEST_P
// Tests that assertion macros evaluate their arguments exactly once.
@@ -3491,7 +3565,7 @@ class TestInfoTest : public Test {
protected:
static TestInfo * GetTestInfo(const char* test_name) {
return UnitTest::GetInstance()->impl()->
GetTestCase("TestInfoTest", NULL, NULL)->
GetTestCase("TestInfoTest", "", NULL, NULL)->
GetTestInfo(test_name);
}