Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/132.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 将google模拟匹配器描述为std::string_C++_Googlemock - Fatal编程技术网

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