"vscode:/vscode.git/clone" did not exist on "3f162f5e10fc029ed67bab9a03b9a732c72e2cc0"
Unverified Commit 11f5a274 authored by Gennadiy Civil's avatar Gennadiy Civil Committed by GitHub
Browse files

Merge branch 'master' into cross-testing-patch-1

parents 008e54c1 66bd580b
...@@ -89,15 +89,18 @@ using testing::Mock; ...@@ -89,15 +89,18 @@ using testing::Mock;
using testing::NaggyMock; using testing::NaggyMock;
using testing::Ne; using testing::Ne;
using testing::Return; using testing::Return;
using testing::SaveArg;
using testing::Sequence; using testing::Sequence;
using testing::SetArgPointee; using testing::SetArgPointee;
using testing::internal::ExpectationTester; using testing::internal::ExpectationTester;
using testing::internal::FormatFileLocation; using testing::internal::FormatFileLocation;
using testing::internal::kAllow;
using testing::internal::kErrorVerbosity; using testing::internal::kErrorVerbosity;
using testing::internal::kFail;
using testing::internal::kInfoVerbosity; using testing::internal::kInfoVerbosity;
using testing::internal::kWarn;
using testing::internal::kWarningVerbosity; using testing::internal::kWarningVerbosity;
using testing::internal::linked_ptr; using testing::internal::linked_ptr;
using testing::internal::string;
#if GTEST_HAS_STREAM_REDIRECTION #if GTEST_HAS_STREAM_REDIRECTION
using testing::HasSubstr; using testing::HasSubstr;
...@@ -692,6 +695,60 @@ TEST(ExpectCallSyntaxTest, WarnsOnTooFewActions) { ...@@ -692,6 +695,60 @@ TEST(ExpectCallSyntaxTest, WarnsOnTooFewActions) {
b.DoB(); b.DoB();
} }
TEST(ExpectCallSyntaxTest, WarningIsErrorWithFlag) {
int original_behavior = testing::GMOCK_FLAG(default_mock_behavior);
testing::GMOCK_FLAG(default_mock_behavior) = kAllow;
CaptureStdout();
{
MockA a;
a.DoA(0);
}
std::string output = GetCapturedStdout();
EXPECT_TRUE(output.empty()) << output;
testing::GMOCK_FLAG(default_mock_behavior) = kWarn;
CaptureStdout();
{
MockA a;
a.DoA(0);
}
std::string warning_output = GetCapturedStdout();
EXPECT_PRED_FORMAT2(IsSubstring, "GMOCK WARNING", warning_output);
EXPECT_PRED_FORMAT2(IsSubstring, "Uninteresting mock function call",
warning_output);
testing::GMOCK_FLAG(default_mock_behavior) = kFail;
EXPECT_NONFATAL_FAILURE({
MockA a;
a.DoA(0);
}, "Uninteresting mock function call");
// Out of bounds values are converted to kWarn
testing::GMOCK_FLAG(default_mock_behavior) = -1;
CaptureStdout();
{
MockA a;
a.DoA(0);
}
warning_output = GetCapturedStdout();
EXPECT_PRED_FORMAT2(IsSubstring, "GMOCK WARNING", warning_output);
EXPECT_PRED_FORMAT2(IsSubstring, "Uninteresting mock function call",
warning_output);
testing::GMOCK_FLAG(default_mock_behavior) = 3;
CaptureStdout();
{
MockA a;
a.DoA(0);
}
warning_output = GetCapturedStdout();
EXPECT_PRED_FORMAT2(IsSubstring, "GMOCK WARNING", warning_output);
EXPECT_PRED_FORMAT2(IsSubstring, "Uninteresting mock function call",
warning_output);
testing::GMOCK_FLAG(default_mock_behavior) = original_behavior;
}
#endif // GTEST_HAS_STREAM_REDIRECTION #endif // GTEST_HAS_STREAM_REDIRECTION
// Tests the semantics of ON_CALL(). // Tests the semantics of ON_CALL().
...@@ -1954,7 +2011,7 @@ class MockC { ...@@ -1954,7 +2011,7 @@ class MockC {
public: public:
MockC() {} MockC() {}
MOCK_METHOD6(VoidMethod, void(bool cond, int n, string s, void* p, MOCK_METHOD6(VoidMethod, void(bool cond, int n, std::string s, void* p,
const Printable& x, Unprintable y)); const Printable& x, Unprintable y));
MOCK_METHOD0(NonVoidMethod, int()); // NOLINT MOCK_METHOD0(NonVoidMethod, int()); // NOLINT
...@@ -1970,7 +2027,7 @@ class VerboseFlagPreservingFixture : public testing::Test { ...@@ -1970,7 +2027,7 @@ class VerboseFlagPreservingFixture : public testing::Test {
~VerboseFlagPreservingFixture() { GMOCK_FLAG(verbose) = saved_verbose_flag_; } ~VerboseFlagPreservingFixture() { GMOCK_FLAG(verbose) = saved_verbose_flag_; }
private: private:
const string saved_verbose_flag_; const std::string saved_verbose_flag_;
GTEST_DISALLOW_COPY_AND_ASSIGN_(VerboseFlagPreservingFixture); GTEST_DISALLOW_COPY_AND_ASSIGN_(VerboseFlagPreservingFixture);
}; };
...@@ -2062,8 +2119,8 @@ class GMockVerboseFlagTest : public VerboseFlagPreservingFixture { ...@@ -2062,8 +2119,8 @@ class GMockVerboseFlagTest : public VerboseFlagPreservingFixture {
// contain the given function name in the stack trace. When it's // contain the given function name in the stack trace. When it's
// false, the output should be empty.) // false, the output should be empty.)
void VerifyOutput(const std::string& output, bool should_print, void VerifyOutput(const std::string& output, bool should_print,
const string& expected_substring, const std::string& expected_substring,
const string& function_name) { const std::string& function_name) {
if (should_print) { if (should_print) {
EXPECT_THAT(output.c_str(), HasSubstr(expected_substring)); EXPECT_THAT(output.c_str(), HasSubstr(expected_substring));
# ifndef NDEBUG # ifndef NDEBUG
...@@ -2113,11 +2170,13 @@ class GMockVerboseFlagTest : public VerboseFlagPreservingFixture { ...@@ -2113,11 +2170,13 @@ class GMockVerboseFlagTest : public VerboseFlagPreservingFixture {
// Tests how the flag affects uninteresting calls on a naggy mock. // Tests how the flag affects uninteresting calls on a naggy mock.
void TestUninterestingCallOnNaggyMock(bool should_print) { void TestUninterestingCallOnNaggyMock(bool should_print) {
NaggyMock<MockA> a; NaggyMock<MockA> a;
const string note = const std::string note =
"NOTE: You can safely ignore the above warning unless this " "NOTE: You can safely ignore the above warning unless this "
"call should not happen. Do not suppress it by blindly adding " "call should not happen. Do not suppress it by blindly adding "
"an EXPECT_CALL() if you don't mean to enforce the call. " "an EXPECT_CALL() if you don't mean to enforce the call. "
"See https://github.com/google/googletest/blob/master/googlemock/docs/CookBook.md#" "See "
"https://github.com/google/googletest/blob/master/googlemock/docs/"
"CookBook.md#"
"knowing-when-to-expect for details."; "knowing-when-to-expect for details.";
// A void-returning function. // A void-returning function.
...@@ -2623,9 +2682,78 @@ TEST(SynchronizationTest, CanCallMockMethodInAction) { ...@@ -2623,9 +2682,78 @@ TEST(SynchronizationTest, CanCallMockMethodInAction) {
// EXPECT_CALL() did not specify an action. // EXPECT_CALL() did not specify an action.
} }
TEST(ParameterlessExpectationsTest, CanSetExpectationsWithoutMatchers) {
MockA a;
int do_a_arg0 = 0;
ON_CALL(a, DoA).WillByDefault(SaveArg<0>(&do_a_arg0));
int do_a_47_arg0 = 0;
ON_CALL(a, DoA(47)).WillByDefault(SaveArg<0>(&do_a_47_arg0));
a.DoA(17);
EXPECT_THAT(do_a_arg0, 17);
EXPECT_THAT(do_a_47_arg0, 0);
a.DoA(47);
EXPECT_THAT(do_a_arg0, 17);
EXPECT_THAT(do_a_47_arg0, 47);
ON_CALL(a, Binary).WillByDefault(Return(true));
ON_CALL(a, Binary(_, 14)).WillByDefault(Return(false));
EXPECT_THAT(a.Binary(14, 17), true);
EXPECT_THAT(a.Binary(17, 14), false);
}
TEST(ParameterlessExpectationsTest, CanSetExpectationsForOverloadedMethods) {
MockB b;
ON_CALL(b, DoB()).WillByDefault(Return(9));
ON_CALL(b, DoB(5)).WillByDefault(Return(11));
EXPECT_THAT(b.DoB(), 9);
EXPECT_THAT(b.DoB(1), 0); // default value
EXPECT_THAT(b.DoB(5), 11);
}
struct MockWithConstMethods {
public:
MOCK_CONST_METHOD1(Foo, int(int));
MOCK_CONST_METHOD2(Bar, int(int, const char*));
};
TEST(ParameterlessExpectationsTest, CanSetExpectationsForConstMethods) {
MockWithConstMethods mock;
ON_CALL(mock, Foo).WillByDefault(Return(7));
ON_CALL(mock, Bar).WillByDefault(Return(33));
EXPECT_THAT(mock.Foo(17), 7);
EXPECT_THAT(mock.Bar(27, "purple"), 33);
}
class MockConstOverload {
public:
MOCK_METHOD1(Overloaded, int(int));
MOCK_CONST_METHOD1(Overloaded, int(int));
};
TEST(ParameterlessExpectationsTest,
CanSetExpectationsForConstOverloadedMethods) {
MockConstOverload mock;
ON_CALL(mock, Overloaded(_)).WillByDefault(Return(7));
ON_CALL(mock, Overloaded(5)).WillByDefault(Return(9));
ON_CALL(Const(mock), Overloaded(5)).WillByDefault(Return(11));
ON_CALL(Const(mock), Overloaded(7)).WillByDefault(Return(13));
EXPECT_THAT(mock.Overloaded(1), 7);
EXPECT_THAT(mock.Overloaded(5), 9);
EXPECT_THAT(mock.Overloaded(7), 7);
const MockConstOverload& const_mock = mock;
EXPECT_THAT(const_mock.Overloaded(1), 0);
EXPECT_THAT(const_mock.Overloaded(5), 11);
EXPECT_THAT(const_mock.Overloaded(7), 13);
}
} // namespace } // namespace
// Allows the user to define his own main and then invoke gmock_main // Allows the user to define their own main and then invoke gmock_main
// from it. This might be necessary on some platforms which require // from it. This might be necessary on some platforms which require
// specific setup and teardown. // specific setup and teardown.
#if GMOCK_RENAME_MAIN #if GMOCK_RENAME_MAIN
...@@ -2634,7 +2762,6 @@ int gmock_main(int argc, char **argv) { ...@@ -2634,7 +2762,6 @@ int gmock_main(int argc, char **argv) {
int main(int argc, char **argv) { int main(int argc, char **argv) {
#endif // GMOCK_RENAME_MAIN #endif // GMOCK_RENAME_MAIN
testing::InitGoogleMock(&argc, argv); testing::InitGoogleMock(&argc, argv);
// Ensures that the tests pass no matter what value of // Ensures that the tests pass no matter what value of
// --gmock_catch_leaked_mocks and --gmock_verbose the user specifies. // --gmock_catch_leaked_mocks and --gmock_verbose the user specifies.
testing::GMOCK_FLAG(catch_leaked_mocks) = true; testing::GMOCK_FLAG(catch_leaked_mocks) = true;
......
...@@ -34,9 +34,11 @@ ...@@ -34,9 +34,11 @@
#include "gmock/gmock.h" #include "gmock/gmock.h"
#include "gtest/gtest.h" #include "gtest/gtest.h"
#if GTEST_HAS_EXCEPTIONS
namespace { namespace {
using testing::HasSubstr; using testing::HasSubstr;
using testing::internal::GoogleTestFailureException; using testing::internal::GoogleTestFailureException;
// A type that cannot be default constructed. // A type that cannot be default constructed.
...@@ -52,8 +54,6 @@ class MockFoo { ...@@ -52,8 +54,6 @@ class MockFoo {
MOCK_METHOD0(GetNonDefaultConstructible, NonDefaultConstructible()); MOCK_METHOD0(GetNonDefaultConstructible, NonDefaultConstructible());
}; };
#if GTEST_HAS_EXCEPTIONS
TEST(DefaultValueTest, ThrowsRuntimeErrorWhenNoDefaultValue) { TEST(DefaultValueTest, ThrowsRuntimeErrorWhenNoDefaultValue) {
MockFoo mock; MockFoo mock;
try { try {
...@@ -76,6 +76,6 @@ TEST(DefaultValueTest, ThrowsRuntimeErrorWhenNoDefaultValue) { ...@@ -76,6 +76,6 @@ TEST(DefaultValueTest, ThrowsRuntimeErrorWhenNoDefaultValue) {
} }
} }
#endif
} // unnamed namespace } // unnamed namespace
#endif
...@@ -33,10 +33,8 @@ ...@@ -33,10 +33,8 @@
__author__ = 'wan@google.com (Zhanyong Wan)' __author__ = 'wan@google.com (Zhanyong Wan)'
import gmock_test_utils import gmock_test_utils
PROGRAM_PATH = gmock_test_utils.GetTestExecutablePath('gmock_leak_test_') PROGRAM_PATH = gmock_test_utils.GetTestExecutablePath('gmock_leak_test_')
TEST_WITH_EXPECT_CALL = [PROGRAM_PATH, '--gtest_filter=*ExpectCall*'] TEST_WITH_EXPECT_CALL = [PROGRAM_PATH, '--gtest_filter=*ExpectCall*']
TEST_WITH_ON_CALL = [PROGRAM_PATH, '--gtest_filter=*OnCall*'] TEST_WITH_ON_CALL = [PROGRAM_PATH, '--gtest_filter=*OnCall*']
......
...@@ -37,4 +37,4 @@ ...@@ -37,4 +37,4 @@
#define LinkTest LinkTest2 #define LinkTest LinkTest2
#include "test/gmock_link_test.h" #include "test/gmock_link_test.h"
...@@ -37,4 +37,4 @@ ...@@ -37,4 +37,4 @@
#define LinkTest LinkTest1 #define LinkTest LinkTest1
#include "test/gmock_link_test.h" #include "test/gmock_link_test.h"
...@@ -90,8 +90,10 @@ ...@@ -90,8 +90,10 @@
// Field // Field
// Property // Property
// ResultOf(function) // ResultOf(function)
// ResultOf(callback)
// Pointee // Pointee
// Truly(predicate) // Truly(predicate)
// AddressSatisfies
// AllOf // AllOf
// AnyOf // AnyOf
// Not // Not
...@@ -120,13 +122,15 @@ ...@@ -120,13 +122,15 @@
# include <errno.h> # include <errno.h>
#endif #endif
#include "gmock/internal/gmock-port.h"
#include "gtest/gtest.h"
#include <iostream> #include <iostream>
#include <vector> #include <vector>
#include "gtest/gtest.h"
#include "gtest/internal/gtest-port.h"
using testing::_; using testing::_;
using testing::A; using testing::A;
using testing::Action;
using testing::AllOf; using testing::AllOf;
using testing::AnyOf; using testing::AnyOf;
using testing::Assign; using testing::Assign;
...@@ -148,6 +152,8 @@ using testing::Invoke; ...@@ -148,6 +152,8 @@ using testing::Invoke;
using testing::InvokeArgument; using testing::InvokeArgument;
using testing::InvokeWithoutArgs; using testing::InvokeWithoutArgs;
using testing::IsNull; using testing::IsNull;
using testing::IsSubsetOf;
using testing::IsSupersetOf;
using testing::Le; using testing::Le;
using testing::Lt; using testing::Lt;
using testing::Matcher; using testing::Matcher;
...@@ -592,6 +598,22 @@ TEST(LinkTest, TestMatcherElementsAreArray) { ...@@ -592,6 +598,22 @@ TEST(LinkTest, TestMatcherElementsAreArray) {
ON_CALL(mock, VoidFromVector(ElementsAreArray(arr))).WillByDefault(Return()); ON_CALL(mock, VoidFromVector(ElementsAreArray(arr))).WillByDefault(Return());
} }
// Tests the linkage of the IsSubsetOf matcher.
TEST(LinkTest, TestMatcherIsSubsetOf) {
Mock mock;
char arr[] = {'a', 'b'};
ON_CALL(mock, VoidFromVector(IsSubsetOf(arr))).WillByDefault(Return());
}
// Tests the linkage of the IsSupersetOf matcher.
TEST(LinkTest, TestMatcherIsSupersetOf) {
Mock mock;
char arr[] = {'a', 'b'};
ON_CALL(mock, VoidFromVector(IsSupersetOf(arr))).WillByDefault(Return());
}
// Tests the linkage of the ContainerEq matcher. // Tests the linkage of the ContainerEq matcher.
TEST(LinkTest, TestMatcherContainerEq) { TEST(LinkTest, TestMatcherContainerEq) {
Mock mock; Mock mock;
......
...@@ -31,11 +31,11 @@ ...@@ -31,11 +31,11 @@
"""Tests the text output of Google C++ Mocking Framework. """Tests the text output of Google C++ Mocking Framework.
SYNOPSIS To update the golden file:
gmock_output_test.py --build_dir=BUILD/DIR --gengolden gmock_output_test.py --build_dir=BUILD/DIR --gengolden
# where BUILD/DIR contains the built gmock_output_test_ file. # where BUILD/DIR contains the built gmock_output_test_ file.
gmock_output_test.py --gengolden gmock_output_test.py --gengolden
gmock_output_test.py gmock_output_test.py
""" """
__author__ = 'wan@google.com (Zhanyong Wan)' __author__ = 'wan@google.com (Zhanyong Wan)'
...@@ -43,7 +43,6 @@ __author__ = 'wan@google.com (Zhanyong Wan)' ...@@ -43,7 +43,6 @@ __author__ = 'wan@google.com (Zhanyong Wan)'
import os import os
import re import re
import sys import sys
import gmock_test_utils import gmock_test_utils
...@@ -176,5 +175,8 @@ if __name__ == '__main__': ...@@ -176,5 +175,8 @@ if __name__ == '__main__':
golden_file = open(GOLDEN_PATH, 'wb') golden_file = open(GOLDEN_PATH, 'wb')
golden_file.write(output) golden_file.write(output)
golden_file.close() golden_file.close()
# Suppress the error "googletest was imported but a call to its main()
# was never detected."
os._exit(0)
else: else:
gmock_test_utils.Main() gmock_test_utils.Main()
...@@ -39,6 +39,12 @@ ...@@ -39,6 +39,12 @@
#include "gtest/gtest.h" #include "gtest/gtest.h"
// Silence C4100 (unreferenced formal parameter)
#ifdef _MSC_VER
# pragma warning(push)
# pragma warning(disable:4100)
#endif
using testing::_; using testing::_;
using testing::AnyNumber; using testing::AnyNumber;
using testing::Ge; using testing::Ge;
...@@ -47,6 +53,7 @@ using testing::NaggyMock; ...@@ -47,6 +53,7 @@ using testing::NaggyMock;
using testing::Ref; using testing::Ref;
using testing::Return; using testing::Return;
using testing::Sequence; using testing::Sequence;
using testing::Value;
class MockFoo { class MockFoo {
public: public:
...@@ -268,6 +275,15 @@ TEST_F(GMockOutputTest, CatchesLeakedMocks) { ...@@ -268,6 +275,15 @@ TEST_F(GMockOutputTest, CatchesLeakedMocks) {
// Both foo1 and foo2 are deliberately leaked. // Both foo1 and foo2 are deliberately leaked.
} }
MATCHER_P2(IsPair, first, second, "") {
return Value(arg.first, first) && Value(arg.second, second);
}
TEST_F(GMockOutputTest, PrintsMatcher) {
const testing::Matcher<int> m1 = Ge(48);
EXPECT_THAT((std::pair<int, bool>(42, true)), IsPair(m1, true));
}
void TestCatchesLeakedMocksInAdHocTests() { void TestCatchesLeakedMocksInAdHocTests() {
MockFoo* foo = new MockFoo; MockFoo* foo = new MockFoo;
...@@ -280,7 +296,6 @@ void TestCatchesLeakedMocksInAdHocTests() { ...@@ -280,7 +296,6 @@ void TestCatchesLeakedMocksInAdHocTests() {
int main(int argc, char **argv) { int main(int argc, char **argv) {
testing::InitGoogleMock(&argc, argv); testing::InitGoogleMock(&argc, argv);
// Ensures that the tests pass no matter what value of // Ensures that the tests pass no matter what value of
// --gmock_catch_leaked_mocks and --gmock_verbose the user specifies. // --gmock_catch_leaked_mocks and --gmock_verbose the user specifies.
testing::GMOCK_FLAG(catch_leaked_mocks) = true; testing::GMOCK_FLAG(catch_leaked_mocks) = true;
...@@ -289,3 +304,7 @@ int main(int argc, char **argv) { ...@@ -289,3 +304,7 @@ int main(int argc, char **argv) {
TestCatchesLeakedMocksInAdHocTests(); TestCatchesLeakedMocksInAdHocTests();
return RUN_ALL_TESTS(); return RUN_ALL_TESTS();
} }
#ifdef _MSC_VER
# pragma warning(pop)
#endif
...@@ -288,6 +288,12 @@ Stack trace: ...@@ -288,6 +288,12 @@ Stack trace:
[ OK ] GMockOutputTest.ExplicitActionsRunOutWithDefaultAction [ OK ] GMockOutputTest.ExplicitActionsRunOutWithDefaultAction
[ RUN ] GMockOutputTest.CatchesLeakedMocks [ RUN ] GMockOutputTest.CatchesLeakedMocks
[ OK ] GMockOutputTest.CatchesLeakedMocks [ OK ] GMockOutputTest.CatchesLeakedMocks
[ RUN ] GMockOutputTest.PrintsMatcher
FILE:#: Failure
Value of: (std::pair<int, bool>(42, true))
Expected: is pair (is >= 48, true)
Actual: (42, true) (of type std::pair<int, bool>)
[ FAILED ] GMockOutputTest.PrintsMatcher
[ FAILED ] GMockOutputTest.UnexpectedCall [ FAILED ] GMockOutputTest.UnexpectedCall
[ FAILED ] GMockOutputTest.UnexpectedCallToVoidFunction [ FAILED ] GMockOutputTest.UnexpectedCallToVoidFunction
[ FAILED ] GMockOutputTest.ExcessiveCall [ FAILED ] GMockOutputTest.ExcessiveCall
...@@ -302,9 +308,10 @@ Stack trace: ...@@ -302,9 +308,10 @@ Stack trace:
[ FAILED ] GMockOutputTest.MismatchArgumentsAndWith [ FAILED ] GMockOutputTest.MismatchArgumentsAndWith
[ FAILED ] GMockOutputTest.UnexpectedCallWithDefaultAction [ FAILED ] GMockOutputTest.UnexpectedCallWithDefaultAction
[ FAILED ] GMockOutputTest.ExcessiveCallWithDefaultAction [ FAILED ] GMockOutputTest.ExcessiveCallWithDefaultAction
[ FAILED ] GMockOutputTest.PrintsMatcher
FILE:#: ERROR: this mock object should be deleted but never is. Its address is @0x#. FILE:#: ERROR: this mock object should be deleted but never is. Its address is @0x#.
FILE:#: ERROR: this mock object should be deleted but never is. Its address is @0x#. FILE:#: ERROR: this mock object should be deleted but never is. Its address is @0x#.
FILE:#: ERROR: this mock object should be deleted but never is. Its address is @0x#. FILE:#: ERROR: this mock object should be deleted but never is. Its address is @0x#.
ERROR: 3 leaked mock objects found at program exit. ERROR: 3 leaked mock objects found at program exit. Expectations on a mock object is verified when the object is destructed. Leaking a mock means that its expectations aren't verified, which is usually a test bug. If you really intend to leak a mock, you can suppress this error using testing::Mock::AllowLeak(mock_object), or you may use a fake or stub instead of a mock.
...@@ -38,7 +38,7 @@ ...@@ -38,7 +38,7 @@
namespace testing { namespace testing {
namespace { namespace {
// From <gtest/internal/gtest-port.h>. // From gtest-port.h.
using ::testing::internal::ThreadWithParam; using ::testing::internal::ThreadWithParam;
// The maximum number of test threads (not including helper threads) // The maximum number of test threads (not including helper threads)
...@@ -51,7 +51,7 @@ const int kRepeat = 50; ...@@ -51,7 +51,7 @@ const int kRepeat = 50;
class MockFoo { class MockFoo {
public: public:
MOCK_METHOD1(Bar, int(int n)); // NOLINT MOCK_METHOD1(Bar, int(int n)); // NOLINT
MOCK_METHOD2(Baz, char(const char* s1, const internal::string& s2)); // NOLINT MOCK_METHOD2(Baz, char(const char* s1, const std::string& s2)); // NOLINT
}; };
// Helper for waiting for the given thread to finish and then deleting it. // Helper for waiting for the given thread to finish and then deleting it.
......
...@@ -37,9 +37,11 @@ ...@@ -37,9 +37,11 @@
#include <string> #include <string>
#include "gtest/gtest.h" #include "gtest/gtest.h"
#include "gtest/internal/custom/gtest.h"
#if !defined(GTEST_CUSTOM_INIT_GOOGLE_TEST_FUNCTION_) #if !defined(GTEST_CUSTOM_INIT_GOOGLE_TEST_FUNCTION_)
using testing::GMOCK_FLAG(default_mock_behavior);
using testing::GMOCK_FLAG(verbose); using testing::GMOCK_FLAG(verbose);
using testing::InitGoogleMock; using testing::InitGoogleMock;
...@@ -50,9 +52,9 @@ void TestInitGoogleMock(const Char* (&argv)[M], const Char* (&new_argv)[N], ...@@ -50,9 +52,9 @@ void TestInitGoogleMock(const Char* (&argv)[M], const Char* (&new_argv)[N],
const ::std::string& expected_gmock_verbose) { const ::std::string& expected_gmock_verbose) {
const ::std::string old_verbose = GMOCK_FLAG(verbose); const ::std::string old_verbose = GMOCK_FLAG(verbose);
int argc = M; int argc = M - 1;
InitGoogleMock(&argc, const_cast<Char**>(argv)); InitGoogleMock(&argc, const_cast<Char**>(argv));
ASSERT_EQ(N, argc) << "The new argv has wrong number of elements."; ASSERT_EQ(N - 1, argc) << "The new argv has wrong number of elements.";
for (int i = 0; i < N; i++) { for (int i = 0; i < N; i++) {
EXPECT_STREQ(new_argv[i], argv[i]); EXPECT_STREQ(new_argv[i], argv[i]);
...@@ -103,6 +105,26 @@ TEST(InitGoogleMockTest, ParsesSingleFlag) { ...@@ -103,6 +105,26 @@ TEST(InitGoogleMockTest, ParsesSingleFlag) {
TestInitGoogleMock(argv, new_argv, "info"); TestInitGoogleMock(argv, new_argv, "info");
} }
TEST(InitGoogleMockTest, ParsesMultipleFlags) {
int old_default_behavior = GMOCK_FLAG(default_mock_behavior);
const wchar_t* argv[] = {
L"foo.exe",
L"--gmock_verbose=info",
L"--gmock_default_mock_behavior=2",
NULL
};
const wchar_t* new_argv[] = {
L"foo.exe",
NULL
};
TestInitGoogleMock(argv, new_argv, "info");
EXPECT_EQ(2, GMOCK_FLAG(default_mock_behavior));
EXPECT_NE(2, old_default_behavior);
GMOCK_FLAG(default_mock_behavior) = old_default_behavior;
}
TEST(InitGoogleMockTest, ParsesUnrecognizedFlag) { TEST(InitGoogleMockTest, ParsesUnrecognizedFlag) {
const char* argv[] = { const char* argv[] = {
"foo.exe", "foo.exe",
...@@ -177,6 +199,26 @@ TEST(WideInitGoogleMockTest, ParsesSingleFlag) { ...@@ -177,6 +199,26 @@ TEST(WideInitGoogleMockTest, ParsesSingleFlag) {
TestInitGoogleMock(argv, new_argv, "info"); TestInitGoogleMock(argv, new_argv, "info");
} }
TEST(WideInitGoogleMockTest, ParsesMultipleFlags) {
int old_default_behavior = GMOCK_FLAG(default_mock_behavior);
const wchar_t* argv[] = {
L"foo.exe",
L"--gmock_verbose=info",
L"--gmock_default_mock_behavior=2",
NULL
};
const wchar_t* new_argv[] = {
L"foo.exe",
NULL
};
TestInitGoogleMock(argv, new_argv, "info");
EXPECT_EQ(2, GMOCK_FLAG(default_mock_behavior));
EXPECT_NE(2, old_default_behavior);
GMOCK_FLAG(default_mock_behavior) = old_default_behavior;
}
TEST(WideInitGoogleMockTest, ParsesUnrecognizedFlag) { TEST(WideInitGoogleMockTest, ParsesUnrecognizedFlag) {
const wchar_t* argv[] = { const wchar_t* argv[] = {
L"foo.exe", L"foo.exe",
......
#!/usr/bin/env python
#
# Copyright 2006, Google Inc. # Copyright 2006, Google Inc.
# All rights reserved. # All rights reserved.
# #
...@@ -36,19 +34,19 @@ __author__ = 'wan@google.com (Zhanyong Wan)' ...@@ -36,19 +34,19 @@ __author__ = 'wan@google.com (Zhanyong Wan)'
import os import os
import sys import sys
# Determines path to gtest_test_utils and imports it. # Determines path to gtest_test_utils and imports it.
SCRIPT_DIR = os.path.dirname(__file__) or '.' SCRIPT_DIR = os.path.dirname(__file__) or '.'
# isdir resolves symbolic links. # isdir resolves symbolic links.
gtest_tests_util_dir = os.path.join(SCRIPT_DIR, '../gtest/test') gtest_tests_util_dir = os.path.join(SCRIPT_DIR, '../../googletest/test')
if os.path.isdir(gtest_tests_util_dir): if os.path.isdir(gtest_tests_util_dir):
GTEST_TESTS_UTIL_DIR = gtest_tests_util_dir GTEST_TESTS_UTIL_DIR = gtest_tests_util_dir
else: else:
GTEST_TESTS_UTIL_DIR = os.path.join(SCRIPT_DIR, '../../gtest/test') GTEST_TESTS_UTIL_DIR = os.path.join(SCRIPT_DIR, '../../googletest/test')
sys.path.append(GTEST_TESTS_UTIL_DIR) sys.path.append(GTEST_TESTS_UTIL_DIR)
import gtest_test_utils # pylint: disable-msg=C6204
# pylint: disable=C6204
import gtest_test_utils
def GetSourceDir(): def GetSourceDir():
......
...@@ -44,8 +44,17 @@ endif() ...@@ -44,8 +44,17 @@ endif()
# as ${gtest_SOURCE_DIR} and to the root binary directory as # as ${gtest_SOURCE_DIR} and to the root binary directory as
# ${gtest_BINARY_DIR}. # ${gtest_BINARY_DIR}.
# Language "C" is required for find_package(Threads). # Language "C" is required for find_package(Threads).
project(gtest CXX C) if (CMAKE_VERSION VERSION_LESS 3.0)
cmake_minimum_required(VERSION 2.6.2) project(gtest CXX C)
else()
cmake_policy(SET CMP0048 NEW)
project(gtest VERSION 1.9.0 LANGUAGES CXX C)
endif()
cmake_minimum_required(VERSION 2.6.4)
if (POLICY CMP0063) # Visibility
cmake_policy(SET CMP0063 NEW)
endif (POLICY CMP0063)
if (COMMAND set_up_hermetic_build) if (COMMAND set_up_hermetic_build)
set_up_hermetic_build() set_up_hermetic_build()
...@@ -63,11 +72,8 @@ config_compiler_and_linker() # Defined in internal_utils.cmake. ...@@ -63,11 +72,8 @@ config_compiler_and_linker() # Defined in internal_utils.cmake.
# Where Google Test's .h files can be found. # Where Google Test's .h files can be found.
include_directories( include_directories(
${gtest_SOURCE_DIR}/include "${gtest_SOURCE_DIR}/include"
${gtest_SOURCE_DIR}) "${gtest_SOURCE_DIR}")
# Where Google Test's libraries can be found.
link_directories(${gtest_BINARY_DIR}/src)
# Summary of tuple support for Microsoft Visual Studio: # Summary of tuple support for Microsoft Visual Studio:
# Compiler version(MS) version(cmake) Support # Compiler version(MS) version(cmake) Support
...@@ -75,6 +81,8 @@ link_directories(${gtest_BINARY_DIR}/src) ...@@ -75,6 +81,8 @@ link_directories(${gtest_BINARY_DIR}/src)
# <= VS 2010 <= 10 <= 1600 Use Google Tests's own tuple. # <= VS 2010 <= 10 <= 1600 Use Google Tests's own tuple.
# VS 2012 11 1700 std::tr1::tuple + _VARIADIC_MAX=10 # VS 2012 11 1700 std::tr1::tuple + _VARIADIC_MAX=10
# VS 2013 12 1800 std::tr1::tuple # VS 2013 12 1800 std::tr1::tuple
# VS 2015 14 1900 std::tuple
# VS 2017 15 >= 1910 std::tuple
if (MSVC AND MSVC_VERSION EQUAL 1700) if (MSVC AND MSVC_VERSION EQUAL 1700)
add_definitions(/D _VARIADIC_MAX=10) add_definitions(/D _VARIADIC_MAX=10)
endif() endif()
...@@ -95,17 +103,33 @@ target_link_libraries(gtest_main gtest) ...@@ -95,17 +103,33 @@ target_link_libraries(gtest_main gtest)
# to the targets for when we are part of a parent build (ie being pulled # to the targets for when we are part of a parent build (ie being pulled
# in via add_subdirectory() rather than being a standalone build). # in via add_subdirectory() rather than being a standalone build).
if (DEFINED CMAKE_VERSION AND NOT "${CMAKE_VERSION}" VERSION_LESS "2.8.11") if (DEFINED CMAKE_VERSION AND NOT "${CMAKE_VERSION}" VERSION_LESS "2.8.11")
target_include_directories(gtest INTERFACE "${gtest_SOURCE_DIR}/include") target_include_directories(gtest SYSTEM INTERFACE "${gtest_SOURCE_DIR}/include")
target_include_directories(gtest_main INTERFACE "${gtest_SOURCE_DIR}/include") target_include_directories(gtest_main SYSTEM INTERFACE "${gtest_SOURCE_DIR}/include")
endif() endif()
######################################################################## ########################################################################
# #
# Install rules # Install rules
install(TARGETS gtest gtest_main if(INSTALL_GTEST)
DESTINATION lib) install(TARGETS gtest gtest_main
install(DIRECTORY ${gtest_SOURCE_DIR}/include/gtest RUNTIME DESTINATION "${CMAKE_INSTALL_BINDIR}"
DESTINATION include) ARCHIVE DESTINATION "${CMAKE_INSTALL_LIBDIR}"
LIBRARY DESTINATION "${CMAKE_INSTALL_LIBDIR}")
install(DIRECTORY "${gtest_SOURCE_DIR}/include/gtest"
DESTINATION "${CMAKE_INSTALL_INCLUDEDIR}")
# configure and install pkgconfig files
configure_file(
cmake/gtest.pc.in
"${CMAKE_BINARY_DIR}/gtest.pc"
@ONLY)
configure_file(
cmake/gtest_main.pc.in
"${CMAKE_BINARY_DIR}/gtest_main.pc"
@ONLY)
install(FILES "${CMAKE_BINARY_DIR}/gtest.pc" "${CMAKE_BINARY_DIR}/gtest_main.pc"
DESTINATION "${CMAKE_INSTALL_LIBDIR}/pkgconfig")
endif()
######################################################################## ########################################################################
# #
...@@ -147,28 +171,28 @@ if (gtest_build_tests) ...@@ -147,28 +171,28 @@ if (gtest_build_tests)
############################################################ ############################################################
# C++ tests built with standard compiler flags. # C++ tests built with standard compiler flags.
cxx_test(gtest-death-test_test gtest_main) cxx_test(googletest-death-test-test gtest_main)
cxx_test(gtest_environment_test gtest) cxx_test(gtest_environment_test gtest)
cxx_test(gtest-filepath_test gtest_main) cxx_test(googletest-filepath-test gtest_main)
cxx_test(gtest-linked_ptr_test gtest_main) cxx_test(googletest-linked-ptr-test gtest_main)
cxx_test(gtest-listener_test gtest_main) cxx_test(googletest-listener-test gtest_main)
cxx_test(gtest_main_unittest gtest_main) cxx_test(gtest_main_unittest gtest_main)
cxx_test(gtest-message_test gtest_main) cxx_test(googletest-message-test gtest_main)
cxx_test(gtest_no_test_unittest gtest) cxx_test(gtest_no_test_unittest gtest)
cxx_test(gtest-options_test gtest_main) cxx_test(googletest-options-test gtest_main)
cxx_test(gtest-param-test_test gtest cxx_test(googletest-param-test-test gtest
test/gtest-param-test2_test.cc) test/googletest-param-test2-test.cc)
cxx_test(gtest-port_test gtest_main) cxx_test(googletest-port-test gtest_main)
cxx_test(gtest_pred_impl_unittest gtest_main) cxx_test(gtest_pred_impl_unittest gtest_main)
cxx_test(gtest_premature_exit_test gtest cxx_test(gtest_premature_exit_test gtest
test/gtest_premature_exit_test.cc) test/gtest_premature_exit_test.cc)
cxx_test(gtest-printers_test gtest_main) cxx_test(googletest-printers-test gtest_main)
cxx_test(gtest_prod_test gtest_main cxx_test(gtest_prod_test gtest_main
test/production.cc) test/production.cc)
cxx_test(gtest_repeat_test gtest) cxx_test(gtest_repeat_test gtest)
cxx_test(gtest_sole_header_test gtest_main) cxx_test(gtest_sole_header_test gtest_main)
cxx_test(gtest_stress_test gtest) cxx_test(gtest_stress_test gtest)
cxx_test(gtest-test-part_test gtest_main) cxx_test(googletest-test-part-test gtest_main)
cxx_test(gtest_throw_on_failure_ex_test gtest) cxx_test(gtest_throw_on_failure_ex_test gtest)
cxx_test(gtest-typed-test_test gtest_main cxx_test(gtest-typed-test_test gtest_main
test/gtest-typed-test2_test.cc) test/gtest-typed-test2_test.cc)
...@@ -190,10 +214,10 @@ if (gtest_build_tests) ...@@ -190,10 +214,10 @@ if (gtest_build_tests)
cxx_test_with_flags(gtest-death-test_ex_nocatch_test cxx_test_with_flags(gtest-death-test_ex_nocatch_test
"${cxx_exception} -DGTEST_ENABLE_CATCH_EXCEPTIONS_=0" "${cxx_exception} -DGTEST_ENABLE_CATCH_EXCEPTIONS_=0"
gtest test/gtest-death-test_ex_test.cc) gtest test/googletest-death-test_ex_test.cc)
cxx_test_with_flags(gtest-death-test_ex_catch_test cxx_test_with_flags(gtest-death-test_ex_catch_test
"${cxx_exception} -DGTEST_ENABLE_CATCH_EXCEPTIONS_=1" "${cxx_exception} -DGTEST_ENABLE_CATCH_EXCEPTIONS_=1"
gtest test/gtest-death-test_ex_test.cc) gtest test/googletest-death-test_ex_test.cc)
cxx_test_with_flags(gtest_no_rtti_unittest "${cxx_no_rtti}" cxx_test_with_flags(gtest_no_rtti_unittest "${cxx_no_rtti}"
gtest_main_no_rtti test/gtest_unittest.cc) gtest_main_no_rtti test/gtest_unittest.cc)
...@@ -214,73 +238,75 @@ if (gtest_build_tests) ...@@ -214,73 +238,75 @@ if (gtest_build_tests)
cxx_library(gtest_main_use_own_tuple "${cxx_use_own_tuple}" cxx_library(gtest_main_use_own_tuple "${cxx_use_own_tuple}"
src/gtest-all.cc src/gtest_main.cc) src/gtest-all.cc src/gtest_main.cc)
cxx_test_with_flags(gtest-tuple_test "${cxx_use_own_tuple}" cxx_test_with_flags(googletest-tuple-test "${cxx_use_own_tuple}"
gtest_main_use_own_tuple test/gtest-tuple_test.cc) gtest_main_use_own_tuple test/googletest-tuple-test.cc)
cxx_test_with_flags(gtest_use_own_tuple_test "${cxx_use_own_tuple}" cxx_test_with_flags(gtest_use_own_tuple_test "${cxx_use_own_tuple}"
gtest_main_use_own_tuple gtest_main_use_own_tuple
test/gtest-param-test_test.cc test/gtest-param-test2_test.cc) test/googletest-param-test-test.cc test/googletest-param-test2-test.cc)
endif() endif()
############################################################ ############################################################
# Python tests. # Python tests.
cxx_executable(gtest_break_on_failure_unittest_ test gtest) cxx_executable(googletest-break-on-failure-unittest_ test gtest)
py_test(gtest_break_on_failure_unittest) py_test(googletest-break-on-failure-unittest)
# Visual Studio .NET 2003 does not support STL with exceptions disabled. # Visual Studio .NET 2003 does not support STL with exceptions disabled.
if (NOT MSVC OR MSVC_VERSION GREATER 1310) # 1310 is Visual Studio .NET 2003 if (NOT MSVC OR MSVC_VERSION GREATER 1310) # 1310 is Visual Studio .NET 2003
cxx_executable_with_flags( cxx_executable_with_flags(
gtest_catch_exceptions_no_ex_test_ googletest-catch-exceptions-no-ex-test_
"${cxx_no_exception}" "${cxx_no_exception}"
gtest_main_no_exception gtest_main_no_exception
test/gtest_catch_exceptions_test_.cc) test/googletest-catch-exceptions-test_.cc)
endif() endif()
cxx_executable_with_flags( cxx_executable_with_flags(
gtest_catch_exceptions_ex_test_ googletest-catch-exceptions-ex-test_
"${cxx_exception}" "${cxx_exception}"
gtest_main gtest_main
test/gtest_catch_exceptions_test_.cc) test/googletest-catch-exceptions-test_.cc)
py_test(gtest_catch_exceptions_test) py_test(googletest-catch-exceptions-test)
cxx_executable(gtest_color_test_ test gtest) cxx_executable(googletest-color-test_ test gtest)
py_test(gtest_color_test) py_test(googletest-color-test)
cxx_executable(gtest_env_var_test_ test gtest) cxx_executable(googletest-env-var-test_ test gtest)
py_test(gtest_env_var_test) py_test(googletest-env-var-test)
cxx_executable(gtest_filter_unittest_ test gtest) cxx_executable(googletest-filter-unittest_ test gtest)
py_test(gtest_filter_unittest) py_test(googletest-filter-unittest)
cxx_executable(gtest_help_test_ test gtest_main) cxx_executable(gtest_help_test_ test gtest_main)
py_test(gtest_help_test) py_test(gtest_help_test)
cxx_executable(gtest_list_tests_unittest_ test gtest) cxx_executable(googletest-list-tests-unittest_ test gtest)
py_test(gtest_list_tests_unittest) py_test(googletest-list-tests-unittest)
cxx_executable(gtest_output_test_ test gtest) cxx_executable(googletest-output-test_ test gtest)
py_test(gtest_output_test) py_test(googletest-output-test --no_stacktrace_support)
cxx_executable(gtest_shuffle_test_ test gtest) cxx_executable(googletest-shuffle-test_ test gtest)
py_test(gtest_shuffle_test) py_test(googletest-shuffle-test)
# MSVC 7.1 does not support STL with exceptions disabled. # MSVC 7.1 does not support STL with exceptions disabled.
if (NOT MSVC OR MSVC_VERSION GREATER 1310) if (NOT MSVC OR MSVC_VERSION GREATER 1310)
cxx_executable(gtest_throw_on_failure_test_ test gtest_no_exception) cxx_executable(googletest-throw-on-failure-test_ test gtest_no_exception)
set_target_properties(gtest_throw_on_failure_test_ set_target_properties(googletest-throw-on-failure-test_
PROPERTIES PROPERTIES
COMPILE_FLAGS "${cxx_no_exception}") COMPILE_FLAGS "${cxx_no_exception}")
py_test(gtest_throw_on_failure_test) py_test(googletest-throw-on-failure-test)
endif() endif()
cxx_executable(gtest_uninitialized_test_ test gtest) cxx_executable(googletest-uninitialized-test_ test gtest)
py_test(gtest_uninitialized_test) py_test(googletest-uninitialized-test)
cxx_executable(gtest_xml_outfile1_test_ test gtest_main) cxx_executable(gtest_xml_outfile1_test_ test gtest_main)
cxx_executable(gtest_xml_outfile2_test_ test gtest_main) cxx_executable(gtest_xml_outfile2_test_ test gtest_main)
py_test(gtest_xml_outfiles_test) py_test(gtest_xml_outfiles_test)
py_test(googletest-json-outfiles-test)
cxx_executable(gtest_xml_output_unittest_ test gtest) cxx_executable(gtest_xml_output_unittest_ test gtest)
py_test(gtest_xml_output_unittest) py_test(gtest_xml_output_unittest --no_stacktrace_support)
py_test(googletest-json-output-unittest --no_stacktrace_support)
endif() endif()
...@@ -34,6 +34,7 @@ EXTRA_DIST += $(GTEST_SRC) ...@@ -34,6 +34,7 @@ EXTRA_DIST += $(GTEST_SRC)
# Sample files that we don't compile. # Sample files that we don't compile.
EXTRA_DIST += \ EXTRA_DIST += \
samples/prime_tables.h \ samples/prime_tables.h \
samples/sample1_unittest.cc \
samples/sample2_unittest.cc \ samples/sample2_unittest.cc \
samples/sample3_unittest.cc \ samples/sample3_unittest.cc \
samples/sample4_unittest.cc \ samples/sample4_unittest.cc \
...@@ -52,40 +53,40 @@ EXTRA_DIST += \ ...@@ -52,40 +53,40 @@ EXTRA_DIST += \
test/gtest-listener_test.cc \ test/gtest-listener_test.cc \
test/gtest-message_test.cc \ test/gtest-message_test.cc \
test/gtest-options_test.cc \ test/gtest-options_test.cc \
test/gtest-param-test2_test.cc \ test/googletest-param-test2-test.cc \
test/gtest-param-test2_test.cc \ test/googletest-param-test2-test.cc \
test/gtest-param-test_test.cc \ test/googletest-param-test-test.cc \
test/gtest-param-test_test.cc \ test/googletest-param-test-test.cc \
test/gtest-param-test_test.h \ test/gtest-param-test_test.h \
test/gtest-port_test.cc \ test/gtest-port_test.cc \
test/gtest_premature_exit_test.cc \ test/gtest_premature_exit_test.cc \
test/gtest-printers_test.cc \ test/gtest-printers_test.cc \
test/gtest-test-part_test.cc \ test/gtest-test-part_test.cc \
test/gtest-tuple_test.cc \ test/googletest-tuple-test.cc \
test/gtest-typed-test2_test.cc \ test/gtest-typed-test2_test.cc \
test/gtest-typed-test_test.cc \ test/gtest-typed-test_test.cc \
test/gtest-typed-test_test.h \ test/gtest-typed-test_test.h \
test/gtest-unittest-api_test.cc \ test/gtest-unittest-api_test.cc \
test/gtest_break_on_failure_unittest_.cc \ test/googletest-break-on-failure-unittest_.cc \
test/gtest_catch_exceptions_test_.cc \ test/googletest-catch-exceptions-test_.cc \
test/gtest_color_test_.cc \ test/googletest-color-test_.cc \
test/gtest_env_var_test_.cc \ test/googletest-env-var-test_.cc \
test/gtest_environment_test.cc \ test/gtest_environment_test.cc \
test/gtest_filter_unittest_.cc \ test/googletest-filter-unittest_.cc \
test/gtest_help_test_.cc \ test/gtest_help_test_.cc \
test/gtest_list_tests_unittest_.cc \ test/googletest-list-tests-unittest_.cc \
test/gtest_main_unittest.cc \ test/gtest_main_unittest.cc \
test/gtest_no_test_unittest.cc \ test/gtest_no_test_unittest.cc \
test/gtest_output_test_.cc \ test/googletest-output-test_.cc \
test/gtest_pred_impl_unittest.cc \ test/gtest_pred_impl_unittest.cc \
test/gtest_prod_test.cc \ test/gtest_prod_test.cc \
test/gtest_repeat_test.cc \ test/gtest_repeat_test.cc \
test/gtest_shuffle_test_.cc \ test/googletest-shuffle-test_.cc \
test/gtest_sole_header_test.cc \ test/gtest_sole_header_test.cc \
test/gtest_stress_test.cc \ test/gtest_stress_test.cc \
test/gtest_throw_on_failure_ex_test.cc \ test/gtest_throw_on_failure_ex_test.cc \
test/gtest_throw_on_failure_test_.cc \ test/googletest-throw-on-failure-test_.cc \
test/gtest_uninitialized_test_.cc \ test/googletest-uninitialized-test_.cc \
test/gtest_unittest.cc \ test/gtest_unittest.cc \
test/gtest_unittest.cc \ test/gtest_unittest.cc \
test/gtest_xml_outfile1_test_.cc \ test/gtest_xml_outfile1_test_.cc \
...@@ -96,19 +97,19 @@ EXTRA_DIST += \ ...@@ -96,19 +97,19 @@ EXTRA_DIST += \
# Python tests that we don't run. # Python tests that we don't run.
EXTRA_DIST += \ EXTRA_DIST += \
test/gtest_break_on_failure_unittest.py \ test/googletest-break-on-failure-unittest.py \
test/gtest_catch_exceptions_test.py \ test/googletest-catch-exceptions-test.py \
test/gtest_color_test.py \ test/googletest-color-test.py \
test/gtest_env_var_test.py \ test/googletest-env-var-test.py \
test/gtest_filter_unittest.py \ test/googletest-filter-unittest.py \
test/gtest_help_test.py \ test/gtest_help_test.py \
test/gtest_list_tests_unittest.py \ test/googletest-list-tests-unittest.py \
test/gtest_output_test.py \ test/googletest-output-test.py \
test/gtest_output_test_golden_lin.txt \ test/googletest-output-test_golden_lin.txt \
test/gtest_shuffle_test.py \ test/googletest-shuffle-test.py \
test/gtest_test_utils.py \ test/gtest_test_utils.py \
test/gtest_throw_on_failure_test.py \ test/googletest-throw-on-failure-test.py \
test/gtest_uninitialized_test.py \ test/googletest-uninitialized-test.py \
test/gtest_xml_outfiles_test.py \ test/gtest_xml_outfiles_test.py \
test/gtest_xml_output_unittest.py \ test/gtest_xml_output_unittest.py \
test/gtest_xml_test_utils.py test/gtest_xml_test_utils.py
...@@ -120,16 +121,16 @@ EXTRA_DIST += \ ...@@ -120,16 +121,16 @@ EXTRA_DIST += \
# MSVC project files # MSVC project files
EXTRA_DIST += \ EXTRA_DIST += \
msvc/gtest-md.sln \ msvc/2010/gtest-md.sln \
msvc/gtest-md.vcproj \ msvc/2010/gtest-md.vcxproj \
msvc/gtest.sln \ msvc/2010/gtest.sln \
msvc/gtest.vcproj \ msvc/2010/gtest.vcxproj \
msvc/gtest_main-md.vcproj \ msvc/2010/gtest_main-md.vcxproj \
msvc/gtest_main.vcproj \ msvc/2010/gtest_main.vcxproj \
msvc/gtest_prod_test-md.vcproj \ msvc/2010/gtest_prod_test-md.vcxproj \
msvc/gtest_prod_test.vcproj \ msvc/2010/gtest_prod_test.vcxproj \
msvc/gtest_unittest-md.vcproj \ msvc/2010/gtest_unittest-md.vcxproj \
msvc/gtest_unittest.vcproj msvc/2010/gtest_unittest.vcxproj
# xcode project files # xcode project files
EXTRA_DIST += \ EXTRA_DIST += \
...@@ -216,40 +217,68 @@ pkginclude_internal_HEADERS = \ ...@@ -216,40 +217,68 @@ pkginclude_internal_HEADERS = \
lib_libgtest_main_la_SOURCES = src/gtest_main.cc lib_libgtest_main_la_SOURCES = src/gtest_main.cc
lib_libgtest_main_la_LIBADD = lib/libgtest.la lib_libgtest_main_la_LIBADD = lib/libgtest.la
# Bulid rules for samples and tests. Automake's naming for some of # Build rules for samples and tests. Automake's naming for some of
# these variables isn't terribly obvious, so this is a brief # these variables isn't terribly obvious, so this is a brief
# reference: # reference:
# #
# TESTS -- Programs run automatically by "make check" # TESTS -- Programs run automatically by "make check"
# check_PROGRAMS -- Programs built by "make check" but not necessarily run # check_PROGRAMS -- Programs built by "make check" but not necessarily run
noinst_LTLIBRARIES = samples/libsamples.la
samples_libsamples_la_SOURCES = \
samples/sample1.cc \
samples/sample1.h \
samples/sample2.cc \
samples/sample2.h \
samples/sample3-inl.h \
samples/sample4.cc \
samples/sample4.h
TESTS= TESTS=
TESTS_ENVIRONMENT = GTEST_SOURCE_DIR="$(srcdir)/test" \ TESTS_ENVIRONMENT = GTEST_SOURCE_DIR="$(srcdir)/test" \
GTEST_BUILD_DIR="$(top_builddir)/test" GTEST_BUILD_DIR="$(top_builddir)/test"
check_PROGRAMS= check_PROGRAMS=
# A simple sample on using gtest. # A simple sample on using gtest.
TESTS += samples/sample1_unittest TESTS += samples/sample1_unittest \
check_PROGRAMS += samples/sample1_unittest samples/sample2_unittest \
samples_sample1_unittest_SOURCES = samples/sample1_unittest.cc samples/sample3_unittest \
samples/sample4_unittest \
samples/sample5_unittest \
samples/sample6_unittest \
samples/sample7_unittest \
samples/sample8_unittest \
samples/sample9_unittest \
samples/sample10_unittest
check_PROGRAMS += samples/sample1_unittest \
samples/sample2_unittest \
samples/sample3_unittest \
samples/sample4_unittest \
samples/sample5_unittest \
samples/sample6_unittest \
samples/sample7_unittest \
samples/sample8_unittest \
samples/sample9_unittest \
samples/sample10_unittest
samples_sample1_unittest_SOURCES = samples/sample1_unittest.cc samples/sample1.cc
samples_sample1_unittest_LDADD = lib/libgtest_main.la \ samples_sample1_unittest_LDADD = lib/libgtest_main.la \
lib/libgtest.la \ lib/libgtest.la
samples/libsamples.la samples_sample2_unittest_SOURCES = samples/sample2_unittest.cc samples/sample2.cc
samples_sample2_unittest_LDADD = lib/libgtest_main.la \
# Another sample. It also verifies that libgtest works. lib/libgtest.la
TESTS += samples/sample10_unittest samples_sample3_unittest_SOURCES = samples/sample3_unittest.cc
check_PROGRAMS += samples/sample10_unittest samples_sample3_unittest_LDADD = lib/libgtest_main.la \
lib/libgtest.la
samples_sample4_unittest_SOURCES = samples/sample4_unittest.cc samples/sample4.cc
samples_sample4_unittest_LDADD = lib/libgtest_main.la \
lib/libgtest.la
samples_sample5_unittest_SOURCES = samples/sample5_unittest.cc samples/sample1.cc
samples_sample5_unittest_LDADD = lib/libgtest_main.la \
lib/libgtest.la
samples_sample6_unittest_SOURCES = samples/sample6_unittest.cc
samples_sample6_unittest_LDADD = lib/libgtest_main.la \
lib/libgtest.la
samples_sample7_unittest_SOURCES = samples/sample7_unittest.cc
samples_sample7_unittest_LDADD = lib/libgtest_main.la \
lib/libgtest.la
samples_sample8_unittest_SOURCES = samples/sample8_unittest.cc
samples_sample8_unittest_LDADD = lib/libgtest_main.la \
lib/libgtest.la
# Also verify that libgtest works by itself.
samples_sample9_unittest_SOURCES = samples/sample9_unittest.cc
samples_sample9_unittest_LDADD = lib/libgtest.la
samples_sample10_unittest_SOURCES = samples/sample10_unittest.cc samples_sample10_unittest_SOURCES = samples/sample10_unittest.cc
samples_sample10_unittest_LDADD = lib/libgtest.la samples_sample10_unittest_LDADD = lib/libgtest.la
......
### Generic Build Instructions
### Generic Build Instructions ### #### Setup
#### Setup #### To build Google Test and your tests that use it, you need to tell your build
system where to find its headers and source files. The exact way to do it
depends on which build system you use, and is usually straightforward.
To build Google Test and your tests that use it, you need to tell your #### Build
build system where to find its headers and source files. The exact
way to do it depends on which build system you use, and is usually
straightforward.
#### Build #### Suppose you put Google Test in directory `${GTEST_DIR}`. To build it, create a
library build target (or a project as called by Visual Studio and Xcode) to
Suppose you put Google Test in directory `${GTEST_DIR}`. To build it, compile
create a library build target (or a project as called by Visual Studio
and Xcode) to compile
${GTEST_DIR}/src/gtest-all.cc ${GTEST_DIR}/src/gtest-all.cc
with `${GTEST_DIR}/include` in the system header search path and `${GTEST_DIR}` with `${GTEST_DIR}/include` in the system header search path and `${GTEST_DIR}`
in the normal header search path. Assuming a Linux-like system and gcc, in the normal header search path. Assuming a Linux-like system and gcc,
something like the following will do: something like the following will do:
g++ -isystem ${GTEST_DIR}/include -I${GTEST_DIR} \ g++ -isystem ${GTEST_DIR}/include -I${GTEST_DIR} \
...@@ -26,136 +24,239 @@ something like the following will do: ...@@ -26,136 +24,239 @@ something like the following will do:
(We need `-pthread` as Google Test uses threads.) (We need `-pthread` as Google Test uses threads.)
Next, you should compile your test source file with Next, you should compile your test source file with `${GTEST_DIR}/include` in
`${GTEST_DIR}/include` in the system header search path, and link it the system header search path, and link it with gtest and any other necessary
with gtest and any other necessary libraries: libraries:
g++ -isystem ${GTEST_DIR}/include -pthread path/to/your_test.cc libgtest.a \ g++ -isystem ${GTEST_DIR}/include -pthread path/to/your_test.cc libgtest.a \
-o your_test -o your_test
As an example, the make/ directory contains a Makefile that you can As an example, the make/ directory contains a Makefile that you can use to build
use to build Google Test on systems where GNU make is available Google Test on systems where GNU make is available (e.g. Linux, Mac OS X, and
(e.g. Linux, Mac OS X, and Cygwin). It doesn't try to build Google Cygwin). It doesn't try to build Google Test's own tests. Instead, it just
Test's own tests. Instead, it just builds the Google Test library and builds the Google Test library and a sample test. You can use it as a starting
a sample test. You can use it as a starting point for your own build point for your own build script.
script.
If the default settings are correct for your environment, the If the default settings are correct for your environment, the following commands
following commands should succeed: should succeed:
cd ${GTEST_DIR}/make cd ${GTEST_DIR}/make
make make
./sample1_unittest ./sample1_unittest
If you see errors, try to tweak the contents of `make/Makefile` to make If you see errors, try to tweak the contents of `make/Makefile` to make them go
them go away. There are instructions in `make/Makefile` on how to do away. There are instructions in `make/Makefile` on how to do it.
it.
### Using CMake ### ### Using CMake
Google Test comes with a CMake build script ( Google Test comes with a CMake build script (
[CMakeLists.txt](CMakeLists.txt)) that can be used on a wide range of platforms ("C" stands for [CMakeLists.txt](https://github.com/google/googletest/blob/master/CMakeLists.txt))
cross-platform.). If you don't have CMake installed already, you can that can be used on a wide range of platforms ("C" stands for cross-platform.).
download it for free from <http://www.cmake.org/>. If you don't have CMake installed already, you can download it for free from
<http://www.cmake.org/>.
CMake works by generating native makefiles or build projects that can be used in
the compiler environment of your choice. You can either build Google Test as a
standalone project or it can be incorporated into an existing CMake build for
another project.
CMake works by generating native makefiles or build projects that can #### Standalone CMake Project
be used in the compiler environment of your choice. The typical
workflow starts with: When building Google Test as a standalone project, the typical workflow starts
with:
mkdir mybuild # Create a directory to hold the build output. mkdir mybuild # Create a directory to hold the build output.
cd mybuild cd mybuild
cmake ${GTEST_DIR} # Generate native build scripts. cmake ${GTEST_DIR} # Generate native build scripts.
If you want to build Google Test's samples, you should replace the If you want to build Google Test's samples, you should replace the last command
last command with with
cmake -Dgtest_build_samples=ON ${GTEST_DIR} cmake -Dgtest_build_samples=ON ${GTEST_DIR}
If you are on a \*nix system, you should now see a Makefile in the If you are on a \*nix system, you should now see a Makefile in the current
current directory. Just type 'make' to build gtest. directory. Just type 'make' to build gtest.
If you use Windows and have Visual Studio installed, a `gtest.sln` file If you use Windows and have Visual Studio installed, a `gtest.sln` file and
and several `.vcproj` files will be created. You can then build them several `.vcproj` files will be created. You can then build them using Visual
using Visual Studio. Studio.
On Mac OS X with Xcode installed, a `.xcodeproj` file will be generated. On Mac OS X with Xcode installed, a `.xcodeproj` file will be generated.
### Legacy Build Scripts ### #### Incorporating Into An Existing CMake Project
If you want to use gtest in a project which already uses CMake, then a more
robust and flexible approach is to build gtest as part of that project directly.
This is done by making the GoogleTest source code available to the main build
and adding it using CMake's `add_subdirectory()` command. This has the
significant advantage that the same compiler and linker settings are used
between gtest and the rest of your project, so issues associated with using
incompatible libraries (eg debug/release), etc. are avoided. This is
particularly useful on Windows. Making GoogleTest's source code available to the
main build can be done a few different ways:
* Download the GoogleTest source code manually and place it at a known
location. This is the least flexible approach and can make it more difficult
to use with continuous integration systems, etc.
* Embed the GoogleTest source code as a direct copy in the main project's
source tree. This is often the simplest approach, but is also the hardest to
keep up to date. Some organizations may not permit this method.
* Add GoogleTest as a git submodule or equivalent. This may not always be
possible or appropriate. Git submodules, for example, have their own set of
advantages and drawbacks.
* Use CMake to download GoogleTest as part of the build's configure step. This
is just a little more complex, but doesn't have the limitations of the other
methods.
The last of the above methods is implemented with a small piece of CMake code in
a separate file (e.g. `CMakeLists.txt.in`) which is copied to the build area and
then invoked as a sub-build _during the CMake stage_. That directory is then
pulled into the main build with `add_subdirectory()`. For example:
New file `CMakeLists.txt.in`:
cmake_minimum_required(VERSION 2.8.2)
project(googletest-download NONE)
include(ExternalProject)
ExternalProject_Add(googletest
GIT_REPOSITORY https://github.com/google/googletest.git
GIT_TAG master
SOURCE_DIR "${CMAKE_BINARY_DIR}/googletest-src"
BINARY_DIR "${CMAKE_BINARY_DIR}/googletest-build"
CONFIGURE_COMMAND ""
BUILD_COMMAND ""
INSTALL_COMMAND ""
TEST_COMMAND ""
)
Existing build's `CMakeLists.txt`:
# Download and unpack googletest at configure time
configure_file(CMakeLists.txt.in googletest-download/CMakeLists.txt)
execute_process(COMMAND ${CMAKE_COMMAND} -G "${CMAKE_GENERATOR}" .
RESULT_VARIABLE result
WORKING_DIRECTORY ${CMAKE_BINARY_DIR}/googletest-download )
if(result)
message(FATAL_ERROR "CMake step for googletest failed: ${result}")
endif()
execute_process(COMMAND ${CMAKE_COMMAND} --build .
RESULT_VARIABLE result
WORKING_DIRECTORY ${CMAKE_BINARY_DIR}/googletest-download )
if(result)
message(FATAL_ERROR "Build step for googletest failed: ${result}")
endif()
# Prevent overriding the parent project's compiler/linker
# settings on Windows
set(gtest_force_shared_crt ON CACHE BOOL "" FORCE)
# Add googletest directly to our build. This defines
# the gtest and gtest_main targets.
add_subdirectory(${CMAKE_BINARY_DIR}/googletest-src
${CMAKE_BINARY_DIR}/googletest-build
EXCLUDE_FROM_ALL)
# The gtest/gtest_main targets carry header search path
# dependencies automatically when using CMake 2.8.11 or
# later. Otherwise we have to add them here ourselves.
if (CMAKE_VERSION VERSION_LESS 2.8.11)
include_directories("${gtest_SOURCE_DIR}/include")
endif()
# Now simply link against gtest or gtest_main as needed. Eg
add_executable(example example.cpp)
target_link_libraries(example gtest_main)
add_test(NAME example_test COMMAND example)
Note that this approach requires CMake 2.8.2 or later due to its use of the
`ExternalProject_Add()` command. The above technique is discussed in more detail
in [this separate article](http://crascit.com/2015/07/25/cmake-gtest/) which
also contains a link to a fully generalized implementation of the technique.
##### Visual Studio Dynamic vs Static Runtimes
By default, new Visual Studio projects link the C runtimes dynamically but
Google Test links them statically. This will generate an error that looks
something like the following: gtest.lib(gtest-all.obj) : error LNK2038: mismatch
detected for 'RuntimeLibrary': value 'MTd_StaticDebug' doesn't match value
'MDd_DynamicDebug' in main.obj
Google Test already has a CMake option for this: `gtest_force_shared_crt`
Enabling this option will make gtest link the runtimes dynamically too, and
match the project in which it is included.
### Legacy Build Scripts
Before settling on CMake, we have been providing hand-maintained build Before settling on CMake, we have been providing hand-maintained build
projects/scripts for Visual Studio, Xcode, and Autotools. While we projects/scripts for Visual Studio, Xcode, and Autotools. While we continue to
continue to provide them for convenience, they are not actively provide them for convenience, they are not actively maintained any more. We
maintained any more. We highly recommend that you follow the highly recommend that you follow the instructions in the above sections to
instructions in the previous two sections to integrate Google Test integrate Google Test with your existing build system.
with your existing build system.
If you still need to use the legacy build scripts, here's how: If you still need to use the legacy build scripts, here's how:
The msvc\ folder contains two solutions with Visual C++ projects. The msvc\ folder contains two solutions with Visual C++ projects. Open the
Open the `gtest.sln` or `gtest-md.sln` file using Visual Studio, and you `gtest.sln` or `gtest-md.sln` file using Visual Studio, and you are ready to
are ready to build Google Test the same way you build any Visual build Google Test the same way you build any Visual Studio project. Files that
Studio project. Files that have names ending with -md use DLL have names ending with -md use DLL versions of Microsoft runtime libraries (the
versions of Microsoft runtime libraries (the /MD or the /MDd compiler /MD or the /MDd compiler option). Files without that suffix use static versions
option). Files without that suffix use static versions of the runtime of the runtime libraries (the /MT or the /MTd option). Please note that one must
libraries (the /MT or the /MTd option). Please note that one must use use the same option to compile both gtest and the test code. If you use Visual
the same option to compile both gtest and the test code. If you use Studio 2005 or above, we recommend the -md version as /MD is the default for new
Visual Studio 2005 or above, we recommend the -md version as /MD is projects in these versions of Visual Studio.
the default for new projects in these versions of Visual Studio.
On Mac OS X, open the `gtest.xcodeproj` in the `xcode/` folder using Xcode.
On Mac OS X, open the `gtest.xcodeproj` in the `xcode/` folder using Build the "gtest" target. The universal binary framework will end up in your
Xcode. Build the "gtest" target. The universal binary framework will selected build directory (selected in the Xcode "Preferences..." -> "Building"
end up in your selected build directory (selected in the Xcode pane and defaults to xcode/build). Alternatively, at the command line, enter:
"Preferences..." -> "Building" pane and defaults to xcode/build).
Alternatively, at the command line, enter:
xcodebuild xcodebuild
This will build the "Release" configuration of gtest.framework in your This will build the "Release" configuration of gtest.framework in your default
default build location. See the "xcodebuild" man page for more build location. See the "xcodebuild" man page for more information about
information about building different configurations and building in building different configurations and building in different locations.
different locations.
If you wish to use the Google Test Xcode project with Xcode 4.x and If you wish to use the Google Test Xcode project with Xcode 4.x and above, you
above, you need to either: need to either:
* update the SDK configuration options in xcode/Config/General.xconfig. * update the SDK configuration options in xcode/Config/General.xconfig.
Comment options `SDKROOT`, `MACOS_DEPLOYMENT_TARGET`, and `GCC_VERSION`. If Comment options `SDKROOT`, `MACOS_DEPLOYMENT_TARGET`, and `GCC_VERSION`. If
you choose this route you lose the ability to target earlier versions you choose this route you lose the ability to target earlier versions of
of MacOS X. MacOS X.
* Install an SDK for an earlier version. This doesn't appear to be * Install an SDK for an earlier version. This doesn't appear to be supported
supported by Apple, but has been reported to work by Apple, but has been reported to work
(http://stackoverflow.com/questions/5378518). (http://stackoverflow.com/questions/5378518).
### Tweaking Google Test ### ### Tweaking Google Test
Google Test can be used in diverse environments. The default Google Test can be used in diverse environments. The default configuration may
configuration may not work (or may not work well) out of the box in not work (or may not work well) out of the box in some environments. However,
some environments. However, you can easily tweak Google Test by you can easily tweak Google Test by defining control macros on the compiler
defining control macros on the compiler command line. Generally, command line. Generally, these macros are named like `GTEST_XYZ` and you define
these macros are named like `GTEST_XYZ` and you define them to either 1 them to either 1 or 0 to enable or disable a certain feature.
or 0 to enable or disable a certain feature.
We list the most frequently used macros below. For a complete list, We list the most frequently used macros below. For a complete list, see file
see file [include/gtest/internal/gtest-port.h](include/gtest/internal/gtest-port.h). [include/gtest/internal/gtest-port.h](https://github.com/google/googletest/blob/master/include/gtest/internal/gtest-port.h).
### Choosing a TR1 Tuple Library ### ### Choosing a TR1 Tuple Library
Some Google Test features require the C++ Technical Report 1 (TR1) Some Google Test features require the C++ Technical Report 1 (TR1) tuple
tuple library, which is not yet available with all compilers. The library, which is not yet available with all compilers. The good news is that
good news is that Google Test implements a subset of TR1 tuple that's Google Test implements a subset of TR1 tuple that's enough for its own need, and
enough for its own need, and will automatically use this when the will automatically use this when the compiler doesn't provide TR1 tuple.
compiler doesn't provide TR1 tuple.
Usually you don't need to care about which tuple library Google Test Usually you don't need to care about which tuple library Google Test uses.
uses. However, if your project already uses TR1 tuple, you need to However, if your project already uses TR1 tuple, you need to tell Google Test to
tell Google Test to use the same TR1 tuple library the rest of your use the same TR1 tuple library the rest of your project uses, or the two tuple
project uses, or the two tuple implementations will clash. To do implementations will clash. To do that, add
that, add
-DGTEST_USE_OWN_TR1_TUPLE=0 -DGTEST_USE_OWN_TR1_TUPLE=0
to the compiler flags while compiling Google Test and your tests. If to the compiler flags while compiling Google Test and your tests. If you want to
you want to force Google Test to use its own tuple library, just add force Google Test to use its own tuple library, just add
-DGTEST_USE_OWN_TR1_TUPLE=1 -DGTEST_USE_OWN_TR1_TUPLE=1
...@@ -167,15 +268,15 @@ If you don't want Google Test to use tuple at all, add ...@@ -167,15 +268,15 @@ If you don't want Google Test to use tuple at all, add
and all features using tuple will be disabled. and all features using tuple will be disabled.
### Multi-threaded Tests ### ### Multi-threaded Tests
Google Test is thread-safe where the pthread library is available. Google Test is thread-safe where the pthread library is available. After
After `#include "gtest/gtest.h"`, you can check the `GTEST_IS_THREADSAFE` `#include "gtest/gtest.h"`, you can check the `GTEST_IS_THREADSAFE` macro to see
macro to see whether this is the case (yes if the macro is `#defined` to whether this is the case (yes if the macro is `#defined` to 1, no if it's
1, no if it's undefined.). undefined.).
If Google Test doesn't correctly detect whether pthread is available If Google Test doesn't correctly detect whether pthread is available in your
in your environment, you can force it with environment, you can force it with
-DGTEST_HAS_PTHREAD=1 -DGTEST_HAS_PTHREAD=1
...@@ -183,26 +284,24 @@ or ...@@ -183,26 +284,24 @@ or
-DGTEST_HAS_PTHREAD=0 -DGTEST_HAS_PTHREAD=0
When Google Test uses pthread, you may need to add flags to your When Google Test uses pthread, you may need to add flags to your compiler and/or
compiler and/or linker to select the pthread library, or you'll get linker to select the pthread library, or you'll get link errors. If you use the
link errors. If you use the CMake script or the deprecated Autotools CMake script or the deprecated Autotools script, this is taken care of for you.
script, this is taken care of for you. If you use your own build If you use your own build script, you'll need to read your compiler and linker's
script, you'll need to read your compiler and linker's manual to manual to figure out what flags to add.
figure out what flags to add.
### As a Shared Library (DLL) ### ### As a Shared Library (DLL)
Google Test is compact, so most users can build and link it as a Google Test is compact, so most users can build and link it as a static library
static library for the simplicity. You can choose to use Google Test for the simplicity. You can choose to use Google Test as a shared library (known
as a shared library (known as a DLL on Windows) if you prefer. as a DLL on Windows) if you prefer.
To compile *gtest* as a shared library, add To compile *gtest* as a shared library, add
-DGTEST_CREATE_SHARED_LIBRARY=1 -DGTEST_CREATE_SHARED_LIBRARY=1
to the compiler flags. You'll also need to tell the linker to produce to the compiler flags. You'll also need to tell the linker to produce a shared
a shared library instead - consult your linker's manual for how to do library instead - consult your linker's manual for how to do it.
it.
To compile your *tests* that use the gtest shared library, add To compile your *tests* that use the gtest shared library, add
...@@ -210,31 +309,28 @@ To compile your *tests* that use the gtest shared library, add ...@@ -210,31 +309,28 @@ To compile your *tests* that use the gtest shared library, add
to the compiler flags. to the compiler flags.
Note: while the above steps aren't technically necessary today when Note: while the above steps aren't technically necessary today when using some
using some compilers (e.g. GCC), they may become necessary in the compilers (e.g. GCC), they may become necessary in the future, if we decide to
future, if we decide to improve the speed of loading the library (see improve the speed of loading the library (see
<http://gcc.gnu.org/wiki/Visibility> for details). Therefore you are <http://gcc.gnu.org/wiki/Visibility> for details). Therefore you are recommended
recommended to always add the above flags when using Google Test as a to always add the above flags when using Google Test as a shared library.
shared library. Otherwise a future release of Google Test may break Otherwise a future release of Google Test may break your build script.
your build script.
### Avoiding Macro Name Clashes ### ### Avoiding Macro Name Clashes
In C++, macros don't obey namespaces. Therefore two libraries that In C++, macros don't obey namespaces. Therefore two libraries that both define a
both define a macro of the same name will clash if you `#include` both macro of the same name will clash if you `#include` both definitions. In case a
definitions. In case a Google Test macro clashes with another Google Test macro clashes with another library, you can force Google Test to
library, you can force Google Test to rename its macro to avoid the rename its macro to avoid the conflict.
conflict.
Specifically, if both Google Test and some other code define macro Specifically, if both Google Test and some other code define macro FOO, you can
FOO, you can add add
-DGTEST_DONT_DEFINE_FOO=1 -DGTEST_DONT_DEFINE_FOO=1
to the compiler flags to tell Google Test to change the macro's name to the compiler flags to tell Google Test to change the macro's name from `FOO`
from `FOO` to `GTEST_FOO`. Currently `FOO` can be `FAIL`, `SUCCEED`, to `GTEST_FOO`. Currently `FOO` can be `FAIL`, `SUCCEED`, or `TEST`. For
or `TEST`. For example, with `-DGTEST_DONT_DEFINE_TEST=1`, you'll example, with `-DGTEST_DONT_DEFINE_TEST=1`, you'll need to write
need to write
GTEST_TEST(SomeTest, DoesThis) { ... } GTEST_TEST(SomeTest, DoesThis) { ... }
...@@ -243,38 +339,3 @@ instead of ...@@ -243,38 +339,3 @@ instead of
TEST(SomeTest, DoesThis) { ... } TEST(SomeTest, DoesThis) { ... }
in order to define a test. in order to define a test.
## Developing Google Test ##
This section discusses how to make your own changes to Google Test.
### Testing Google Test Itself ###
To make sure your changes work as intended and don't break existing
functionality, you'll want to compile and run Google Test's own tests.
For that you can use CMake:
mkdir mybuild
cd mybuild
cmake -Dgtest_build_tests=ON ${GTEST_DIR}
Make sure you have Python installed, as some of Google Test's tests
are written in Python. If the cmake command complains about not being
able to find Python (`Could NOT find PythonInterp (missing:
PYTHON_EXECUTABLE)`), try telling it explicitly where your Python
executable can be found:
cmake -DPYTHON_EXECUTABLE=path/to/python -Dgtest_build_tests=ON ${GTEST_DIR}
Next, you can build Google Test and all of its own tests. On \*nix,
this is usually done by 'make'. To run the tests, do
make test
All tests should pass.
Normally you don't need to worry about regenerating the source files,
unless you need to modify them. In that case, you should modify the
corresponding .pump files instead and run the pump.py Python script to
regenerate them. You can find pump.py in the [scripts/](scripts/) directory.
Read the [Pump manual](docs/PumpManual.md) for how to use it.
libdir=@CMAKE_INSTALL_FULL_LIBDIR@
includedir=@CMAKE_INSTALL_FULL_INCLUDEDIR@
Name: gtest
Description: GoogleTest (without main() function)
Version: @PROJECT_VERSION@
URL: https://github.com/google/googletest
Libs: -L${libdir} -lgtest @CMAKE_THREAD_LIBS_INIT@
Cflags: -I${includedir} @GTEST_HAS_PTHREAD_MACRO@ @CMAKE_THREAD_LIBS_INIT@
libdir=@CMAKE_INSTALL_FULL_LIBDIR@
includedir=@CMAKE_INSTALL_FULL_INCLUDEDIR@
Name: gtest_main
Description: GoogleTest (with main() function)
Version: @PROJECT_VERSION@
URL: https://github.com/google/googletest
Requires: gtest
Libs: -L${libdir} -lgtest_main @CMAKE_THREAD_LIBS_INIT@
Cflags: -I${includedir} @GTEST_HAS_PTHREAD_MACRO@ @CMAKE_THREAD_LIBS_INIT@
...@@ -20,7 +20,7 @@ macro(fix_default_compiler_settings_) ...@@ -20,7 +20,7 @@ macro(fix_default_compiler_settings_)
if (MSVC) if (MSVC)
# For MSVC, CMake sets certain flags to defaults we want to override. # For MSVC, CMake sets certain flags to defaults we want to override.
# This replacement code is taken from sample in the CMake Wiki at # This replacement code is taken from sample in the CMake Wiki at
# http://www.cmake.org/Wiki/CMake_FAQ#Dynamic_Replace. # https://gitlab.kitware.com/cmake/community/wikis/FAQ#dynamic-replace.
foreach (flag_var foreach (flag_var
CMAKE_CXX_FLAGS CMAKE_CXX_FLAGS_DEBUG CMAKE_CXX_FLAGS_RELEASE CMAKE_CXX_FLAGS CMAKE_CXX_FLAGS_DEBUG CMAKE_CXX_FLAGS_RELEASE
CMAKE_CXX_FLAGS_MINSIZEREL CMAKE_CXX_FLAGS_RELWITHDEBINFO) CMAKE_CXX_FLAGS_MINSIZEREL CMAKE_CXX_FLAGS_RELWITHDEBINFO)
...@@ -48,9 +48,14 @@ endmacro() ...@@ -48,9 +48,14 @@ endmacro()
macro(config_compiler_and_linker) macro(config_compiler_and_linker)
# Note: pthreads on MinGW is not supported, even if available # Note: pthreads on MinGW is not supported, even if available
# instead, we use windows threading primitives # instead, we use windows threading primitives
unset(GTEST_HAS_PTHREAD)
if (NOT gtest_disable_pthreads AND NOT MINGW) if (NOT gtest_disable_pthreads AND NOT MINGW)
# Defines CMAKE_USE_PTHREADS_INIT and CMAKE_THREAD_LIBS_INIT. # Defines CMAKE_USE_PTHREADS_INIT and CMAKE_THREAD_LIBS_INIT.
set(THREADS_PREFER_PTHREAD_FLAG ON)
find_package(Threads) find_package(Threads)
if (CMAKE_USE_PTHREADS_INIT)
set(GTEST_HAS_PTHREAD ON)
endif()
endif() endif()
fix_default_compiler_settings_() fix_default_compiler_settings_()
...@@ -82,18 +87,17 @@ macro(config_compiler_and_linker) ...@@ -82,18 +87,17 @@ macro(config_compiler_and_linker)
# http://stackoverflow.com/questions/3232669 explains the issue. # http://stackoverflow.com/questions/3232669 explains the issue.
set(cxx_base_flags "${cxx_base_flags} -wd4702") set(cxx_base_flags "${cxx_base_flags} -wd4702")
endif() endif()
if (NOT (MSVC_VERSION GREATER 1900)) # 1900 is Visual Studio 2015
# BigObj required for tests.
set(cxx_base_flags "${cxx_base_flags} -bigobj")
endif()
set(cxx_base_flags "${cxx_base_flags} -D_UNICODE -DUNICODE -DWIN32 -D_WIN32") set(cxx_base_flags "${cxx_base_flags} -D_UNICODE -DUNICODE -DWIN32 -D_WIN32")
set(cxx_base_flags "${cxx_base_flags} -DSTRICT -DWIN32_LEAN_AND_MEAN") set(cxx_base_flags "${cxx_base_flags} -DSTRICT -DWIN32_LEAN_AND_MEAN")
set(cxx_exception_flags "-EHsc -D_HAS_EXCEPTIONS=1") set(cxx_exception_flags "-EHsc -D_HAS_EXCEPTIONS=1")
set(cxx_no_exception_flags "-D_HAS_EXCEPTIONS=0") set(cxx_no_exception_flags "-EHs-c- -D_HAS_EXCEPTIONS=0")
set(cxx_no_rtti_flags "-GR-") set(cxx_no_rtti_flags "-GR-")
elseif (CMAKE_COMPILER_IS_GNUCXX) elseif (CMAKE_COMPILER_IS_GNUCXX)
set(cxx_base_flags "-Wall -Wshadow") set(cxx_base_flags "-Wall -Wshadow -Werror")
if(NOT CMAKE_CXX_COMPILER_VERSION VERSION_LESS 7.0.0)
set(cxx_base_flags "${cxx_base_flags} -Wno-error=dangling-else")
endif()
set(cxx_exception_flags "-fexceptions") set(cxx_exception_flags "-fexceptions")
set(cxx_no_exception_flags "-fno-exceptions") set(cxx_no_exception_flags "-fno-exceptions")
# Until version 4.3.2, GCC doesn't define a macro to indicate # Until version 4.3.2, GCC doesn't define a macro to indicate
...@@ -125,11 +129,13 @@ macro(config_compiler_and_linker) ...@@ -125,11 +129,13 @@ macro(config_compiler_and_linker)
set(cxx_no_rtti_flags "") set(cxx_no_rtti_flags "")
endif() endif()
if (CMAKE_USE_PTHREADS_INIT) # The pthreads library is available and allowed. # The pthreads library is available and allowed?
set(cxx_base_flags "${cxx_base_flags} -DGTEST_HAS_PTHREAD=1") if (DEFINED GTEST_HAS_PTHREAD)
set(GTEST_HAS_PTHREAD_MACRO "-DGTEST_HAS_PTHREAD=1")
else() else()
set(cxx_base_flags "${cxx_base_flags} -DGTEST_HAS_PTHREAD=0") set(GTEST_HAS_PTHREAD_MACRO "-DGTEST_HAS_PTHREAD=0")
endif() endif()
set(cxx_base_flags "${cxx_base_flags} ${GTEST_HAS_PTHREAD_MACRO}")
# For building gtest's own tests and samples. # For building gtest's own tests and samples.
set(cxx_exception "${CMAKE_CXX_FLAGS} ${cxx_base_flags} ${cxx_exception_flags}") set(cxx_exception "${CMAKE_CXX_FLAGS} ${cxx_base_flags} ${cxx_exception_flags}")
...@@ -152,12 +158,16 @@ function(cxx_library_with_type name type cxx_flags) ...@@ -152,12 +158,16 @@ function(cxx_library_with_type name type cxx_flags)
set_target_properties(${name} set_target_properties(${name}
PROPERTIES PROPERTIES
COMPILE_FLAGS "${cxx_flags}") COMPILE_FLAGS "${cxx_flags}")
# Generate debug library name with a postfix.
set_target_properties(${name}
PROPERTIES
DEBUG_POSTFIX "d")
if (BUILD_SHARED_LIBS OR type STREQUAL "SHARED") if (BUILD_SHARED_LIBS OR type STREQUAL "SHARED")
set_target_properties(${name} set_target_properties(${name}
PROPERTIES PROPERTIES
COMPILE_DEFINITIONS "GTEST_CREATE_SHARED_LIBRARY=1") COMPILE_DEFINITIONS "GTEST_CREATE_SHARED_LIBRARY=1")
endif() endif()
if (CMAKE_USE_PTHREADS_INIT) if (DEFINED GTEST_HAS_PTHREAD)
target_link_libraries(${name} ${CMAKE_THREAD_LIBS_INIT}) target_link_libraries(${name} ${CMAKE_THREAD_LIBS_INIT})
endif() endif()
endfunction() endfunction()
...@@ -180,6 +190,10 @@ endfunction() ...@@ -180,6 +190,10 @@ endfunction()
# is built from the given source files with the given compiler flags. # is built from the given source files with the given compiler flags.
function(cxx_executable_with_flags name cxx_flags libs) function(cxx_executable_with_flags name cxx_flags libs)
add_executable(${name} ${ARGN}) add_executable(${name} ${ARGN})
if (MSVC AND (NOT (MSVC_VERSION LESS 1700))) # 1700 is Visual Studio 2012.
# BigObj required for tests.
set(cxx_flags "${cxx_flags} -bigobj")
endif()
if (cxx_flags) if (cxx_flags)
set_target_properties(${name} set_target_properties(${name}
PROPERTIES PROPERTIES
...@@ -234,23 +248,33 @@ endfunction() ...@@ -234,23 +248,33 @@ endfunction()
# creates a Python test with the given name whose main module is in # creates a Python test with the given name whose main module is in
# test/name.py. It does nothing if Python is not installed. # test/name.py. It does nothing if Python is not installed.
function(py_test name) function(py_test name)
# We are not supporting Python tests on Linux yet as they consider
# all Linux environments to be google3 and try to use google3 features.
if (PYTHONINTERP_FOUND) if (PYTHONINTERP_FOUND)
# ${CMAKE_BINARY_DIR} is known at configuration time, so we can
# directly bind it from cmake. ${CTEST_CONFIGURATION_TYPE} is known
# only at ctest runtime (by calling ctest -c <Configuration>), so
# we have to escape $ to delay variable substitution here.
if (${CMAKE_MAJOR_VERSION}.${CMAKE_MINOR_VERSION} GREATER 3.1) if (${CMAKE_MAJOR_VERSION}.${CMAKE_MINOR_VERSION} GREATER 3.1)
add_test( if (CMAKE_CONFIGURATION_TYPES)
NAME ${name} # Multi-configuration build generators as for Visual Studio save
COMMAND ${PYTHON_EXECUTABLE} ${CMAKE_CURRENT_SOURCE_DIR}/test/${name}.py # output in a subdirectory of CMAKE_CURRENT_BINARY_DIR (Debug,
--build_dir=${CMAKE_CURRENT_BINARY_DIR}/$<CONFIGURATION>) # Release etc.), so we have to provide it here.
add_test(
NAME ${name}
COMMAND ${PYTHON_EXECUTABLE} ${CMAKE_CURRENT_SOURCE_DIR}/test/${name}.py
--build_dir=${CMAKE_CURRENT_BINARY_DIR}/$<CONFIG> ${ARGN})
else (CMAKE_CONFIGURATION_TYPES)
# Single-configuration build generators like Makefile generators
# don't have subdirs below CMAKE_CURRENT_BINARY_DIR.
add_test(
NAME ${name}
COMMAND ${PYTHON_EXECUTABLE} ${CMAKE_CURRENT_SOURCE_DIR}/test/${name}.py
--build_dir=${CMAKE_CURRENT_BINARY_DIR} ${ARGN})
endif (CMAKE_CONFIGURATION_TYPES)
else (${CMAKE_MAJOR_VERSION}.${CMAKE_MINOR_VERSION} GREATER 3.1) else (${CMAKE_MAJOR_VERSION}.${CMAKE_MINOR_VERSION} GREATER 3.1)
# ${CMAKE_CURRENT_BINARY_DIR} is known at configuration time, so we can
# directly bind it from cmake. ${CTEST_CONFIGURATION_TYPE} is known
# only at ctest runtime (by calling ctest -c <Configuration>), so
# we have to escape $ to delay variable substitution here.
add_test( add_test(
${name} ${name}
${PYTHON_EXECUTABLE} ${CMAKE_CURRENT_SOURCE_DIR}/test/${name}.py ${PYTHON_EXECUTABLE} ${CMAKE_CURRENT_SOURCE_DIR}/test/${name}.py
--build_dir=${CMAKE_CURRENT_BINARY_DIR}/\${CTEST_CONFIGURATION_TYPE}) --build_dir=${CMAKE_CURRENT_BINARY_DIR}/\${CTEST_CONFIGURATION_TYPE} ${ARGN})
endif (${CMAKE_MAJOR_VERSION}.${CMAKE_MINOR_VERSION} GREATER 3.1) endif (${CMAKE_MAJOR_VERSION}.${CMAKE_MINOR_VERSION} GREATER 3.1)
endif() endif(PYTHONINTERP_FOUND)
endfunction() endfunction()
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