Skip to content

Commit

Permalink
New feature: no longer need to specify argument count in the declaration
Browse files Browse the repository at this point in the history
  • Loading branch information
meekrosoft committed Apr 26, 2011
1 parent 01aebe4 commit e34f577
Show file tree
Hide file tree
Showing 4 changed files with 95 additions and 17 deletions.
54 changes: 53 additions & 1 deletion fakegen.rb
Expand Up @@ -223,9 +223,60 @@ def include_guard
puts "#endif // FAKE_FUNCTIONS"
end

def output_macro_counting_shortcuts
puts <<-MACRO_COUNTING
#define PP_NARG_MINUS2(...) \
PP_NARG_MINUS2_(__VA_ARGS__, PP_RSEQ_N_MINUS2())
#define PP_NARG_MINUS2_(...) \
PP_ARG_MINUS2_N(__VA_ARGS__)
#define PP_ARG_MINUS2_N(returnVal, _0, _1, _2, _3, _4, _5, _6, _7, _8, _9, N, ...) N
#define PP_RSEQ_N_MINUS2() \
9,8,7,6,5,4,3,2,1,0
#define FAKE_VALUE_FUNC(...) \
FUNC_VALUE_(PP_NARG_MINUS2(__VA_ARGS__), __VA_ARGS__)
#define FUNC_VALUE_(N,...) \
FUNC_VALUE_N(N,__VA_ARGS__)
#define FUNC_VALUE_N(N,...) \
FAKE_VALUE_FUNC ## N(__VA_ARGS__)
#define PP_NARG_MINUS1(...) \
PP_NARG_MINUS1_(__VA_ARGS__, PP_RSEQ_N_MINUS1())
#define PP_NARG_MINUS1_(...) \
PP_ARG_MINUS1_N(__VA_ARGS__)
#define PP_ARG_MINUS1_N(_0, _1, _2, _3, _4, _5, _6, _7, _8, _9, N, ...) N
#define PP_RSEQ_N_MINUS1() \
9,8,7,6,5,4,3,2,1,0
#define FAKE_VOID_FUNC(...) \
FUNC_VOID_(PP_NARG_MINUS1(__VA_ARGS__), __VA_ARGS__)
#define FUNC_VOID_(N,...) \
FUNC_VOID_N(N,__VA_ARGS__)
#define FUNC_VOID_N(N,...) \
FAKE_VOID_FUNC ## N(__VA_ARGS__)
MACRO_COUNTING
end

def output_c_and_cpp

include_guard {
output_constants

puts "#ifdef __cplusplus"
$cpp_output = true
yield
Expand All @@ -235,12 +286,13 @@ def output_c_and_cpp
$cpp_output = false
yield
puts "#endif /* cpp/ansi c */"

output_macro_counting_shortcuts
}
end

# lets generate!!
output_c_and_cpp{
output_constants
define_reset_fake
define_call_history
define_return_sequence
Expand Down
41 changes: 33 additions & 8 deletions fff.h
@@ -1,14 +1,14 @@
#ifndef FAKE_FUNCTIONS
#define FAKE_FUNCTIONS

#ifdef __cplusplus
#define FFF_MAX_ARGS (10u)
#ifndef FFF_ARG_HISTORY_LEN
#define FFF_ARG_HISTORY_LEN (50u)
#endif
#ifndef FFF_CALL_HISTORY_LEN
#define FFF_CALL_HISTORY_LEN (50u)
#endif
#ifdef __cplusplus

/* Defining a function to reset a fake function */
#define RESET_FAKE(FUNCNAME) { \
Expand Down Expand Up @@ -1320,13 +1320,6 @@ extern "C"{ \
STATIC_INIT(FUNCNAME) \

#else /* ansi c */
#define FFF_MAX_ARGS (10u)
#ifndef FFF_ARG_HISTORY_LEN
#define FFF_ARG_HISTORY_LEN (50u)
#endif
#ifndef FFF_CALL_HISTORY_LEN
#define FFF_CALL_HISTORY_LEN (50u)
#endif

/* Defining a function to reset a fake function */
#define RESET_FAKE(FUNCNAME) { \
Expand Down Expand Up @@ -2555,4 +2548,36 @@ static void RESET_HISTORY() {

#endif /* cpp/ansi c */

#define PP_NARG_MINUS2(...) PP_NARG_MINUS2_(__VA_ARGS__, PP_RSEQ_N_MINUS2())

#define PP_NARG_MINUS2_(...) PP_ARG_MINUS2_N(__VA_ARGS__)

#define PP_ARG_MINUS2_N(returnVal, _0, _1, _2, _3, _4, _5, _6, _7, _8, _9, N, ...) N

#define PP_RSEQ_N_MINUS2() 9,8,7,6,5,4,3,2,1,0


#define FAKE_VALUE_FUNC(...) FUNC_VALUE_(PP_NARG_MINUS2(__VA_ARGS__), __VA_ARGS__)

#define FUNC_VALUE_(N,...) FUNC_VALUE_N(N,__VA_ARGS__)

#define FUNC_VALUE_N(N,...) FAKE_VALUE_FUNC ## N(__VA_ARGS__)



#define PP_NARG_MINUS1(...) PP_NARG_MINUS1_(__VA_ARGS__, PP_RSEQ_N_MINUS1())

#define PP_NARG_MINUS1_(...) PP_ARG_MINUS1_N(__VA_ARGS__)

#define PP_ARG_MINUS1_N(_0, _1, _2, _3, _4, _5, _6, _7, _8, _9, N, ...) N

#define PP_RSEQ_N_MINUS1() 9,8,7,6,5,4,3,2,1,0

#define FAKE_VOID_FUNC(...) FUNC_VOID_(PP_NARG_MINUS1(__VA_ARGS__), __VA_ARGS__)

#define FUNC_VOID_(N,...) FUNC_VOID_N(N,__VA_ARGS__)

#define FUNC_VOID_N(N,...) FAKE_VOID_FUNC ## N(__VA_ARGS__)


#endif // FAKE_FUNCTIONS
11 changes: 6 additions & 5 deletions test/fff_test_c.c
Expand Up @@ -25,11 +25,11 @@ struct MyStruct {
int y;
};

FAKE_VOID_FUNC1(voidfunc1, int);
FAKE_VOID_FUNC2(voidfunc2, char, char);
FAKE_VALUE_FUNC0(long, longfunc0);
FAKE_VALUE_FUNC0(enum MYBOOL, enumfunc0);
FAKE_VALUE_FUNC0(struct MyStruct, structfunc0);
FAKE_VOID_FUNC(voidfunc1, int);
FAKE_VOID_FUNC(voidfunc2, char, char);
FAKE_VALUE_FUNC(long, longfunc0);
FAKE_VALUE_FUNC(enum MYBOOL, enumfunc0);
FAKE_VALUE_FUNC(struct MyStruct, structfunc0);


void setup()
Expand Down Expand Up @@ -254,6 +254,7 @@ TEST_F(FFFTestSuite, default_constants_can_be_overridden)
ASSERT_EQ(OVERRIDE_ARG_HIST_LEN, voidfunc2_arg_history_len);
}


int main()
{
setbuf(stdout, NULL);
Expand Down
6 changes: 3 additions & 3 deletions test/fff_test_cpp.cpp
Expand Up @@ -15,9 +15,9 @@
#include "../fff.h"
#include <gtest/gtest.h>

FAKE_VOID_FUNC1(voidfunc1, int);
FAKE_VOID_FUNC2(voidfunc2, char, char);
FAKE_VALUE_FUNC0(long, longfunc0);
FAKE_VOID_FUNC(voidfunc1, int);
FAKE_VOID_FUNC(voidfunc2, char, char);
FAKE_VALUE_FUNC(long, longfunc0);

class FFFTestSuite: public testing::Test
{
Expand Down

0 comments on commit e34f577

Please sign in to comment.