runtimeClassRepTest.cpp 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. //-----------------------------------------------------------------------------
  2. // Copyright (c) 2014 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. #ifdef TORQUE_TESTS_ENABLED
  23. #include "testing/unitTesting.h"
  24. #include "platform/platform.h"
  25. #include "console/simBase.h"
  26. #include "console/consoleTypes.h"
  27. #include "console/runtimeClassRep.h"
  28. class RuntimeRegisteredSimObject : public SimObject
  29. {
  30. typedef SimObject Parent;
  31. protected:
  32. bool mFoo;
  33. public:
  34. RuntimeRegisteredSimObject() : mFoo(false) {};
  35. DECLARE_RUNTIME_CONOBJECT(RuntimeRegisteredSimObject);
  36. static void initPersistFields();
  37. };
  38. IMPLEMENT_RUNTIME_CONOBJECT(RuntimeRegisteredSimObject);
  39. void RuntimeRegisteredSimObject::initPersistFields()
  40. {
  41. addField("fooField", TypeBool, Offset(mFoo, RuntimeRegisteredSimObject));
  42. }
  43. TEST(Console, RuntimeClassRep)
  44. {
  45. // First test to make sure that the test class is not registered (don't
  46. // know how it could be, but that's programming for you). Stop the test if
  47. // it is.
  48. ASSERT_TRUE(!RuntimeRegisteredSimObject::dynRTClassRep.isRegistered())
  49. << "RuntimeRegisteredSimObject class was already registered with the console";
  50. // This should not be able to find the class, and return null (this may
  51. // AssertWarn as well).
  52. ConsoleObject *conobj = ConsoleObject::create("RuntimeRegisteredSimObject");
  53. EXPECT_TRUE(conobj == NULL)
  54. << "Unregistered AbstractClassRep returned non-NULL value! That is really bad!";
  55. // Register with console system.
  56. RuntimeRegisteredSimObject::dynRTClassRep.consoleRegister();
  57. // Make sure that the object knows it's registered.
  58. EXPECT_TRUE(RuntimeRegisteredSimObject::dynRTClassRep.isRegistered())
  59. << "RuntimeRegisteredSimObject class failed console registration";
  60. // Now try again to create the instance.
  61. conobj = ConsoleObject::create("RuntimeRegisteredSimObject");
  62. EXPECT_TRUE(conobj != NULL)
  63. << "AbstractClassRep::create method failed!";
  64. // Cast the instance, and test it.
  65. RuntimeRegisteredSimObject *rtinst =
  66. dynamic_cast<RuntimeRegisteredSimObject *>(conobj);
  67. EXPECT_TRUE(rtinst != NULL)
  68. << "Casting failed for some reason";
  69. // Register it with a name.
  70. rtinst->registerObject("_utRRTestObject");
  71. EXPECT_TRUE(rtinst->isProperlyAdded())
  72. << "registerObject failed on test object";
  73. // Now execute some script on it.
  74. Con::evaluate("_utRRTestObject.fooField = true;");
  75. // Test to make sure field worked.
  76. EXPECT_TRUE(dAtob(rtinst->getDataField(StringTable->insert("fooField"), NULL)))
  77. << "Set property failed on instance.";
  78. // BALETED
  79. rtinst->deleteObject();
  80. // Unregister the type.
  81. RuntimeRegisteredSimObject::dynRTClassRep.consoleUnRegister();
  82. // And make sure we can't create another one.
  83. conobj = ConsoleObject::create("RuntimeRegisteredSimObject");
  84. EXPECT_TRUE(conobj == NULL)
  85. << "Unregistration of type failed";
  86. }
  87. #endif