Gabriel Staples
Gabriel Staples

Reputation: 53055

Googletest (gtest) / googlemock (gmock): Why is "interleav[ing] `EXPECT_CALL()`s and calls to the mock functions" undefined behavior?

Despite writing

...I still don't know why Google says this is undefined behavior (source: https://google.github.io/googletest/gmock_for_dummies.html#expectation-ordering):

...otherwise the behavior is undefined. Do not alternate between calls to EXPECT_CALL() and calls to the mock functions, and do not set any expectations on a mock after passing the mock to an API.

They simply glossed over my issue above, restating it was in fact undefined behavior, but without explaining why.

Can anyone explain the details of why and how it is undefined behavior?

Upvotes: 0

Views: 478

Answers (1)

user4442671
user4442671

Reputation:

Undefined Behavior (UB) in general doesn't mean that it doesn't work. It means that there is no guarantee that it'll work.

Another way to put it is that it's more a contract between the library writer and user than a description of what the library does.

All UB means here is basically "don't rely on this working". If it does, then just count yourself lucky, but don't expect it to keep working when anything else changes.

So in essence:

I still don't know why Google says this is undefined behavior

They don't have to motivate that decision, since it's just part of the API design.

Now, if the question is "Why did Google choose to make this undefined behavior?" Then that's a different matter.

Framed like this, the answer might as well be: "Providing the guarantee that this will work in EVERY usage scenario is too much effort for not enough gain, so we won't commit to it"

Upvotes: 3

Related Questions