简体   繁体   中英

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:

...otherwise the behavior is undefined. In particular, you mustn't interleave EXPECT_CALL()s and calls to the mock functions" ( https://github.com/google/googletest/blob/master/googlemock/docs/for_dummies.md#using-mocks-in-tests )

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?

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"

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM