4 ms·
Yet another example of "suspicious" test code which intention is very clear: TEST_METHOD(TestSwitchAndReselectCurrentlyActiveValueDoesNothing) {
by lifthrasiir 8y ago
Yet another example of "suspicious" test code which intention is very clear:
TEST_METHOD(TestSwitchAndReselectCurrentlyActiveValueDoesNothing)
{
// *snip*
vm.Value2Active = true;
// Establish base condition
VERIFY_ARE_EQUAL((UINT)1, mock->m_switchActiveCallCount);
VERIFY_ARE_EQUAL((UINT)1, mock->m_sendCommandCallCount);
VERIFY_ARE_EQUAL((UINT)1, mock->m_setCurUnitTypesCallCount);
vm.Value2Active = true;
VERIFY_ARE_EQUAL((UINT)1, mock->m_switchActiveCallCount);
VERIFY_ARE_EQUAL((UINT)1, mock->m_sendCommandCallCount);
VERIFY_ARE_EQUAL((UINT)1, mock->m_setCurUnitTypesCallCount);
}
> [...] The analyzer has detected two identical code fragments executing immediately one after the other. It looks like this code was written using the copy-paste technique and the programmer forgot to modify the copies.
Here Value2Active is a C++/CX property rigged to a PropertyChanged event [1] with a macro:
public ref class UnitConverterViewModel sealed: public Windows::UI::Xaml::Data::INotifyPropertyChanged
{
// other members omitted
OBSERVABLE_OBJECT();
OBSERVABLE_PROPERTY_RW(bool, Value2Active);
};
Can you be sure that this actually does not have a side effect? I bet not. Indeed, the intention of the original test is clear: a property, once set ("Switch") to a particular value, should not update other states even when set ("Reselect") to that same value---as it will really trigger a side effect! This idempotency guarantee is an important interface contract worth testing, no matter what a linter and clueless blog author says.
[1] https://github.com/Microsoft/calculator/blob/master/docs/ApplicationArchitecture.md#propertychanged-events https://github.com/Microsoft/calculator/blob/master/docs/App...
- amedvednikov 8y agoWow, I can imagine how much fun it is to debug such properties.