MocksTest.cpp

Go to the documentation of this file.
00001 /*
00002 
00003 Copyright (C) 2006-2007 by Peter Dimov.
00004 
00005 This file is part of Calitko (http://www.calitko.org).
00006 
00007 Calitko is free software; you can redistribute it and/or modify
00008 it under the terms of the GNU General Public License as published by
00009 the Free Software Foundation; either version 2 of the License, or
00010 (at your option) any later version.
00011 
00012 Calitko is distributed in the hope that it will be useful,
00013 but WITHOUT ANY WARRANTY; without even the implied warranty of
00014 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00015 GNU General Public License for more details.
00016 
00017 You should have received a copy of the GNU General Public License
00018 along with Calitko; if not, write to the Free Software
00019 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
00020 
00021 */
00022 
00023 #include "Qt.h"
00024 #include "../Mocks.h"
00025 #include "Imports.cpp"
00026 
00027 /*
00028     This whole file requires revising! The examples should use the mock and
00029     driver code generator, so that modifying the implementations should not
00030     require reqriting of these examples.
00031 */
00032 #if 0
00033 
00034 using std::string;
00035 
00036 namespace Utils {
00037 namespace CalitkoMocks {
00038 namespace Testing {
00039 
00041 
00048 class IntCalculator
00049 {
00050 public:
00051     int     add (int a, int b)      { return a + b; }
00052     int     mul (int a, int b)      { return a * b; }
00053     int     div (int a, int b)      { return a / b; }
00054     int     mod (int a, int b)      { return a % b; }
00055 };
00056 
00058 
00070 class IntCalculatorDriver
00071 {
00072     TEST_DRIVER_FOR (IntCalculator);
00073 
00074 public:
00075     BOUND_FUNCTION_FACTORY2 (add, int, int, int);
00076     BOUND_FUNCTION_FACTORY2 (mul, int, int, int);
00077     BOUND_FUNCTION_FACTORY2 (div, int, int, int);
00078     BOUND_FUNCTION_FACTORY2 (mod, int, int, int);
00079 };
00080 
00082 
00084 class Server : public QObject
00085 {
00086     Q_OBJECT
00087 
00088 public slots:
00089     virtual void    connectTo (int number) = 0;
00090     virtual void    login (const string &username, const string &password) = 0;
00091     virtual void    disconnect() = 0;
00092     virtual void    sendMessage (const string &) = 0;
00093 
00094 signals:
00095     void    connected();
00096     void    disconnected();
00097     void    loginSucceeded();
00098     void    loginFailed();
00099     void    messageReceived  (string);
00100 };
00101 
00102 class ServerMock : public MockBase
00103 {
00104 public:
00105     MOCK_FOR (Server);
00106 
00107     EXPECTED_FUNCTION_FACTORY1 (0, connectTo, void, int);
00108     EXPECTED_FUNCTION_FACTORY2 (0, login, void, const string &, const string &);
00109     EXPECTED_FUNCTION_FACTORY0 (0, disconnect, void);
00110     EXPECTED_FUNCTION_FACTORY1 (0, sendMessage, void, const string &);
00111 
00112 private:
00113     class ServerImp : public Server
00114     {
00115     public:
00116         ServerImp (ServerMock &m) : mock (m) {}
00117 
00118         EXPECTED_FUNCTION_CHECKER1 (0, connectTo, void, int);
00119         EXPECTED_FUNCTION_CHECKER2 (0, login, void, const string &, const string &);
00120         EXPECTED_FUNCTION_CHECKER0 (0, disconnect, void);
00121         EXPECTED_FUNCTION_CHECKER1 (0, sendMessage, void, const string &);
00122 
00123         ServerMock  &mock;
00124     };
00125     ServerImp       imp_;
00126     friend class    ServerImp;
00127 };
00128 
00129 class Client : public QObject
00130 {
00131     Q_OBJECT
00132     CALITKO_TESTABLE;
00133 
00134 public:
00135     enum State
00136     {
00137         Disconnected,
00138         Connecting,
00139         Authenticating,
00140         Authenticated,
00141         Disconnecting,
00142     };
00143 
00144             Client (Server *server);
00145     void    joinChat (int serverNumber, const string &user, const string &pass);
00146     void    sendMessage (const string &message);
00147     void    leaveChat();
00148 
00149 private slots:
00150     void    connected();
00151     void    disconnected();
00152     void    loginSucceeded();
00153     void    loginFailed();
00154     void    messageReceived  (string);
00155 
00156 signals:
00157     void    connectedToServer();
00158     void    disconnectedFromServer();
00159 
00160 private:
00161     Server  *server_;
00162     int     serverNumber_;
00163     string  userName_;
00164     string  password_;
00165     State   state_;
00166 };
00167 
00168 class ClientDriver : public QObject
00169 {
00170     Q_OBJECT
00171     TEST_DRIVER_FOR (Client);
00172 
00173 public:
00174     ClientDriver (Server *server) : object (server)
00175     {
00176         MONITOR_SIGNAL (connectedToServer());
00177         MONITOR_SIGNAL (disconnectedFromServer());
00178     }
00179 
00180     BOUND_FUNCTION_FACTORY3 (joinChat, void, int, const string &, const string &);
00181     BOUND_FUNCTION_FACTORY1 (sendMessage, void, const string &);
00182     BOUND_FUNCTION_FACTORY0 (leaveChat, void);
00183     BOUND_FUNCTION_FACTORY0 (connected, void);
00184     BOUND_FUNCTION_FACTORY0 (disconnected, void);
00185     BOUND_FUNCTION_FACTORY0 (loginSucceeded, void);
00186     BOUND_FUNCTION_FACTORY1 (messageReceived, void, string);
00187 
00188     EXPECTED_FUNCTION_FACTORY0 (0, connectedToServer, void);
00189     EXPECTED_FUNCTION_FACTORY0 (0, disconnectedFromServer, void);
00190 
00191 private slots:
00192     void _checker_connectedToServer()
00193     {
00194         EXPECTED_SIGNAL_CHECKER0 (0, connectedToServer);
00195     }
00196 
00197     void _checker_disconnectedFromServer()
00198     {
00199         EXPECTED_SIGNAL_CHECKER0 (0, disconnectedFromServer);
00200     }
00201 };
00202 
00203 Client::Client (Server *server)
00204 {
00205     server_ = server;
00206     serverNumber_ = 0;
00207     /* A typical implementation would connect the server's signals with the
00208     private slots of this, but we don't need to do that for our sample test to
00209     work */
00210 }
00211 
00212 void Client::joinChat (int serverNumber, const string &user, const string &pass)
00213 {
00214     state_ = Connecting;
00215     userName_ = user;
00216     password_ = pass;
00217     server_->connectTo (serverNumber);
00218 }
00219 
00220 void Client::leaveChat()
00221 {
00222     state_ = Disconnecting;
00223     server_->disconnect();
00224 }
00225 
00226 void Client::connected()
00227 {
00228     state_ = Authenticating;
00229     server_->login (userName_, password_);
00230 }
00231 
00232 void Client::disconnected()
00233 {
00234     state_ = Disconnected;
00235 }
00236 
00237 void Client::loginSucceeded()
00238 {
00239     state_ = Authenticated;
00240     emit connectedToServer();
00241 }
00242 
00243 void Client::loginFailed()
00244 {
00245     leaveChat();
00246 }
00247 
00248 void Client::sendMessage (const string &message)
00249 {
00250     server_->sendMessage (message);
00251 }
00252 
00253 void Client::messageReceived (string)
00254 {
00255 }
00256 
00257 class ServerRequests
00258 {
00259 public:
00260     virtual void    connectTo (int number) = 0;
00261     virtual void    login (const string &username, const string &password) = 0;
00262     virtual void    disconnect() = 0;
00263     virtual void    sendMessage (const string &) = 0;
00264 };
00265 
00266 class ServerResponses
00267 {
00268 public:
00269     virtual void    connected() = 0;
00270     virtual void    disconnected() = 0;
00271     virtual void    loginSucceeded() = 0;
00272     virtual void    loginFailed() = 0;
00273     virtual void    messageReceived  (string) = 0;
00274 };
00275 
00277 
00279 class MocksTest : public CppUnit::TestFixture
00280 {
00281     CPPUNIT_TEST_SUITE (MocksTest);
00282     CPPUNIT_TEST (testIntCalculatorCppUnit);
00283     CPPUNIT_TEST (testIntCalculatorCalitko);
00284     CPPUNIT_TEST (testServerLoginOk);
00285     CPPUNIT_TEST_SUITE_END();
00286 
00287 public:
00288 
00290     void testIntCalculatorCppUnit()
00291     {
00292         IntCalculator calc;
00293         CPPUNIT_ASSERT (calc.add (1, 2) == 3);
00294         CPPUNIT_ASSERT (calc.mul (1, 2) == 2);
00295         CPPUNIT_ASSERT (calc.div (1, 2) == 0);
00296         CPPUNIT_ASSERT (calc.mod (1, 2) == 1);
00297     }
00298 
00300     void testIntCalculatorCalitko()
00301     {
00302         IntCalculatorDriver calc;
00303         call (calc.add (1, 2)).willReturn (3);
00304         call (calc.mul (1, 2)).willReturn (2);
00305         call (calc.div (1, 2)).willReturn (0);
00306         call (calc.mod (1, 2)).willReturn (1);
00307     }
00308 
00309     void testServerLoginOk()
00310     {
00311         ServerMock server;
00312         ClientDriver client (server.imp());
00313 
00314         call (client.joinChat (4, "peter", "mypassword"))
00315             .willCall (server.connectTo (4))
00316             .returns()
00317         .returns();
00318 
00319         server.calls (client.connected())
00320             .willCall (server.login ("peter", "mypassword"))
00321             .returns()
00322         .returns();
00323 
00324         server.calls (client.loginSucceeded())
00325             .willEmit (client.connectedToServer())
00326         .returns();
00327     }
00328 };
00329 
00330 CPPUNIT_TEST_SUITE_REGISTRATION (MocksTest);
00331 
00332 } // namespace Testing
00333 } // namespace CalitkoMocks
00334 } // namespace Utils
00335 
00336 #include "MocksTest.moc"
00337 
00338 #endif // 0