Giter VIP home page Giter VIP logo

Comments (5)

vberlier avatar vberlier commented on July 2, 2024

I wasn't aware of these builtins, looks like perfect forwarding is possible after all. However I'm still not sure how you would use va_start, va_arg and va_end to gather the variadic arguments in an array for inspection. It's up to each function to decide how many arguments should be provided, we can't figure out the number of arguments from the va_* macros alone.

from narmock.

vberlier avatar vberlier commented on July 2, 2024

Just published a new version that incorporates the GCC builtins to forward the variadic arguments instead of ignoring them. This allows you to provide variadic stubs. For now the variadic arguments are not recorded in last_call like the fixed arguments.

from narmock.

Munken avatar Munken commented on July 2, 2024

Nice additions!

I've outlined a rough sketch of how one could gather the variable arguments.
It requires a printf-like format string to specify the type of arguments to capture.
The arguments are then collected into a uint64_t array.
The user must then perform a cast when performing ASSERT_EQ.
main...Munken:varg-capture

This restricts the captured types to <= 64 bits. If this is too narrow then one could use a dummy struct instead.
Perhaps

struct narmock_vargs_capture_type {
  uint64_t a,b,c,d;
};

from narmock.

Munken avatar Munken commented on July 2, 2024

Just revised the last idea a bit.

Instead of using uint64_t and requiring a cast, I'm now using a union instead.

from narmock.

vberlier avatar vberlier commented on July 2, 2024

The idea of using a printf-like format string is interesting. Instead of parsing formatting syntax manually maybe it's worth considering just forwarding the arguments to vsnprintf() to simply capture the arguments as a string in a static buffer. It would be pretty easy to implement but you would only be able to make assertions about the resulting string and not the individual arguments.

Of course your proposal looks pretty good but I just need to experiment a bit with it. I'm also thinking about something more structured:

int a;
int b;
int c;

MOCK(sum_variadic)
    ->mock_implementation(fake_sum_variadic1)
    ->capture_variadic()
        ->arg_int(&a)
        ->arg_int(&b)
        ->arg_int(&c);

sum_variadic(3, &value, 1, 2, 3);

ASSERT_EQ(a, 1);
ASSERT_EQ(b, 2);
ASSERT_EQ(c, 3);

I also wonder if instead of setting up a static variadic capture you could have something like capture_variadic_from_count_argument(0) which would tell narmock the index of the fixed argument corresponding to the number of variadic arguments. Similarly something like capture_variadic_from_format_argument(0) which would do the same but the specified argument index would correspond to a format string argument from which narmock would dynamically deduce the number and types of variadic arguments.

I'm gonna try to play with some of these concepts later.

from narmock.

Related Issues (10)

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.