| 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 "CppUTest/TestHarness.h" |
| 29 | #include "CppUTest/TestRegistry.h" |
| 30 | #include "CppUTest/TestOutput.h" |
| 31 | #include "CppUTest/TestTestingFixture.h" |
| 32 | |
| 33 | #define GENERIC_PLUGIN "GenericPlugin" |
| 34 | #define GENERIC_PLUGIN2 "GenericPlugin2" |
| 35 | #define GENERIC_PLUGIN3 "GenericPlugin3" |
| 36 | |
| 37 | static int sequenceNumber; |
| 38 | |
| 39 | class DummyPlugin: public TestPlugin |
| 40 | { |
| 41 | public: |
| 42 | DummyPlugin(const SimpleString& name) : |
| 43 | TestPlugin(name), preAction(0), preActionSequence(0), postAction(0), postActionSequence(0) |
| 44 | { |
| 45 | } |
| 46 | |
| 47 | virtual void preTestAction(UtestShell&, TestResult&) |
| 48 | { |
| 49 | preAction++; |
| 50 | preActionSequence = sequenceNumber++; |
| 51 | } |
| 52 | |
| 53 | virtual void postTestAction(UtestShell&, TestResult&) |
| 54 | { |
| 55 | postAction++; |
| 56 | postActionSequence = sequenceNumber++; |
| 57 | } |
| 58 | |
| 59 | int preAction; |
| 60 | int preActionSequence; |
| 61 | int postAction; |
| 62 | int postActionSequence; |
| 63 | }; |
| 64 | |
| 65 | class DummyPluginWhichAcceptsParameters: public DummyPlugin |
| 66 | { |
| 67 | public: |
| 68 | DummyPluginWhichAcceptsParameters(const SimpleString& name) : |
| 69 | DummyPlugin(name) |
| 70 | { |
| 71 | } |
| 72 | |
| 73 | virtual bool parseArguments(int ac, const char *const *av, int index) |
| 74 | { |
| 75 | SimpleString argument (av[index]); |
| 76 | if (argument == "-paccept") |
| 77 | return true; |
| 78 | return TestPlugin::parseArguments(ac, av, index); |
| 79 | } |
| 80 | |
| 81 | }; |
| 82 | |
| 83 | TEST_GROUP(PluginTest) |
| 84 | { |
| 85 | DummyPlugin* firstPlugin; |
| 86 | DummyPluginWhichAcceptsParameters* secondPlugin; |
| 87 | DummyPlugin* thirdPlugin; |
| 88 | TestTestingFixture *genFixture; |
| 89 | TestRegistry* registry; |
| 90 | |
| 91 | void setup() |
| 92 | { |
| 93 | firstPlugin = new DummyPlugin(GENERIC_PLUGIN); |
| 94 | secondPlugin = new DummyPluginWhichAcceptsParameters(GENERIC_PLUGIN2); |
| 95 | thirdPlugin = new DummyPlugin(GENERIC_PLUGIN3); |
| 96 | genFixture = new TestTestingFixture; |
| 97 | registry = genFixture->getRegistry(); |
| 98 | registry->installPlugin(firstPlugin); |
| 99 | sequenceNumber = 1; |
| 100 | } |
| 101 | |
| 102 | void teardown() |
| 103 | { |
| 104 | delete firstPlugin; |
| 105 | delete secondPlugin; |
| 106 | delete thirdPlugin; |
| 107 | delete genFixture; |
| 108 | } |
| 109 | }; |
| 110 | |
| 111 | #define GENERIC_PLUGIN "GenericPlugin" |
| 112 | |
| 113 | TEST(PluginTest, PluginHasName) |
| 114 | { |
| 115 | STRCMP_EQUAL(GENERIC_PLUGIN, firstPlugin->getName().asCharString()); |
| 116 | } |
| 117 | |
| 118 | TEST(PluginTest, InstallPlugin) |
| 119 | { |
| 120 | CHECK_EQUAL(firstPlugin, registry->getFirstPlugin()); |
| 121 | CHECK_EQUAL(firstPlugin, registry->getPluginByName(GENERIC_PLUGIN)); |
| 122 | LONGS_EQUAL(1, registry->countPlugins()); |
| 123 | } |
| 124 | |
| 125 | TEST(PluginTest, InstallMultiplePlugins) |
| 126 | { |
| 127 | registry->installPlugin(thirdPlugin); |
| 128 | CHECK_EQUAL(firstPlugin, registry->getPluginByName(GENERIC_PLUGIN)); |
| 129 | CHECK_EQUAL(thirdPlugin, registry->getPluginByName(GENERIC_PLUGIN3)); |
| 130 | POINTERS_EQUAL(NULLPTR, registry->getPluginByName("I do not exist")); |
| 131 | } |
| 132 | |
| 133 | TEST(PluginTest, ActionsAllRun) |
| 134 | { |
| 135 | genFixture->runAllTests(); |
| 136 | genFixture->runAllTests(); |
| 137 | CHECK_EQUAL(2, firstPlugin->preAction); |
| 138 | CHECK_EQUAL(2, firstPlugin->postAction); |
| 139 | } |
| 140 | |
| 141 | TEST(PluginTest, Sequence) |
| 142 | { |
| 143 | registry->installPlugin(thirdPlugin); |
| 144 | genFixture->runAllTests(); |
| 145 | CHECK_EQUAL(1, thirdPlugin->preActionSequence); |
| 146 | CHECK_EQUAL(2, firstPlugin->preActionSequence); |
| 147 | CHECK_EQUAL(3, firstPlugin->postActionSequence); |
| 148 | CHECK_EQUAL(4, thirdPlugin->postActionSequence); |
| 149 | LONGS_EQUAL(2, registry->countPlugins()); |
| 150 | } |
| 151 | |
| 152 | TEST(PluginTest, RemovePluginByName) |
| 153 | { |
| 154 | registry->installPlugin(secondPlugin); |
| 155 | registry->installPlugin(thirdPlugin); |
| 156 | LONGS_EQUAL(3, registry->countPlugins()); |
| 157 | registry->removePluginByName(GENERIC_PLUGIN2); |
| 158 | LONGS_EQUAL(2, registry->countPlugins()); |
| 159 | } |
| 160 | |
| 161 | struct DefaultPlugin : public TestPlugin |
| 162 | { |
| 163 | DefaultPlugin() : TestPlugin("default") {} |
| 164 | }; |
| 165 | |
| 166 | TEST(PluginTest, DefaultPostTestActionDoesntDoAnything) |
| 167 | { |
| 168 | DefaultPlugin defaultPlugin; |
| 169 | registry->installPlugin(&defaultPlugin); |
| 170 | genFixture->runAllTests(); |
| 171 | } |
| 172 | |
| 173 | TEST(PluginTest, DisablesPluginsDontRun) |
| 174 | { |
| 175 | registry->installPlugin(thirdPlugin); |
| 176 | thirdPlugin->disable(); |
| 177 | genFixture->runAllTests(); |
| 178 | CHECK(!thirdPlugin->isEnabled()); |
| 179 | thirdPlugin->enable(); |
| 180 | genFixture->runAllTests(); |
| 181 | CHECK_EQUAL(2, firstPlugin->preAction); |
| 182 | CHECK_EQUAL(1, thirdPlugin->preAction); |
| 183 | CHECK(thirdPlugin->isEnabled()); |
| 184 | } |
| 185 | |
| 186 | TEST(PluginTest, ParseArgumentsForUnknownArgumentsFails) |
| 187 | { |
| 188 | registry->installPlugin(secondPlugin); |
| 189 | const char *cmd_line[] = {"nonsense", "andmorenonsense"}; |
| 190 | CHECK(registry->getFirstPlugin()->parseAllArguments(2, const_cast<char**>(cmd_line), 0) == false); /* cover non-const wrapper, too */ |
| 191 | } |
| 192 | |
| 193 | TEST(PluginTest, ParseArgumentsContinuesAndSucceedsWhenAPluginCanParse) |
| 194 | { |
| 195 | registry->installPlugin(secondPlugin); |
| 196 | const char *cmd_line[] = {"-paccept", "andmorenonsense"}; |
| 197 | CHECK(registry->getFirstPlugin()->parseAllArguments(2, const_cast<char**>(cmd_line), 0)); /* cover non-const wrapper, too */ |
| 198 | } |