gpt4 book ai didi

c++ - RapidJSON::Value&的GoogleTest/Mock SaveArg

转载 作者:行者123 更新时间:2023-12-02 10:33:08 25 4
gpt4 key购买 nike

我正在尝试使用GoogleTest来获取传递给函数的参数。该函数的声明是这样的

void foo(rapidjson::Value &element, int number) {}

我会这样创建一个 ON_CALL
rapidjson::Value elementVal;
ON_CALL(SomeClassNameWhereFooIsAMember, foo).WillByDefault(SaveArg<0>(&elementVal));

我什至无法编译。
error C2248: 'rapidjson::GenericValue<rapidjson::UTF8<char>,rapidjson::MemoryPoolAllocator<rapidjson::CrtAllocator>>::GenericValue': cannot access private member declared in class 'rapidjson::GenericValue<rapidjson::UTF8<char>,rapidjson::MemoryPoolAllocator<rapidjson::CrtAllocator>>'

有任何想法吗?

更新1:

根据克里斯·奥尔森的回应,我尝试了以下方法
#include "gtest/gtest.h"
#include "gmock/gmock.h"
#include "rapidjson/document.h"

using namespace ::testing;

class FooClass
{
public:
virtual void foo(rapidjson::Value& element, int number) {}
};

class MockFoo : public FooClass
{
public:
MOCK_METHOD(void, foo, (rapidjson::Value& element, int number));
};

ACTION_P(SaveRapidJsonValueArg, p)
{
rapidjson::Document d; // Needed only to get the required allocator param
p->CopyFrom(arg0, d.GetAllocator());
}

TEST(RapidJsonTests, SaveArgTest)
{
MockFoo mock;

rapidjson::Document document;
rapidjson::Document::AllocatorType& allocator = document.GetAllocator();

rapidjson::Value object(rapidjson::kObjectType);
object.AddMember("Math", "50", allocator);
object.AddMember("Science", "70", allocator);
object.AddMember("English", "50", allocator);
object.AddMember("Social Science", "70", allocator);

rapidjson::Value saveVal;

ON_CALL(mock, foo).WillByDefault(SaveRapidJsonValueArg(&saveVal));

mock.foo(object, 1);

if (saveVal.HasMember("Math"))
std::cout << saveVal["Math"].GetString() << "\n";
}

执行在 HasMember()调用时崩溃。

更新2:

再次感谢克里斯·奥尔森!这就是我最终要做的。
ACTION_P2(SaveRapidJsonValueArg, valuePtr, doc)
{
valuePtr->CopyFrom(arg1, doc->GetAllocator());
}

TEST(RapidJsonTests, SaveArgTest)
{
...
...

ON_CALL(mock, foo).WillByDefault(SaveRapidJsonValueArg(&saveVal, &document));

...
...
}

再次感谢克里斯!

最佳答案

从显示的代码很难分辨出问题出在哪里。您应该尽可能提供minimal reproducible example

您可能会遇到的一个问题是RapidJSON不允许正常复制Value对象。它使用移动语义代替。从RapidJSON Tutorial:

A very special decision during design of RapidJSON is that, assignment of value does not copy the source value to destination value. Instead, the value from source is moved to the destination. For example,

Value a(123);
Value b(456);
b = a; // a becomes a Null value, b becomes number 123.


要实际复制 Value对象,可以使用 Value::CopyFrom方法。可以在gMock中使用 custom action来做到这一点:
#include "gtest/gtest.h"
#include "gmock/gmock.h"
#include "rapidjson/document.h"

using namespace ::testing;

class FooClass
{
public:
virtual void foo(rapidjson::Value& element, int number) {}
};

class MockFoo : public FooClass
{
public:
MOCK_METHOD(void, foo, (rapidjson::Value& element, int number));
};

struct CaptureFooValue
{
void operator()(const rapidjson::Value &element, int) const
{
v.CopyFrom(element, d.GetAllocator(), true);
}
rapidjson::Value &v;
rapidjson::Document &d;
};

TEST(RapidJsonTests, SaveArgTest)
{
MockFoo mock;
rapidjson::Document document;
rapidjson::Document::AllocatorType& allocator = document.GetAllocator();

rapidjson::Value object(rapidjson::kObjectType);
object.AddMember("Math", "50", allocator);
object.AddMember("Science", "70", allocator);
object.AddMember("English", "50", allocator);
object.AddMember("Social Science", "70", allocator);

rapidjson::Value saveVal;

ON_CALL(mock, foo).WillByDefault(CaptureFooValue{saveVal, document});

mock.foo(object, 1);

if (saveVal.HasMember("Math"))
std::cout << saveVal["Math"].GetString() << "\n";
}

关于c++ - RapidJSON::Value&的GoogleTest/Mock SaveArg,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61489528/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com