forked from facebook/fbthrift
-
Notifications
You must be signed in to change notification settings - Fork 0
/
diff.h
95 lines (84 loc) · 2.25 KB
/
diff.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
/*
* Copyright (c) Meta Platforms, Inc. and affiliates.
*
* 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 <string>
#include <folly/Range.h>
#include <thrift/lib/cpp2/reflection/debug.h>
#include <thrift/lib/cpp2/reflection/pretty_print.h>
namespace apache {
namespace thrift {
/**
* A handy callback for `debug_equals()` that outputs a diff-like format to a
* given stream.
*
* See `make_diff_output_callback` for a convenient way to create an instance
* of this callback.
*/
template <typename Output>
struct diff_output_callback {
diff_output_callback(
Output& out, folly::StringPiece lhs, folly::StringPiece rhs)
: out_(out), lhs_(lhs), rhs_(rhs) {}
template <typename TC, typename T>
void operator()(
TC,
T const* lhs,
T const* rhs,
folly::StringPiece path,
folly::StringPiece) const {
out_ << path << ":\n";
if (lhs) {
pretty_print<TC>(out_, *lhs, " ", lhs_.str());
out_ << "\n";
}
if (rhs) {
pretty_print<TC>(out_, *rhs, " ", rhs_.str());
out_ << "\n";
}
}
private:
Output& out_;
folly::StringPiece lhs_;
folly::StringPiece rhs_;
};
/**
* A convenient way to create an instance of `diff_output_callback`.
*
* Example:
*
* bool const equals = debug_equals(
* lhs,
* rhs,
* make_diff_output_callback(std::cout)
* );
*
* EXPECT_TRUE(
* debug_equals(
* lhs,
* rhs,
* make_diff_output_callback(LOG(ERROR))
* )
* );
*/
template <typename Output>
diff_output_callback<Output> make_diff_output_callback(
Output& output,
folly::StringPiece lhs = "- ",
folly::StringPiece rhs = "+ ") {
return diff_output_callback<Output>(output, lhs, rhs);
}
} // namespace thrift
} // namespace apache