CppUTest
TestMockFailure.h
1 
2 /*
3  * Copyright (c) 2007, Michael Feathers, James Grenning and Bas Vodde
4  * All rights reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions are met:
8  * * Redistributions of source code must retain the above copyright
9  * notice, this list of conditions and the following disclaimer.
10  * * Redistributions in binary form must reproduce the above copyright
11  * notice, this list of conditions and the following disclaimer in the
12  * documentation and/or other materials provided with the distribution.
13  * * Neither the name of the <organization> nor the
14  * names of its contributors may be used to endorse or promote products
15  * derived from this software without specific prior written permission.
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE EARLIER MENTIONED AUTHORS ``AS IS'' AND ANY
18  * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
19  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
20  * DISCLAIMED. IN NO EVENT SHALL <copyright holder> BE LIABLE FOR ANY
21  * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
22  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
23  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
24  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
26  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27  */
28 
29 #ifndef D_TestMockFailure_h
30 #define D_TestMockFailure_h
31 
32 #define CHECK_EXPECTED_MOCK_FAILURE(expectedFailure) CHECK_EXPECTED_MOCK_FAILURE_LOCATION(expectedFailure, __FILE__, __LINE__)
33 #define CHECK_NO_MOCK_FAILURE() CHECK_NO_MOCK_FAILURE_LOCATION(__FILE__, __LINE__)
34 
35 
37 {
38 public:
39 
40  SimpleString mockFailureString;
41  int amountOfFailures;
42 
43  MockFailureReporterForTest() : amountOfFailures(0) {}
44 
45  virtual void failTest(const MockFailure& failure)
46  {
47  amountOfFailures++;
48  mockFailureString = failure.getMessage();
49  }
50 
51  virtual int getAmountOfTestFailures()
52  {
53  return amountOfFailures;
54  }
55 
56  static MockFailureReporterForTest* getReporter()
57  {
58  static MockFailureReporterForTest reporter;
59  return &reporter;
60  }
61 };
62 
63 inline UtestShell* mockFailureTest()
64 {
65  return MockFailureReporterForTest::getReporter()->getTestToFail();
66 }
67 
68 inline SimpleString mockFailureString()
69 {
70  return MockFailureReporterForTest::getReporter()->mockFailureString;
71 }
72 
73 inline void CLEAR_MOCK_FAILURE()
74 {
75  MockFailureReporterForTest::getReporter()->mockFailureString = "";
76  MockFailureReporterForTest::getReporter()->amountOfFailures = 0;
77 }
78 
79 inline void CHECK_EXPECTED_MOCK_FAILURE_LOCATION(const MockFailure& expectedFailure, const char* file, int line)
80 {
81  SimpleString expectedFailureString = expectedFailure.getMessage();
82  SimpleString actualFailureString = mockFailureString();
83  CLEAR_MOCK_FAILURE();
84  if (expectedFailureString != actualFailureString)
85  {
86  SimpleString error = "MockFailures are different.\n";
87  error += "Expected MockFailure:\n\t";
88  error += expectedFailureString;
89  error += "\nActual MockFailure:\n\t";
90  error += actualFailureString;
91  FAIL_LOCATION(error.asCharString(), file, line);
92  }
93 }
94 
95 inline void CHECK_NO_MOCK_FAILURE_LOCATION(const char* file, int line)
96 {
97  if (mockFailureString() != "") {
98  SimpleString error = "Unexpected mock failure:\n";
99  error += mockFailureString();
100  CLEAR_MOCK_FAILURE();
101  FAIL_LOCATION(error.asCharString(), file, line);
102 
103  }
104  CLEAR_MOCK_FAILURE();
105 }
106 
107 #endif
Definition: SimpleString.h:46
Definition: Utest.h:61
Definition: TestMockFailure.h:36
Definition: MockFailure.h:54
Definition: MockFailure.h:39