meekrosoft / fff

A testing micro framework for creating function test doubles
Other
761 stars 167 forks source link

Question: Usage of __weak__ #43

Closed susundberg closed 5 years ago

susundberg commented 6 years ago

Hi!

Love this library. I have used it several places, for example in in this arduino testing frame

I am not sure how this is thought, but i usually like to make testing so that all functions are mocked, except few special. I make this by linker; i have fake-objects that contains fake implementation of all functions and before that the real ones. This works fine, but to have it even nicer, one wraps the thing to "libfakes.a" that contains all fakes. To make this work, one should mark all fakes with __attribute__((weak)).

This would make minor pull request, that i am super willing to make, but is there a reason why it has been skipped for now?

Cheers, Pauli

Here is example of 'fixed' function:

#define DEFINE_FAKE_VOID_FUNC0(FUNCNAME) \
    FFF_EXTERN_C \
        FUNCNAME##_Fake FUNCNAME##_fake; \
        __attribute__((weak))  void FUNCNAME(void)  { \
            if(ROOM_FOR_MORE_HISTORY(FUNCNAME)){ \
            } \
            else{ \
                HISTORY_DROPPED(FUNCNAME); \
            } \
            INCREMENT_CALL_COUNT(FUNCNAME); \
            REGISTER_CALL(FUNCNAME); \
            if (FUNCNAME##_fake.custom_fake_seq_len){ /* a sequence of custom fakes */ \
                if (FUNCNAME##_fake.custom_fake_seq_idx < FUNCNAME##_fake.custom_fake_seq_len){ \
                    FUNCNAME##_fake.custom_fake_seq[FUNCNAME##_fake.custom_fake_seq_idx++](); \
                } \
                else{ \
                    FUNCNAME##_fake.custom_fake_seq[FUNCNAME##_fake.custom_fake_seq_len-1](); \
                } \
            } \
            if (FUNCNAME##_fake.custom_fake) FUNCNAME##_fake.custom_fake(); \
        } \
        DEFINE_RESET_FUNCTION(FUNCNAME) \
    FFF_END_EXTERN_C \
wulfgarpro commented 5 years ago

Closing this issue ticket in favour of reviewing and closing PR #47.