1 #include <simgear/math/SGMath.hxx>
4 #include "NasalHash.hxx"
5 #include "NasalString.hxx"
6 #include <simgear/structure/map.hxx>
8 #include <boost/shared_ptr.hpp>
9 #include <boost/weak_ptr.hpp>
17 std::cerr << "failed: line " << __LINE__ << ": " << #a << std::endl; \
29 naRef member(const nasal::CallContext&) { return naNil(); }
32 std::string getString() const { return ""; }
33 void setString(const std::string&) {}
34 void constVoidFunc() const {}
35 size_t test1Arg(const std::string& str) const { return str.length(); }
36 bool test2Args(const std::string& s, bool c) { return c && s.empty(); }
39 const std::string& getVar() const { return var; }
40 void setVar(const std::string v) { var = v; }
42 unsigned long getThis() const { return (unsigned long)this; }
43 bool genericSet(const std::string& key, const std::string& val)
49 void baseVoidFunc(Base& b) {}
50 void baseConstVoidFunc(const Base& b) {}
51 size_t baseFunc2Args(Base& b, int x, const std::string& s) { return x + s.size(); }
52 std::string testPtr(Base& b) { return b.getString(); }
53 void baseFuncCallContext(const Base&, const nasal::CallContext&) {}
59 int getX() const { return _x; }
60 void setX(int x) { _x = x; }
68 typedef boost::shared_ptr<Base> BasePtr;
69 typedef std::vector<BasePtr> BaseVec;
71 struct DoubleDerived2:
74 const BasePtr& getBase() const{return _base;}
76 BaseVec doSomeBaseWork(const BaseVec& v) { return v; }
79 class SGReferenceBasedClass:
85 class SGWeakReferenceBasedClass:
86 public SGWeakReferenced
91 typedef boost::shared_ptr<Derived> DerivedPtr;
92 typedef boost::shared_ptr<DoubleDerived> DoubleDerivedPtr;
93 typedef boost::shared_ptr<DoubleDerived2> DoubleDerived2Ptr;
94 typedef SGSharedPtr<SGReferenceBasedClass> SGRefBasedPtr;
95 typedef SGSharedPtr<SGWeakReferenceBasedClass> SGWeakRefBasedPtr;
97 typedef boost::weak_ptr<Derived> DerivedWeakPtr;
99 naRef derivedFreeMember(Derived&, const nasal::CallContext&) { return naNil(); }
100 naRef f_derivedGetX(naContext c, const Derived& d)
102 return nasal::to_nasal(c, d.getX());
104 naRef f_freeFunction(nasal::CallContext c) { return c.requireArg<naRef>(0); }
106 int main(int argc, char* argv[])
108 naContext c = naNewContext();
111 using namespace nasal;
113 r = to_nasal(c, ENUM_ANOTHER);
114 VERIFY( from_nasal<int>(c, r) == ENUM_ANOTHER );
116 r = to_nasal(c, "Test");
117 VERIFY( strncmp("Test", naStr_data(r), naStr_len(r)) == 0 );
118 VERIFY( from_nasal<std::string>(c, r) == "Test" );
120 r = to_nasal(c, std::string("Test"));
121 VERIFY( strncmp("Test", naStr_data(r), naStr_len(r)) == 0 );
122 VERIFY( from_nasal<std::string>(c, r) == "Test" );
125 VERIFY( naNumValue(r).num == 42 );
126 VERIFY( from_nasal<int>(c, r) == 42 );
128 r = to_nasal(c, 4.2f);
129 VERIFY( naNumValue(r).num == 4.2f );
130 VERIFY( from_nasal<float>(c, r) == 4.2f );
132 float test_data[3] = {0, 4, 2};
133 r = to_nasal(c, test_data);
136 r = to_nasal(c, vec);
137 VERIFY( from_nasal<SGVec2f>(c, r) == vec );
139 std::vector<int> std_vec;
140 r = to_nasal(c, std_vec);
142 r = to_nasal(c, "string");
145 from_nasal<int>(c, r);
147 std::cerr << "failed: Expected bad_nasal_cast to be thrown" << std::endl;
150 catch(nasal::bad_nasal_cast&)
155 hash.set("vec2", vec);
156 hash.set("name", "my-name");
157 hash.set("string", std::string("blub"));
158 hash.set("func", &f_freeFunction);
160 r = to_nasal(c, hash);
161 VERIFY( naIsHash(r) );
163 VERIFY( hash.get<std::string>("name") == "my-name" );
164 VERIFY( naIsString(hash.get("name")) );
166 Hash mod = hash.createHash("mod");
167 mod.set("parent", hash);
170 // 'func' is a C++ function registered to Nasal and now converted back to C++
171 boost::function<int (int)> f = hash.get<int (int)>("func");
175 boost::function<std::string (int)> fs = hash.get<std::string (int)>("func");
177 VERIFY( fs(14) == "14" );
179 typedef boost::function<void (int)> FuncVoidInt;
180 FuncVoidInt fvi = hash.get<FuncVoidInt>("func");
184 typedef boost::function<std::string (const std::string&, int, float)> FuncMultiArg;
185 FuncMultiArg fma = hash.get<FuncMultiArg>("func");
187 VERIFY( fma("test", 3, .5) == "test" );
189 typedef boost::function<naRef (naRef)> naRefMemFunc;
190 naRefMemFunc fmem = hash.get<naRefMemFunc>("func");
192 naRef ret = fmem(hash.get_naRef()),
193 hash_ref = hash.get_naRef();
194 VERIFY( naIsIdentical(ret, hash_ref) );
196 // Check if nasal::Me gets passed as self/me and remaining arguments are
197 // passed on to function
198 typedef boost::function<int (Me, int)> MeIntFunc;
199 MeIntFunc fmeint = hash.get<MeIntFunc>("func");
200 VERIFY( fmeint(naNil(), 5) == 5 );
202 //----------------------------------------------------------------------------
203 // Test exposing classes to Nasal
204 //----------------------------------------------------------------------------
206 Ghost<BasePtr>::init("BasePtr")
207 .method("member", &Base::member)
208 .method("strlen", &Base::test1Arg)
209 .member("str", &Base::getString, &Base::setString)
210 .method("str_m", &Base::getString)
211 .method("void", &Base::constVoidFunc)
212 .member("var_r", &Base::getVar)
213 .member("var_w", &Base::setVar)
214 .member("var", &Base::getVar, &Base::setVar)
215 .method("void", &baseVoidFunc)
216 .method("void_c", &baseConstVoidFunc)
217 .method("int2args", &baseFunc2Args)
218 .method("bool2args", &Base::test2Args)
219 .method("str_ptr", &testPtr)
220 .method("this", &Base::getThis)
221 ._set(&Base::genericSet);
222 Ghost<DerivedPtr>::init("DerivedPtr")
224 .member("x", &Derived::getX, &Derived::setX)
225 .member("x_alternate", &f_derivedGetX)
226 .method("free_fn", &derivedFreeMember)
227 .method("free_member", &derivedFreeMember)
228 .method("baseDoIt", &baseFuncCallContext);
229 Ghost<DoubleDerivedPtr>::init("DoubleDerivedPtr")
230 .bases<DerivedPtr>();
231 Ghost<DoubleDerived2Ptr>::init("DoubleDerived2Ptr")
232 .bases< Ghost<DerivedPtr> >()
233 .member("base", &DoubleDerived2::getBase)
234 .method("doIt", &DoubleDerived2::doSomeBaseWork);
236 Ghost<DerivedWeakPtr>::init("DerivedWeakPtr");
237 Ghost<SGRefBasedPtr>::init("SGRefBasedPtr");
238 Ghost<SGWeakRefBasedPtr>::init("SGWeakRefBasedPtr");
240 SGWeakRefBasedPtr weak_ptr(new SGWeakReferenceBasedClass());
241 naRef nasal_ref = to_nasal(c, weak_ptr),
242 nasal_ptr = to_nasal(c, weak_ptr.get());
244 VERIFY( naIsGhost(nasal_ref) );
245 VERIFY( naIsGhost(nasal_ptr) );
247 SGWeakRefBasedPtr ptr1 = from_nasal<SGWeakRefBasedPtr>(c, nasal_ref),
248 ptr2 = from_nasal<SGWeakRefBasedPtr>(c, nasal_ptr);
250 VERIFY( weak_ptr == ptr1 );
251 VERIFY( weak_ptr == ptr2 );
254 VERIFY( Ghost<BasePtr>::isInit() );
255 nasal::to_nasal(c, DoubleDerived2Ptr());
257 BasePtr d( new Derived );
258 naRef derived = to_nasal(c, d);
259 VERIFY( naIsGhost(derived) );
260 VERIFY( std::string("DerivedPtr") == naGhost_type(derived)->name );
262 // Get member function from ghost...
263 naRef thisGetter = naNil();
264 VERIFY( naMember_get(c, derived, to_nasal(c, "this"), &thisGetter) );
265 VERIFY( naIsFunc(thisGetter) );
267 // ...and check if it really gets passed the correct instance
268 typedef boost::function<unsigned long (Me)> MemFunc;
269 MemFunc fGetThis = from_nasal<MemFunc>(c, thisGetter);
271 VERIFY( fGetThis(derived) == (unsigned long)d.get() );
273 BasePtr d2( new DoubleDerived );
274 derived = to_nasal(c, d2);
275 VERIFY( naIsGhost(derived) );
276 VERIFY( std::string("DoubleDerivedPtr") == naGhost_type(derived)->name );
278 BasePtr d3( new DoubleDerived2 );
279 derived = to_nasal(c, d3);
280 VERIFY( naIsGhost(derived) );
281 VERIFY( std::string("DoubleDerived2Ptr") == naGhost_type(derived)->name );
283 SGRefBasedPtr ref_based( new SGReferenceBasedClass );
284 naRef na_ref_based = to_nasal(c, ref_based.get());
285 VERIFY( naIsGhost(na_ref_based) );
286 VERIFY( from_nasal<SGReferenceBasedClass*>(c, na_ref_based)
287 == ref_based.get() );
288 VERIFY( from_nasal<SGRefBasedPtr>(c, na_ref_based) == ref_based );
290 VERIFY( Ghost<BasePtr>::isBaseOf(derived) );
291 VERIFY( Ghost<DerivedPtr>::isBaseOf(derived) );
292 VERIFY( Ghost<DoubleDerived2Ptr>::isBaseOf(derived) );
294 VERIFY( from_nasal<BasePtr>(c, derived) == d3 );
295 VERIFY( from_nasal<BasePtr>(c, derived) != d2 );
296 VERIFY( from_nasal<DerivedPtr>(c, derived)
297 == boost::dynamic_pointer_cast<Derived>(d3) );
298 VERIFY( from_nasal<DoubleDerived2Ptr>(c, derived)
299 == boost::dynamic_pointer_cast<DoubleDerived2>(d3) );
300 VERIFY( !from_nasal<DoubleDerivedPtr>(c, derived) );
302 std::map<std::string, BasePtr> instances;
303 VERIFY( naIsHash(to_nasal(c, instances)) );
305 std::map<std::string, DerivedPtr> instances_d;
306 VERIFY( naIsHash(to_nasal(c, instances_d)) );
308 std::map<std::string, int> int_map;
309 VERIFY( naIsHash(to_nasal(c, int_map)) );
311 std::map<std::string, std::vector<int> > int_vector_map;
312 VERIFY( naIsHash(to_nasal(c, int_vector_map)) );
314 simgear::StringMap dict =
315 simgear::StringMap("hello", "value")
317 naRef na_dict = to_nasal(c, dict);
318 VERIFY( naIsHash(na_dict) );
319 VERIFY( Hash(na_dict, c).get<std::string>("key2") == "value2" );
321 // Check converting to Ghost if using Nasal hashes with actual ghost inside
322 // the hashes parents vector
323 std::vector<naRef> parents;
324 parents.push_back(hash.get_naRef());
325 parents.push_back(derived);
328 obj.set("parents", parents);
329 VERIFY( from_nasal<BasePtr>(c, obj.get_naRef()) == d3 );
331 // Check recursive parents (aka parent-of-parent)
332 std::vector<naRef> parents2;
333 parents2.push_back(obj.get_naRef());
335 derived_obj.set("parents", parents2);
336 VERIFY( from_nasal<BasePtr>(c, derived_obj.get_naRef()) == d3 );
338 std::vector<naRef> nasal_objects;
339 nasal_objects.push_back( Ghost<BasePtr>::create(c, d) );
340 nasal_objects.push_back( Ghost<BasePtr>::create(c, d2) );
341 nasal_objects.push_back( Ghost<BasePtr>::create(c, d3) );
342 naRef obj_vec = to_nasal(c, nasal_objects);
344 std::vector<BasePtr> objects = from_nasal<std::vector<BasePtr> >(c, obj_vec);
345 VERIFY( objects[0] == d );
346 VERIFY( objects[1] == d2 );
347 VERIFY( objects[2] == d3 );
349 // Calling fallback setter for unset values
350 const char* src_code = "me.test = 3;";
352 naRef code = naParseCode( c, to_nasal(c, "source.nas"), 0,
353 (char*)src_code, strlen(src_code),
355 ret = naCallMethod(code, derived, 0, 0, naNil());
357 VERIFY( !naGetError(c) ) // TODO real error check (this seems to always
359 VERIFY( from_nasal<int>(c, ret) == 3 )
361 //----------------------------------------------------------------------------
362 // Test nasal::CallContext
363 //----------------------------------------------------------------------------
366 int int_vec[] = {1,2,3};
367 std::map<std::string, std::string> map;
369 to_nasal(c, std::string("test-arg")),
371 to_nasal(c, int_vec),
374 CallContext cc(c, sizeof(args)/sizeof(args[0]), args);
375 VERIFY( cc.requireArg<std::string>(0) == "test-arg" );
376 VERIFY( cc.getArg<std::string>(0) == "test-arg" );
377 VERIFY( cc.getArg<std::string>(10) == "" );
378 VERIFY( cc.isString(0) );
379 VERIFY( !cc.isNumeric(0) );
380 VERIFY( !cc.isVector(0) );
381 VERIFY( !cc.isHash(0) );
382 VERIFY( !cc.isGhost(0) );
383 VERIFY( cc.isNumeric(1) );
384 VERIFY( cc.isVector(2) );
385 VERIFY( cc.isHash(3) );
387 naRef args_vec = nasal::to_nasal(c, args);
388 VERIFY( naIsVector(args_vec) );
390 //----------------------------------------------------------------------------
391 // Test nasal::String
392 //----------------------------------------------------------------------------
394 String string( to_nasal(c, "Test") );
395 VERIFY( from_nasal<std::string>(c, string.get_naRef()) == "Test" );
396 VERIFY( string.c_str() == std::string("Test") );
397 VERIFY( string.starts_with(string) );
398 VERIFY( string.starts_with(String(c, "T")) );
399 VERIFY( string.starts_with(String(c, "Te")) );
400 VERIFY( string.starts_with(String(c, "Tes")) );
401 VERIFY( string.starts_with(String(c, "Test")) );
402 VERIFY( !string.starts_with(String(c, "Test1")) );
403 VERIFY( !string.starts_with(String(c, "bb")) );
404 VERIFY( !string.starts_with(String(c, "bbasdasdafasd")) );
405 VERIFY( string.ends_with(String(c, "t")) );
406 VERIFY( string.ends_with(String(c, "st")) );
407 VERIFY( string.ends_with(String(c, "est")) );
408 VERIFY( string.ends_with(String(c, "Test")) );
409 VERIFY( !string.ends_with(String(c, "1Test")) );
410 VERIFY( !string.ends_with(String(c, "abc")) );
411 VERIFY( !string.ends_with(String(c, "estasdasd")) );
412 VERIFY( string.find('e') == 1 );
413 VERIFY( string.find('9') == String::npos );
414 VERIFY( string.find_first_of(String(c, "st")) == 2 );
415 VERIFY( string.find_first_of(String(c, "st"), 3) == 3 );
416 VERIFY( string.find_first_of(String(c, "xyz")) == String::npos );
417 VERIFY( string.find_first_not_of(String(c, "Tst")) == 1 );
418 VERIFY( string.find_first_not_of(String(c, "Tse"), 2) == 3 );
419 VERIFY( string.find_first_not_of(String(c, "abc")) == 0 );
420 VERIFY( string.find_first_not_of(String(c, "abc"), 20) == String::npos );