1 #include <simgear/math/SGMath.hxx>
4 #include "NasalHash.hxx"
5 #include "NasalString.hxx"
7 #include <boost/shared_ptr.hpp>
8 #include <boost/weak_ptr.hpp>
16 std::cerr << "failed: line " << __LINE__ << ": " << #a << std::endl; \
22 naRef member(const nasal::CallContext&) { return naNil(); }
25 std::string getString() const { return ""; }
26 void setString(const std::string&) {}
27 void constVoidFunc() const {}
28 size_t test1Arg(const std::string& str) const { return str.length(); }
29 bool test2Args(const std::string& s, bool c) { return c && s.empty(); }
32 const std::string& getVar() const { return var; }
33 void setVar(const std::string v) { var = v; }
36 void baseVoidFunc(Base& b) {}
37 void baseConstVoidFunc(const Base& b) {}
38 size_t baseFunc2Args(Base& b, int x, const std::string& s) { return x + s.size(); }
39 std::string testPtr(Base& b) { return b.getString(); }
40 void baseFuncCallContext(const Base&, const nasal::CallContext&) {}
46 int getX() const { return _x; }
47 void setX(int x) { _x = x; }
55 typedef boost::shared_ptr<Base> BasePtr;
56 typedef std::vector<BasePtr> BaseVec;
58 struct DoubleDerived2:
61 const BasePtr& getBase() const{return _base;}
63 BaseVec doSomeBaseWork(const BaseVec& v) { return v; }
66 typedef boost::shared_ptr<Derived> DerivedPtr;
67 typedef boost::shared_ptr<DoubleDerived> DoubleDerivedPtr;
68 typedef boost::shared_ptr<DoubleDerived2> DoubleDerived2Ptr;
70 typedef boost::weak_ptr<Derived> DerivedWeakPtr;
72 naRef derivedFreeMember(Derived&, const nasal::CallContext&) { return naNil(); }
73 naRef f_derivedGetX(naContext c, const Derived& d)
75 return nasal::to_nasal(c, d.getX());
77 naRef f_freeFunction(nasal::CallContext) { return naNil(); }
79 int main(int argc, char* argv[])
81 naContext c = naNewContext();
84 using namespace nasal;
86 r = to_nasal(c, "Test");
87 VERIFY( strncmp("Test", naStr_data(r), naStr_len(r)) == 0 );
88 VERIFY( from_nasal<std::string>(c, r) == "Test" );
90 r = to_nasal(c, std::string("Test"));
91 VERIFY( strncmp("Test", naStr_data(r), naStr_len(r)) == 0 );
92 VERIFY( from_nasal<std::string>(c, r) == "Test" );
95 VERIFY( naNumValue(r).num == 42 );
96 VERIFY( from_nasal<int>(c, r) == 42 );
98 r = to_nasal(c, 4.2f);
99 VERIFY( naNumValue(r).num == 4.2f );
100 VERIFY( from_nasal<float>(c, r) == 4.2f );
102 float test_data[3] = {0, 4, 2};
103 r = to_nasal(c, test_data);
106 r = to_nasal(c, vec);
107 VERIFY( from_nasal<SGVec2f>(c, r) == vec );
109 std::vector<int> std_vec;
110 r = to_nasal(c, std_vec);
112 r = to_nasal(c, "string");
115 from_nasal<int>(c, r);
117 std::cerr << "failed: Expected bad_nasal_cast to be thrown" << std::endl;
120 catch(nasal::bad_nasal_cast&)
125 hash.set("vec2", vec);
126 hash.set("name", "my-name");
127 hash.set("string", std::string("blub"));
128 hash.set("func", &f_freeFunction);
130 r = to_nasal(c, hash);
131 VERIFY( naIsHash(r) );
133 VERIFY( hash.get<std::string>("name") == "my-name" );
134 VERIFY( naIsString(hash.get("name")) );
136 Hash mod = hash.createHash("mod");
137 mod.set("parent", hash);
139 //----------------------------------------------------------------------------
140 // Test exposing classes to Nasal
141 //----------------------------------------------------------------------------
143 Ghost<BasePtr>::init("BasePtr")
144 .method("member", &Base::member)
145 .method("strlen", &Base::test1Arg)
146 .member("str", &Base::getString, &Base::setString)
147 .method("str_m", &Base::getString)
148 .method("void", &Base::constVoidFunc)
149 .member("var_r", &Base::getVar)
150 .member("var_w", &Base::setVar)
151 .member("var", &Base::getVar, &Base::setVar)
152 .method("void", &baseVoidFunc)
153 .method("void_c", &baseConstVoidFunc)
154 .method("int2args", &baseFunc2Args)
155 .method("bool2args", &Base::test2Args)
156 .method("str_ptr", &testPtr);
157 Ghost<DerivedPtr>::init("DerivedPtr")
159 .member("x", &Derived::getX, &Derived::setX)
160 .member("x_alternate", &f_derivedGetX)
161 .method("free_fn", &derivedFreeMember)
162 .method("free_member", &derivedFreeMember)
163 .method("baseDoIt", &baseFuncCallContext);
164 Ghost<DoubleDerivedPtr>::init("DoubleDerivedPtr")
165 .bases<DerivedPtr>();
166 Ghost<DoubleDerived2Ptr>::init("DoubleDerived2Ptr")
167 .bases< Ghost<DerivedPtr> >()
168 .member("base", &DoubleDerived2::getBase)
169 .method("doIt", &DoubleDerived2::doSomeBaseWork);
171 Ghost<DerivedWeakPtr>::init("DerivedWeakPtr");
173 VERIFY( Ghost<BasePtr>::isInit() );
174 nasal::to_nasal(c, DoubleDerived2Ptr());
176 BasePtr d( new Derived );
177 naRef derived = Ghost<BasePtr>::create(c, d);
178 VERIFY( naIsGhost(derived) );
179 VERIFY( std::string("DerivedPtr") == naGhost_type(derived)->name );
181 BasePtr d2( new DoubleDerived );
182 derived = Ghost<BasePtr>::create(c, d2);
183 VERIFY( naIsGhost(derived) );
184 VERIFY( std::string("DoubleDerivedPtr") == naGhost_type(derived)->name );
186 BasePtr d3( new DoubleDerived2 );
187 derived = Ghost<BasePtr>::create(c, d3);
188 VERIFY( naIsGhost(derived) );
189 VERIFY( std::string("DoubleDerived2Ptr") == naGhost_type(derived)->name );
191 VERIFY( Ghost<BasePtr>::isBaseOf(derived) );
192 VERIFY( Ghost<DerivedPtr>::isBaseOf(derived) );
193 VERIFY( Ghost<DoubleDerived2Ptr>::isBaseOf(derived) );
195 VERIFY( Ghost<BasePtr>::fromNasal(c, derived) == d3 );
196 VERIFY( Ghost<BasePtr>::fromNasal(c, derived) != d2 );
197 VERIFY( Ghost<DerivedPtr>::fromNasal(c, derived)
198 == boost::dynamic_pointer_cast<Derived>(d3) );
199 VERIFY( Ghost<DoubleDerived2Ptr>::fromNasal(c, derived)
200 == boost::dynamic_pointer_cast<DoubleDerived2>(d3) );
201 VERIFY( !Ghost<DoubleDerivedPtr>::fromNasal(c, derived) );
203 std::map<std::string, BasePtr> instances;
204 VERIFY( naIsHash(to_nasal(c, instances)) );
206 std::map<std::string, DerivedPtr> instances_d;
207 VERIFY( naIsHash(to_nasal(c, instances_d)) );
209 std::map<std::string, int> int_map;
210 VERIFY( naIsHash(to_nasal(c, int_map)) );
212 std::map<std::string, std::vector<int> > int_vector_map;
213 VERIFY( naIsHash(to_nasal(c, int_vector_map)) );
215 // Check converting to Ghost if using Nasal hashes with actual ghost inside
216 // the hashes parents vector
217 std::vector<naRef> parents;
218 parents.push_back(hash.get_naRef());
219 parents.push_back(derived);
222 obj.set("parents", parents);
223 VERIFY( Ghost<BasePtr>::fromNasal(c, obj.get_naRef()) == d3 );
225 // Check recursive parents (aka parent-of-parent)
226 std::vector<naRef> parents2;
227 parents2.push_back(obj.get_naRef());
229 derived_obj.set("parents", parents2);
230 VERIFY( Ghost<BasePtr>::fromNasal(c, derived_obj.get_naRef()) == d3 );
232 std::vector<naRef> nasal_objects;
233 nasal_objects.push_back( Ghost<BasePtr>::create(c, d) );
234 nasal_objects.push_back( Ghost<BasePtr>::create(c, d2) );
235 nasal_objects.push_back( Ghost<BasePtr>::create(c, d3) );
236 naRef obj_vec = to_nasal(c, nasal_objects);
238 std::vector<BasePtr> objects = from_nasal<std::vector<BasePtr> >(c, obj_vec);
239 VERIFY( objects[0] == d );
240 VERIFY( objects[1] == d2 );
241 VERIFY( objects[2] == d3 );
243 // TODO actually do something with the ghosts...
245 //----------------------------------------------------------------------------
246 // Test nasal::CallContext
247 //----------------------------------------------------------------------------
250 int int_vec[] = {1,2,3};
251 std::map<std::string, std::string> map;
253 to_nasal(c, std::string("test-arg")),
255 to_nasal(c, int_vec),
258 CallContext cc(c, sizeof(args)/sizeof(args[0]), args);
259 VERIFY( cc.requireArg<std::string>(0) == "test-arg" );
260 VERIFY( cc.getArg<std::string>(0) == "test-arg" );
261 VERIFY( cc.getArg<std::string>(10) == "" );
262 VERIFY( cc.isString(0) );
263 VERIFY( !cc.isNumeric(0) );
264 VERIFY( !cc.isVector(0) );
265 VERIFY( !cc.isHash(0) );
266 VERIFY( !cc.isGhost(0) );
267 VERIFY( cc.isNumeric(1) );
268 VERIFY( cc.isVector(2) );
269 VERIFY( cc.isHash(3) );
271 naRef args_vec = nasal::to_nasal(c, args);
272 VERIFY( naIsVector(args_vec) );
274 //----------------------------------------------------------------------------
275 // Test nasal::String
276 //----------------------------------------------------------------------------
278 String string( to_nasal(c, "Test") );
279 VERIFY( from_nasal<std::string>(c, string.get_naRef()) == "Test" );
280 VERIFY( string.c_str() == std::string("Test") );
281 VERIFY( string.starts_with(string) );
282 VERIFY( string.starts_with(String(c, "T")) );
283 VERIFY( string.starts_with(String(c, "Te")) );
284 VERIFY( string.starts_with(String(c, "Tes")) );
285 VERIFY( string.starts_with(String(c, "Test")) );
286 VERIFY( !string.starts_with(String(c, "Test1")) );
287 VERIFY( !string.starts_with(String(c, "bb")) );
288 VERIFY( !string.starts_with(String(c, "bbasdasdafasd")) );
289 VERIFY( string.find('e') == 1 );
290 VERIFY( string.find('9') == String::npos );
291 VERIFY( string.find_first_of(String(c, "st")) == 2 );
292 VERIFY( string.find_first_of(String(c, "st"), 3) == 3 );
293 VERIFY( string.find_first_of(String(c, "xyz")) == String::npos );
294 VERIFY( string.find_first_not_of(String(c, "Tst")) == 1 );
295 VERIFY( string.find_first_not_of(String(c, "Tse"), 2) == 3 );
296 VERIFY( string.find_first_not_of(String(c, "abc")) == 0 );
297 VERIFY( string.find_first_not_of(String(c, "abc"), 20) == String::npos );