Skip to content

Commit 407e769

Browse files
committed
New DEFINE_FUNC_*FALLBACK macros
`YT_DEFINE_FUNC_FALLBACK` and `YT_DEFINE_FUNC_VOID_FALLBACK` should be used for functions with non integer arguments (floating point or `structs` passed by-value etc) as such arguments do not work with `MUST_CALL*`/`NEVER_CALL*` macros at this time. Also includes: * Added new integration test for this case.
1 parent 1d2e902 commit 407e769

File tree

4 files changed

+118
-8
lines changed

4 files changed

+118
-8
lines changed

Readme.md

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,7 @@ in what order and which what parameters.
117117
| `YT_MUST_CALL_ANY_ORDER(f, ...)` | Function `f` is called with the given arguments at least once in no particular order |
118118
| `YT_MUST_CALL_ANY_ORDER_ATLEAST_TIMES(n, f, ...)` | Function `f` is called with the given arguments at least `n` times in no particular order |
119119
| `YT_MUST_NEVER_CALL(f, ...)` | Function `f` with the given arguments is never called |
120-
| `YT_IN_SEQUENCE(n)` | Repeats expectations `n` number of times. Used to put expectations for a loop. |
120+
| `YT_IN_SEQUENCE(n)` | Repeats expectations `n` number of times. Used to put expectations for a loop. |
121121

122122
See these examples
123123
* [printer_fail](./example/sensor_test.c) example
@@ -129,13 +129,20 @@ When unittesting it might be required to provide a fake definitions of external
129129
where these macros come in. These fake functions also enable the above mentioned interaction
130130
validations and one can modify the behaviour of these fake functions in various ways.
131131

132-
| Macro name | Validates |
133-
|--------------------------------|-------------------------------------------------------------------------------------------------------|
134-
| `YT_DECLARE_FUNC(rt, f, ...)` | Declaration for fake function `f` which takes any number of arguments returns some non void type `rt` |
135-
| `YT_DECLARE_FUNC_VOID(f, ...)` | Declaration for fake function `f` which takes any number of arguments returns void |
136-
| `YT_DEFINE_FUNC(rt, f, ...)` | Definition for fake function `f` previously declared using `YT_DECLARE_FUNC`. |
137-
| `YT_DEFINE_FUNC_void(f, ...)` | Definition for fake function `f` previously declared using `YT_DECLARE_FUNC_VOID`. |
138-
| `YT_RESET_MOCK(f)` | Resets internal state of a fake function previously defined using `YT_DEFINE_FUNC*`. |
132+
| Macro name | Validates |
133+
|----------------------------------------|----------------------------------------------------------------------------------------------------------------------------------------------|
134+
| `YT_DECLARE_FUNC(rt, f, ...)` | Declaration for fake function `f` which takes any number of arguments returns some non void type `rt` |
135+
| `YT_DECLARE_FUNC_VOID(f, ...)` | Declaration for fake function `f` which takes any number of arguments returns void |
136+
| `YT_DEFINE_FUNC(rt, f, ...)` | Definition for fake function `f` previously declared using `YT_DECLARE_FUNC`. |
137+
| `YT_DEFINE_FUNC_VOID(f, ...)` | Definition for fake function `f` previously declared using `YT_DECLARE_FUNC_VOID`. |
138+
| `YT_DEFINE_FUNC_FALLBACK(rt, f, ...)` | Definition for fake function `f` previously declared using `YT_DECLARE_FUNC`. `MUST_CALL*`/`NEVER_CALL*` macros cannot be used on them. |
139+
| `YT_DEFINE_FUNC_VOID_FALLBACK(f, ...)` | Definition for fake function `f` previously declared using `YT_DECLARE_FUNC_VOID`. `MUST_CALL*`/`NEVER_CALL*` macros cannot be used on them. |
140+
| `YT_RESET_MOCK(f)` | Resets internal state of a mock/fake function previously defined using `YT_DEFINE_FUNC*`. |
141+
142+
`YT_DEFINE_FUNC_FALLBACK` and `YT_DEFINE_FUNC_VOID_FALLBACK` should be used for functions with non
143+
integer arguments (floating point or `structs` passed by-value etc) as such arguments do not work
144+
with `MUST_CALL*`/`NEVER_CALL*` macros at this time. These provide just the fake definitions without
145+
extra data needed by interaction testing macros.
139146

140147
See [Mocking and faking](./example/sensor_test.c) example
141148

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
  mock:mock_return_value  OK [0 of 1 failed]
2+
  mock:mock_handler  OK [0 of 3 failed]
3+
  mock:mock_handler_resource_passing  OK [0 of 2 failed]
4+
5+
All tests passed [0 of 3 failed]
6+

tests/mocking_success_fallback.c

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
#define YUKTI_TEST_IMPLEMENTATION
2+
#include "../yukti.h"
3+
4+
typedef struct {
5+
int a;
6+
} SomeType;
7+
8+
int func_extern_1 (SomeType, int);
9+
int sut_func (SomeType a, int b)
10+
{
11+
return func_extern_1 (a, b);
12+
}
13+
14+
YT_DECLARE_FUNC (int, func_extern_1, SomeType, int);
15+
YT_DEFINE_FUNC_FALLBACK (int, func_extern_1, SomeType, int);
16+
17+
SomeType dummy = { 0 };
18+
19+
YT_TEST (mock, mock_return_value)
20+
{
21+
func_extern_1_fake.ret = 0xFABC;
22+
23+
YT_EQ_SCALAR (sut_func (dummy, 0), 0xFABC);
24+
25+
YT_END();
26+
}
27+
28+
int func_extern_1_handler_1 (SomeType a, int b)
29+
{
30+
YT_EQ_MEM (&a, &dummy, sizeof (a));
31+
YT_EQ_SCALAR (b, 0x2222);
32+
return 0;
33+
}
34+
35+
YT_TEST (mock, mock_handler)
36+
{
37+
func_extern_1_fake.handler = func_extern_1_handler_1;
38+
YT_EQ_SCALAR (sut_func (dummy, 0x2222), 0x0);
39+
40+
YT_END();
41+
}
42+
43+
int func_extern_1_handler_2 (SomeType a, int b)
44+
{
45+
(void)a;
46+
(void)b;
47+
YT_EQ_SCALAR (*(int*)func_extern_1_fake.resources, 0x1212);
48+
return 0;
49+
}
50+
51+
YT_TEST (mock, mock_handler_resource_passing)
52+
{
53+
int resource = 0x1212;
54+
func_extern_1_fake.resources = &resource;
55+
func_extern_1_fake.handler = func_extern_1_handler_2;
56+
YT_EQ_SCALAR (sut_func (dummy, 0), 0);
57+
58+
YT_END();
59+
}
60+
61+
void yt_reset()
62+
{
63+
YT_RESET_MOCK (func_extern_1);
64+
}
65+
66+
int main (void)
67+
{
68+
YT_INIT();
69+
mock_return_value();
70+
mock_handler();
71+
mock_handler_resource_passing();
72+
YT_RETURN_WITH_REPORT();
73+
}

yukti.h

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -285,11 +285,19 @@ void yt_reset(); // MUST BE DEFINED BY THE USER OF THIS HEADER FILE.
285285
YT__DEFINE_FUNC_STRUCT (f); \
286286
YT__DEFINE_FUNC_BODY_VOID (YT__COUNT_ARGS (__VA_ARGS__), f, __VA_ARGS__)
287287

288+
#define YT_DEFINE_FUNC_VOID_FALLBACK(f, ...) \
289+
YT__DEFINE_FUNC_STRUCT (f); \
290+
YT__DEFINE_FUNC_BODY_VOID_FALLBACK (YT__COUNT_ARGS (__VA_ARGS__), f, __VA_ARGS__)
291+
288292
// ----
289293
#define YT_DEFINE_FUNC(rt, f, ...) \
290294
YT__DEFINE_FUNC_STRUCT (f); \
291295
YT__DEFINE_FUNC_BODY (YT__COUNT_ARGS (__VA_ARGS__), rt, f, __VA_ARGS__)
292296

297+
#define YT_DEFINE_FUNC_FALLBACK(rt, f, ...) \
298+
YT__DEFINE_FUNC_STRUCT (f); \
299+
YT__DEFINE_FUNC_BODY_FALLBACK (YT__COUNT_ARGS (__VA_ARGS__), rt, f, __VA_ARGS__)
300+
293301
// ----
294302

295303
// clang-format off
@@ -310,6 +318,13 @@ void yt_reset(); // MUST BE DEFINED BY THE USER OF THIS HEADER FILE.
310318
YT__RETURN_VOID (f, __VA_ARGS__); \
311319
}
312320

321+
#define YT__DEFINE_FUNC_BODY_VOID_FALLBACK(n, f, ...) \
322+
void f (YT__FUNC_PARAMS_X (__VA_ARGS__)) \
323+
{ \
324+
YT__STRUCT_VAR (f).invokeCount++; \
325+
YT__RETURN_VOID (f, __VA_ARGS__); \
326+
}
327+
313328
#define YT__DEFINE_FUNC_BODY(n, rt, f, ...) \
314329
rt f (YT__FUNC_PARAMS_X (__VA_ARGS__)) \
315330
{ \
@@ -318,6 +333,13 @@ void yt_reset(); // MUST BE DEFINED BY THE USER OF THIS HEADER FILE.
318333
YT__RETURN (f, __VA_ARGS__); \
319334
}
320335

336+
#define YT__DEFINE_FUNC_BODY_FALLBACK(n, rt, f, ...) \
337+
rt f (YT__FUNC_PARAMS_X (__VA_ARGS__)) \
338+
{ \
339+
YT__STRUCT_VAR (f).invokeCount++; \
340+
YT__RETURN (f, __VA_ARGS__); \
341+
}
342+
321343
#define YT__RETURN_VOID(f, ...) \
322344
if (YT__STRUCT_VAR (f).handler) { \
323345
YT__STRUCT_VAR (f).handler (YT__FCALL_ARGS_X (__VA_ARGS__)); \
@@ -1122,7 +1144,9 @@ static double yt__test_elapsed_time_ms()
11221144
#define END YT_END
11231145
#define V YT_V
11241146
#define DEFINE_FUNC_VOID YT_DEFINE_FUNC_VOID
1147+
#define DEFINE_FUNC_VOID_FALLBACK YT_DEFINE_FUNC_VOID_FALLBACK
11251148
#define DEFINE_FUNC YT_DEFINE_FUNC
1149+
#define DEFINE_FUNC_FALLBACK YT_DEFINE_FUNC_FALLBACK
11261150
#define DECLARE_FUNC_VOID YT_DECLARE_FUNC_VOID
11271151
#define DECLARE_FUNC YT_DECLARE_FUNC
11281152
#define RESET_MOCK YT_RESET_MOCK

0 commit comments

Comments
 (0)