[sheepdog] [PATCH v3 0/4] Introduce mock

Kai Zhang kyle at zelin.io
Fri Jul 12 08:41:34 CEST 2013


v3:
- fixed compile error when configured without --enable-zookeeper
- added "local" testcase to test_cluster_driver.c
- added method_reset_all() to mock.h
- removed dependency of logger.h from mock.c

v2:
- added cover-letter
- fixed code style problem
- update commit log to show simple example of mock

Mock is useful when we do unit test.

This patch introduce some useful macro to simply testing.

The two most useful macro are MOCK_METHOD and MOCK_VOID_METHOD.

MOCK_VOID_METHOD is used to mock method how has no return value.
For example:
void foo(int a, long b)
{
	return;
}

We define the mock method as:
MOCK_VOID_METHOD(foo, int a, long b)

MOCK_METHOD is used to mock method how has return value.
User have to specified the return value's type and the default return value.
For example:
bool foo(int a, long b)
{
	return true;
}

We define the mock method as:
MOCK_METHCO(foo, bool, true, int a, long b)

Use macro to define mock method has many benifits:
- we can record each call of the method and its arguments
- we can control the return value, based on input arguments

However, current implementation is quite initial.
We just record how many times the method is called.

Another useful macro is method_nr_call(method) which returns how many times the
method is called.

Kai Zhang (4):
  tests/unit: add missing copyright and license
  tests/unit: introduce mock
  tests/unit: use MOCK_METHOD when define mock method
  tests/unit: add test_cluster_driver

 .gitignore                             |    1 +
 configure.ac                           |    1 +
 tests/unit/Makefile.am                 |    2 +-
 tests/unit/collie/Makefile.am          |    3 +-
 tests/unit/collie/mock_collie.c        |   25 +++---
 tests/unit/collie/test_common.c        |   13 ++++
 tests/unit/mock/Makefile.am            |    7 ++
 tests/unit/mock/mock.c                 |   51 +++++++++++++
 tests/unit/mock/mock.h                 |   60 +++++++++++++++
 tests/unit/sheep/Makefile.am           |   18 ++++-
 tests/unit/sheep/mock_group.c          |   32 ++++++++
 tests/unit/sheep/mock_request.c        |   29 ++++---
 tests/unit/sheep/mock_sheep.c          |   14 ++++
 tests/unit/sheep/mock_store.c          |   49 ++++++------
 tests/unit/sheep/test_cluster_driver.c |  130 ++++++++++++++++++++++++++++++++
 tests/unit/sheep/test_vdi.c            |   13 ++++
 16 files changed, 400 insertions(+), 48 deletions(-)
 create mode 100644 tests/unit/mock/Makefile.am
 create mode 100644 tests/unit/mock/mock.c
 create mode 100644 tests/unit/mock/mock.h
 create mode 100644 tests/unit/sheep/mock_group.c
 rewrite tests/unit/sheep/mock_request.c (81%)
 rewrite tests/unit/sheep/mock_store.c (93%)
 create mode 100644 tests/unit/sheep/test_cluster_driver.c

--
1.7.9.5




More information about the sheepdog mailing list