2009-11-19 01:12:39 +00:00
|
|
|
// Cyphesis Online RPG Server and AI Engine
|
|
|
|
|
// Copyright (C) 2009 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
|
|
|
|
|
|
|
|
|
|
|
2011-02-15 09:30:46 +00:00
|
|
|
#ifdef NDEBUG
|
|
|
|
|
#undef NDEBUG
|
|
|
|
|
#endif
|
|
|
|
|
#ifndef DEBUG
|
|
|
|
|
#define DEBUG
|
|
|
|
|
#endif
|
|
|
|
|
|
2020-01-20 23:17:55 +01:00
|
|
|
#include "../TestBase.h"
|
2013-02-21 23:22:04 +00:00
|
|
|
|
2009-11-19 01:12:39 +00:00
|
|
|
#include "server/EntityFactory.h"
|
|
|
|
|
|
2018-11-03 16:43:33 +01:00
|
|
|
#include "rules/simulation/World.h"
|
2009-11-19 01:12:39 +00:00
|
|
|
|
2011-12-16 08:33:58 +00:00
|
|
|
#include "common/ScriptKit.h"
|
2011-09-15 23:54:33 +01:00
|
|
|
#include "common/TypeNode.h"
|
|
|
|
|
|
2013-10-23 22:36:55 +02:00
|
|
|
#include <Atlas/Objects/Entity.h>
|
2009-11-19 01:12:39 +00:00
|
|
|
#include <cassert>
|
2019-08-10 11:28:20 +02:00
|
|
|
#include "common/debug.h"
|
2020-03-22 23:36:52 +01:00
|
|
|
#include "../TestPropertyManager.h"
|
2009-11-19 01:12:39 +00:00
|
|
|
|
2019-08-10 11:28:20 +02:00
|
|
|
class TestScriptFactory : public ScriptKit<LocatedEntity>
|
|
|
|
|
{
|
|
|
|
|
protected:
|
|
|
|
|
std::string m_package;
|
|
|
|
|
public:
|
|
|
|
|
const std::string& package() const override
|
|
|
|
|
{
|
|
|
|
|
return m_package;
|
|
|
|
|
}
|
|
|
|
|
|
2021-05-31 00:05:25 +02:00
|
|
|
int addScript(LocatedEntity& entity) const override
|
2019-08-10 11:28:20 +02:00
|
|
|
{
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
int refreshClass() override
|
|
|
|
|
{
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
struct EntityFactorytest : public Cyphesis::TestBase
|
|
|
|
|
{
|
|
|
|
|
EntityFactoryBase* m_ek;
|
|
|
|
|
|
|
|
|
|
EntityFactorytest()
|
|
|
|
|
{
|
|
|
|
|
ADD_TEST(EntityFactorytest::test_newEntity);
|
|
|
|
|
ADD_TEST(EntityFactorytest::test_updateProperties);
|
|
|
|
|
ADD_TEST(EntityFactorytest::test_updateProperties_child);
|
|
|
|
|
ADD_TEST(EntityFactorytest::test_classAttributes);
|
2011-11-08 20:32:35 +00:00
|
|
|
}
|
2009-11-19 01:12:39 +00:00
|
|
|
|
2019-08-10 11:28:20 +02:00
|
|
|
void setup() override
|
|
|
|
|
{
|
|
|
|
|
m_ek = new EntityFactory<Thing>;
|
|
|
|
|
m_ek->m_type = new TypeNode("foo");
|
2009-11-19 01:12:39 +00:00
|
|
|
}
|
|
|
|
|
|
2019-08-10 11:28:20 +02:00
|
|
|
void teardown() override
|
|
|
|
|
{
|
|
|
|
|
delete m_ek->m_type;
|
|
|
|
|
delete m_ek;
|
2009-11-19 01:12:39 +00:00
|
|
|
}
|
|
|
|
|
|
2019-08-10 11:28:20 +02:00
|
|
|
void test_classAttributes()
|
|
|
|
|
{
|
|
|
|
|
{
|
|
|
|
|
//Test with no existing value and only a default value
|
|
|
|
|
Atlas::Message::Element existing = Atlas::Message::Element();
|
2009-11-19 01:12:39 +00:00
|
|
|
|
2019-08-10 11:28:20 +02:00
|
|
|
ClassAttribute tested{"a string"};
|
2009-11-19 01:12:39 +00:00
|
|
|
|
2019-08-10 11:28:20 +02:00
|
|
|
tested.combine(existing);
|
|
|
|
|
ASSERT_EQUAL(Atlas::Message::Element("a string"), existing)
|
2009-11-19 01:12:39 +00:00
|
|
|
|
2019-08-10 11:28:20 +02:00
|
|
|
existing = Atlas::Message::Element();
|
|
|
|
|
tested.defaultValue = 1;
|
|
|
|
|
tested.combine(existing);
|
|
|
|
|
ASSERT_EQUAL(Atlas::Message::Element(1), existing)
|
2009-11-19 01:12:39 +00:00
|
|
|
|
2019-08-10 11:28:20 +02:00
|
|
|
existing = Atlas::Message::Element();
|
|
|
|
|
tested.defaultValue = 1.0;
|
|
|
|
|
tested.combine(existing);
|
|
|
|
|
ASSERT_EQUAL(Atlas::Message::Element(1.0), existing)
|
2009-11-19 01:12:39 +00:00
|
|
|
|
2019-08-10 11:28:20 +02:00
|
|
|
existing = Atlas::Message::Element();
|
|
|
|
|
tested.defaultValue = Atlas::Message::MapType{{"foo", "bar"}};
|
|
|
|
|
tested.combine(existing);
|
|
|
|
|
ASSERT_EQUAL(Atlas::Message::Element(Atlas::Message::MapType{{"foo", "bar"}}), existing)
|
2009-11-19 01:12:39 +00:00
|
|
|
|
2019-08-10 11:28:20 +02:00
|
|
|
existing = Atlas::Message::Element();
|
|
|
|
|
tested.defaultValue = Atlas::Message::ListType{"foo"};
|
|
|
|
|
tested.combine(existing);
|
|
|
|
|
ASSERT_EQUAL(Atlas::Message::Element(Atlas::Message::ListType{"foo"}), existing)
|
2011-09-15 23:05:41 +01:00
|
|
|
|
2019-08-10 11:28:20 +02:00
|
|
|
}
|
2011-09-15 23:05:41 +01:00
|
|
|
|
2019-08-10 11:28:20 +02:00
|
|
|
{
|
|
|
|
|
//Test with an existing value and only a default value. The value should be replaced
|
|
|
|
|
Atlas::Message::Element existing = "another string";
|
2013-10-23 22:36:55 +02:00
|
|
|
|
2019-08-10 11:28:20 +02:00
|
|
|
ClassAttribute tested{"a string"};
|
2013-10-23 22:36:55 +02:00
|
|
|
|
2019-08-10 11:28:20 +02:00
|
|
|
tested.combine(existing);
|
|
|
|
|
ASSERT_EQUAL(Atlas::Message::Element("a string"), existing)
|
2013-10-23 22:36:55 +02:00
|
|
|
|
2019-08-10 11:28:20 +02:00
|
|
|
existing = 2;
|
|
|
|
|
tested.defaultValue = 1;
|
|
|
|
|
tested.combine(existing);
|
|
|
|
|
ASSERT_EQUAL(Atlas::Message::Element(1), existing)
|
|
|
|
|
|
|
|
|
|
existing = 2.0;
|
|
|
|
|
tested.defaultValue = 1.0;
|
|
|
|
|
tested.combine(existing);
|
|
|
|
|
ASSERT_EQUAL(Atlas::Message::Element(1.0), existing)
|
|
|
|
|
|
|
|
|
|
existing = Atlas::Message::MapType{{"foz", "baz"}};
|
|
|
|
|
tested.defaultValue = Atlas::Message::MapType{{"foo", "bar"}};
|
|
|
|
|
tested.combine(existing);
|
|
|
|
|
ASSERT_EQUAL(Atlas::Message::Element(Atlas::Message::MapType{{"foo", "bar"}}), existing)
|
|
|
|
|
|
|
|
|
|
existing = Atlas::Message::ListType{"bar"};
|
|
|
|
|
tested.defaultValue = Atlas::Message::ListType{"foo"};
|
|
|
|
|
tested.combine(existing);
|
|
|
|
|
ASSERT_EQUAL(Atlas::Message::Element(Atlas::Message::ListType{"foo"}), existing)
|
2013-10-23 22:36:55 +02:00
|
|
|
|
2019-08-10 11:28:20 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
{
|
|
|
|
|
//Test with incompatible values
|
|
|
|
|
Atlas::Message::Element existing = 1;
|
|
|
|
|
|
|
|
|
|
ClassAttribute tested{"a string"};
|
|
|
|
|
|
|
|
|
|
tested.combine(existing);
|
|
|
|
|
ASSERT_EQUAL(Atlas::Message::Element("a string"), existing)
|
|
|
|
|
|
|
|
|
|
existing = "a string";
|
|
|
|
|
tested.defaultValue = 1;
|
|
|
|
|
tested.combine(existing);
|
|
|
|
|
ASSERT_EQUAL(Atlas::Message::Element(1), existing)
|
|
|
|
|
|
|
|
|
|
existing = "a string";
|
|
|
|
|
tested.defaultValue = 1.0;
|
|
|
|
|
tested.combine(existing);
|
|
|
|
|
ASSERT_EQUAL(Atlas::Message::Element(1.0), existing)
|
|
|
|
|
|
|
|
|
|
existing = "a string";
|
|
|
|
|
tested.defaultValue = Atlas::Message::MapType{{"foo", "bar"}};
|
|
|
|
|
tested.combine(existing);
|
|
|
|
|
ASSERT_EQUAL(Atlas::Message::Element(Atlas::Message::MapType{{"foo", "bar"}}), existing)
|
|
|
|
|
|
|
|
|
|
existing = "a string";
|
|
|
|
|
tested.defaultValue = Atlas::Message::ListType{"foo"};
|
|
|
|
|
tested.combine(existing);
|
|
|
|
|
ASSERT_EQUAL(Atlas::Message::Element(Atlas::Message::ListType{"foo"}), existing)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
{
|
|
|
|
|
//Test with append
|
|
|
|
|
Atlas::Message::Element existing = "a string";
|
|
|
|
|
|
|
|
|
|
ClassAttribute tested{Atlas::Message::Element(), "another string"};
|
|
|
|
|
|
|
|
|
|
tested.combine(existing);
|
|
|
|
|
ASSERT_EQUAL(Atlas::Message::Element("a stringanother string"), existing)
|
|
|
|
|
|
|
|
|
|
existing = 1;
|
|
|
|
|
tested.append = 1;
|
|
|
|
|
tested.combine(existing);
|
|
|
|
|
ASSERT_EQUAL(Atlas::Message::Element(2), existing)
|
|
|
|
|
|
|
|
|
|
existing = 1.0;
|
|
|
|
|
tested.append = 1.0;
|
|
|
|
|
tested.combine(existing);
|
|
|
|
|
ASSERT_EQUAL(Atlas::Message::Element(2.0), existing)
|
|
|
|
|
|
|
|
|
|
existing = Atlas::Message::MapType{{"foz", "baz"}};
|
|
|
|
|
tested.append = Atlas::Message::MapType{{"foo", "bar"}};
|
|
|
|
|
tested.combine(existing);
|
|
|
|
|
ASSERT_EQUAL(Atlas::Message::Element(Atlas::Message::MapType{{"foz", "baz"},
|
|
|
|
|
{"foo", "bar"}}), existing)
|
|
|
|
|
|
|
|
|
|
existing = Atlas::Message::ListType{"foo"};
|
|
|
|
|
tested.append = Atlas::Message::ListType{"bar"};
|
|
|
|
|
tested.combine(existing);
|
|
|
|
|
ASSERT_EQUAL(Atlas::Message::Element(Atlas::Message::ListType{"foo", "bar"}), existing)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
{
|
|
|
|
|
//Test with prepend
|
|
|
|
|
Atlas::Message::Element existing = "a string";
|
|
|
|
|
|
|
|
|
|
ClassAttribute tested{Atlas::Message::Element(), Atlas::Message::Element(), "another string"};
|
|
|
|
|
|
|
|
|
|
tested.combine(existing);
|
|
|
|
|
ASSERT_EQUAL(Atlas::Message::Element("another stringa string"), existing)
|
|
|
|
|
|
|
|
|
|
existing = 1;
|
|
|
|
|
tested.prepend = 1;
|
|
|
|
|
tested.combine(existing);
|
|
|
|
|
ASSERT_EQUAL(Atlas::Message::Element(2), existing)
|
|
|
|
|
|
|
|
|
|
existing = 1.0;
|
|
|
|
|
tested.prepend = 1.0;
|
|
|
|
|
tested.combine(existing);
|
|
|
|
|
ASSERT_EQUAL(Atlas::Message::Element(2.0), existing)
|
|
|
|
|
|
|
|
|
|
existing = Atlas::Message::MapType{{"foz", "baz"}};
|
|
|
|
|
tested.prepend = Atlas::Message::MapType{{"foo", "bar"}};
|
|
|
|
|
tested.combine(existing);
|
|
|
|
|
ASSERT_EQUAL(Atlas::Message::Element(Atlas::Message::MapType{{"foz", "baz"},
|
|
|
|
|
{"foo", "bar"}}), existing)
|
|
|
|
|
|
|
|
|
|
existing = Atlas::Message::ListType{"foo"};
|
|
|
|
|
tested.prepend = Atlas::Message::ListType{"bar"};
|
|
|
|
|
tested.combine(existing);
|
|
|
|
|
ASSERT_EQUAL(Atlas::Message::Element(Atlas::Message::ListType{"bar", "foo"}), existing)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
{
|
|
|
|
|
//Test with subtract
|
|
|
|
|
Atlas::Message::Element existing = "a string";
|
|
|
|
|
|
|
|
|
|
ClassAttribute tested{Atlas::Message::Element(), Atlas::Message::Element(), Atlas::Message::Element(), "a string"};
|
|
|
|
|
|
|
|
|
|
tested.combine(existing);
|
|
|
|
|
//subtract of string does nothing
|
|
|
|
|
ASSERT_EQUAL(Atlas::Message::Element("a string"), existing)
|
|
|
|
|
|
|
|
|
|
existing = 2;
|
|
|
|
|
tested.subtract = 1;
|
|
|
|
|
tested.combine(existing);
|
|
|
|
|
ASSERT_EQUAL(Atlas::Message::Element(1), existing)
|
|
|
|
|
|
|
|
|
|
existing = 2.0;
|
|
|
|
|
tested.subtract = 1.0;
|
|
|
|
|
tested.combine(existing);
|
|
|
|
|
ASSERT_EQUAL(Atlas::Message::Element(1.0), existing)
|
|
|
|
|
|
|
|
|
|
existing = Atlas::Message::MapType{{"foz", "baz"},
|
|
|
|
|
{"foo", "bar"}};
|
|
|
|
|
tested.subtract = Atlas::Message::MapType{{"foo", ""}};
|
|
|
|
|
tested.combine(existing);
|
|
|
|
|
ASSERT_EQUAL(Atlas::Message::Element(Atlas::Message::MapType{{"foz", "baz"}}), existing)
|
|
|
|
|
|
2019-08-11 10:54:59 +02:00
|
|
|
existing = Atlas::Message::ListType{"foo", "bar", "bar"};
|
2019-08-10 11:28:20 +02:00
|
|
|
tested.subtract = Atlas::Message::ListType{"bar"};
|
|
|
|
|
tested.combine(existing);
|
|
|
|
|
ASSERT_EQUAL(Atlas::Message::Element(Atlas::Message::ListType{"foo"}), existing)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
{
|
|
|
|
|
//Test with multiple effects
|
|
|
|
|
Atlas::Message::Element existing;
|
|
|
|
|
|
|
|
|
|
ClassAttribute tested{"default", "append", "prepend", "subtract"};
|
|
|
|
|
|
|
|
|
|
tested.combine(existing);
|
|
|
|
|
//subtract of string does nothing
|
|
|
|
|
ASSERT_EQUAL(Atlas::Message::Element("prependdefaultappend"), existing)
|
|
|
|
|
|
|
|
|
|
tested.defaultValue = 1;
|
|
|
|
|
tested.append = 2;
|
|
|
|
|
tested.prepend = 4;
|
|
|
|
|
tested.subtract = 8;
|
|
|
|
|
tested.combine(existing);
|
|
|
|
|
ASSERT_EQUAL(Atlas::Message::Element(-1), existing)
|
|
|
|
|
|
|
|
|
|
tested.defaultValue = 1.0;
|
|
|
|
|
tested.append = 2.0;
|
|
|
|
|
tested.prepend = 4.0;
|
|
|
|
|
tested.subtract = 8.0;
|
|
|
|
|
tested.combine(existing);
|
|
|
|
|
ASSERT_EQUAL(Atlas::Message::Element(-1.0), existing)
|
|
|
|
|
|
|
|
|
|
tested.defaultValue = Atlas::Message::MapType{{"foo", "bar"}};
|
|
|
|
|
tested.append = Atlas::Message::MapType{{"foo1", "bar1"}};
|
|
|
|
|
tested.prepend = Atlas::Message::MapType{{"foo2", "bar2"}};
|
|
|
|
|
tested.subtract = Atlas::Message::MapType{{"foo", "bar"}};
|
|
|
|
|
tested.combine(existing);
|
|
|
|
|
ASSERT_EQUAL(Atlas::Message::Element(Atlas::Message::MapType{{"foo1", "bar1"}, {"foo2", "bar2"}}), existing)
|
|
|
|
|
|
|
|
|
|
tested.defaultValue = Atlas::Message::ListType{"foo"};
|
|
|
|
|
tested.append = Atlas::Message::ListType{"bar"};
|
|
|
|
|
tested.prepend = Atlas::Message::ListType{"baz"};
|
|
|
|
|
tested.subtract = Atlas::Message::ListType{"foo"};
|
|
|
|
|
tested.combine(existing);
|
|
|
|
|
ASSERT_EQUAL(Atlas::Message::Element(Atlas::Message::ListType{"baz", "bar"}), existing)
|
|
|
|
|
}
|
|
|
|
|
}
|
2011-09-15 23:05:41 +01:00
|
|
|
|
2010-03-23 01:32:29 +00:00
|
|
|
|
2019-08-10 11:28:20 +02:00
|
|
|
void test_newEntity()
|
|
|
|
|
{
|
2022-07-04 15:37:51 +02:00
|
|
|
auto e = m_ek->newEntity(1, Atlas::Objects::Entity::RootEntity());
|
2010-03-23 01:32:29 +00:00
|
|
|
|
2019-08-10 11:28:20 +02:00
|
|
|
ASSERT_TRUE(e)
|
|
|
|
|
}
|
2010-03-23 01:32:29 +00:00
|
|
|
|
2019-08-10 11:28:20 +02:00
|
|
|
void test_updateProperties()
|
|
|
|
|
{
|
2020-03-22 23:36:52 +01:00
|
|
|
TestPropertyManager propertyManager;
|
2019-08-10 11:28:20 +02:00
|
|
|
std::map<const TypeNode*, TypeNode::PropertiesUpdate> changes;
|
2020-03-22 23:36:52 +01:00
|
|
|
m_ek->updateProperties(changes, propertyManager);
|
2019-08-10 11:28:20 +02:00
|
|
|
}
|
2010-03-23 01:32:29 +00:00
|
|
|
|
2019-08-10 11:28:20 +02:00
|
|
|
void test_updateProperties_child()
|
|
|
|
|
{
|
2020-03-22 23:36:52 +01:00
|
|
|
TestPropertyManager propertyManager{};
|
2021-05-27 17:38:58 +02:00
|
|
|
EntityFactory<Thing> ekc{};
|
|
|
|
|
ekc.m_type = m_ek->m_type;
|
|
|
|
|
ekc.m_classAttributes.emplace("foo", ClassAttribute{"value"});
|
2019-08-10 11:28:20 +02:00
|
|
|
|
2021-05-27 17:38:58 +02:00
|
|
|
m_ek->m_children.insert(&ekc);
|
2019-08-10 11:28:20 +02:00
|
|
|
|
|
|
|
|
std::map<const TypeNode*, TypeNode::PropertiesUpdate> changes;
|
2020-03-22 23:36:52 +01:00
|
|
|
m_ek->updateProperties(changes, propertyManager);
|
2019-08-10 11:28:20 +02:00
|
|
|
|
2021-05-27 17:38:58 +02:00
|
|
|
assert(ekc.m_attributes.find("foo") != ekc.m_attributes.end());
|
2019-08-10 11:28:20 +02:00
|
|
|
}
|
|
|
|
|
};
|
2010-03-23 01:32:29 +00:00
|
|
|
|
2019-08-10 11:28:20 +02:00
|
|
|
|
|
|
|
|
int main()
|
2010-03-23 01:32:29 +00:00
|
|
|
{
|
2019-08-10 11:28:20 +02:00
|
|
|
EntityFactorytest t;
|
|
|
|
|
|
|
|
|
|
return t.run();
|
2010-03-23 01:32:29 +00:00
|
|
|
}
|
|
|
|
|
|
2019-08-10 11:28:20 +02:00
|
|
|
// stubs
|
2010-03-23 01:32:29 +00:00
|
|
|
|
2020-01-20 23:17:55 +01:00
|
|
|
#include "../stubs/rules/simulation/stubThing.h"
|
|
|
|
|
#include "../stubs/rules/simulation/stubEntity.h"
|
|
|
|
|
#include "../stubs/rules/stubLocatedEntity.h"
|
|
|
|
|
#include "../stubs/common/stubRouter.h"
|
|
|
|
|
#include "../stubs/common/stubTypeNode.h"
|
|
|
|
|
#include "../stubs/common/stubProperty.h"
|
2016-01-18 16:21:00 +01:00
|
|
|
#include "common/Property_impl.h"
|
2020-01-20 23:17:55 +01:00
|
|
|
#include "../stubs/rules/stubLocation.h"
|
|
|
|
|
#include "../stubs/common/stublog.h"
|
2020-03-22 23:36:52 +01:00
|
|
|
#include "../stubs/common/stubProperty.h"
|
|
|
|
|
#include "../stubs/common/stubPropertyManager.h"
|
2013-10-23 22:36:55 +02:00
|
|
|
|