使用谷歌模拟对C code [英] Using google mock for C code

查看:130
本文介绍了使用谷歌模拟对C code的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我保持C语言编写的一个传统项目,这是不可行的把它用C ++编译器运行。然而由于code为横编译是可能的运行单元测试或在宿主环境相似。因此,它也可以用C ++编译器的主机接口,并使用谷歌测试和谷歌 - 模拟。

I'm maintaining a legacy project written in C and it's unfeasible to get it running with a C++ compiler. Since the code is cross compiled it is however possible to run unit-tests or similar in a host environment. hence it's also possible to interface with a C++ host compiler and use google-test and google-mock.

有这似乎是很诱人的用于测试的调用真正的实现,并设置呼叫预期谷歌,模拟的某些功能。

There are certain capabilities of google-mock which seem to be very tempting to be used for testing as invoking real implementations and setting call expectations.

我希望能在C code到使用它们。我可以看到,确实有可能使用谷歌,模仿不使用虚函数表,但它需要的模板。

I would like to be able to use them in C code. I can see that it is indeed possible to use google-mock without using vtables, but it requires templates.

有没有办法来嘲笑裸露的C函数与谷歌模仿?

Is there a way to mock bare C functions with google mock?

推荐答案

我找到了一种方法,能够在谷歌,模仿嘲笑裸露的C函数。

I found a way to be able to mock bare C functions in google-mock.

解决方案是使用主机上的弱别名和强大的别名目标。

The solution is to use weak aliases on the host and strong aliases on the target.

下面的解决方案是GCC具体的,但应该有不同的编译器/链接其他解决方案。

The following solution is GCC specific but there should be other solutions for different compiler/linkers.


  • 功能无效foobar的()重命名; 无效foobarImpl();

  • 的属性添加到函数 foobar的,如:无效foobar的()__attribute __((弱,别名(foobarImpl)));

  • 如果你想有一个非弱别名使用preproessor指令从属性中删除弱。

  • rename the function void foobar(); to void foobarImpl();
  • add an attribute to the function foobar like: void foobar() __attribute__((weak, alias("foobarImpl") ));
  • if you want to have a non weak alias use a preproessor directive to remove the weak from the attributes.

因此​​:

#pragma once
void foobar();

变为

// header.h
#pragma once

void foobar();    
void foobarImpl(); // real implementation

// code.c
void foobarImpl() {
  /* do sth */
}
void foobar() __attribute__(( weak, alias ("foobarImpl") )); // add weak reference

这将告诉GNU链接器链接与 foobarImpl foobar的的()调用()每当有没有符号名为 foobar的()

This will tell the gnu linker to link calls of foobar() with foobarImpl() whenever there is no symbol called foobar()

然后添加测试code

then add the testing code

struct FooInterface {
   virtual ~FooInterface() {}
   virtual void invokeFoo() const { }
};

class MockFoo : public FooInterface {
public:
  MOCK_CONST_METHOD0(invokeFoo, void());
}

struct RealFoo : public FooInterface {
   virtual ~RealFoo() {}
   virtual void invokeFoo() const { FooImpl(); }
};

MockFoo mockFoo;
RealFoo realFoo;
void foobar() {
  mockFoo.invokeFoo();
}

如果这个code编译和链接将取代 foobar的通过模拟电话。
如果你真的想调用 foobar的()你仍然可以添加默认调用。

if this code is compiled and linked it will replace foobar with the mock call. if you really want to call foobar() you can still do add a default invocation.

ON_CALL(mockFoo, invokeFoo())
       .WillByDefault(Invoke(&realFoo,&RealFoo::invokeFoo));

这篇关于使用谷歌模拟对C code的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆