Debian Codegraph @

src/vendor/azure-uamqp-c/deps/azure-c-shared-utility/testtools/micromock/inc/strictorderedcallcomparer.h

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