Skip to content

Commit 8a2a52a

Browse files
committed
Update manual
1 parent a90845c commit 8a2a52a

File tree

1 file changed

+132
-29
lines changed

1 file changed

+132
-29
lines changed

MANUAL.md

+132-29
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,8 @@ CppUMockGen can automatically generate for each mockable function its correspond
3232

3333
To generate expectation helper functions, pass the path where you want the files with the expectation helper functions to be generated as output using the `-e` / `--expect-output` option (additionally to or instead of the `-m` option). If the output option parameter is a directory path (i.e. ending with a path separator) then the output file names will be deduced from the input file name by replacing its extension by *"_expect.cpp"* / *"_expect.hpp"* and appended to the passed directory. If the output option parameter is empty it is equivalent to passing the current directory. If the output option parameter is **'@'**, the mock is printed to the console. In other cases the output file names will be deduced from the output option parameter by replacing its extension by *".cpp"* / *".hpp"*.
3434

35+
Expectation helper functions are declared inside root namespace `expect`, using the same namespace hierarchy than the mocked function but appending character **'$'** to each original namespace to avoid name clashes. Additionally, for member functions an innermost namespace hierarchy is declared according to the function class nesting hierarchy, also appending **'$'** to class names (e.g., expectation helper for member functions of class `foo` inside namespace `bar ` are declared inside namespace `expect::bar$::foo$`).
36+
3537
## Command-Line Options
3638

3739
`CppUMockGen [OPTION...] [<input>]`
@@ -62,12 +64,12 @@ CppUMockGen deduces the <code>with<i>&lt;MockedType></i>Parameter</code>/<code>r
6264
| `enum` <br> `enum class` | `withIntParameter` | `returnIntValue` |
6365
| `class` <br> `struct` | `withParameterOfType` | `returnConstPointerValue` |
6466
| `const char *` | `withStringParameter` | `returnStringValue` |
65-
| `const <PrimitiveType> *` <br> `const <PrimitiveType> &` | `withConstPointerParameter` | `returnConstPointerValue` |
66-
| `<PrimitiveType> *` <br> `<PrimitiveType> &` | `withOutputParameter` | `returnPointerValue` |
67-
| `<PrimitiveType> &&` | _Same as_ `<PrimitiveType>` | `returnPointerValue` |
67+
| <code>const <i>&lt;PrimitiveType></i> &ast;</code> <br> <code>const <i>&lt;PrimitiveType></i> &</code> | `withConstPointerParameter` | `returnConstPointerValue` |
68+
| <code><i>&lt;PrimitiveType></i> &ast;</code> <br> <code><i>&lt;PrimitiveType></i> &</code> | `withOutputParameter` | `returnPointerValue` |
69+
| <code><i>&lt;PrimitiveType></i> &&</code> | _Same as_ <code><i>&lt;PrimitiveType></i></code> | `returnPointerValue` |
6870
| `const class *` <br> `const struct *` <br> `const class &` <br> `const struct &` | `withParameterOfType` | `returnConstPointerValue` |
6971
| `class *` <br> `struct *` <br> `class &` <br> `struct &` <br> `class &&` <br> `struct &&` | `withOutputParameterOfType` | `returnPointerValue` |
70-
| `void *` <br> `<PrimitiveType> * *` <br> `<PrimitiveType> * &` | `withPointerParameter` | `returnPointerValue` |
72+
| `void *` <br> <code><i>&lt;PrimitiveType></i> &ast; &ast;</code> <br> <code><i>&lt;PrimitiveType></i> &ast; &</code> | `withPointerParameter` | `returnPointerValue` |
7173

7274
##### Handling of Typedefs
7375

@@ -77,64 +79,80 @@ If the resulting call for a parameter is `withParameterOfType` or `withOutputPar
7779

7880
## Overriding Mocked Parameter and Return Types
7981

82+
To override the <code>with<i>&lt;MockedType></i>Parameter</code>/<code>return<i>&lt;MockedType></i>Value</code> method to call on `MockActualCall` objects, override specifications can be passed on the command line. The override specifications can be for specific parameters or return values of specific mocked functions, or can be generic (based on data type).
83+
84+
Override specifications also define the <code>with<i>&lt;MockedType></i>Parameter</code> method call and the specific <code>andReturnValue</code> override method call on `MockExpectedCall` objects.
85+
8086
### Specific Mocked Parameter / Return Type Override
8187

82-
To override the <code>with<i>&lt;MockedType></i>Parameter</code>/<code>return<i>&lt;MockedType></i>Value</code> method to call on `MockActualCall` objects for a specific parameter or return value of a mocked function, an override specification can be passed on the command line using the `-p` / `--param-override` option.
88+
A function/parameter specifc override specification is passed on the command line using the `-p` / `--param-override` option.
8389

8490
##### Specific parameter type override
8591

8692
Specific parameter type override options take the form:
8793

88-
&ensp; <code><i>&lt;QualifiedFunctionName></i><b>'#'</b><i>&lt;ParameterName></i><b>'='</b><i>&lt;MockedType></i>[<b>'&#47;'</b><i>&lt;ArgExpr></i>]</code>
94+
&ensp; <code><i>&lt;QualifiedFunctionName></i><b>'#'</b><i>&lt;ParameterName></i><b>'='</b><i>&lt;MockedType></i>[<b>'~'</b><i>&lt;ArgExpr></i>]</code>
8995

9096
Where:
9197
- <code><i>&lt;QualifiedFunctionName></i></code> is the fully qualified name of a mocked function, including namespace (e.g. `namespace1::class1::method1`).
9298
- <code><i>&lt;ParameterName></i></code> is the name of the parameter.
93-
- <code><i>&lt;MockedType></i></code> indicates the CppUMock type to use for the parameter, admitted values are: _Bool, Int, UnsignedInt, LongInt, UnsignedLongInt, Double, String, Pointer, ConstPointer, Output, Skip_.
94-
- When set to _Skip_ the corresponding method call on the `MockActualCall` object for the parameter will be skipped.
95-
- <code><i>&lt;ArgExpr></i></code> is an optional argument expression that must contain the **'$'** character. If defined, it will be passed as the argument for the CppUMock actual call parameter method, replacing **'$'** by the mocked function parameter name.
99+
- <code><i>&lt;MockedType></i></code> indicates the CppUMock type to use for the parameter, admitted values are: _Bool, Int, UnsignedInt, LongInt, UnsignedLongInt, Double, String, Pointer, ConstPointer, Output, Skip_, or an <code>InputOfType</code> or <code>OutputOfType</code> option.
100+
- When set to _Skip_ the corresponding method call on the `MockActualCall` and `MockExpectedCall` objects for the parameter will be skipped, and the parameter will not appear in the signature of expectation helper functions.
101+
- An <code>InputOfType</code> option takes the form <b>'InputOfType:'</b><code><i>&lt;DeclaredType></i></code>[<b>'&lt;'</b><code><i>&lt;ExpectationHelperType></i></code>], where:
102+
- <code><i>&lt;DeclaredType></i></code> indicates the type name to be passed to the <code>withParameterOfType</code> method of <code>MockActualCall</code> and <code>MockExpectedCall</code>.
103+
- <code><i>&lt;ExpectationHelperType></i></code> optionally indicates an override type to use as the argument of expectation helper functions.
104+
- An <code>OutputOfType</code> option takes the form <b>'OutputOfType:'</b><code><i>&lt;DeclaredType></i></code>[<b>'&lt;'</b><code><i>&lt;ExpectationHelperType></i></code>], where:
105+
- <code><i>&lt;DeclaredType></i></code> indicates the type name to be passed to the <code>withOutputParameterOfType</code> method of <code>MockActualCall</code> and to the <code>withOutputParameterOfTypeReturning</code> method of <code>MockExpectedCall</code>.
106+
- <code><i>&lt;ExpectationHelperType></i></code> optionally indicates an override type to use as the argument of expectation helper functions.
107+
- <code><i>&lt;ArgExpr></i></code> is an optional C++ expression that must contain the **'$'** placeholder character. If defined, it will be passed as the argument for the CppUMock actual call parameter method, replacing **'$'** by the mocked function parameter name.
96108

97109
##### Specific return type override
98110

99111
Specific return type override options take the form:
100112

101-
&ensp; <code><i>&lt;QualifiedFunctionName></i><b>'@='</b><i>&lt;MockedType></i>[<b>'&#47;'</b><i>&lt;RetExpr></i>]</code>
113+
&ensp; <code><i>&lt;QualifiedFunctionName></i><b>'@='</b><i>&lt;MockedType></i>[<b>'~'</b><i>&lt;RetExpr></i>]</code>
102114

103115
Where:
104116
- <code><i>&lt;QualifiedFunctionName></i></code> is the fully qualified name of a mocked function, including namespace (e.g. `namespace1::class1::method1`).
105117
- <code><i>&lt;MockedType></i></code> indicates the CppUMock type to use for the return value, admitted values are: _Bool, Int, UnsignedInt, LongInt, UnsignedLongInt, Double, String, Pointer, ConstPointer_.
106-
- <code><i>&lt;ArgExpr></i></code> is an optional argument expression that must contain the **'$'** character. If defined, it will be used as the return value of the mocked function, replacing **'$'** by the CppUMock actual call sequence.
118+
- <code><i>&lt;RetExpr></i></code> is an optional C++ expression that must contain the **'$'** placeholder character. If defined, it will be used as the return value of the mocked function, replacing **'$'** by the CppUMock actual call sequence.
107119

108120
### Generic Mocked Parameter / Return Type Override
109121

110-
To override the <code>with<i>&lt;MockedType></i>Parameter</code>/<code>return<i>&lt;MockedType></i>Value</code> method to call on `MockActualCall` objects for parameter or return values of a mocked function based on its data type, an override specification can be passed on the command line using the `-t` / `--type-override` option.
122+
A generic override specification is passed on the command line using the `-t` / `--type-override` option.
111123

112124
##### Generic parameter type override
113125

114126
Generic parameter type override options take the form:
115127

116-
&ensp; <code><b>'#'</b><i>&lt;ParameterType></i><b>'='</b><i>&lt;MockedType></i>[<b>'&#47;'</b><i>&lt;ArgExpr></i>]</code>
128+
&ensp; <code><b>'#'</b><i>&lt;ParameterType></i><b>'='</b><i>&lt;MockedType></i>[<b>'~'</b><i>&lt;ArgExpr></i>]</code>
117129

118130
Where:
119131
- <code><i>&lt;ParameterType></i></code> is a C/C++ data type.
120-
- <code><i>&lt;MockedType></i></code> indicates the CppUMock type to use for the parameter, admitted values are: _Bool, Int, UnsignedInt, LongInt, UnsignedLongInt, Double, String, Pointer, ConstPointer, Output, Skip_.
121-
- When set to _Skip_ the corresponding method call on the `MockActualCall` object for the parameter will be skipped.
122-
- <code><i>&lt;ArgExpr></i></code> is an optional argument expression that must contain the **'$'** character. If defined, it will be passed as the argument for the CppUMock actual call parameter method, replacing **'$'** by the mocked function parameter name.
132+
- <code><i>&lt;MockedType></i></code> indicates the CppUMock type to use for the parameter, admitted values are: _Bool, Int, UnsignedInt, LongInt, UnsignedLongInt, Double, String, Pointer, ConstPointer, Output, Skip_, or an <code>InputOfType</code> or <code>OutputOfType</code> option.
133+
- When set to _Skip_ the corresponding method call on the `MockActualCall` and `MockExpectedCall` objects for the parameter will be skipped, and the parameter will not appear in the signature of expectation helper functions.
134+
- An <code>InputOfType</code> option takes the form <b>'InputOfType:'</b><code><i>&lt;DeclaredType></i></code>[<b>'&lt;'</b><code><i>&lt;ExpectationHelperType></i></code>], where:
135+
- <code><i>&lt;DeclaredType></i></code> indicates the type name to be passed to the <code>withParameterOfType</code> method of <code>MockActualCall</code> and <code>MockExpectedCall</code>.
136+
- <code><i>&lt;ExpectationHelperType></i></code> optionally indicates an override type to use as the argument of expectation helper functions.
137+
- An <code>OutputOfType</code> option takes the form <b>'OutputOfType:'</b><code><i>&lt;DeclaredType></i></code>[<b>'&lt;'</b><code><i>&lt;ExpectationHelperType></i></code>], where:
138+
- <code><i>&lt;DeclaredType></i></code> indicates the type name to be passed to the <code>withOutputParameterOfType</code> method of <code>MockActualCall</code> and to the <code>withOutputParameterOfTypeReturning</code> method of <code>MockExpectedCall</code>.
139+
- <code><i>&lt;ExpectationHelperType></i></code> optionally indicates an override type to use as the argument of expectation helper functions.
140+
- <code><i>&lt;ArgExpr></i></code> is an optional C++ expression that must contain the **'$'** placeholder character. If defined, it will be passed as the argument for the CppUMock actual call parameter method, replacing **'$'** by the mocked function parameter name.
123141

124142
##### Generic return type override
125143

126144
Generic return type override options take the form:
127145

128-
&ensp; <code><b>'@'</b><i>&lt;ReturnType></i><b>'='</b><i>&lt;MockedType></i>[<b>'&#47;'</b><i>&lt;ArgExpr></i>]</code>
146+
&ensp; <code><b>'@'</b><i>&lt;ReturnType></i><b>'='</b><i>&lt;MockedType></i>[<b>'~'</b><i>&lt;RetExpr></i>]</code>
129147

130148
Where:
131149
- <code><i>&lt;ReturnType></i></code> is a C/C++ data type.
132150
- <code><i>&lt;MockedType></i></code> indicates the CppUMock type to use for the return value, admitted values are: _Bool, Int, UnsignedInt, LongInt, UnsignedLongInt, Double, String, Pointer, ConstPointer_.
133-
- <code><i>&lt;ArgExpr></i></code> is an optional argument expression that must contain the **'$'** character. If defined, it will be used as the return value of the mocked function, replacing **'$'** by the CppUMock actual call sequence.
151+
- <code><i>&lt;RetExpr></i></code> is an optional C++ expression that must contain the **'$'** character. If defined, it will be used as the return value of the mocked function, replacing **'$'** by the CppUMock actual call sequence.
134152

135-
#### Mocked Type Override Examples
153+
### Mocked Type Override Examples
136154

137-
###### Example 1: Specific parameter type override
155+
##### Example 1: Specific parameter type override
138156

139157
Assuming that we want to mock the following class:
140158

@@ -151,20 +169,29 @@ namespace namespace1
151169
If we want to override parameter `p1` to check just the pointed value, we can use the following option:
152170

153171
```less
154-
CppUMockGen <InputFilename> -p namespace1::class1::method1#p1=Int/*$
172+
CppUMockGen <InputFilename> -m -e -p namespace1::class1::method1#p1=Int~*$
155173
```
156174

157175
The generated mock would be as follows:
158176

159177
```cpp
160178
int namespace1::class1::method1(int * p1)
161179
{
162-
return mock().actualCall("namespace1::class1::method1").withIntParameter("p1", *p1).returnIntValue();
180+
return mock().actualCall("namespace1::class1::method1").onObject(this).withIntParameter("p1", *p1).returnIntValue();
163181
}
164182
```
165183
184+
The generated expectation helpers signature would be as follows:
185+
186+
```cpp
187+
namespace expect { namespace namespace1$ { namespace class1$ {
188+
MockExpectedCall& method1(CppUMockGen::Parameter<const namespace1::class1*> __object__, CppUMockGen::Parameter<int> p1, int __return__);
189+
MockExpectedCall& method1(unsigned int __numCalls__, CppUMockGen::Parameter<const namespace1::class1*> __object__, CppUMockGen::Parameter<int> p1, int __return__);
190+
} } }
191+
```
192+
166193
&ensp;
167-
###### Example 2: Specific parameter and return type overrides
194+
##### Example 2: Specific parameter and return type overrides
168195

169196
Assuming that we want to mock the following function:
170197

@@ -175,20 +202,29 @@ char* function2( const char *p );
175202
If we want to override parameter `p` because it is not really a string and the return type because the real function is ill-designed and really just returns pointers to unmutable strings, we can use the following options:
176203
177204
```less
178-
CppUMockGen <InputFilename> -p function2#p=ConstPointer -p function2@=String/static_cast<char*>($)
205+
CppUMockGen <InputFilename> -m -e -p function2#p=ConstPointer -p function2@=String~const_cast<char*>($)
179206
```
180207

181208
The generated mock would be as follows:
182209

183210
```cpp
184211
char * function2(const char * p)
185212
{
186-
return static_cast<char*>(mock().actualCall("function2").withConstPointerParameter("p", p).returnStringValue());
213+
return const_cast<char*>(mock().actualCall("function2").withConstPointerParameter("p", p).returnStringValue());
214+
}
215+
```
216+
217+
The generated expectation helpers signature would be as follows:
218+
219+
```cpp
220+
namespace expect {
221+
MockExpectedCall& function2(CppUMockGen::Parameter<const void*> p, const char* __return__);
222+
MockExpectedCall& function2(unsigned int __numCalls__, CppUMockGen::Parameter<const void*> p, const char* __return__);
187223
}
188224
```
189225

190226
&ensp;
191-
###### Example 3: Generic parameter and return type overrides
227+
##### Example 3: Generic parameter and return type overrides
192228

193229
Assuming that we want to mock the following functions:
194230

@@ -200,7 +236,7 @@ std::string functionB( const std::string &p1, std::string &p2 );
200236
If we want to override all parameters of type `const std::string&` and `std::string` return types to be used as strings, we can use the following options:
201237
202238
```less
203-
CppUMockGen <InputFilename> -t "#const std::string &=String/$.c_str()" -t @std::string=String
239+
CppUMockGen <InputFilename> -m -e -t "#const std::string &=String~$.c_str()" -t @std::string=String
204240
```
205241

206242
The generated mock would be as follows:
@@ -217,8 +253,19 @@ std::string functionB(const std::string & p1, std::string & p2)
217253
}
218254
```
219255
256+
The generated expectation helpers signature would be as follows:
257+
258+
```cpp
259+
namespace expect {
260+
MockExpectedCall& functionA(CppUMockGen::Parameter<const char*> p, const char* __return__);
261+
MockExpectedCall& functionA(unsigned int __numCalls__, CppUMockGen::Parameter<const char*> p, const char* __return__);
262+
MockExpectedCall& functionB(CppUMockGen::Parameter<const char*> p1, std::string & p2, const char* __return__);
263+
MockExpectedCall& functionB(unsigned int __numCalls__, CppUMockGen::Parameter<const char*> p1, std::string & p2, const char* __return__);
264+
}
265+
```
266+
220267
&ensp;
221-
###### Example 4: Skipping a parameter
268+
##### Example 4: Skipping a parameter
222269

223270
Assuming that we want to mock the following function:
224271

@@ -229,7 +276,7 @@ void foo( const char *p1, char *p2, int p3 );
229276
If we want to skip parameter `p2`, we can use the following options:
230277
231278
```less
232-
CppUMockGen <InputFilename> -p foo#p2=Skip
279+
CppUMockGen <InputFilename> -m -e -p foo#p2=Skip
233280
```
234281

235282
The generated mock would be as follows:
@@ -240,3 +287,59 @@ void foo(const char * p1, char *, int p3)
240287
mock().actualCall("foo").withStringParameter("p1", p1).withIntParameter("p3", p3);
241288
}
242289
```
290+
291+
The generated expectation helpers signature would be as follows:
292+
293+
```cpp
294+
namespace expect {
295+
MockExpectedCall& foo(CppUMockGen::Parameter<const char *> p1, CppUMockGen::Parameter<int> p3);
296+
MockExpectedCall& foo(unsigned int __numCalls__, CppUMockGen::Parameter<const char *> p1, CppUMockGen::Parameter<int> p3);
297+
}
298+
```
299+
300+
&ensp;
301+
##### Examle 5: Typed output parameter with asymetrical copier
302+
303+
Assuming that we want to mock the following function:
304+
305+
```cpp
306+
void bar( std::ostream & output );
307+
```
308+
309+
As it is hard (if not even impossible) to copy an output stream into another, we can define an asymetrical CppUMock copier that outputs a string into the output stream:
310+
311+
```cpp
312+
class StdOstreamCopier : public MockNamedValueCopier
313+
{
314+
public:
315+
virtual void copy(void* out, const void* in)
316+
{
317+
*(std::ostream*)out << *(const std::string*)in;
318+
}
319+
};
320+
```
321+
322+
CppUMockGen normal behavior with typed output parameters is to expect a symetrical copier. To override the normal behavior and force usage of an asymetrical copier, we can use the following options:
323+
324+
```less
325+
CppUMockGen <InputFilename> -m -e -t "#std::ostream &=OutputOfType:std::ostream<std::string~&$"
326+
```
327+
328+
The generated mock would be as follows:
329+
330+
```cpp
331+
void bar(std::ostream & output)
332+
{
333+
mock().actualCall("bar").withOutputParameterofType("std::ostream", "output", &output);
334+
}
335+
```
336+
337+
The generated expectation helpers would be as follows:
338+
339+
```cpp
340+
namespace expect {
341+
MockExpectedCall& bar(const std::string * output);
342+
MockExpectedCall& bar(unsigned int __numCalls__, const std::string * output);
343+
}
344+
```
345+

0 commit comments

Comments
 (0)