Commit 747f6e3c authored by Nathan Bronson's avatar Nathan Bronson Committed by Facebook Github Bot

split JSON gmock helpers into a separate header

Summary:
This diff splits the gmock helpers out of JsonTestUtil.h into
JsonMockUtil.h so that JsonTestUtil.h doesn't introduce a dependency on
gmock that is problematic for some platforms.

(Note: this ignores all push blocking failures!)

Differential Revision: D13293728

fbshipit-source-id: 2a0e7f4a7c96fc05495b56fb02e848e92edc90d8
parent 60895e07
/*
* Copyright 2018-present Facebook, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#pragma once
#include <folly/portability/GMock.h>
#include <folly/test/JsonTestUtil.h>
namespace folly {
namespace detail {
template <typename T>
class JsonEqMatcher : public ::testing::MatcherInterface<T> {
public:
explicit JsonEqMatcher(std::string expected, std::string prefixBeforeJson)
: expected_(std::move(expected)),
prefixBeforeJson_(std::move(prefixBeforeJson)) {}
virtual bool MatchAndExplain(
T const& actual,
::testing::MatchResultListener* /*listener*/) const override {
StringPiece sp{actual};
if (!sp.startsWith(prefixBeforeJson_)) {
return false;
}
return compareJson(sp.subpiece(prefixBeforeJson_.size()), expected_);
}
virtual void DescribeTo(::std::ostream* os) const override {
*os << "JSON is equivalent to " << expected_;
if (prefixBeforeJson_.size() > 0) {
*os << " after removing prefix '" << prefixBeforeJson_ << "'";
}
}
virtual void DescribeNegationTo(::std::ostream* os) const override {
*os << "JSON is not equivalent to " << expected_;
if (prefixBeforeJson_.size() > 0) {
*os << " after removing prefix '" << prefixBeforeJson_ << "'";
}
}
private:
std::string expected_;
std::string prefixBeforeJson_;
};
} // namespace detail
/**
* GMock matcher that uses compareJson, expecting exactly a type T as an
* argument. Popular choices for T are std::string const&, StringPiece, and
* std::string. prefixBeforeJson should not be present in expected.
*/
template <typename T>
::testing::Matcher<T> JsonEq(
std::string expected,
std::string prefixBeforeJson = "") {
return ::testing::MakeMatcher(new detail::JsonEqMatcher<T>(
std::move(expected), std::move(prefixBeforeJson)));
}
} // namespace folly
...@@ -21,7 +21,6 @@ ...@@ -21,7 +21,6 @@
#include <folly/Range.h> #include <folly/Range.h>
#include <folly/dynamic.h> #include <folly/dynamic.h>
#include <folly/portability/GMock.h>
namespace folly { namespace folly {
...@@ -81,57 +80,6 @@ bool compareDynamicWithTolerance( ...@@ -81,57 +80,6 @@ bool compareDynamicWithTolerance(
const dynamic& obj2, const dynamic& obj2,
double tolerance); double tolerance);
namespace detail {
template <typename T>
class JsonEqMatcher : public ::testing::MatcherInterface<T> {
public:
explicit JsonEqMatcher(std::string expected, std::string prefixBeforeJson)
: expected_(std::move(expected)),
prefixBeforeJson_(std::move(prefixBeforeJson)) {}
virtual bool MatchAndExplain(
T const& actual,
::testing::MatchResultListener* /*listener*/) const override {
StringPiece sp{actual};
if (!sp.startsWith(prefixBeforeJson_)) {
return false;
}
return compareJson(sp.subpiece(prefixBeforeJson_.size()), expected_);
}
virtual void DescribeTo(::std::ostream* os) const override {
*os << "JSON is equivalent to " << expected_;
if (prefixBeforeJson_.size() > 0) {
*os << " after removing prefix '" << prefixBeforeJson_ << "'";
}
}
virtual void DescribeNegationTo(::std::ostream* os) const override {
*os << "JSON is not equivalent to " << expected_;
if (prefixBeforeJson_.size() > 0) {
*os << " after removing prefix '" << prefixBeforeJson_ << "'";
}
}
private:
std::string expected_;
std::string prefixBeforeJson_;
};
} // namespace detail
/**
* GMock matcher that uses compareJson, expecting exactly a type T as an
* argument. Popular choices for T are std::string const&, StringPiece, and
* std::string. prefixBeforeJson should not be present in expected.
*/
template <typename T>
::testing::Matcher<T> JsonEq(
std::string expected,
std::string prefixBeforeJson = "") {
return ::testing::MakeMatcher(new detail::JsonEqMatcher<T>(
std::move(expected), std::move(prefixBeforeJson)));
}
} // namespace folly } // namespace folly
/** /**
......
...@@ -16,7 +16,9 @@ ...@@ -16,7 +16,9 @@
#include <stdexcept> #include <stdexcept>
#include <folly/portability/GMock.h>
#include <folly/portability/GTest.h> #include <folly/portability/GTest.h>
#include <folly/test/JsonMockUtil.h>
#include <folly/test/JsonTestUtil.h> #include <folly/test/JsonTestUtil.h>
using namespace folly; using namespace folly;
......
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