我正在开发使用的代码boost::asio.为了测试它,我需要从这个库中模拟一组类.我正在使用Google Mock,它允许模拟虚拟方法.通常(和繁琐)的过程是为我需要使用的每个类编写一个接口.
另一方面,Google Mock Cookbook在模拟非虚拟方法时描述了一种替代方法:使用模板.我的问题是我可能需要同时模拟几个类(因此直接使用模板不起作用).所以我想:为什么不使用两级模板?我提出了以下解决方案:
// Classes to be mocked.
class RealA
{
public:
void a() { cout << "RealA::a()" << endl; };
};
class RealB
{
public:
void b() { cout << "RealB::b()" << endl; };
};
// Mock classes.
class MockA
{
public:
void a() { cout << "MockA::a()" << endl; };
};
class MockB
{
public:
void b() { cout << "MockB::b()" << endl; };
};
template<class ABFactory>
class Program
{
public:
void setFactory(ABFactory* factory) …Run Code Online (Sandbox Code Playgroud) 我使用Boost :: Spirit这个非常简单的解析器:
rule<std::string::iterator, std::string()> zeroTo255 = (string("25") >> char_('0', '5'))
| (char_('2') >> char_('0', '4') >> digit)
| (char_('1') >> repeat[2](digit))
| (char_('1', '9') >> digit) | digit;
Run Code Online (Sandbox Code Playgroud)
当我尝试解析时
std::string o{"1"};
std::string s;
parse(o.begin(), o.end(), zeroTo255, s);
std::cout << o << ": " << s << std::endl;
Run Code Online (Sandbox Code Playgroud)
我有输出
1: 111
Run Code Online (Sandbox Code Playgroud)
我显然做错了什么,但是什么?
#include <stdio.h>
int main(int argc, char* argv[]) {
unsigned char c = 10;
unsigned short d = 10;
unsigned int e = 10;
unsigned long f = 10;
double g = -c;
double h = -d;
double i = -e;
double j = -f;
printf("%d %lf\n", c, g);
printf("%u %lf\n", d, h);
printf("%u %lf\n", e, i);
printf("%lu %lf\n", f, j);
}
Run Code Online (Sandbox Code Playgroud)
给出输出
10 -10.000000
10 -10.000000
10 4294967286.000000
10 18446744073709551616.000000
Run Code Online (Sandbox Code Playgroud)
为什么结果不一致,某些类型产生-10,而其他类型产生巨大价值?
我知道这个问题的第一部分之前已被问过,但那是很久以前的事了:).我想知道在模拟非虚拟方法和C函数时,任何开源模拟框架的平均时间是否赶上了Typemock Isolator ++.我最感兴趣的是Linux下的gcc.到目前为止,我对模拟访问器感兴趣(这样我可以模拟模拟对象中的状态 - 见下文)并从其他库中替换C函数(select,pcap_*等).
class Foo {
public:
...
bool IsCondition() { return condition; };
...
private:
bool condition;
}
// I want a framework that allows me to do something like this:
TEST(TestFoo) {
MOCK_INTERFACE(Foo) mock_foo;
EXPECT_CALL(mock_foo, IsCondition).returns(true);
EXPECT(mock_foo.IsCondition());
}
Run Code Online (Sandbox Code Playgroud) 在strace以下程序上运行时:
#include <boost/asio.hpp>
#include <pcap.h>
using namespace boost;
int main(int argc, char* argv[])
{
asio::io_service io;
asio::posix::stream_descriptor stream(io);
char errorBuffer[BUFSIZ];
pcap_t* p = pcap_open_live("any", BUFSIZ, false, 0, errorBuffer);
stream.assign(pcap_get_selectable_fd(p));
io.run();
stream.close();
pcap_close(p);
return 0;
}
Run Code Online (Sandbox Code Playgroud)
我明白了:
close(6) = 0
setsockopt(6, SOL_PACKET, PACKET_RX_RING, {block_size=0, block_nr=0, frame_size=0, frame_nr=0}, 16) = -1 EBADF (Bad file descriptor)
munmap(0xb733c000, 4145152) = 0
close(6) = -1 EBADF (Bad file descriptor)
Run Code Online (Sandbox Code Playgroud)
如你所见,close在同一个fd(第一个stream.close(),然后是pcap_close(p))上调用两次.虽然程序可能没有意义,但我需要同时调用stream.close()(以防止io_service调用epoll_ctl …
c++ ×5
boost ×2
boost-asio ×2
mocking ×2
boost-spirit ×1
c ×1
gcc ×1
googlemock ×1
googletest ×1
linux ×1
pcap ×1
unit-testing ×1