C++ 将google模拟匹配器描述为std::string
我的问题(针对住院患者) 给出一个谷歌模拟匹配器,我想用一个字符串来描述它。例如:C++ 将google模拟匹配器描述为std::string,c++,googlemock,C++,Googlemock,我的问题(针对住院患者) 给出一个谷歌模拟匹配器,我想用一个字符串来描述它。例如: std::string description = DescribeMatcher(Ge(0)) // puts "size is > 0" in the string 有人知道一个简单的方法吗?在googlemock文档中未找到任何内容。我自己是这样做的: template<typename T, typename S> std::string DescribeMatcher(S match
std::string description = DescribeMatcher(Ge(0)) // puts "size is > 0" in the string
有人知道一个简单的方法吗?在googlemock文档中未找到任何内容。我自己是这样做的:
template<typename T, typename S>
std::string DescribeMatcher(S matcher)
{
Matcher<T> matcherCast = matcher;
std::ostringstream os;
matcherCast.DescribeTo(&os);
return os.str();
}
以下是其用法示例:
EXPECT_THAT(someFileName, FileSizeIs(Ge(100)); // the size of the file is at-least 100 bytes
EXPECT_THAT(someFileName, FileSizeIs(AllOf(Ge(200), Le(1000)); // the size of the file is between 200 and 1000 bytes
问题出在MATCHER\u p宏的最后一个参数中。我希望
FileSizeIs
的描述基于sizeMatcher
的描述。但是,我在googlemock中没有找到任何这样的函数,我不得不自己编写。我在创建嵌套匹配器时也遇到过类似的问题。我的解决方案使用MatcherInterface而不是MATCHER\u P。对于您的情况,这将类似于:
template <typename InternalMatcher>
class FileSizeIsMatcher : public MatcherInterface<fs::path> {
public:
FileSizeIsMatcher(InternalMatcher internalMatcher)
: mInternalMatcher(SafeMatcherCast<std::uintmax_t>(internalMatcher))
{
}
bool MatchAndExplain(fs::path arg, MatchResultListener* listener) const override {
auto fileSize = fs::file_size(arg);
*listener << "whose size is " << PrintToString(fileSize) << " ";
return mInternalMatcher.MatchAndExplain(fileSize, listener);
}
void DescribeTo(std::ostream* os) const override {
*os << "file whose size ";
mInternalMatcher.DescribeTo(os);
}
void DescribeNegationTo(std::ostream* os) const override {
*os << "file whose size ";
mInternalMatcher.DescribeNegationTo(os);
}
private:
Matcher<std::uintmax_t> mInternalMatcher;
};
template <typename InternalMatcher>
Matcher<fs::path> FileSizeIs(InternalMatcher m) {
return MakeMatcher(new FileSizeIsMatcher<InternalMatcher>(m));
};
给出输出:
Value of: "myFile.txt"
Expected: file whose size is < 100
Actual: "myFile.txt" (of type char [11]), whose size is 123
“myFile.txt”的值
应为:大小小于100的文件
实际:“myFile.txt”(类型为char[11]),大小为123
TEST_F(DetectorPlacementControllerTests, TmpTest) {
EXPECT_THAT("myFile.txt", FileSizeIs(Lt(100ul)));
}
Value of: "myFile.txt"
Expected: file whose size is < 100
Actual: "myFile.txt" (of type char [11]), whose size is 123