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; \
28 naRef member(const nasal::CallContext&) { return naNil(); }
31 std::string getString() const { return ""; }
32 void setString(const std::string&) {}
33 void constVoidFunc() const {}
34 size_t test1Arg(const std::string& str) const { return str.length(); }
35 bool test2Args(const std::string& s, bool c) { return c && s.empty(); }
38 const std::string& getVar() const { return var; }
39 void setVar(const std::string v) { var = v; }
42 void baseVoidFunc(Base& b) {}
43 void baseConstVoidFunc(const Base& b) {}
44 size_t baseFunc2Args(Base& b, int x, const std::string& s) { return x + s.size(); }
45 std::string testPtr(Base& b) { return b.getString(); }
46 void baseFuncCallContext(const Base&, const nasal::CallContext&) {}
52 int getX() const { return _x; }
53 void setX(int x) { _x = x; }
61 typedef boost::shared_ptr<Base> BasePtr;
62 typedef std::vector<BasePtr> BaseVec;
64 struct DoubleDerived2:
67 const BasePtr& getBase() const{return _base;}
69 BaseVec doSomeBaseWork(const BaseVec& v) { return v; }
72 typedef boost::shared_ptr<Derived> DerivedPtr;
73 typedef boost::shared_ptr<DoubleDerived> DoubleDerivedPtr;
74 typedef boost::shared_ptr<DoubleDerived2> DoubleDerived2Ptr;
76 typedef boost::weak_ptr<Derived> DerivedWeakPtr;
78 naRef derivedFreeMember(Derived&, const nasal::CallContext&) { return naNil(); }
79 naRef f_derivedGetX(naContext c, const Derived& d)
81 return nasal::to_nasal(c, d.getX());
83 naRef f_freeFunction(nasal::CallContext) { return naNil(); }
85 int main(int argc, char* argv[])
87 naContext c = naNewContext();
90 using namespace nasal;
92 r = to_nasal(c, ENUM_ANOTHER);
93 VERIFY( from_nasal<int>(c, r) == ENUM_ANOTHER );
95 r = to_nasal(c, "Test");
96 VERIFY( strncmp("Test", naStr_data(r), naStr_len(r)) == 0 );
97 VERIFY( from_nasal<std::string>(c, r) == "Test" );
99 r = to_nasal(c, std::string("Test"));
100 VERIFY( strncmp("Test", naStr_data(r), naStr_len(r)) == 0 );
101 VERIFY( from_nasal<std::string>(c, r) == "Test" );
104 VERIFY( naNumValue(r).num == 42 );
105 VERIFY( from_nasal<int>(c, r) == 42 );
107 r = to_nasal(c, 4.2f);
108 VERIFY( naNumValue(r).num == 4.2f );
109 VERIFY( from_nasal<float>(c, r) == 4.2f );
111 float test_data[3] = {0, 4, 2};
112 r = to_nasal(c, test_data);
115 r = to_nasal(c, vec);
116 VERIFY( from_nasal<SGVec2f>(c, r) == vec );
118 std::vector<int> std_vec;
119 r = to_nasal(c, std_vec);
121 r = to_nasal(c, "string");
124 from_nasal<int>(c, r);
126 std::cerr << "failed: Expected bad_nasal_cast to be thrown" << std::endl;
129 catch(nasal::bad_nasal_cast&)
134 hash.set("vec2", vec);
135 hash.set("name", "my-name");
136 hash.set("string", std::string("blub"));
137 hash.set("func", &f_freeFunction);
139 r = to_nasal(c, hash);
140 VERIFY( naIsHash(r) );
142 VERIFY( hash.get<std::string>("name") == "my-name" );
143 VERIFY( naIsString(hash.get("name")) );
145 Hash mod = hash.createHash("mod");
146 mod.set("parent", hash);
148 //----------------------------------------------------------------------------
149 // Test exposing classes to Nasal
150 //----------------------------------------------------------------------------
152 Ghost<BasePtr>::init("BasePtr")
153 .method("member", &Base::member)
154 .method("strlen", &Base::test1Arg)
155 .member("str", &Base::getString, &Base::setString)
156 .method("str_m", &Base::getString)
157 .method("void", &Base::constVoidFunc)
158 .member("var_r", &Base::getVar)
159 .member("var_w", &Base::setVar)
160 .member("var", &Base::getVar, &Base::setVar)
161 .method("void", &baseVoidFunc)
162 .method("void_c", &baseConstVoidFunc)
163 .method("int2args", &baseFunc2Args)
164 .method("bool2args", &Base::test2Args)
165 .method("str_ptr", &testPtr);
166 Ghost<DerivedPtr>::init("DerivedPtr")
168 .member("x", &Derived::getX, &Derived::setX)
169 .member("x_alternate", &f_derivedGetX)
170 .method("free_fn", &derivedFreeMember)
171 .method("free_member", &derivedFreeMember)
172 .method("baseDoIt", &baseFuncCallContext);
173 Ghost<DoubleDerivedPtr>::init("DoubleDerivedPtr")
174 .bases<DerivedPtr>();
175 Ghost<DoubleDerived2Ptr>::init("DoubleDerived2Ptr")
176 .bases< Ghost<DerivedPtr> >()
177 .member("base", &DoubleDerived2::getBase)
178 .method("doIt", &DoubleDerived2::doSomeBaseWork);
180 Ghost<DerivedWeakPtr>::init("DerivedWeakPtr");
182 VERIFY( Ghost<BasePtr>::isInit() );
183 nasal::to_nasal(c, DoubleDerived2Ptr());
185 BasePtr d( new Derived );
186 naRef derived = Ghost<BasePtr>::create(c, d);
187 VERIFY( naIsGhost(derived) );
188 VERIFY( std::string("DerivedPtr") == naGhost_type(derived)->name );
190 BasePtr d2( new DoubleDerived );
191 derived = Ghost<BasePtr>::create(c, d2);
192 VERIFY( naIsGhost(derived) );
193 VERIFY( std::string("DoubleDerivedPtr") == naGhost_type(derived)->name );
195 BasePtr d3( new DoubleDerived2 );
196 derived = Ghost<BasePtr>::create(c, d3);
197 VERIFY( naIsGhost(derived) );
198 VERIFY( std::string("DoubleDerived2Ptr") == naGhost_type(derived)->name );
200 VERIFY( Ghost<BasePtr>::isBaseOf(derived) );
201 VERIFY( Ghost<DerivedPtr>::isBaseOf(derived) );
202 VERIFY( Ghost<DoubleDerived2Ptr>::isBaseOf(derived) );
204 VERIFY( Ghost<BasePtr>::fromNasal(c, derived) == d3 );
205 VERIFY( Ghost<BasePtr>::fromNasal(c, derived) != d2 );
206 VERIFY( Ghost<DerivedPtr>::fromNasal(c, derived)
207 == boost::dynamic_pointer_cast<Derived>(d3) );
208 VERIFY( Ghost<DoubleDerived2Ptr>::fromNasal(c, derived)
209 == boost::dynamic_pointer_cast<DoubleDerived2>(d3) );
210 VERIFY( !Ghost<DoubleDerivedPtr>::fromNasal(c, derived) );
212 std::map<std::string, BasePtr> instances;
213 VERIFY( naIsHash(to_nasal(c, instances)) );
215 std::map<std::string, DerivedPtr> instances_d;
216 VERIFY( naIsHash(to_nasal(c, instances_d)) );
218 std::map<std::string, int> int_map;
219 VERIFY( naIsHash(to_nasal(c, int_map)) );
221 std::map<std::string, std::vector<int> > int_vector_map;
222 VERIFY( naIsHash(to_nasal(c, int_vector_map)) );
224 // Check converting to Ghost if using Nasal hashes with actual ghost inside
225 // the hashes parents vector
226 std::vector<naRef> parents;
227 parents.push_back(hash.get_naRef());
228 parents.push_back(derived);
231 obj.set("parents", parents);
232 VERIFY( Ghost<BasePtr>::fromNasal(c, obj.get_naRef()) == d3 );
234 // Check recursive parents (aka parent-of-parent)
235 std::vector<naRef> parents2;
236 parents2.push_back(obj.get_naRef());
238 derived_obj.set("parents", parents2);
239 VERIFY( Ghost<BasePtr>::fromNasal(c, derived_obj.get_naRef()) == d3 );
241 std::vector<naRef> nasal_objects;
242 nasal_objects.push_back( Ghost<BasePtr>::create(c, d) );
243 nasal_objects.push_back( Ghost<BasePtr>::create(c, d2) );
244 nasal_objects.push_back( Ghost<BasePtr>::create(c, d3) );
245 naRef obj_vec = to_nasal(c, nasal_objects);
247 std::vector<BasePtr> objects = from_nasal<std::vector<BasePtr> >(c, obj_vec);
248 VERIFY( objects[0] == d );
249 VERIFY( objects[1] == d2 );
250 VERIFY( objects[2] == d3 );
252 // TODO actually do something with the ghosts...
254 //----------------------------------------------------------------------------
255 // Test nasal::CallContext
256 //----------------------------------------------------------------------------
259 int int_vec[] = {1,2,3};
260 std::map<std::string, std::string> map;
262 to_nasal(c, std::string("test-arg")),
264 to_nasal(c, int_vec),
267 CallContext cc(c, sizeof(args)/sizeof(args[0]), args);
268 VERIFY( cc.requireArg<std::string>(0) == "test-arg" );
269 VERIFY( cc.getArg<std::string>(0) == "test-arg" );
270 VERIFY( cc.getArg<std::string>(10) == "" );
271 VERIFY( cc.isString(0) );
272 VERIFY( !cc.isNumeric(0) );
273 VERIFY( !cc.isVector(0) );
274 VERIFY( !cc.isHash(0) );
275 VERIFY( !cc.isGhost(0) );
276 VERIFY( cc.isNumeric(1) );
277 VERIFY( cc.isVector(2) );
278 VERIFY( cc.isHash(3) );
280 naRef args_vec = nasal::to_nasal(c, args);
281 VERIFY( naIsVector(args_vec) );
283 //----------------------------------------------------------------------------
284 // Test nasal::String
285 //----------------------------------------------------------------------------
287 String string( to_nasal(c, "Test") );
288 VERIFY( from_nasal<std::string>(c, string.get_naRef()) == "Test" );
289 VERIFY( string.c_str() == std::string("Test") );
290 VERIFY( string.starts_with(string) );
291 VERIFY( string.starts_with(String(c, "T")) );
292 VERIFY( string.starts_with(String(c, "Te")) );
293 VERIFY( string.starts_with(String(c, "Tes")) );
294 VERIFY( string.starts_with(String(c, "Test")) );
295 VERIFY( !string.starts_with(String(c, "Test1")) );
296 VERIFY( !string.starts_with(String(c, "bb")) );
297 VERIFY( !string.starts_with(String(c, "bbasdasdafasd")) );
298 VERIFY( string.ends_with(String(c, "t")) );
299 VERIFY( string.ends_with(String(c, "st")) );
300 VERIFY( string.ends_with(String(c, "est")) );
301 VERIFY( string.ends_with(String(c, "Test")) );
302 VERIFY( !string.ends_with(String(c, "1Test")) );
303 VERIFY( !string.ends_with(String(c, "abc")) );
304 VERIFY( !string.ends_with(String(c, "estasdasd")) );
305 VERIFY( string.find('e') == 1 );
306 VERIFY( string.find('9') == String::npos );
307 VERIFY( string.find_first_of(String(c, "st")) == 2 );
308 VERIFY( string.find_first_of(String(c, "st"), 3) == 3 );
309 VERIFY( string.find_first_of(String(c, "xyz")) == String::npos );
310 VERIFY( string.find_first_not_of(String(c, "Tst")) == 1 );
311 VERIFY( string.find_first_not_of(String(c, "Tse"), 2) == 3 );
312 VERIFY( string.find_first_not_of(String(c, "abc")) == 0 );
313 VERIFY( string.find_first_not_of(String(c, "abc"), 20) == String::npos );