Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[FR]: Add WhenStaticCastTo matcher #4710

Open
dkaszews opened this issue Jan 30, 2025 · 0 comments · May be fixed by #4719
Open

[FR]: Add WhenStaticCastTo matcher #4710

dkaszews opened this issue Jan 30, 2025 · 0 comments · May be fixed by #4719

Comments

@dkaszews
Copy link

dkaszews commented Jan 30, 2025

Does the feature exist in the most recent commit?

No

Why do we need this feature?

The existing (Safe)MatcherCast and WhenDynamicCastTo do not work well for pointers such as void*, since the former requires the underlying types to be convertible (void => T) and the second simply does not allow void* as the argument.

This is especially needed when mocking APIs which type-erase their arguments as void*, e.g. something like int api(enum message_type type, void* arg).

Example usage:

const auto matcher = WhenStaticCastTo<ReadMessage*>(Field(&ReadMessage::length, 10));
EXPECT_CALL(mock, api(ReadMessageId, matcher)).WillOnce(Return(0));

Describe the proposal.

I was perfectly able to get the expected behavior by simply duplicating all of WhenDynamicCastTo with its underlying implementation and just switching dynamic_cast to static_cast with the corresponding message. Real implementation should probably avoid such duplication and instead just abstract away the casting into something like:

struct static_caster {
    static constexpr const char* name = "static";
    template <typename To, typename From>
    To cast(From* from) { return static_cast<To>(from); }
    // plus overload references
};

// same for dynamic

Other types of casts such as reinterpret, const and C++20 bit should also be considered.

Is the feature specific to an operating system, compiler, or build system version?

No

@dkaszews dkaszews linked a pull request Feb 5, 2025 that will close this issue
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant