Skip to main content

A minimal mocking utility for C projects.

Project description

🎣 narmock

Build Status PyPI PyPI - Python Version Code style: black

A minimal mocking utility for C projects.

🚧 Work in progress 🚧

Narmock finds the functions mocked in your tests and generates mocks with a slick API.

#include <time.h>

#include "__mocks__.h"
#include "narwhal.h"

TEST(example)
{
    MOCK(time)->mock_return(42);
    ASSERT_EQ(time(NULL), 42);
}

This example is a test written with Narwhal but Narmock can be used with other test frameworks and anywhere in regular source code.

Installation

The package can be installed with pip.

$ pip install narmock

Getting started

The command-line utility provides two essential commands that should make it possible to integrate Narmock in any kind of build system.

usage: narmock [-h] (-g [<code>] | -f) [-d <directory>]

A minimal mocking utility for C projects.

optional arguments:
  -h, --help      show this help message and exit
  -g [<code>]     generate mocks
  -f              output linker flags
  -d <directory>  mocks directory

Check out the basic example for a simple Makefile that integrates both Narwhal and Narmock.

Generating mocks

The narmock -g command finds the functions mocked in your code and generates a __mocks__.c file and a __mocks__.h file that respectively define and declare all the required mocks.

$ gcc -E *.c | narmock -g

Narmock requires source code to be expanded by the preprocessor. You can directly pipe the output of gcc -E to the command-line utility.

By default, __mocks__.c and __mocks__.h will be created in the current directory. You can specify a different output directory with the -d option.

$ gcc -E tests/*.c | narmock -g -d tests

Retrieving linker flags

The narmock -f command reads the generated __mocks__.h file and outputs the necessary linker flags for linking all your source files together.

$ gcc $(narmock -f) *.c

By default, the command looks for __mocks__.h in the current directory. You can specify a different directory with the -d option.

$ gcc $(narmock -f -d tests) tests/*.c

Mock API

The MOCK macro returns a pointer to the mock API of a given function.

MOCK(time);

Mock return

You can make a function return a specific value without calling its original implementation.

MOCK(time)->mock_return(42);

printf("%ld\n", time(NULL));  // Outputs 42

Mock implementation

You can switch the implementation of a function.

time_t time_stub(time_t *timer)
{
    return 42;
}

MOCK(time)->mock_implementation(time_stub);

printf("%ld\n", time(NULL));  // Outputs 42

Disable mock

You can disable the mock and make the function call its original implementation.

MOCK(time)->disable_mock();

printf("%ld\n", time(NULL));  // Outputs the current time

Contributing

Contributions are welcome. Feel free to open issues and suggest improvements. This project uses poetry so you'll need to install it first if you want to be able to work with the project locally.

$ curl -sSL https://raw.githubusercontent.com/sdispater/poetry/master/get-poetry.py | python

You should now be able to install the required dependencies.

$ poetry install

The code follows the black code style.

$ poetry run black narmock

You can run the tests with poetry run make -C tests. The test suite is built with Narwhal.

$ poetry run make -C tests

License - MIT

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

narmock-0.2.0.tar.gz (10.6 kB view details)

Uploaded Source

Built Distribution

narmock-0.2.0-py3-none-any.whl (25.5 kB view details)

Uploaded Python 3

File details

Details for the file narmock-0.2.0.tar.gz.

File metadata

  • Download URL: narmock-0.2.0.tar.gz
  • Upload date:
  • Size: 10.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/0.12.11 CPython/3.7.4 Linux/4.19.66-1-MANJARO

File hashes

Hashes for narmock-0.2.0.tar.gz
Algorithm Hash digest
SHA256 5b26a26acfc802314f1776e66d63ba465414839df40f0372a958ff0b1f3b1ac8
MD5 f1017aa46923a9afacce0301a2c698e3
BLAKE2b-256 d18d218afbdee344bf695e7d34739522b42180c840214e22a0dfcf532181ea9f

See more details on using hashes here.

File details

Details for the file narmock-0.2.0-py3-none-any.whl.

File metadata

  • Download URL: narmock-0.2.0-py3-none-any.whl
  • Upload date:
  • Size: 25.5 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/0.12.11 CPython/3.7.4 Linux/4.19.66-1-MANJARO

File hashes

Hashes for narmock-0.2.0-py3-none-any.whl
Algorithm Hash digest
SHA256 d3a0799d3dbf36b2b5e1f46913ff9a73984f124e44885cea825bc2cef6587c40
MD5 5d80ebae0a15f90f8b1a28de42cf5784
BLAKE2b-256 a4f6bb6eb3439d22ebf2cc7253ab5effbdb836f21adc62844103c9cb461d497d

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page