Skip to content

Commit

Permalink
Export Test - Do Not Merge
Browse files Browse the repository at this point in the history
Merge 3bdefdb into fb49e6c

Closes #2407

COPYBARA_INTEGRATE_REVIEW=#2407 from kuzkry:StaticAssertTypeEq 3bdefdb
PiperOrigin-RevId: 269255328
  • Loading branch information
kuzkry authored and shaindelschwartz committed Sep 16, 2019
1 parent cad3bc4 commit 81ecf77
Show file tree
Hide file tree
Showing 7 changed files with 26 additions and 38 deletions.
24 changes: 12 additions & 12 deletions googlemock/include/gmock/gmock-matchers.h
Original file line number Diff line number Diff line change
Expand Up @@ -2068,15 +2068,15 @@ class ContainerEqMatcher {
typedef typename View::type StlContainer;
typedef typename View::const_reference StlContainerReference;

static_assert(!std::is_const<Container>::value,
"Container type must not be const");
static_assert(!std::is_reference<Container>::value,
"Container type must not be a reference");

// We make a copy of expected in case the elements in it are modified
// after this matcher is created.
explicit ContainerEqMatcher(const Container& expected)
: expected_(View::Copy(expected)) {
// Makes sure the user doesn't instantiate this class template
// with a const or reference type.
(void)testing::StaticAssertTypeEq<Container,
GTEST_REMOVE_REFERENCE_AND_CONST_(Container)>();
}
: expected_(View::Copy(expected)) {}

void DescribeTo(::std::ostream* os) const {
*os << "equals ";
Expand Down Expand Up @@ -2243,15 +2243,15 @@ class PointwiseMatcher {
typedef typename RhsView::type RhsStlContainer;
typedef typename RhsStlContainer::value_type RhsValue;

static_assert(!std::is_const<RhsContainer>::value,
"RhsContainer type must not be const");
static_assert(!std::is_reference<RhsContainer>::value,
"RhsContainer type must not be a reference");

// Like ContainerEq, we make a copy of rhs in case the elements in
// it are modified after this matcher is created.
PointwiseMatcher(const TupleMatcher& tuple_matcher, const RhsContainer& rhs)
: tuple_matcher_(tuple_matcher), rhs_(RhsView::Copy(rhs)) {
// Makes sure the user doesn't instantiate this class template
// with a const or reference type.
(void)testing::StaticAssertTypeEq<RhsContainer,
GTEST_REMOVE_REFERENCE_AND_CONST_(RhsContainer)>();
}
: tuple_matcher_(tuple_matcher), rhs_(RhsView::Copy(rhs)) {}

template <typename LhsContainer>
operator Matcher<LhsContainer>() const {
Expand Down
9 changes: 4 additions & 5 deletions googlemock/include/gmock/internal/gmock-internal-utils.h
Original file line number Diff line number Diff line change
Expand Up @@ -384,9 +384,8 @@ class StlContainerView {
typedef const type& const_reference;

static const_reference ConstReference(const RawContainer& container) {
// Ensures that RawContainer is not a const type.
testing::StaticAssertTypeEq<
RawContainer, typename std::remove_const<RawContainer>::type>();
static_assert(!std::is_const<RawContainer>::value,
"RawContainer type must not be const");
return container;
}
static type Copy(const RawContainer& container) { return container; }
Expand All @@ -406,8 +405,8 @@ class StlContainerView<Element[N]> {
typedef const type const_reference;

static const_reference ConstReference(const Element (&array)[N]) {
// Ensures that Element is not a const type.
testing::StaticAssertTypeEq<Element, RawElement>();
static_assert(std::is_same<Element, RawElement>::value,
"Element type must not be const");
return type(array, N, RelationToSourceReference());
}
static type Copy(const Element (&array)[N]) {
Expand Down
1 change: 0 additions & 1 deletion googlemock/test/gmock-more-actions_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,6 @@ using testing::ReturnPointee;
using testing::SaveArg;
using testing::SaveArgPointee;
using testing::SetArgReferee;
using testing::StaticAssertTypeEq;
using testing::Unused;
using testing::WithArg;
using testing::WithoutArgs;
Expand Down
7 changes: 4 additions & 3 deletions googletest/docs/advanced.md
Original file line number Diff line number Diff line change
Expand Up @@ -464,9 +464,10 @@ You can call the function

to assert that types `T1` and `T2` are the same. The function does nothing if
the assertion is satisfied. If the types are different, the function call will
fail to compile, and the compiler error message will likely (depending on the
compiler) show you the actual values of `T1` and `T2`. This is mainly useful
inside template code.
fail to compile, the compiler error message will say that
`type1 and type2 are not the same type` and most likely (depending on the compiler)
show you the actual values of `T1` and `T2`. This is mainly useful inside
template code.

**Caveat**: When used inside a member function of a class template or a function
template, `StaticAssertTypeEq<T1, T2>()` is effective only if the function is
Expand Down
5 changes: 3 additions & 2 deletions googletest/include/gtest/gtest.h
Original file line number Diff line number Diff line change
Expand Up @@ -2297,8 +2297,9 @@ class GTEST_API_ ScopedTrace {
//
// to cause a compiler error.
template <typename T1, typename T2>
bool StaticAssertTypeEq() {
(void)internal::StaticAssertTypeEqHelper<T1, T2>();
constexpr bool StaticAssertTypeEq() noexcept {
static_assert(std::is_same<T1, T2>::value,
"type1 and type2 are not the same type");
return true;
}

Expand Down
7 changes: 3 additions & 4 deletions googletest/include/gtest/internal/gtest-internal.h
Original file line number Diff line number Diff line change
Expand Up @@ -1069,10 +1069,9 @@ class NativeArray {
}

private:
enum {
kCheckTypeIsNotConstOrAReference = StaticAssertTypeEqHelper<
Element, GTEST_REMOVE_REFERENCE_AND_CONST_(Element)>::value
};
static_assert(!std::is_const<Element>::value, "Type must not be const");
static_assert(!std::is_reference<Element>::value,
"Type must not be a reference");

// Initializes this object with a copy of the input.
void InitCopy(const Element* array, size_t a_size) {
Expand Down
11 changes: 0 additions & 11 deletions googletest/include/gtest/internal/gtest-port.h
Original file line number Diff line number Diff line change
Expand Up @@ -856,17 +856,6 @@ class Secret;
// expression is false, compiler will issue an error containing this identifier.
#define GTEST_COMPILE_ASSERT_(expr, msg) static_assert(expr, #msg)

// StaticAssertTypeEqHelper is used by StaticAssertTypeEq defined in gtest.h.
//
// This template is declared, but intentionally undefined.
template <typename T1, typename T2>
struct StaticAssertTypeEqHelper;

template <typename T>
struct StaticAssertTypeEqHelper<T, T> {
enum { value = true };
};

// Evaluates to the number of elements in 'array'.
#define GTEST_ARRAY_SIZE_(array) (sizeof(array) / sizeof(array[0]))

Expand Down

0 comments on commit 81ecf77

Please sign in to comment.