Skip to content

Commit 3ba340d

Browse files
committed
Merge pull request #29 from arstrube/master
Include new functionality and fix up some missing
2 parents 36e3b19 + 9cc6c8e commit 3ba340d

File tree

5 files changed

+359
-108
lines changed

5 files changed

+359
-108
lines changed

_includes/header.html

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,15 +22,17 @@ <h2>CppUTest unit testing and mocking framework for C/C++</h2>
2222
<div class="navbar navbar-inverse">
2323
<div class="navbar-inner">
2424
<a class="brand" href="index.html">CppUTest</a>
25+
<a class="brand" href='https://coveralls.io/github/arstrube/cpputest?branch=master'><img src='https://coveralls.io/repos/github/arstrube/cpputest/badge.svg?branch=master' alt='Coverage Status' /></a>
26+
2527
<ul class="nav">
2628
<li class="divider-vertical"></li>
2729
<li><a href="manual.html">Core Manual</a></li>
2830
<li class="divider-vertical"></li>
2931
<li><a href="mocking_manual.html">CppUMock Manual</a></li>
3032
<li class="divider-vertical"></li>
31-
<li><a href="stories.html">Platforms stories</a></li>
33+
<li><a href="plugin_manual.html">Plugin Manual</a></li>
3234
<li class="divider-vertical"></li>
33-
<li><a href="docs/index.html">Doxygen</a></li>
35+
<li><a href="stories.html">Platforms stories</a></li>
3436
<li class="divider-vertical"></li>
3537
<li><a href="https://github.com/cpputest/cpputest"><span class="github_icon"></span><span>View on
3638
GitHub</span></a></li>

manual.markdown

Lines changed: 70 additions & 79 deletions
Original file line numberDiff line numberDiff line change
@@ -137,15 +137,26 @@ The failure of one of these macros causes the current test to immediately exit:
137137

138138
* CHECK(boolean condition) - checks any boolean result.
139139
* CHECK_TEXT(boolean condition, text) - checks any boolean result and prints text on failure.
140+
* CHECK_FALSE(condition) - checks any boolean result
141+
* CHECK_FALSE_TEXT(condition, text) - checks any boolean result and prints text on failure.
140142
* CHECK_EQUAL(expected, actual) - checks for equality between entities using ==. So if you have a class that supports operator==() you can use this macro to compare two instances. You will also need to add a StringFrom() function like those found in SimpleString. This is for printing the objects when the check failed.
141143
* CHECK_THROWS(expected_exception, expression) - checks if expression throws expected_exception (e.g. std::exception). CHECK_THROWS is only available if CppUTest is built with the Standard C++ Library (default).
142-
* STRCMP_EQUAL(expected, actual) - check const char* strings for equality using strcmp().
143-
* LONGS_EQUAL(expected, actual) - Compares two numbers.
144-
* BYTES_EQUAL(expected, actual) - Compares two numbers, eight bits wide.
145-
* POINTERS_EQUAL(expected, actual) - Compares two pointers.
146-
* DOUBLES_EQUAL(expected, actual, tolerance) - Compares two doubles within some tolerance
144+
* STRCMP_EQUAL(expected, actual) - checks const char* strings for equality using strcmp().
145+
* STRNCMP_EQUAL(expected, actual, length) - checks const char* strings for equality using strncmp().
146+
* STRCMP_NOCASE_EQUAL(expected, actual) - checks const char* strings for equality, not considering case.
147+
* STRCMP_CONTAINS(expected, actual) - checks whether const char* actual contains const char* expected.
148+
* LONGS_EQUAL(expected, actual) - compares two numbers.
149+
* UNSIGNED_LONGS_EQUAL(expected, actual) - compares two positive numbers.
150+
* BYTES_EQUAL(expected, actual) - compares two numbers, eight bits wide.
151+
* POINTERS_EQUAL(expected, actual) - compares two pointers.
152+
* DOUBLES_EQUAL(expected, actual, tolerance) - compares two floating point numbers within some tolerance
153+
* FUNCTIONPOINTERS_EQUAL_TEXT(expected, actual, text) - compares two void (*)() function pointers
154+
* MEMCMP_EQUAL(expected, actual, size) - compares two areas of memory
155+
* BITS_EQUAL(expected, actual, mask) - compares expected to actual bit by bit, applying mask
147156
* FAIL(text) - always fails
148157

158+
*NOTE* Many macros have _TEXT() equivalents, which are not explicitly listed here.
159+
149160
<a id="setup_teardown"> </a>
150161

151162
*CHECK_EQUAL Warning:*
@@ -222,16 +233,22 @@ The test execution of this will *likely* (no guarantee of order in CppUTest) be:
222233

223234
## Command line Switches
224235

225-
* *-v* verbose, print each test name as it runs
226236
* *-c* colorize output, print green if OK, or red if failed
227-
* *-r#* repeat the tests some number (#) of times, or two times if # is not specified. This is handy if you are experiencing memory leaks. A second run that has no leaks indicates that someone is allocating statics and not releasing them.
228-
* *-g* group only run test whose group contains the substring group
229-
* *-sg* group only run test whose group exactly matches the string group
230-
* *-n* name only run test whose name contains the substring name
231-
* *-sn* name only run test whose name exactly matches the string name
232-
* *"TEST(group, name)"* only run test whose group and name matches the strings group and name. This can be used to copy-paste output from the -v option on the command line.
233-
* *-ojunit* output to JUnit ant plugin style xml files (for CI systems)
237+
* *-g group* only run test whose group contains the substring *group*
234238
* *-k* package name, Add a package name in JUnit output (for classification in CI systems)
239+
* *-lg* print a list of group names, separated by spaces
240+
* *-ln* print a list of test names in the form of *group.name*, separated by spaces
241+
* *-n name* only run test whose name contains the substring *name*
242+
* *-ojunit* output to JUnit ant plugin style xml files (for CI systems)
243+
* *-oteamcity* output to xml files (as the name suggests, for TeamCity)
244+
* *-p* run tests in a separate process.
245+
* *-r#* repeat the tests some number (#) of times, or twice if # is not specified. This is handy if you are experiencing memory leaks. A second run that has no leaks indicates that someone is allocating statics and not releasing them.
246+
* *-sg group* only run test whose group exactly matches the string *group*
247+
* *-sn name* only run test whose name exactly matches the string *name*
248+
* *-v* verbose, print each test name as it runs
249+
* *-xg group* exclude tests whose group contains the substring *group* (v3.8)
250+
* *-xn name* exclude tests whose name contains the substring *name* (v3.8)
251+
* *"TEST(group, name)"* only run test whose group and name matches the strings group and name. This can be used to copy-paste output from the -v option on the command line.
235252

236253
You can specify multiple -s&#124;sg, -s&#124;sn and "TEST(group, name)" parameters:
237254

@@ -241,6 +258,27 @@ Specifying only test names with multiple -s&#124;sn parameters will run all test
241258

242259
Mixing multiple -s&#124;sg and -s&#124;sn parameters (or using "TEST(group, name)" will only run tests whose groups match as well as their names.
243260

261+
Combining one -xg parameter with one -xn parameter will run only those tests that satisfy both criteria.
262+
263+
Combining -s&#124;sg with -xn, or -s&#124;sn with -xg will run only those tests that satisfy both criteria.
264+
265+
Specifying several -xg or -xn with each other or in other combinations has no effect.
266+
267+
*NOTE* Be careful with *-p*:
268+
269+
* Some systems do not support this feature, in which case tests will fail
270+
with a suitable message.
271+
* Using *-p* to run tests in a separate process can have unexpected side
272+
effects.
273+
* While running tests in a separate process can help to get more information
274+
about an unexpected crash, when an expected crash is part of the test scenario,
275+
the *-p* command line option should not be used, but running in a separate
276+
process should be enabled on a per-test basis like this:
277+
{% highlight c++ %}
278+
TestRegistry::getCurrentRegistry()->setRunTestsInSeperateProcess();
279+
{% endhighlight %}
280+
Examples for this can be found in CppUTests's own tests.
281+
244282
<a id="memory_leak_detection"> </a>
245283

246284
## Memory Leak Detection
@@ -353,73 +391,10 @@ Test plugins let you add a pre-action and a post-action to each test case. Plug
353391

354392
* Memory leak detector (provided)
355393
* Pointer restore mechanism (provided) - helpful when tests overwrite a pointer that must be restored to its original value after the test. This is especially helpful when a pointer to a function is modified for test purposes.
356-
* All Mutex's released - you could write a plugin that checks that any Mutexs or other shared resource is released before the test exists.
357-
358-
Example of a main with a SetPointerPlugin:
359-
360-
{% highlight c++ %}
361-
int main(int ac, char** av)
362-
{
363-
TestRegistry* r = TestRegistry::getCurrentRegistry();
364-
SetPointerPlugin ps("PointerStore");
365-
r->installPlugin(&ps);
366-
return CommandLineTestRunner::RunAllTests(ac, av);
367-
}
368-
369-
TEST_GROUP(HelloWorld)
370-
{
371-
static int output_method(const char* output, ...)
372-
{
373-
va_list arguments;
374-
va_start(arguments, output);
375-
cpputest_snprintf(buffer, BUFFER_SIZE, output, arguments);
376-
va_end(arguments);
377-
return 1;
378-
}
379-
void setup()
380-
{
381-
//overwrite the production function pointer witha an output method that captures
382-
//output in a buffer.
383-
UT_PTR_SET(helloWorldApiInstance.printHelloWorld_output, &output_method);
384-
}
385-
void teardown()
386-
{
387-
}
388-
};
389-
390-
TEST(HelloWorld, PrintOk)
391-
{
392-
printHelloWorld();
393-
STRCMP_EQUAL("Hello World!\n", buffer)
394-
}
395-
396-
//Hello.h
397-
#ifndef HELLO_H_
398-
#define HELLO_H_
399-
400-
extern void printHelloWorld();
401-
402-
struct helloWorldApi {
403-
int (*printHelloWorld_output) (const char*, ...);
404-
};
405-
406-
#endif /*HELLO_H_*/
394+
* IEEE754 Floating point exceptions (provided; v3.8) - automatically checks whether any floating point exception flags are set at the end of every test and if so, fails the test.
395+
* All Mutex's released - you could write a plugin that checks that any Mutexs or other shared resource is released before the test exits.
407396

408-
//Hello.c
409-
410-
#include <stdio.h>
411-
#include "hello.h"
412-
413-
//in production, print with printf.
414-
struct helloWorldApi helloWorldApiInstance = {
415-
&printf
416-
};
417-
418-
void printHelloWorld()
419-
{
420-
helloWorldApiInstance.printHelloWorld_output("Hello World!\n");
421-
}
422-
{% endhighlight %}
397+
Complete Documentation for provided plugins can be found on the [Plugins Manual](plugins_manual.html) page.
423398

424399
<a id="scripts"> </a>
425400

@@ -553,6 +528,22 @@ int main(int ac, char** av)
553528

554529
You can leave out TEST_GROUP_C_SETUP() / TEST_GROUP_C_TEARDOWN() and TEST_GROUP_C_SETUP_WRAPPER() / TEST_GROUP_C_TEARDOWN_WRAPPER(), if you don't need them.
555530

531+
The following assertion macros are supported in the pure C interface:
532+
533+
{% highlight c++ %}
534+
CHECK_EQUAL_C_INT(expected,actual);
535+
CHECK_EQUAL_C_REAL(expected,actual,threshold);
536+
CHECK_EQUAL_C_CHAR(expected,actual);
537+
CHECK_EQUAL_C_STRING(expected,actual);
538+
CHECK_EQUAL_C_POINTER(expected,actual); /* v3.8 */
539+
CHECK_EQUAL_C_BITS(expected, actual, mask); /* v3.8, pending */
540+
FAIL_TEXT_C(text);
541+
FAIL_C();
542+
CHECK_C(condition);
543+
{% endhighlight %}
544+
545+
These macros ensure tests get terminated in a way appropriate for pure C code.
546+
556547
<a id="gmock"> </a>
557548

558549
## Using Google Mock

mocking_manual.markdown

Lines changed: 89 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -21,12 +21,14 @@ The main idea is to make manual mocking easier, rather than to make automated mo
2121
* [Using Parameters](#parameters)
2222
* [Objects as Parameters](#objects_as_parameters)
2323
* [Output Parameters](#output_parameters)
24+
* [Output Parameters Using Objects](#output_parameters_using_objects)
2425
* [Return Values](#return_values)
2526
* [Passing other data](#other_data)
2627
* [Other MockSupport](#other_mock_support)
2728
* [MockSupport Scope](#mock_scope)
28-
* [MockPlugin](#mock_plugin)
29+
* [MockSupportPlugin](#mock_plugin)
2930
* [C Interface](#c_interface)
31+
* [Miscellaneous](#miscellaneous)
3032

3133
<a id="simple_scenario"> </a>
3234

@@ -99,6 +101,12 @@ If the call to productionCode wouldn't happen, then the test would fail with the
99101
ACTUAL calls that did happen:
100102
<none>
101103

104+
Sometimes you expect *several identical calls* to the same function, for example five calls to productionCode. There is a convenient shorthand for that situation:
105+
106+
{% highlight c++ %}
107+
mock().expectNCalls(5, "productionCode");
108+
{% endhighlight %}
109+
102110
<a id="objects"> </a>
103111

104112
### Using Objects
@@ -213,10 +221,10 @@ public:
213221

214222
The isEqual is called to compare the two parameters. The valueToString is called when an error message is printed and it needs to print the actual and expected values. If you want to use normal C functions, you can use the MockFunctionComparator which accepts pointers to functions in the constructor.
215223

216-
To remove the comparators, all you needs to do is call removeAllComparators, like:
224+
To remove the comparators, all you needs to do is call removeAllComparatorsAndCopiers, like:
217225

218226
{% highlight c++ %}
219-
mock().removeAllComparators();
227+
mock().removeAllComparatorsAndCopiers();
220228
{% endhighlight %}
221229

222230
Comparators sometimes lead to surprises, so a couple of warnings on its usage:
@@ -251,7 +259,7 @@ mock().expectOneCall("Foo").withOutputParameterReturning("bar", &outputValue, si
251259
{% highlight c++ %}
252260
void Foo(int *bar)
253261
{
254-
mock().actualCall("foo").withOutputParameter("bar", bar);
262+
mock().actualCall("Foo").withOutputParameter("bar", bar);
255263
}
256264
{% endhighlight %}
257265

@@ -287,6 +295,54 @@ mock().expectOneCall("Foo").withOutputParameterReturning("bar", &doubleOutputVal
287295

288296
* When a char, int, etc. array is passed to withOutputParameter, you must use the generic withOutputParameterReturning and provide the actual size of the array or only one element will be copied.
289297

298+
<a id="output_parameters_using_objects"> </a>
299+
300+
### Output Parameters Using Objects
301+
302+
By far the best way to handle output parameters is by using a custom type copier (v3.8). The general principle is similar to the custom comparators described above:
303+
304+
{% highlight c++ %}
305+
MyType outputValue = 4;
306+
mock().expectOneCall("Foo").withOutputParameterOfTypeReturning("MyType", "bar", &outputValue);
307+
{% endhighlight %}
308+
309+
The corresponding actual call is:
310+
311+
{% highlight c++ %}
312+
void Foo(int *bar)
313+
{
314+
mock().actualCall("Foo").withOutputParameterOfType("MyType", "bar", bar);
315+
}
316+
{% endhighlight %}
317+
318+
When using withOutputParameterOfTypeReturning, the mocking framework needs to know how to copy the type and therefore a Copier has to be installed before using parameters of this type. This is done using installCopier, as below:
319+
320+
{% highlight c++ %}
321+
MyTypeCopier copier;
322+
mock().installCopier("myType", copier);
323+
{% endhighlight %}
324+
325+
MyTypeCopier is a custom copier, which implements the MockNamedValueCopier interface. For example:
326+
327+
{% highlight c++ %}
328+
class MyTypeCopier : public MockNamedValueCopier
329+
{
330+
public:
331+
virtual void copy(void* out, const void* in)
332+
{
333+
*(MyType*)out = *(const MyType*)in;
334+
}
335+
};
336+
{% endhighlight %}
337+
338+
To remove the copier, you need to call removeAllComparatorsAndCopiers, like:
339+
340+
{% highlight c++ %}
341+
mock().removeAllComparatorsAndCopiers();
342+
{% endhighlight %}
343+
344+
*Warning 1* and *Warning 2* above apply to copiers as well.
345+
290346
<a id="return_values"> </a>
291347

292348
### Return Values
@@ -447,29 +503,9 @@ mock("filesystem").ignoreOtherCalls();
447503

448504
<a id="mock_plugin"> </a>
449505

450-
### MockPlugin
451-
452-
CppUTest plugins can be installed in the main and 'extent' the unit test framework. It is a place where you can put work that needs to be done in all unit tests. There is a MockPlugin to make the work with mocks easier. It does the following work:
453-
454-
* checkExpectations at the end of every test (on global scope, which goes recursive over all scopes)
455-
* clear all expectations at the end of every test
456-
* install all comparators that were configured in the plugin at the beginning of every test
457-
* remove all comparators at the end of every test
506+
### MockSupportPlugin
458507

459-
Installing the MockPlugin means you'll have to add to main something like:
460-
461-
{% highlight c++ %}
462-
#include "CppUTest/TestRegistry.h"
463-
#include "CppUTestExt/MockSupportPlugin.h"
464-
465-
MyDummyComparator dummyComparator;
466-
MockSupportPlugin mockPlugin;
467-
468-
mockPlugin.installComparator("MyDummyType", dummyComparator);
469-
TestRegistry::getCurrentRegistry()->installPlugin(&mockPlugin);
470-
{% endhighlight %}
471-
472-
This code creates a comparator for MyDummy and installs it at the plugin. This means the comparator is available for all test cases. It creates the plugin and installs it at the current test registry. After installing the plugin, you don't have to worry too much anymore about calling checkExpectations or cleaning your MockSupport.
508+
There is a MockSupportPlugin to make the work with mocks easier. Complete Documentation for MockSupportPlugin can be found on the [Plugins Manual](plugins_manual.html) page.
473509

474510
<a id="c_interface"> </a>
475511

@@ -481,7 +517,7 @@ Sometimes it is useful to access the mocking framework from a .c file rather tha
481517
#include "CppUTestExt/MockSupport_c.h"
482518

483519
mock_c()->expectOneCall("foo")->withIntParameters("integer", 10)->andReturnDoubleValue(1.11);
484-
mock_c()->actualCall("foo")->withIntParameters("integer", 10)->returnValue().value.doubleValue;
520+
return mock_c()->actualCall("foo")->withIntParameters("integer", 10)->returnValue().value.doubleValue;
485521

486522
mock_c()->installComparator("type", equalMethod, toStringMethod);
487523
mock_scope_c("scope")->expectOneCall("bar")->withParameterOfType("type", "name", object);
@@ -495,3 +531,29 @@ mock_c()->clear();
495531
{% endhighlight %}
496532

497533
The C interface uses a similar builder structure as the C++ interface. It is far less common in C, but it works the same.
534+
535+
It is now also possible to specify the actual return value in the same way as with C++ (v3.8):
536+
537+
{% highlight c++ %}
538+
return mock_c()->actualCall("foo")->withIntParameters("integer", 10)->doubleReturnValue();
539+
return mock_c()->doubleReturnValue();
540+
{% endhighlight %}
541+
542+
and to specify a default return value, in case mocking is currently disabled when the actual call occurs (v3.8):
543+
544+
{% highlight c++ %}
545+
return mock_c()->actualCall("foo")->withIntParameters("integer", 10)->returnDoubleValueOrDefault(2.25);
546+
return mock_c()->returnDoubleValueOrDefault(2.25);
547+
{% endhighlight %}
548+
549+
<a id="miscellaneous"></a>
550+
551+
### Miscellaneous
552+
553+
If you want your test to be more explicit about that a certain mocked function call should not occur, you can write (v3.8):
554+
555+
{% highlight c++ %}
556+
mock().expectNoCall("productionCode");
557+
{% endhighlight %}
558+
559+
Doing so is functionally equivalent to stating no expectations at all.

0 commit comments

Comments
 (0)