Code Monkey home page Code Monkey logo

Comments (7)

cormacc avatar cormacc commented on May 9, 2024

Looks like the VAARG mock requires at least one arg. Have you tried letting the format string be the first arg -- i.e. something like this:

DECLARE_FAKE_VALUE_FUNC_VARARG(int, printf, ...);

int mock_printf(va_list arg)
{
    int result = vfprintf(stdout, arg);
    fprintf(stdout, "Goodbye\n");
    return(result);
}

from fff.

wulfgarpro avatar wulfgarpro commented on May 9, 2024

@CanadianHoser, have you attempted @cormacc's suggestion?

from fff.

CanadianHoser avatar CanadianHoser commented on May 9, 2024

The suggestion by @cormacc won't work because it doesn't match the vfprintf prototype:

printf_test.cpp:12:38: error: too few arguments to function β€˜int vfprintf(FILE*, const char*, __gnuc_va_list)’
int result = vfprintf(stdout, arg);

from fff.

CanadianHoser avatar CanadianHoser commented on May 9, 2024

I do think there's something to our system implementation. The code listed above does perform as expected (Tests 1 and 2 are suppressed, 3 & 4 are displayed) when compiled standalone, but the same code integrated into our much larger code base (which overrides printf elsewhere) behaves as indicated above. I did try putting put the mock_printf() implementation into a separate .c file to confirm it's not a compiler difference.

from fff.

wulfgarpro avatar wulfgarpro commented on May 9, 2024

@CanadianHoser, it looks like the generator for VARARG fakes, only generates N=2-20:

define FAKE_VALUE_FUNC2_VARARG(RETURN_TYPE, FUNCNAME, ARG0_TYPE, ...) \
    DECLARE_FAKE_VALUE_FUNC2_VARARG(RETURN_TYPE, FUNCNAME, ARG0_TYPE, ...) \
    DEFINE_FAKE_VALUE_FUNC2_VARARG(RETURN_TYPE, FUNCNAME, ARG0_TYPE, ...) \

We could probably manage N=1 for these kinds of situtations:

define FAKE_VALUE_FUNC1_VARARG(RETURN_TYPE, FUNCNAME, ARG0_TYPE) \
...

I'll see if I can put together a patch over the next couple of days.

from fff.

rubiot avatar rubiot commented on May 9, 2024

It seems you're just declaring the mock, but not defining it. You either have to use FAKE_VALUE_FUNC_VARARG, which directly defines your mock function, or use the pair DEFINE_FAKE_VALUE_FUNC_VARARG/DECLARE_FAKE_VALUE_FUNC_VARARG if you want to split the declaration/definition.

from fff.

AravindGopala avatar AravindGopala commented on May 9, 2024

Hi @wulfgarpro, @rubiot:
I am having a similar issue with fprintf mocking, In the below code I tried to mock the fprintf from the standard library, unless I provided at least two parameters in the varArg list, the mocked fprintf is not being called, So the SampleTestPass will successfully call the mock fprintf function but the SampleTestFAIL will never call the mock function instead it calls the fprintf from the standard library as only one argument is provided in the varArgList.

can someone suggest what I am doing wrong:

#include "unity.h"
#include "unity_fixture.h"
#include "fff.h"
DEFINE_FFF_GLOBALS;

FAKE_VALUE_FUNC_VARARG(int, fprintf, FILE *, const char *, ...);

// This test will  call the mocked fprintf
TEST(SampleTestGroup, SampleTestPass)
{
    FILE * fd = fopen("/dev/stdout", "w");
    fprintf(fd,"%s, %s", "first_var_arg", "second_var_arg");
    printf("Datla-> (%s)", fprintf_fake.arg1_val); // This will print "Datla -> (%s, %s)"
    TEST_FAIL(); // comment this line if needed
}

// This test will fail to call the mocked fprintf
TEST(SampleTestGroup, SampleTestFAIL)
{
    FILE * fd = fopen("/dev/stdout", "w");
    fprintf(fd,"%s", "first_var_arg");
    printf("Datla-> (%s)", fprintf_fake.arg1_val); // This will print "Datla -> (null)" as mocked fprintf is not called 
    TEST_FAIL(); // comment this line if needed
}

TEST_GROUP_RUNNER(SampleTestGroup)
{
    RUN_TEST_CASE(SampleTestGroup, SampleTestPass);
    RUN_TEST_CASE(SampleTestGroup, SampleTestFAIL);

}
static void RunAllTests() {
    RUN_TEST_GROUP(SampleTestGroup);
}

/**
 * @brief Main entry point
 */
int main(int argc, char const *argv[])
{
    return UnityMain(argc, argv, RunAllTests);
}

I am wondering why the SampleTestFAIL is not calling the mocked fprintf.

from fff.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    πŸ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. πŸ“ŠπŸ“ˆπŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❀️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.