Fix game:addSpawnShapesByZone
[ryzomcore.git] / nel / tools / nel_unit_test / ut_ligo_primitive.h
blobd4ba5df5a5e087fb241cd630efa5276066a44c71
1 // NeL - MMORPG Framework <http://dev.ryzom.com/projects/nel/>
2 // Copyright (C) 2010 Winch Gate Property Limited
3 //
4 // This program is free software: you can redistribute it and/or modify
5 // it under the terms of the GNU Affero General Public License as
6 // published by the Free Software Foundation, either version 3 of the
7 // License, or (at your option) any later version.
8 //
9 // This program is distributed in the hope that it will be useful,
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 // GNU Affero General Public License for more details.
14 // You should have received a copy of the GNU Affero General Public License
15 // along with this program. If not, see <http://www.gnu.org/licenses/>.
17 #ifndef UT_LIGO_PRIMITIVE
18 #define UT_LIGO_PRIMITIVE
20 #include <nel/ligo/ligo_config.h>
21 #include <nel/ligo/primitive_utils.h>
23 class CUTLigoPrimitive : public Test::Suite
25 public:
26 CUTLigoPrimitive()
28 TEST_ADD(CUTLigoPrimitive::testAliasGenerator)
31 private:
33 string _RestorePath;
34 string _WorkingPath;
35 string _RefPrimFileName;
36 void setup()
38 _RestorePath = NLMISC::CPath::getCurrentPath();
39 NLMISC::CPath::setCurrentPath(_WorkingPath.c_str());
41 _RefPrimFileName = "__test_prim.primitive";
43 // register ligo class factory
44 NLLIGO::Register();
46 // create a primitive config file
47 nlinfo("Building a default ligo class file");
49 const char *CLASS_FILE_NAME = "__ligo_class.xml";
51 string classfile;
52 classfile = string()
53 + "<?xml version=\"1.0\"?>\n"
54 + "<NEL_LIGO_PRIMITIVE_CLASS>\n"
55 + " <ALIAS_DYNAMIC_BITS BIT_COUNT=\"20\"/>\n"
56 + " <ALIAS_STATIC_FILE_ID FILE_NAME=\"file_index.cfg\"/>\n"
57 + "\n"
58 + " <PRIMITIVE CLASS_NAME=\"root\" TYPE=\"node\" AUTO_INIT=\"true\" DELETABLE=\"true\">\n"
59 + " <PARAMETER NAME=\"name\" TYPE=\"string\" VISIBLE=\"true\"/>\n"
60 + " <PARAMETER NAME=\"path\" TYPE=\"string\" VISIBLE=\"true\"/>\n"
61 + " <DYNAMIC_CHILD CLASS_NAME=\"test\"/>\n"
62 + " </PRIMITIVE>\n"
63 + "\n"
64 + " <!-- the alias class, used by all other class that need persistent aliases-->\n"
65 + " <PRIMITIVE CLASS_NAME=\"alias\" TYPE=\"alias\" AUTO_INIT=\"true\" DELETABLE=\"false\">\n"
66 + " </PRIMITIVE>\n"
67 + "\n"
68 + " <PRIMITIVE CLASS_NAME=\"test\" TYPE=\"node\" AUTO_INIT=\"false\" DELETABLE=\"true\" NUMBERIZE=\"false\">\n"
69 + " <PARAMETER NAME=\"name\" TYPE=\"string\" VISIBLE=\"true\"/>\n"
70 + " <STATIC_CHILD CLASS_NAME=\"alias\" NAME=\"alias\"/>\n"
71 + " <DYNAMIC_CHILD CLASS_NAME=\"test\"/>\n"
72 + " </PRIMITIVE>\n"
73 + "</NEL_LIGO_PRIMITIVE_CLASS>";
75 FILE *fp = NLMISC::nlfopen(CLASS_FILE_NAME, "wt");
76 nlassert(fp != NULL);
77 size_t s = fwrite(classfile.data(), 1, classfile.size(), fp);
78 nlassert(s == classfile.size());
79 fclose(fp);
81 // init ligo
82 NLLIGO::CPrimitiveContext::instance().CurrentLigoConfig = &_LigoConfig;
83 _LigoConfig.readPrimitiveClass(CLASS_FILE_NAME, false);
85 // create a reference primitive
86 if (NLMISC::CFile::isExists(_RefPrimFileName))
88 NLMISC::CFile::deleteFile(_RefPrimFileName);
90 NLLIGO::CPrimitives primDoc;
91 nlassert(primDoc.RootNode != NULL);
93 NLLIGO::CPrimitiveContext::instance().CurrentPrimitive = &primDoc;
95 NLLIGO::IPrimitive *p = dynamic_cast<NLLIGO::IPrimitive *> (NLMISC::CClassRegistry::create ("CPrimNode"));
96 p->addPropertyByName("class", new NLLIGO::CPropertyString("test"));
97 p->addPropertyByName("name", new NLLIGO::CPropertyString("test_root"));
98 primDoc.RootNode->insertChild(p);
100 NLLIGO::CPrimAlias *pa = dynamic_cast<NLLIGO::CPrimAlias *> (NLMISC::CClassRegistry::create ("CPrimAlias"));
101 pa->addPropertyByName("class", new NLLIGO::CPropertyString("alias"));
102 pa->addPropertyByName("name", new NLLIGO::CPropertyString("alias"));
103 p->insertChild(pa);
105 NLLIGO::CPrimitiveContext::instance().CurrentPrimitive = NULL;
107 // save the file
108 saveXmlPrimitiveFile(primDoc, _RefPrimFileName);
111 void tear_down()
113 NLMISC::CPath::setCurrentPath(_RestorePath.c_str());
116 void testAliasGenerator()
118 //Known bug : is we load/save a primitive and replacing a primitive node with itself (conserving the alias), the
119 // 'last generated alias' counter is incremented.
120 uint32 lastGeneratedAlias;
122 // First, load then save the doc
124 NLLIGO::CPrimitives primDoc;
126 NLLIGO::CPrimitiveContext::instance().CurrentPrimitive = &primDoc;
127 loadXmlPrimitiveFile(primDoc, _RefPrimFileName, _LigoConfig);
128 NLLIGO::CPrimitiveContext::instance().CurrentPrimitive = NULL;
130 lastGeneratedAlias = primDoc.getLastGeneratedAlias();
132 // get a copy of the primitive
133 NLLIGO::IPrimitive *prim = NULL;
134 NLLIGO::IPrimitive *primCopy = NULL;
135 TEST_ASSERT(primDoc.RootNode->getChild(prim, 0));
136 if (prim)
138 primCopy = prim->copy();
139 TEST_ASSERT(primCopy != NULL);
140 if (primCopy)
142 // remove the primitive
143 primDoc.RootNode->removeChild(prim);
145 // insert the copy
146 NLLIGO::CPrimitiveContext::instance().CurrentPrimitive = &primDoc;
147 primDoc.RootNode->insertChild(primCopy);
148 NLLIGO::CPrimitiveContext::instance().CurrentPrimitive = NULL;
152 // save the file
153 saveXmlPrimitiveFile(primDoc, _RefPrimFileName);
156 // second, reload the file and check the last generated alias
158 NLLIGO::CPrimitives primDoc;
160 NLLIGO::CPrimitiveContext::instance().CurrentPrimitive = &primDoc;
161 loadXmlPrimitiveFile(primDoc, _RefPrimFileName, _LigoConfig);
162 NLLIGO::CPrimitiveContext::instance().CurrentPrimitive = NULL;
164 TEST_ASSERT(lastGeneratedAlias == primDoc.getLastGeneratedAlias());
168 NLLIGO::CLigoConfig _LigoConfig;
171 #endif