https://github.com/db7/mockoto
Simple C mock generator with Racket bindings
https://github.com/db7/mockoto
c-language mock racket-lang testing
Last synced: 2 months ago
JSON representation
Simple C mock generator with Racket bindings
- Host: GitHub
- URL: https://github.com/db7/mockoto
- Owner: db7
- License: 0bsd
- Created: 2023-11-01T07:01:45.000Z (over 2 years ago)
- Default Branch: main
- Last Pushed: 2024-04-26T17:54:35.000Z (about 2 years ago)
- Last Synced: 2024-04-26T18:53:27.077Z (about 2 years ago)
- Topics: c-language, mock, racket-lang, testing
- Language: C++
- Homepage:
- Size: 16.6 KB
- Stars: 0
- Watchers: 1
- Forks: 1
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# Mockoto - C mock and FFI binding generator
Mockoto is a tool help the testing of C code with [Racket][] or
[Chibi Scheme][].
Mockoto generates simple mock functions for C prototypes.
These mock functions do not do anything by default, but can be ajusted
conforming the desired test case.
Mockoto also generates Racket and Chibi Scheme bindings to all related types
and hook functions. In this way, one can write test cases and mock code in
Racket or Chibi Scheme for C code.
[Racket]: https://racket-lang.org
[Chibi Scheme]: https://github.com/ashinn/chibi-scheme
## Building
Ensure your system have these packages installed:
- cmake
- clang
- libclang-dev
- xxd
That should be sufficient to compile Mockoto, but if you want to write test
cases in Racket you, of course, also need Racket installed.
To build Mockoto run:
cd mockoto
cmake -S . -B build
make -j
You should now have the executable `build/mockoto`, which can be installed in
your `PATH`.
## Usage
mockoto --mode C header.h another.h > mock_code.c
mockoto --mode H header.h another.h > mock_code.h
mockoto --mode rkt header.h another.h > bindings.rkt
mockoto --mode chibi header.h another.h > bindings.stub
mockoto -V
mockoto --include-static --mode chibi header.h > bindings-with-static.stub
Your test case can be compiled with `mock_code.c`. If you want to adapt the
mock code from C, you can include `mock_code.h` and use the respective
functions.
For a function `foo` declared in the given header file, the following functions
are available:
// This is the mock foo function, which does nothing by default.
// The return value is a 0-ed value of the return type.
retType foo(paramType ...);
// Returns the number of times foo was called.
// The counter resets once this function is called.
int mockoto_foo_called(void);
// Sets a different return value for foo in case foo has a return type
void mockoto_foo_returns(retType r);
// Sets a callback with the same signature as foo.
// Whenever foo is called, the callback will be called and its return value will be used
// instead of the default value of the mock.
void mockoto_foo_hook(mockoto_foo_f cb);
With `bindings.rkt` you have access to the mock from Racket. Here is the `foo`
example:
#lang racket
(require rackunit
"bindings.rkt")
(load-lib "libmock_code.so")
(mock foo
(lambda (param0 param1 ...)
;; do something
;; return the desired value
some-value))
(check-equal? (call foo ...) some-value)
See [example](example) for a running example.
For `--mode chibi`, the generated `.stub` can be compiled with `chibi-ffi`:
chibi-ffi -c bindings.stub
By default, static functions are skipped. Use `--include-static` to include
them in generated output.
## Mockoto Limitations
- no support for varargs
- assumes enums and structs have unique names