Torque3D Documentation / _generateds / runtimeClassRepTest.cpp

runtimeClassRepTest.cpp

Engine/source/console/test/runtimeClassRepTest.cpp

More...

Detailed Description

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