testRuntimeClassRep.cpp 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. //-----------------------------------------------------------------------------
  2. // Copyright (c) 2012 GarageGames, LLC
  3. //
  4. // Permission is hereby granted, free of charge, to any person obtaining a copy
  5. // of this software and associated documentation files (the "Software"), to
  6. // deal in the Software without restriction, including without limitation the
  7. // rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
  8. // sell copies of the Software, and to permit persons to whom the Software is
  9. // furnished to do so, subject to the following conditions:
  10. //
  11. // The above copyright notice and this permission notice shall be included in
  12. // all copies or substantial portions of the Software.
  13. //
  14. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  19. // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
  20. // IN THE SOFTWARE.
  21. //-----------------------------------------------------------------------------
  22. #include "platform/platform.h"
  23. #include "console/simBase.h"
  24. #include "console/consoleTypes.h"
  25. #include "console/runtimeClassRep.h"
  26. #include "unit/test.h"
  27. using namespace UnitTesting;
  28. //-----------------------------------------------------------------------------
  29. class RuntimeRegisteredSimObject : public SimObject
  30. {
  31. typedef SimObject Parent;
  32. protected:
  33. bool mFoo;
  34. public:
  35. RuntimeRegisteredSimObject() : mFoo( false ) {};
  36. DECLARE_RUNTIME_CONOBJECT(RuntimeRegisteredSimObject);
  37. static void initPersistFields();
  38. };
  39. IMPLEMENT_RUNTIME_CONOBJECT(RuntimeRegisteredSimObject);
  40. void RuntimeRegisteredSimObject::initPersistFields()
  41. {
  42. addField( "fooField", TypeBool, Offset( mFoo, RuntimeRegisteredSimObject ) );
  43. }
  44. //-----------------------------------------------------------------------------
  45. CreateUnitTest( RuntimeClassRepUnitTest, "Console/RuntimeClassRep" )
  46. {
  47. void run()
  48. {
  49. // First test to make sure that the test class is not registered (don't know how it could be, but that's programming for you)
  50. test( !RuntimeRegisteredSimObject::dynRTClassRep.isRegistered(), "RuntimeRegisteredSimObject class was already registered with the console" );
  51. // This should not be able to find the class, and return null (this may AssertWarn as well)
  52. ConsoleObject *conobj = ConsoleObject::create( "RuntimeRegisteredSimObject" );
  53. test( conobj == NULL, "AbstractClassRep returned non-NULL value! That is really bad!" );
  54. // Register with console system
  55. RuntimeRegisteredSimObject::dynRTClassRep.consoleRegister();
  56. // Make sure that the object knows it's registered
  57. test( RuntimeRegisteredSimObject::dynRTClassRep.isRegistered(), "RuntimeRegisteredSimObject class failed console registration" );
  58. // Now try again to create the instance
  59. conobj = ConsoleObject::create( "RuntimeRegisteredSimObject" );
  60. test( conobj != NULL, "AbstractClassRep::create method failed!" );
  61. // Cast the instance, and test it
  62. RuntimeRegisteredSimObject *rtinst = dynamic_cast<RuntimeRegisteredSimObject *>( conobj );
  63. test( rtinst != NULL, "Casting failed for some reason" );
  64. // Register it
  65. rtinst->registerObject( "_utRRTestObject" );
  66. test( rtinst->isProperlyAdded(), "registerObject failed on test object" );
  67. // Now execute some script on it
  68. Con::evaluate( "_utRRTestObject.fooField = true;" );
  69. // Test to make sure field worked
  70. test( dAtob( rtinst->getDataField( StringTable->insert( "fooField" ), NULL ) ), "Script test failed!" );
  71. // BALETED
  72. rtinst->deleteObject();
  73. // Unregister the class
  74. RuntimeRegisteredSimObject::dynRTClassRep.consoleUnRegister();
  75. // And make sure we can't create another one
  76. conobj = ConsoleObject::create( "RuntimeRegisteredSimObject" );
  77. test( conobj == NULL, "Unregistration of type failed" );
  78. }
  79. };