1 // NeL - MMORPG Framework <http://dev.ryzom.com/projects/nel/>
2 // Copyright (C) 2010 Winch Gate Property Limited
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.
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
28 TEST_ADD(CUTLigoPrimitive::testAliasGenerator
)
35 string _RefPrimFileName
;
38 _RestorePath
= NLMISC::CPath::getCurrentPath();
39 NLMISC::CPath::setCurrentPath(_WorkingPath
.c_str());
41 _RefPrimFileName
= "__test_prim.primitive";
43 // register ligo class factory
46 // create a primitive config file
47 nlinfo("Building a default ligo class file");
49 const char *CLASS_FILE_NAME
= "__ligo_class.xml";
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"
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"
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"
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"
73 + "</NEL_LIGO_PRIMITIVE_CLASS>";
75 FILE *fp
= NLMISC::nlfopen(CLASS_FILE_NAME
, "wt");
77 size_t s
= fwrite(classfile
.data(), 1, classfile
.size(), fp
);
78 nlassert(s
== classfile
.size());
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"));
105 NLLIGO::CPrimitiveContext::instance().CurrentPrimitive
= NULL
;
108 saveXmlPrimitiveFile(primDoc
, _RefPrimFileName
);
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));
138 primCopy
= prim
->copy();
139 TEST_ASSERT(primCopy
!= NULL
);
142 // remove the primitive
143 primDoc
.RootNode
->removeChild(prim
);
146 NLLIGO::CPrimitiveContext::instance().CurrentPrimitive
= &primDoc
;
147 primDoc
.RootNode
->insertChild(primCopy
);
148 NLLIGO::CPrimitiveContext::instance().CurrentPrimitive
= NULL
;
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
;