runtimeClassRepTest.cpp 3.9 KB

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