Skip to content
/ cmock Public
forked from codechurch/cmock

The cmock is a mock tool, which is just for C language. And cmock can mock static functions.

Notifications You must be signed in to change notification settings

yysalad/cmock

 
 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

10 Commits
 
 
 
 
 
 
 
 

Repository files navigation

CMOCK

1 Overview

  • Other languages: 中文
  • The cmock is a MOCK tool, which is just for C language.
  • The cmock can call/mock any glocal function (including static functions).
  • Now, cmock is just working on GNU/Linux of arch AMD64.
  • It needs objdump installed which is provided in binutils of Debian’s packages.

2 License

GPLv3

3 Installation

The cmock is perl5 prog. To copy cmock to /usr/bin and change mode。

sudo cp cmock /usr/bin && sudo chmod +x /usr/bin/cmock

4 Usage

cmock <Object File>

The cmock analysises <base>.o and makes out <base>_cmock.h, <base>_cmock.c and <base>_cmock.lds . The file <base>_cmock.h supports CMOCK macros. The file <base>_cmock.c supports cmock functions body. The file <base>_cmock.lds supports ld-script for linker.

5 Example

In the example, some.c is the subject file and test.c is unit testing code. in test.c, you should:

#include "some_cmock.h"
  • Phase 1, to compile some.c:
gcc -c -g -Wall -fPIC -o some.o some.c
  • Phase 2, using cmock to generate some_cmock.h, some_cmock.c and some_cmock.lds .
cmock some.o
  • Phase 3, to compile test.c and some_cmock.c
gcc -c -g -Wall -fPIC -o test.o test.c 
gcc -c -g -Wall -fPIC -o some_cmock.o some_cmock.c
  • Phase 4, to link all and make out program
gcc -g -Wall -o test_some.out some.o some_cmock.o test.o \
    -ldl -lm              \
    -Wl,-T,some_cmock.lds \
    -Wl,--unresolved-symbols=ignore-all 

(–unresolved-symbols is optional)

6 Programming Interface

  • function void cmock_restore()

To restore relocation to real functions.

  • macro CMOCK_CALL(rt, func, args)

To call the real func, return type is rt, argments is args. Example:

int v = CMOCK_CALL(int, foobar, (3));
  • macro CMOCK_FUNC_VAL(func, val) To mock func, set return value is val.
CMOCK_FUNC_VAL(foobar, 10);
  • macro CMOCK_FUNC(rt, func)

To mock func, return type is rt, set args and body.

CMOCK_FUNC(int, foobar) (int a)
  {
    return a * 2;
  }

7 Simple Unit Test Interface

  • Environment Variable V To set verbose output
V=1 ./test_some.out
  • function int cmock_result() To get the result of whole testing.
  • macro CMOCK_RESULT(expr) To set the result of this case.
  • macro CMOCK_INFO(fmt, …) and CMOCK_ERROR(fmt, …) To print text. The CMOCK_ERROR also sets error result.
  • macro CMOCK_CASE(name)

To provide a head of test-case function.

  • macro CMOCK_RUN_CASE(name) To run a test case. You should run test cases in main.

Example:

CMOCK_CASE(some)
{
  ...
  CMOCK_RESULT(1);
}

CMOCK_CASE(other)
{
  if (foobar() == 0)
    CMOCK_INFO("foobar return OK");
  else               
    CMOCK_ERROR("foobar return error");
}

int main()
{
  CMOCK_RUN_CASE(some);
  CMOCK_RUN_CASE(other);
  return cmock_result();
}

About

The cmock is a mock tool, which is just for C language. And cmock can mock static functions.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages

  • Perl 90.3%
  • C 7.2%
  • Makefile 2.5%