| 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 MOCKMETHODCALL_H |
| 5 |
#define MOCKMETHODCALL_H |
| 6 |
|
| 7 |
#pragma once |
| 8 |
|
| 9 |
#include "mockmethodcallbase.h" |
| 10 |
#include "mockresultvalue.h" |
| 11 |
#include "micromocktestrunnerhooks.h" |
| 12 |
#include "mockcallargumentbase.h" |
| 13 |
#include "micromockexception.h" |
| 14 |
|
| 15 |
template<typename returnType> |
| 16 |
class CMockMethodCall : public CMockMethodCallBase |
| 17 |
{ |
| 18 |
public: |
| 19 |
CMockMethodCall(_In_ std::tstring methodName, _In_ size_t argCount = 0, CMockCallArgumentBase** arguments = NULL) : |
| 20 |
CMockMethodCallBase(methodName, argCount, arguments) |
| 21 |
{ |
| 22 |
} |
| 23 |
|
| 24 |
CMockMethodCall<returnType>& IgnoreAllArguments() |
| 25 |
{ |
| 26 |
for (size_t i = 0; i < m_MockCallArguments.size(); i++) |
| 27 |
{ |
| 28 |
IgnoreArgument(i + 1); |
| 29 |
} |
| 30 |
|
| 31 |
return *this; |
| 32 |
} |
| 33 |
CMockMethodCall<returnType>& NeverInvoked() |
| 34 |
{ |
| 35 |
return ExpectedTimesExactly(0); |
| 36 |
} |
| 37 |
|
| 38 |
CMockMethodCall<returnType>& IgnoreArgument(_In_ size_t argumentNo) |
| 39 |
{ |
| 40 |
if ((argumentNo > 0) && |
| 41 |
(argumentNo <= m_MockCallArguments.size())) |
| 42 |
{ |
| 43 |
m_MockCallArguments[argumentNo - 1]->SetIgnored(true); |
| 44 |
} |
| 45 |
else |
| 46 |
{ |
| 47 |
MOCK_THROW(CMicroMockException(MICROMOCK_EXCEPTION_INVALID_ARGUMENT, |
| 48 |
_T("Invalid argument number supplied to IgnoreArgument"))); |
| 49 |
} |
| 50 |
|
| 51 |
return *this; |
| 52 |
} |
| 53 |
|
| 54 |
CMockMethodCall<returnType>& AddExtraCallArgument(_In_ CMockCallArgumentBase* callArgument) |
| 55 |
{ |
| 56 |
m_MockCallArguments.push_back(callArgument); |
| 57 |
return *this; |
| 58 |
} |
| 59 |
|
| 60 |
CMockMethodCall<returnType>& ValidateArgument(_In_ size_t argumentNo) |
| 61 |
{ |
| 62 |
if ((argumentNo > 0) && |
| 63 |
(argumentNo <= m_MockCallArguments.size())) |
| 64 |
{ |
| 65 |
m_MockCallArguments[argumentNo - 1]->SetIgnored(false); |
| 66 |
} |
| 67 |
else |
| 68 |
{ |
| 69 |
MOCK_THROW(CMicroMockException(MICROMOCK_EXCEPTION_INVALID_ARGUMENT, |
| 70 |
_T("Invalid argument number supplied to ValidateArgument"))); |
| 71 |
} |
| 72 |
return *this; |
| 73 |
} |
| 74 |
|
| 75 |
template<typename injectedReturnType> |
| 76 |
CMockMethodCall<returnType>& SetReturn(_In_opt_ injectedReturnType returnValue) |
| 77 |
{ |
| 78 |
if (NULL != m_ReturnValue) |
| 79 |
{ |
| 80 |
delete m_ReturnValue; |
| 81 |
} |
| 82 |
|
| 83 |
m_ReturnValue = new CMockResultValue<returnType>(returnValue); |
| 84 |
|
| 85 |
return *this; |
| 86 |
} |
| 87 |
|
| 88 |
template<typename injectedReturnType> |
| 89 |
CMockMethodCall<returnType>& SetFailReturn(_In_opt_ injectedReturnType failReturnValue) |
| 90 |
{ |
| 91 |
if (NULL != m_FailReturnValue) |
| 92 |
{ |
| 93 |
delete m_FailReturnValue; |
| 94 |
} |
| 95 |
|
| 96 |
m_FailReturnValue = new CMockResultValue<returnType>(failReturnValue); |
| 97 |
|
| 98 |
return *this; |
| 99 |
} |
| 100 |
|
| 101 |
CMockMethodCall<returnType>& OnlySpecifiesActions() |
| 102 |
{ |
| 103 |
m_OnlySpecifiesActions = true; |
| 104 |
return *this; |
| 105 |
} |
| 106 |
|
| 107 |
CMockMethodCall<returnType>& IgnoreAllCalls() |
| 108 |
{ |
| 109 |
m_IgnoreAllCalls = true; |
| 110 |
return *this; |
| 111 |
} |
| 112 |
|
| 113 |
CMockMethodCall<returnType>& ValidateArgumentBuffer(_In_ size_t argumentNo, |
| 114 |
const void* expectedBuffer, _In_ size_t bytesToValidate, _In_ size_t byteOffset = 0) |
| 115 |
{ |
| 116 |
if ((argumentNo > 0) && |
| 117 |
(argumentNo <= m_MockCallArguments.size()) && |
| 118 |
(NULL != expectedBuffer) && |
| 119 |
(bytesToValidate > 0)) |
| 120 |
{ |
| 121 |
m_MockCallArguments[argumentNo - 1]->AddBufferValidation(expectedBuffer, bytesToValidate, byteOffset); |
| 122 |
} |
| 123 |
else |
| 124 |
{ |
| 125 |
MOCK_THROW(CMicroMockException(MICROMOCK_EXCEPTION_INVALID_ARGUMENT, |
| 126 |
_T("Invalid arguments for ValidateArgumentBuffer."))); |
| 127 |
} |
| 128 |
|
| 129 |
return *this; |
| 130 |
} |
| 131 |
CMockMethodCall<returnType>& CopyOutArgumentBuffer(_In_ size_t argumentNo, |
| 132 |
const void* injectedBuffer, _In_ size_t bytesToCopy, _In_ size_t byteOffset = 0) |
| 133 |
{ |
| 134 |
if ((NULL == injectedBuffer) || |
| 135 |
(bytesToCopy == 0)) |
| 136 |
{ |
| 137 |
MOCK_THROW(CMicroMockException(MICROMOCK_EXCEPTION_INVALID_ARGUMENT, |
| 138 |
_T("Invalid arguments for CopyOutArgumentBuffer."))); |
| 139 |
} |
| 140 |
|
| 141 |
if ((argumentNo > 0) && |
| 142 |
(argumentNo <= m_MockCallArguments.size())) |
| 143 |
{ |
| 144 |
m_MockCallArguments[argumentNo - 1]->AddCopyOutArgumentBuffer(injectedBuffer, bytesToCopy, byteOffset); |
| 145 |
} |
| 146 |
|
| 147 |
return *this; |
| 148 |
} |
| 149 |
|
| 150 |
CMockMethodCall<returnType>& ExpectedTimesExactly(size_t expectedTimes) |
| 151 |
{ |
| 152 |
m_ExpectedTimes = expectedTimes; |
| 153 |
m_ExactExpectedTimes = true; |
| 154 |
return *this; |
| 155 |
} |
| 156 |
|
| 157 |
CMockMethodCall<returnType>& ExpectedAtLeastTimes(size_t expectedTimes) |
| 158 |
{ |
| 159 |
if (m_ExactExpectedTimes) |
| 160 |
{ |
| 161 |
MOCK_THROW(CMicroMockException(MICROMOCK_EXCEPTION_INVALID_CALL_MODIFIER_COMBINATION, |
| 162 |
_T("Tried to use ExpectedTimesExactly(or NeverInvoked) with ExpectedAtLeastTimes. Invalid combination."))); |
| 163 |
} |
| 164 |
|
| 165 |
m_ExpectedTimes = expectedTimes; |
| 166 |
return *this; |
| 167 |
} |
| 168 |
|
| 169 |
}; |
| 170 |
|
| 171 |
#endif // MOCKMETHODCALL_H |