Skip to content

Commit 10f7558

Browse files
author
Arnd R. Strube
committed
Move MockSupportPlugin documentation to Plugins Manual
1 parent 3d4aac5 commit 10f7558

File tree

2 files changed

+110
-31
lines changed

2 files changed

+110
-31
lines changed

mocking_manual.markdown

Lines changed: 3 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ The main idea is to make manual mocking easier, rather than to make automated mo
2626
* [Passing other data](#other_data)
2727
* [Other MockSupport](#other_mock_support)
2828
* [MockSupport Scope](#mock_scope)
29-
* [MockPlugin](#mock_plugin)
29+
* [MockSupportPlugin](#mock_plugin)
3030
* [C Interface](#c_interface)
3131
* [Miscellaneous](#miscellaneous)
3232

@@ -503,29 +503,9 @@ mock("filesystem").ignoreOtherCalls();
503503

504504
<a id="mock_plugin"> </a>
505505

506-
### MockPlugin
506+
### MockSupportPlugin
507507

508-
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:
509-
510-
* checkExpectations at the end of every test (on global scope, which goes recursive over all scopes)
511-
* clear all expectations at the end of every test
512-
* install all comparators that were configured in the plugin at the beginning of every test
513-
* remove all comparators at the end of every test
514-
515-
Installing the MockPlugin means you'll have to add to main something like:
516-
517-
{% highlight c++ %}
518-
#include "CppUTest/TestRegistry.h"
519-
#include "CppUTestExt/MockSupportPlugin.h"
520-
521-
MyDummyComparator dummyComparator;
522-
MockSupportPlugin mockPlugin;
523-
524-
mockPlugin.installComparator("MyDummyType", dummyComparator);
525-
TestRegistry::getCurrentRegistry()->installPlugin(&mockPlugin);
526-
{% endhighlight %}
527-
528-
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+
CppUTest plugins can be installed in the main and 'extend' 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 MockSupportPlugin to make the work with mocks easier. Complete Documentation for MockSupportPlugin can be found on the [Plugins Manual](plugins_manual.html) page.
529509

530510
<a id="c_interface"> </a>
531511

plugins_manual.markdown

Lines changed: 107 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,12 @@ title: Plugins Manual
1313

1414
## SetPointerPlugin
1515

16+
### Description
17+
18+
The SetPointerPlugin provides a Pointer restore mechanism - 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.
19+
20+
### Example
21+
1622
{% highlight c++ %}
1723
int main(int ac, char** av)
1824
{
@@ -34,8 +40,8 @@ TEST_GROUP(HelloWorld)
3440
}
3541
void setup()
3642
{
37-
//overwrite the production function pointer witha an output method that captures
38-
//output in a buffer.
43+
// overwrite the production function pointer witha an output method that captures
44+
// output in a buffer.
3945
UT_PTR_SET(helloWorldApiInstance.printHelloWorld_output, &output_method);
4046
}
4147
void teardown()
@@ -49,7 +55,7 @@ TEST(HelloWorld, PrintOk)
4955
STRCMP_EQUAL("Hello World!\n", buffer)
5056
}
5157

52-
//Hello.h
58+
// Hello.h
5359
#ifndef HELLO_H_
5460
#define HELLO_H_
5561

@@ -59,14 +65,14 @@ struct helloWorldApi {
5965
int (*printHelloWorld_output) (const char*, ...);
6066
};
6167

62-
#endif /*HELLO_H_*/
68+
#endif
6369

64-
//Hello.c
70+
// Hello.c
6571

6672
#include <stdio.h>
6773
#include "hello.h"
6874

69-
//in production, print with printf.
75+
// in production, print with printf.
7076
struct helloWorldApi helloWorldApiInstance = {
7177
&printf
7278
};
@@ -81,10 +87,103 @@ void printHelloWorld()
8187

8288
## MockSupportPlugin
8389

84-
Tba
90+
MockSupportPlugin makes the work with mocks easier. It does the following work for you automatically:
91+
92+
* checkExpectations at the end of every test (on global scope, which goes recursive over all scopes)
93+
* clear all expectations at the end of every test
94+
* install all comparators that were configured in the plugin at the beginning of every test
95+
* remove all comparators at the end of every test
96+
97+
Installing the MockPlugin means you'll have to add to main something like:
98+
99+
{% highlight c++ %}
100+
#include "CppUTest/TestRegistry.h"
101+
#include "CppUTestExt/MockSupportPlugin.h"
102+
103+
MyDummyComparator dummyComparator;
104+
MockSupportPlugin mockPlugin;
105+
106+
mockPlugin.installComparator("MyDummyType", dummyComparator);
107+
TestRegistry::getCurrentRegistry()->installPlugin(&mockPlugin);
108+
{% endhighlight %}
109+
110+
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.
85111

86112
<a id="ieee754exceptionsplugin"> </a>
87113

88114
## IEEE754ExceptionsPlugin
89115

90-
Tba
116+
### Description
117+
118+
This plugin detects floating point error conditions and fails the test, if any were found. According to the IEEE754 Floating Point Standard, floating point errors do not by default cause abnormal program termination. Rather, flags are set to indicate a problem, and the operation returns a defined value such as Infinity or Not-a-Number (NaN).
119+
120+
This is a list of floating point error conditions, and how they are supported by the plugin:
121+
122+
{% highlight c++ %}
123+
FE_DIVBYZERO /* supported (3.8) */
124+
FE_OVERFLOW /* supported (3.8) */
125+
FE_UNDERFLOW /* supported (3.8) */
126+
FE_INVALID /* supported (3.8) */
127+
FE_INEXACT /* supported; disabled by default (3.8) */
128+
FE_DENORMAL /* NOT supported (3.8) */
129+
{% endhighlight %}
130+
131+
You can turn on FE_INEXACT checking manually, although this probably won't be very useful most of the time, since almost every floating-point operation is likely to set this flag:
132+
133+
{% highlight c++ %}
134+
IEEE754ExceptionsPlugin::enableInexact();
135+
IEEE754ExceptionsPlugin::disableInexact();
136+
{% endhighlight %}
137+
138+
# Example
139+
140+
{% highlight c++ %}
141+
#include "CppUTest/CommandLineTestRunner.h"
142+
#include "CppUTest/TestRegistry.h"
143+
#include "CppUTestExt/IEEE754ExceptionsPlugin.h"
144+
145+
int main(int ac, char** av)
146+
{
147+
IEEE754ExceptionsPlugin ieee754Plugin;
148+
TestRegistry::getCurrentRegistry()->installPlugin(&ieee754Plugin);
149+
return CommandLineTestRunner::RunAllTests(ac, av);
150+
}
151+
152+
static volatile float f;
153+
154+
TEST_GROUP(CatchFloatingPointErrors)
155+
{
156+
void setup()
157+
{
158+
IEEE754ExceptionsPlugin::disableInexact();
159+
}
160+
};
161+
162+
TEST(CatchFloatingPointErrors, underflow)
163+
{
164+
f = 0.01f;
165+
while (f > 0.0f) f *= f;
166+
CHECK(f == 0.0f);
167+
}
168+
169+
TEST(CatchFloatingPointErrors, inexact) {
170+
IEEE754ExceptionsPlugin::enableInexact();
171+
f = 10.0f;
172+
DOUBLES_EQUAL(f / 3.0f, 3.333f, 0.001f);
173+
}
174+
{% endhighlight %}
175+
176+
The output of these tests will be:
177+
{% highlight bash %}
178+
$ ./example.exe
179+
example.cpp:29: error: Failure in TEST(CatchFloatingPointErrors, inexact)
180+
src/CppUTestExt/IEEE754ExceptionsPlugin.cpp:164: error:
181+
IEEE754_CHECK_CLEAR(FE_INEXACT) failed
182+
.
183+
example.cpp:22: error: Failure in TEST(CatchFloatingPointErrors, underflow)
184+
src/CppUTestExt/IEEE754ExceptionsPlugin.cpp:164: error:
185+
IEEE754_CHECK_CLEAR(FE_UNDERFLOW) failed
186+
.
187+
Errors (2 failures, 2 tests, 2 ran, 12 checks, 0 ignored, 0 filtered out, 39 ms)
188+
$
189+
{% endhighlight %}

0 commit comments

Comments
 (0)