Fix crash
[ryzomcore.git] / nel / tools / nel_unit_test / ut_misc_command.h
blobb8e45c7a9059230a26b2cd1f39d06ceb8c11575a
1 // NeL - MMORPG Framework <http://dev.ryzom.com/projects/nel/>
2 // Copyright (C) 2010 Winch Gate Property Limited
3 //
4 // This program is free software: you can redistribute it and/or modify
5 // it under the terms of the GNU Affero General Public License as
6 // published by the Free Software Foundation, either version 3 of the
7 // License, or (at your option) any later version.
8 //
9 // This program is distributed in the hope that it will be useful,
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 // GNU Affero General Public License for more details.
14 // You should have received a copy of the GNU Affero General Public License
15 // along with this program. If not, see <http://www.gnu.org/licenses/>.
17 #ifndef UT_MISC_COMMAND
18 #define UT_MISC_COMMAND
20 #include <nel/misc/command.h>
22 vector<string> callList;
24 class TTest : public NLMISC::ICommandsHandler
26 protected:
27 std::string _Name;
28 public:
29 const std::string &getCommandHandlerName() const
31 return _Name;
34 void setName(const std::string &name)
36 nlassert(_Name.empty());
37 _Name = name;
39 registerCommandsHandler();
42 NLMISC_COMMAND_HANDLER_TABLE_BEGIN(TTest)
43 NLMISC_COMMAND_HANDLER_ADD(TTest, theCommand1, "help", "args")
44 NLMISC_COMMAND_HANDLER_ADD(TTest, theCommand2, "other help", "other args")
45 NLMISC_COMMAND_HANDLER_TABLE_END
48 NLMISC_CLASS_COMMAND_DECL(theCommand1)
50 callList.push_back(_Name+".theCommand1");
51 return true;
54 NLMISC_CLASS_COMMAND_DECL(theCommand2)
56 callList.push_back(_Name+".theCommand2");
57 return true;
62 class TTestDerived : public TTest
64 public:
65 NLMISC_COMMAND_HANDLER_TABLE_EXTEND_BEGIN(TTestDerived, TTest)
66 NLMISC_COMMAND_HANDLER_ADD(TTestDerived, derivedCommand, "help", "args")
67 NLMISC_COMMAND_HANDLER_ADD(TTestDerived, commandToOverride, "help", "args")
68 NLMISC_COMMAND_HANDLER_TABLE_END
70 NLMISC_CLASS_COMMAND_DECL(derivedCommand)
72 callList.push_back(_Name+".derivedCommand");
73 return true;
76 NLMISC_CLASS_COMMAND_DECL(commandToOverride)
78 callList.push_back(_Name+".commandToOverride");
79 return true;
84 class TTestDerived2 : public TTestDerived
86 public:
87 NLMISC_COMMAND_HANDLER_TABLE_EXTEND_BEGIN(TTestDerived2, TTestDerived)
88 NLMISC_COMMAND_HANDLER_ADD(TTestDerived2, derivedCommand2, "help", "args")
89 NLMISC_COMMAND_HANDLER_ADD(TTestDerived2, commandToOverride, "help", "args")
90 NLMISC_COMMAND_HANDLER_TABLE_END
92 NLMISC_CLASS_COMMAND_DECL(derivedCommand2)
94 callList.push_back(_Name+".derivedCommand2");
95 return true;
98 NLMISC_CLASS_COMMAND_DECL(commandToOverride)
100 callList.push_back(_Name+".command Overidden");
101 return true;
106 class TTestDerived3 : public TTestDerived2
108 // empty class
111 class TTestDerived4 : public TTestDerived3
113 public:
114 NLMISC_COMMAND_HANDLER_TABLE_EXTEND_BEGIN(TTestDerived4, TTestDerived3)
115 NLMISC_COMMAND_HANDLER_ADD(TTestDerived4, derivedCommand4, "help", "args")
116 NLMISC_COMMAND_HANDLER_ADD(TTestDerived4, theCommand1, "help", "args")
117 NLMISC_COMMAND_HANDLER_TABLE_END
119 NLMISC_CLASS_COMMAND_DECL(derivedCommand4)
121 callList.push_back(_Name+".derivedCommand4");
122 return true;
125 NLMISC_CLASS_COMMAND_DECL(theCommand1)
127 callList.push_back(_Name+".recallBase");
128 NLMISC_CLASS_COMMAND_CALL_BASE(TTestDerived3, theCommand1);
129 return true;
133 class CUTMiscCommand : public Test::Suite
135 TTest *t1;
136 TTest *t2;
137 public:
138 CUTMiscCommand()
140 TEST_ADD(CUTMiscCommand::createOneInstance);
141 TEST_ADD(CUTMiscCommand::createAnotherInstance);
142 TEST_ADD(CUTMiscCommand::deleteOneInstance);
143 TEST_ADD(CUTMiscCommand::derivedClass);
144 TEST_ADD(CUTMiscCommand::derivedClassAndBaseCall);
147 void derivedClassAndBaseCall()
149 TTestDerived4 t4;
150 t4.setName("T4");
152 callList.clear();
154 NLMISC::ICommand::execute("T4.derivedCommand4", *NLMISC::InfoLog);
155 TEST_ASSERT(callList.size() == 1);
156 TEST_ASSERT(callList[0] == "T4.derivedCommand4");
158 NLMISC::ICommand::execute("T4.theCommand1", *NLMISC::InfoLog);
159 TEST_ASSERT(callList.size() == 3);
160 TEST_ASSERT(callList[1] == "T4.recallBase");
161 TEST_ASSERT(callList[2] == "T4.theCommand1");
164 void derivedClass()
166 TTestDerived t1;
167 t1.setName("T1");
168 TTestDerived2 t2;
169 t2.setName("T2");
171 callList.clear();
173 NLMISC::ICommand::execute("T1.theCommand1", *NLMISC::InfoLog);
174 TEST_ASSERT(callList.size() == 1);
175 TEST_ASSERT(callList[0] == "T1.theCommand1");
177 NLMISC::ICommand::execute("T1.derivedCommand", *NLMISC::InfoLog);
178 TEST_ASSERT(callList.size() == 2);
179 TEST_ASSERT(callList[1] == "T1.derivedCommand");
181 NLMISC::ICommand::execute("T1.commandToOverride", *NLMISC::InfoLog);
182 TEST_ASSERT(callList.size() == 3);
183 TEST_ASSERT(callList[2] == "T1.commandToOverride");
186 NLMISC::ICommand::execute("T2.theCommand1", *NLMISC::InfoLog);
187 TEST_ASSERT(callList.size() == 4);
188 TEST_ASSERT(callList[3] == "T2.theCommand1");
190 NLMISC::ICommand::execute("T2.derivedCommand", *NLMISC::InfoLog);
191 TEST_ASSERT(callList.size() == 5);
192 TEST_ASSERT(callList[4] == "T2.derivedCommand");
194 NLMISC::ICommand::execute("T2.commandToOverride", *NLMISC::InfoLog);
195 TEST_ASSERT(callList.size() == 6);
196 TEST_ASSERT(callList[5] == "T2.command Overidden");
200 void createOneInstance()
202 t1 = new TTest;
203 t1->setName("inst1");
205 TEST_ASSERT(callList.empty());
207 NLMISC::ICommand::execute("inst1.theCommand1", *NLMISC::InfoLog);
208 TEST_ASSERT(callList.size() == 1);
209 TEST_ASSERT(callList[0] == "inst1.theCommand1");
211 NLMISC::ICommand::execute("inst1.theCommand2", *NLMISC::InfoLog);
212 TEST_ASSERT(callList.size() == 2);
213 TEST_ASSERT(callList[0] == "inst1.theCommand1");
214 TEST_ASSERT(callList[1] == "inst1.theCommand2");
217 void createAnotherInstance()
219 t2 = new TTest;
220 t2->setName("inst2");
222 TEST_ASSERT(callList.size() == 2);
224 NLMISC::ICommand::execute("inst2.theCommand1", *NLMISC::InfoLog);
225 TEST_ASSERT(callList.size() == 3);
226 TEST_ASSERT(callList[0] == "inst1.theCommand1");
227 TEST_ASSERT(callList[1] == "inst1.theCommand2");
228 TEST_ASSERT(callList[2] == "inst2.theCommand1");
230 NLMISC::ICommand::execute("inst2.theCommand2", *NLMISC::InfoLog);
231 TEST_ASSERT(callList.size() == 4);
232 TEST_ASSERT(callList[0] == "inst1.theCommand1");
233 TEST_ASSERT(callList[1] == "inst1.theCommand2");
234 TEST_ASSERT(callList[2] == "inst2.theCommand1");
235 TEST_ASSERT(callList[3] == "inst2.theCommand2");
238 void deleteOneInstance()
240 delete t1;
242 NLMISC::ICommand::execute("inst1.theCommand2", *NLMISC::InfoLog);
243 TEST_ASSERT(callList.size() == 4);
244 TEST_ASSERT(callList[0] == "inst1.theCommand1");
245 TEST_ASSERT(callList[1] == "inst1.theCommand2");
246 TEST_ASSERT(callList[2] == "inst2.theCommand1");
247 TEST_ASSERT(callList[3] == "inst2.theCommand2");
253 #endif