| 1 |
// Copyright (c) Microsoft. All rights reserved. |
| 2 |
// Licensed under the MIT license. See LICENSE file in the project root for full license information. |
| 3 |
|
| 4 |
#ifndef STRICTUNORDEREDCALLCOMPARER_H |
| 5 |
#define STRICTUNORDEREDCALLCOMPARER_H |
| 6 |
|
| 7 |
#pragma once |
| 8 |
|
| 9 |
#include "stdafx.h" |
| 10 |
#include "mockcallcomparer.h" |
| 11 |
#include "mockmethodcallbase.h" |
| 12 |
|
| 13 |
|
| 14 |
template<class T> |
| 15 |
class CStrictUnorderedCallComparer : virtual public CMockCallComparer, public T |
| 16 |
{ |
| 17 |
public: |
| 18 |
CStrictUnorderedCallComparer(_In_ AUTOMATIC_CALL_COMPARISON performAutomaticCallComparison = AUTOMATIC_CALL_COMPARISON_ON) |
| 19 |
{ |
| 20 |
T::SetPerformAutomaticCallComparison(performAutomaticCallComparison); |
| 21 |
T::SetMockCallComparer(this); |
| 22 |
T::CMockCallRecorder::m_MockCallComparer->SetIgnoreUnexpectedCalls(false); |
| 23 |
} |
| 24 |
|
| 25 |
virtual bool IsUnexpectedCall(_In_ const CMockMethodCallBase* actualCall) |
| 26 |
{ |
| 27 |
if (T::CMockCallRecorder::m_MockCallComparer->GetIgnoreUnexpectedCalls()) |
| 28 |
{ |
| 29 |
return actualCall->m_AlwaysReport; |
| 30 |
} |
| 31 |
else |
| 32 |
{ |
| 33 |
return !actualCall->HasMatch(); |
| 34 |
} |
| 35 |
} |
| 36 |
|
| 37 |
virtual bool IsMissingCall(_In_ const CMockMethodCallBase* expectedCall) |
| 38 |
{ |
| 39 |
return ((!expectedCall->HasMatch()) && |
| 40 |
(expectedCall->m_ExpectedTimes > 0) && |
| 41 |
(!expectedCall->m_IgnoreAllCalls) && |
| 42 |
(!expectedCall->m_OnlySpecifiesActions)); |
| 43 |
} |
| 44 |
|
| 45 |
virtual CMockMethodCallBase* MatchCall(std::vector<CMockMethodCallBase*>& expectedCalls, |
| 46 |
CMockMethodCallBase* actualCall) |
| 47 |
{ |
| 48 |
CMockMethodCallBase* result = NULL; |
| 49 |
|
| 50 |
// go through all items in the expected call array |
| 51 |
for (size_t i = 0; i < expectedCalls.size(); i++) |
| 52 |
{ |
| 53 |
CMockMethodCallBase* expectedCall = expectedCalls[i]; |
| 54 |
|
| 55 |
// if the expected call says all calls of this type should be ignored, |
| 56 |
// then match |
| 57 |
if ((expectedCall->m_IgnoreAllCalls) && |
| 58 |
(*expectedCall == *actualCall)) |
| 59 |
{ |
| 60 |
actualCall->m_MatchedCall = expectedCall; |
| 61 |
expectedCall->m_MatchedCall = actualCall; |
| 62 |
break; |
| 63 |
} |
| 64 |
else |
| 65 |
{ |
| 66 |
// if the expected call has not yet been matched |
| 67 |
if ((!expectedCall->HasMatch() || expectedCall->m_ExactExpectedTimes) && |
| 68 |
(!expectedCall->m_OnlySpecifiesActions) && |
| 69 |
// and it matches the actual call |
| 70 |
(*expectedCall == *actualCall)) |
| 71 |
{ |
| 72 |
// check whether the number of expected calls of this type have been found |
| 73 |
// in the actual calls array |
| 74 |
if (expectedCall->m_MatchedTimes < expectedCall->m_ExpectedTimes) |
| 75 |
{ |
| 76 |
// record that we found yet another actual call matching the expected call |
| 77 |
expectedCall->m_MatchedTimes++; |
| 78 |
if (expectedCall->m_MatchedTimes == expectedCall->m_ExpectedTimes) |
| 79 |
{ |
| 80 |
// mark the expected call as fully matched |
| 81 |
// (no other actual call will be matched against it) |
| 82 |
expectedCall->m_MatchedCall = actualCall; |
| 83 |
} |
| 84 |
|
| 85 |
// mark the actual call as having a match |
| 86 |
actualCall->m_MatchedCall = expectedCall; |
| 87 |
result = expectedCall; |
| 88 |
} |
| 89 |
else |
| 90 |
{ |
| 91 |
// too many calls, check if exact is specified |
| 92 |
if (expectedCall->m_ExactExpectedTimes) |
| 93 |
{ |
| 94 |
actualCall->m_AlwaysReport = true; |
| 95 |
} |
| 96 |
} |
| 97 |
break; |
| 98 |
} |
| 99 |
} |
| 100 |
} |
| 101 |
|
| 102 |
// have a second loop to see if we need to get a later set return value |
| 103 |
for (int i = (int)expectedCalls.size() - 1; i >= 0; i--) |
| 104 |
{ |
| 105 |
if ((expectedCalls[(unsigned int)i]->m_OnlySpecifiesActions) && |
| 106 |
(*expectedCalls[(unsigned int)i] == *actualCall)) |
| 107 |
{ |
| 108 |
result = expectedCalls[(unsigned int)i]; |
| 109 |
break; |
| 110 |
} |
| 111 |
} |
| 112 |
|
| 113 |
return result; |
| 114 |
} |
| 115 |
}; |
| 116 |
|
| 117 |
#endif // STRICTUNORDEREDCALLCOMPARER_H |