In my experience unit testing is pretty repetitive. I usually need to to set up my unit under test with the same set of expectations which may become pretty sophisticated. To make reading the code easier and reduce refactoring overhead in the future I tend to write functions which encapsulate a specific setup of expectations.
Often also the need arises to pass in Expectations into such functions to sequence expectations, like such:
While this allows for passing no or one expectation there is no easy way to pass more than one expectation. The following call is not possible:
auto const barExpectation = expectCallsBar(mockFoo, { previous1, previous2 });
The obvious solution is to add a constructor taking an std::initializer_list<Expectation> if supported by the compiler.
Describe the proposal.
Add the following constructor to ExpectationSet:
ExpectationSet(std::initializer_list<value_type> const il)
: expectations_(il)
{}
This allows for constructing an ExpectationSet inline in function calls like such:
expectCallsBar(mockFoo, { previous1, previous2 });
Without this change one needs to write the following: (braces to introduce a scope)
Does the feature exist in the most recent commit?
No
Why do we need this feature?
In my experience unit testing is pretty repetitive. I usually need to to set up my unit under test with the same set of expectations which may become pretty sophisticated. To make reading the code easier and reduce refactoring overhead in the future I tend to write functions which encapsulate a specific setup of expectations. Often also the need arises to pass in
Expectation
s into such functions to sequence expectations, like such:To make this more general I usually use
ExpectationSet
s to be able to give the empty set as a default argument:While this allows for passing no or one expectation there is no easy way to pass more than one expectation. The following call is not possible:
auto const barExpectation = expectCallsBar(mockFoo, { previous1, previous2 });
The obvious solution is to add a constructor taking an
std::initializer_list<Expectation>
if supported by the compiler.Describe the proposal.
Add the following constructor to
ExpectationSet
:This allows for constructing an
ExpectationSet
inline in function calls like such:expectCallsBar(mockFoo, { previous1, previous2 });
Without this change one needs to write the following: (braces to introduce a scope)This is quite a mouthful and not very desirable.
An obvious workaround is a helper function which compiles the set for you:
Is the feature specific to an operating system, compiler, or build system version?
<initializer_list>
needs a compiler which supports at least C++11, but GTest is C++14 already.