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

Kai Zhang kyle at zelin.io
Wed Jul 10 14:02:44 CEST 2013


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 which don't 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 who do have 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 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                 |   41 +++++++++++++
 tests/unit/mock/mock.h                 |   58 ++++++++++++++++++
 tests/unit/sheep/Makefile.am           |   15 ++++-
 tests/unit/sheep/mock_group.c          |   31 ++++++++++
 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 |  103 ++++++++++++++++++++++++++++++++
 tests/unit/sheep/test_vdi.c            |   13 ++++
 16 files changed, 357 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