// Cyphesis Online RPG Server and AI Engine // Copyright (C) 2013 Alistair Riddoch // // This program is free software; you can redistribute it and/or modify // it under the terms of the GNU General Public License as published by // the Free Software Foundation; either version 2 of the License, or // (at your option) any later version. // // This program is distributed in the hope that it will be useful, // but WITHOUT ANY WARRANTY; without even the implied warranty of // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the // GNU General Public License for more details. // // You should have received a copy of the GNU General Public License // along with this program; if not, write to the Free Software Foundation, // Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA #ifdef NDEBUG #undef NDEBUG #endif #ifndef DEBUG #define DEBUG #endif #include "TestBase.h" #include "TestWorld.h" #include "rules/simulation/Entity.h" #include "common/Property.h" #include "common/PropertyManager.h" #include "common/TypeNode.h" #include #include using Atlas::Message::Element; using Atlas::Message::MapType; using Atlas::Message::ListType; // If tests fail, and print out the message below, you'll have to actually // implement this function to find out the details. std::ostream & operator<<(std::ostream & os, const MapType & v) { os << "[ATLAS_MAP]"; return os; } std::ostream & operator<<(std::ostream & os, const PropertyDict::const_iterator &) { os << "[iterator]"; return os; } template class test_values { public: static const char * name; static T initial_value; static T default_value; }; template<> const char * test_values::name = "test_int"; template<> long test_values::initial_value = 42; template<> long test_values::default_value = 23; template<> const char * test_values::name = "test_float"; template<> double test_values::initial_value = 69.5; template<> double test_values::default_value = 17.5; template<> const char * test_values::name = "test_string"; template<> std::string test_values::initial_value = "1356ebe4-220d-46a7-8c69-73f787f6b1ff"; template<> std::string test_values::default_value = "1423a858-f7c6-4b3c-8b0b-726af6dcbeff"; template<> const char * test_values::name = "test_map"; template<> MapType test_values::initial_value = { std::make_pair("map_int", 23), std::make_pair("map_float", 17.5f), }; template<> MapType test_values::default_value = { std::make_pair("map_int", 42l), std::make_pair("map_float", 69.5f), }; class TestPropertyManager : public PropertyManager { public: virtual ~TestPropertyManager(); virtual PropertyBase * addProperty(const std::string & name, int type); }; TestPropertyManager::~TestPropertyManager() { } PropertyBase * TestPropertyManager::addProperty(const std::string & name, int type) { if (name == test_values::name) { return new Property; } else if (name == test_values::name) { return new Property; } else if (name == test_values::name) { return new Property; } else if (name == test_values::name) { return new Property; } else { return new SoftProperty; } } class PropertyEntityintegration : public Cyphesis::TestBase { private: TypeNode * m_type; Ref m_entity; public: PropertyEntityintegration(); void setup(); void teardown(); template void test_requirePropertyClass(); template void test_requirePropertyClass_default(); template void test_modProperty(); template void test_modPropertyClass(); TestPropertyManager* propertyManager; }; template void PropertyEntityintegration::test_requirePropertyClass() { auto p = m_entity->requirePropertyClass>("bill"); ASSERT_NOT_NULL(p); } template void PropertyEntityintegration::test_requirePropertyClass_default() { auto p = m_entity->requirePropertyClass>("bill", Element(test_values::default_value)); ASSERT_EQUAL(p->data(), test_values::default_value); } template void PropertyEntityintegration::test_modProperty() { // Get a pointer to the types default property PropertyBase * dflt = m_type->defaults().find(test_values::name)->second; ASSERT_NOT_NULL(dflt); ASSERT_TRUE(dflt->flags().m_flags & flag_class); // The entity instance should not have a property by this name ASSERT_EQUAL(m_entity->m_properties.find(test_values::name), m_entity->m_properties.end()); PropertyBase * p = m_entity->modProperty(test_values::name); ASSERT_NOT_NULL(p); ASSERT_TRUE((p->flags().m_flags & flag_class) == 0); // modProperty should have forced a new object ASSERT_NOT_EQUAL(p, dflt); auto subp = dynamic_cast *>(p); ASSERT_NOT_NULL(subp); } template void PropertyEntityintegration::test_modPropertyClass() { // Get a pointer to the types default property PropertyBase * dflt = m_type->defaults().find(test_values::name)->second; ASSERT_NOT_NULL(dflt); ASSERT_TRUE(dflt->flags().m_flags & flag_class); // The entity instance should not have a property by this name ASSERT_EQUAL(m_entity->m_properties.find(test_values::name), m_entity->m_properties.end()); auto p = m_entity->modPropertyClass>( test_values::name ); ASSERT_NOT_NULL(p); ASSERT_TRUE((p->flags().m_flags & flag_class) == 0); // modProperty should have forced a new object ASSERT_NOT_EQUAL(p, dflt); ASSERT_EQUAL(p->data(), test_values::initial_value); } PropertyEntityintegration::PropertyEntityintegration() { ADD_TEST(PropertyEntityintegration::test_requirePropertyClass); ADD_TEST(PropertyEntityintegration::test_requirePropertyClass); ADD_TEST(PropertyEntityintegration::test_requirePropertyClass); ADD_TEST(PropertyEntityintegration::test_requirePropertyClass); ADD_TEST(PropertyEntityintegration::test_requirePropertyClass_default); ADD_TEST(PropertyEntityintegration::test_requirePropertyClass_default); ADD_TEST(PropertyEntityintegration::test_requirePropertyClass_default); ADD_TEST(PropertyEntityintegration::test_requirePropertyClass_default); ADD_TEST(PropertyEntityintegration::test_modProperty); ADD_TEST(PropertyEntityintegration::test_modProperty); ADD_TEST(PropertyEntityintegration::test_modProperty); ADD_TEST(PropertyEntityintegration::test_modProperty); ADD_TEST(PropertyEntityintegration::test_modPropertyClass); ADD_TEST(PropertyEntityintegration::test_modPropertyClass); ADD_TEST(PropertyEntityintegration::test_modPropertyClass); ADD_TEST(PropertyEntityintegration::test_modPropertyClass); } void PropertyEntityintegration::setup() { propertyManager = new TestPropertyManager; m_type = new TypeNode("test_type"); m_type->addProperties(MapType{ std::make_pair(test_values::name, test_values::initial_value), std::make_pair(test_values::name, test_values::initial_value), std::make_pair(test_values::name, test_values::initial_value), std::make_pair(test_values::name, test_values::initial_value) }); m_entity = new Entity("1", 1L); m_entity->setType(m_type); } void PropertyEntityintegration::teardown() { m_entity = nullptr; delete m_type; delete propertyManager; } int main() { PropertyEntityintegration t; return t.run(); } // stubs #include "rules/AtlasProperties.h" #include "rules/Domain.h" #include "rules/simulation/DomainProperty.h" #include "rules/Script.h" #include "common/id.h" #include "stubs/common/stubVariable.h" #include "stubs/common/stubMonitors.h" #include "stubs/common/stubcustom.h" #include "stubs/rules/stubDomain.h" #include "stubs/rules/simulation/stubDomainProperty.h" void addToEntity(const Point3D & p, std::vector & vd) { vd.resize(3); vd[0] = p[0]; vd[1] = p[1]; vd[2] = p[2]; } #ifndef STUB_BaseWorld_getEntity #define STUB_BaseWorld_getEntity Ref BaseWorld::getEntity(const std::string & id) const { return getEntity(integerId(id)); } Ref BaseWorld::getEntity(long id) const { auto I = m_eobjects.find(id); if (I != m_eobjects.end()) { assert(I->second); return I->second; } else { return nullptr; } } #endif //STUB_BaseWorld_getEntity #include "stubs/rules/simulation/stubBaseWorld.h" #include "stubs/rules/stubScript.h" #include "stubs/rules/stubLocation.h" #include "stubs/rules/stubAtlasProperties.h" #include "stubs/common/stubPropertyManager.h" #include "stubs/common/stubLink.h" #include "stubs/common/stubRouter.h" long integerId(const std::string & id) { long intId = strtol(id.c_str(), 0, 10); if (intId == 0 && id != "0") { intId = -1L; } return intId; } void log(LogLevel lvl, const std::string & msg) { }