| 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 | |
| 29 | #include "CppUTest/TestHarness.h" |
| 30 | #include "CppUTestExt/MockSupport.h" |
| 31 | #include "CppUTestExt/MockSupport_c.h" |
| 32 | |
| 33 | TEST_GROUP(FirstTestGroup) |
| 34 | { |
| 35 | }; |
| 36 | |
| 37 | TEST(FirstTestGroup, FirsTest) |
| 38 | { |
| 39 | // FAIL("Fail me!"); |
| 40 | } |
| 41 | |
| 42 | TEST(FirstTestGroup, SecondTest) |
| 43 | { |
| 44 | // STRCMP_EQUAL("hello", "world"); |
| 45 | } |
| 46 | |
| 47 | |
| 48 | TEST_GROUP(MockDocumentation) |
| 49 | { |
| 50 | }; |
| 51 | |
| 52 | static void productionCode() |
| 53 | { |
| 54 | mock().actualCall("productionCode"); |
| 55 | } |
| 56 | |
| 57 | TEST(MockDocumentation, SimpleScenario) |
| 58 | { |
| 59 | mock().expectOneCall("productionCode"); |
| 60 | productionCode(); |
| 61 | mock().checkExpectations(); |
| 62 | } |
| 63 | |
| 64 | class ClassFromProductionCode |
| 65 | { |
| 66 | public: |
| 67 | virtual void importantFunction(){} |
| 68 | virtual ~ClassFromProductionCode() {} |
| 69 | }; |
| 70 | |
| 71 | class ClassFromProductionCodeMock : public ClassFromProductionCode |
| 72 | { |
| 73 | public: |
| 74 | virtual void importantFunction() |
| 75 | { |
| 76 | mock().actualCall("importantFunction").onObject(this); |
| 77 | } |
| 78 | }; |
| 79 | |
| 80 | TEST(MockDocumentation, SimpleScenarioObject) |
| 81 | { |
| 82 | ClassFromProductionCode* object = new ClassFromProductionCodeMock; /* create mock instead of real thing */ |
| 83 | |
| 84 | mock().expectOneCall("importantFunction").onObject(object); |
| 85 | object->importantFunction(); |
| 86 | mock().checkExpectations(); |
| 87 | |
| 88 | delete object; |
| 89 | } |
| 90 | |
| 91 | static void parameters_function(int p1, const char* p2) |
| 92 | { |
| 93 | void* object = (void*) 1; |
| 94 | mock().actualCall("function").onObject(object).withParameter("p1", p1).withParameter("p2", p2); |
| 95 | } |
| 96 | |
| 97 | TEST(MockDocumentation, parameters) |
| 98 | { |
| 99 | void* object = (void*) 1; |
| 100 | mock().expectOneCall("function").onObject(object).withParameter("p1", 2).withParameter("p2", "hah"); |
| 101 | parameters_function(2, "hah"); |
| 102 | } |
| 103 | |
| 104 | class MyTypeComparator : public MockNamedValueComparator |
| 105 | { |
| 106 | public: |
| 107 | virtual bool isEqual(const void* object1, const void* object2) |
| 108 | { |
| 109 | return object1 == object2; |
| 110 | } |
| 111 | virtual SimpleString valueToString(const void* object) |
| 112 | { |
| 113 | return StringFrom(object); |
| 114 | } |
| 115 | }; |
| 116 | |
| 117 | TEST(MockDocumentation, ObjectParameters) |
| 118 | { |
| 119 | void* object = (void*) 1; |
| 120 | MyTypeComparator comparator; |
| 121 | mock().installComparator("myType", comparator); |
| 122 | mock().expectOneCall("function").withParameterOfType("myType", "parameterName", object); |
| 123 | mock().clear(); |
| 124 | mock().removeAllComparatorsAndCopiers(); |
| 125 | } |
| 126 | |
| 127 | TEST(MockDocumentation, returnValue) |
| 128 | { |
| 129 | mock().expectOneCall("function").andReturnValue(10); |
| 130 | int value = mock().actualCall("function").returnValue().getIntValue(); |
| 131 | value = mock().returnValue().getIntValue(); |
| 132 | LONGS_EQUAL(10, value); |
| 133 | } |
| 134 | |
| 135 | TEST(MockDocumentation, setData) |
| 136 | { |
| 137 | ClassFromProductionCode object; |
| 138 | mock().setData("importantValue", 10); |
| 139 | mock().setDataObject("importantObject", "ClassFromProductionCode", &object); |
| 140 | |
| 141 | ClassFromProductionCode * pobject; |
| 142 | int value = mock().getData("importantValue").getIntValue(); |
| 143 | pobject = (ClassFromProductionCode*) mock().getData("importantObject").getObjectPointer(); |
| 144 | |
| 145 | LONGS_EQUAL(10, value); |
| 146 | POINTERS_EQUAL(pobject, &object); |
| 147 | } |
| 148 | |
| 149 | static void doSomethingThatWouldOtherwiseBlowUpTheMockingFramework() |
| 150 | { |
| 151 | } |
| 152 | |
| 153 | TEST(MockDocumentation, otherMockSupport) |
| 154 | { |
| 155 | mock().crashOnFailure(); |
| 156 | // mock().actualCall("unex"); |
| 157 | |
| 158 | mock().expectOneCall("foo"); |
| 159 | mock().ignoreOtherCalls(); |
| 160 | |
| 161 | mock().disable(); |
| 162 | doSomethingThatWouldOtherwiseBlowUpTheMockingFramework(); |
| 163 | mock().enable(); |
| 164 | |
| 165 | mock().clear(); |
| 166 | |
| 167 | } |
| 168 | |
| 169 | TEST(MockDocumentation, scope) |
| 170 | { |
| 171 | mock("xmlparser").expectOneCall("open"); |
| 172 | mock("filesystem").ignoreOtherCalls(); |
| 173 | |
| 174 | mock("xmlparser").actualCall("open"); |
| 175 | } |
| 176 | |
| 177 | static int equalMethod(const void* object1, const void* object2) |
| 178 | { |
| 179 | return object1 == object2; |
| 180 | } |
| 181 | |
| 182 | static const char* toStringMethod(const void*) |
| 183 | { |
| 184 | return "string"; |
| 185 | } |
| 186 | |
| 187 | TEST(MockDocumentation, CInterface) |
| 188 | { |
| 189 | void* object = (void*) 0x1; |
| 190 | |
| 191 | mock_c()->expectOneCall("foo")->withIntParameters("integer", 10)->andReturnDoubleValue(1.11); |
| 192 | double d = mock_c()->actualCall("foo")->withIntParameters("integer", 10)->returnValue().value.doubleValue; |
| 193 | DOUBLES_EQUAL(1.11, d, 0.00001); |
| 194 | |
| 195 | |
| 196 | mock_c()->installComparator("type", equalMethod, toStringMethod); |
| 197 | mock_scope_c("scope")->expectOneCall("bar")->withParameterOfType("type", "name", object); |
| 198 | mock_scope_c("scope")->actualCall("bar")->withParameterOfType("type", "name", object); |
| 199 | mock_c()->removeAllComparatorsAndCopiers(); |
| 200 | |
| 201 | mock_c()->setIntData("important", 10); |
| 202 | mock_c()->checkExpectations(); |
| 203 | mock_c()->clear(); |
| 204 | } |
| 205 | |
| 206 | TEST_GROUP(FooTestGroup) |
| 207 | { |
| 208 | void setup() |
| 209 | { |
| 210 | // Init stuff |
| 211 | } |
| 212 | |
| 213 | void teardown() |
| 214 | { |
| 215 | // Uninit stuff |
| 216 | } |
| 217 | }; |
| 218 | |
| 219 | TEST(FooTestGroup, Foo) |
| 220 | { |
| 221 | // Test FOO |
| 222 | } |
| 223 | |
| 224 | TEST(FooTestGroup, MoreFoo) |
| 225 | { |
| 226 | // Test more FOO |
| 227 | } |
| 228 | |
| 229 | TEST_GROUP(BarTestGroup) |
| 230 | { |
| 231 | void setup() |
| 232 | { |
| 233 | // Init Bar |
| 234 | } |
| 235 | }; |
| 236 | |
| 237 | TEST(BarTestGroup, Bar) |
| 238 | { |
| 239 | // Test Bar |
| 240 | } |