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