1 #include <simgear/math/SGMath.hxx>
4 #include "NasalHash.hxx"
5 #include "NasalString.hxx"
7 #include <boost/shared_ptr.hpp>
15 std::cerr << "failed: line " << __LINE__ << ": " << #a << std::endl; \
21 naRef member(const nasal::CallContext&) { return naNil(); }
24 std::string getString() const { return ""; }
25 void setString(const std::string&) {}
26 void constVoidFunc() const {}
27 size_t test1Arg(const std::string& str) const { return str.length(); }
28 bool test2Args(const std::string& s, bool c) { return c && s.empty(); }
31 const std::string& getVar() const { return var; }
32 void setVar(const std::string v) { var = v; }
35 void baseVoidFunc(Base& b) {}
36 void baseConstVoidFunc(const Base& b) {}
37 size_t baseFunc2Args(Base& b, int x, const std::string& s) { return x + s.size(); }
38 std::string testPtr(Base& b) { return b.getString(); }
39 void baseFuncCallContext(const Base&, const nasal::CallContext&) {}
45 int getX() const { return _x; }
46 void setX(int x) { _x = x; }
54 typedef boost::shared_ptr<Base> BasePtr;
56 struct DoubleDerived2:
59 const BasePtr& getBase() const{return _base;}
63 typedef boost::shared_ptr<Derived> DerivedPtr;
64 typedef boost::shared_ptr<DoubleDerived> DoubleDerivedPtr;
65 typedef boost::shared_ptr<DoubleDerived2> DoubleDerived2Ptr;
67 naRef to_nasal_helper(naContext c, const BasePtr& base)
69 return nasal::Ghost<BasePtr>::create(c, base);
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());
78 int main(int argc, char* argv[])
80 naContext c = naNewContext();
83 using namespace nasal;
85 r = to_nasal(c, "Test");
86 VERIFY( strncmp("Test", naStr_data(r), naStr_len(r)) == 0 );
87 VERIFY( from_nasal<std::string>(c, r) == "Test" );
89 r = to_nasal(c, std::string("Test"));
90 VERIFY( strncmp("Test", naStr_data(r), naStr_len(r)) == 0 );
91 VERIFY( from_nasal<std::string>(c, r) == "Test" );
94 VERIFY( naNumValue(r).num == 42 );
95 VERIFY( from_nasal<int>(c, r) == 42 );
97 r = to_nasal(c, 4.2f);
98 VERIFY( naNumValue(r).num == 4.2f );
99 VERIFY( from_nasal<float>(c, r) == 4.2f );
101 float test_data[3] = {0, 4, 2};
102 r = to_nasal(c, test_data);
105 r = to_nasal(c, vec);
106 VERIFY( from_nasal<SGVec2f>(c, r) == vec );
108 std::vector<int> std_vec;
109 r = to_nasal(c, std_vec);
111 r = to_nasal(c, "string");
114 from_nasal<int>(c, r);
116 std::cerr << "failed: Expected bad_nasal_cast to be thrown" << std::endl;
119 catch(nasal::bad_nasal_cast&)
124 hash.set("vec2", vec);
125 hash.set("name", "my-name");
126 hash.set("string", std::string("blub"));
128 r = to_nasal(c, hash);
129 VERIFY( naIsHash(r) );
131 VERIFY( hash.get<std::string>("name") == "my-name" );
132 VERIFY( naIsString(hash.get("name")) );
134 Hash mod = hash.createHash("mod");
135 mod.set("parent", hash);
137 //----------------------------------------------------------------------------
138 // Test exposing classes to Nasal
139 //----------------------------------------------------------------------------
141 Ghost<BasePtr>::init("BasePtr")
142 .method("member", &Base::member)
143 .method("strlen", &Base::test1Arg)
144 .member("str", &Base::getString, &Base::setString)
145 .method("str_m", &Base::getString)
146 .method("void", &Base::constVoidFunc)
147 .member("var_r", &Base::getVar)
148 .member("var_w", &Base::setVar)
149 .member("var", &Base::getVar, &Base::setVar)
150 .method("void", &baseVoidFunc)
151 .method("void_c", &baseConstVoidFunc)
152 .method("int2args", &baseFunc2Args)
153 .method("bool2args", &Base::test2Args)
154 .method("str_ptr", &testPtr);
155 Ghost<DerivedPtr>::init("DerivedPtr")
157 .member("x", &Derived::getX, &Derived::setX)
158 .member("x_alternate", &f_derivedGetX)
159 .method("free_fn", &derivedFreeMember)
160 .method("free_member", &derivedFreeMember)
161 .method("baseDoIt", &baseFuncCallContext);
162 Ghost<DoubleDerivedPtr>::init("DoubleDerivedPtr")
163 .bases<DerivedPtr>();
164 Ghost<DoubleDerived2Ptr>::init("DoubleDerived2Ptr")
165 .bases< Ghost<DerivedPtr> >()
166 .member("base", &DoubleDerived2::getBase);
168 BasePtr d( new Derived );
169 naRef derived = Ghost<BasePtr>::create(c, d);
170 VERIFY( naIsGhost(derived) );
171 VERIFY( std::string("DerivedPtr") == naGhost_type(derived)->name );
173 BasePtr d2( new DoubleDerived );
174 derived = Ghost<BasePtr>::create(c, d2);
175 VERIFY( naIsGhost(derived) );
176 VERIFY( std::string("DoubleDerivedPtr") == naGhost_type(derived)->name );
178 BasePtr d3( new DoubleDerived2 );
179 derived = Ghost<BasePtr>::create(c, d3);
180 VERIFY( naIsGhost(derived) );
181 VERIFY( std::string("DoubleDerived2Ptr") == naGhost_type(derived)->name );
183 VERIFY( Ghost<BasePtr>::isBaseOf(derived) );
184 VERIFY( Ghost<DerivedPtr>::isBaseOf(derived) );
185 VERIFY( Ghost<DoubleDerived2Ptr>::isBaseOf(derived) );
187 VERIFY( Ghost<BasePtr>::fromNasal(c, derived) == d3 );
188 VERIFY( Ghost<BasePtr>::fromNasal(c, derived) != d2 );
189 VERIFY( Ghost<DerivedPtr>::fromNasal(c, derived)
190 == boost::dynamic_pointer_cast<Derived>(d3) );
191 VERIFY( Ghost<DoubleDerived2Ptr>::fromNasal(c, derived)
192 == boost::dynamic_pointer_cast<DoubleDerived2>(d3) );
193 VERIFY( !Ghost<DoubleDerivedPtr>::fromNasal(c, derived) );
195 std::map<std::string, BasePtr> instances;
196 VERIFY( naIsHash(to_nasal(c, instances)) );
198 std::map<std::string, DerivedPtr> instances_d;
199 VERIFY( naIsHash(to_nasal(c, instances_d)) );
201 std::map<std::string, int> int_map;
202 VERIFY( naIsHash(to_nasal(c, int_map)) );
204 std::map<std::string, std::vector<int> > int_vector_map;
205 VERIFY( naIsHash(to_nasal(c, int_vector_map)) );
207 // Check converting to Ghost if using Nasal hashes with actual ghost inside
208 // the hashes parents vector
209 std::vector<naRef> parents;
210 parents.push_back(hash.get_naRef());
211 parents.push_back(derived);
214 obj.set("parents", parents);
215 VERIFY( Ghost<BasePtr>::fromNasal(c, obj.get_naRef()) == d3 );
217 // Check recursive parents (aka parent-of-parent)
218 std::vector<naRef> parents2;
219 parents2.push_back(obj.get_naRef());
221 derived_obj.set("parents", parents2);
222 VERIFY( Ghost<BasePtr>::fromNasal(c, derived_obj.get_naRef()) == d3 );
224 // TODO actually do something with the ghosts...
226 //----------------------------------------------------------------------------
227 // Test nasal::CallContext
228 //----------------------------------------------------------------------------
231 int int_vec[] = {1,2,3};
232 std::map<std::string, std::string> map;
234 to_nasal(c, std::string("test-arg")),
236 to_nasal(c, int_vec),
239 CallContext cc(c, sizeof(args)/sizeof(args[0]), args);
240 VERIFY( cc.requireArg<std::string>(0) == "test-arg" );
241 VERIFY( cc.getArg<std::string>(0) == "test-arg" );
242 VERIFY( cc.getArg<std::string>(10) == "" );
243 VERIFY( cc.isString(0) );
244 VERIFY( !cc.isNumeric(0) );
245 VERIFY( !cc.isVector(0) );
246 VERIFY( !cc.isHash(0) );
247 VERIFY( !cc.isGhost(0) );
248 VERIFY( cc.isNumeric(1) );
249 VERIFY( cc.isVector(2) );
250 VERIFY( cc.isHash(3) );
252 naRef args_vec = nasal::to_nasal(c, args);
253 VERIFY( naIsVector(args_vec) );
255 //----------------------------------------------------------------------------
256 // Test nasal::String
257 //----------------------------------------------------------------------------
259 String string( to_nasal(c, "Test") );
260 VERIFY( from_nasal<std::string>(c, string.get_naRef()) == "Test" );
261 VERIFY( string.c_str() == std::string("Test") );
262 VERIFY( string.starts_with(string) );
263 VERIFY( string.starts_with(String(c, "T")) );
264 VERIFY( string.starts_with(String(c, "Te")) );
265 VERIFY( string.starts_with(String(c, "Tes")) );
266 VERIFY( string.starts_with(String(c, "Test")) );
267 VERIFY( !string.starts_with(String(c, "Test1")) );
268 VERIFY( !string.starts_with(String(c, "bb")) );
269 VERIFY( !string.starts_with(String(c, "bbasdasdafasd")) );
270 VERIFY( string.find('e') == 1 );
271 VERIFY( string.find('9') == String::npos );
272 VERIFY( string.find_first_of(String(c, "st")) == 2 );
273 VERIFY( string.find_first_of(String(c, "st"), 3) == 3 );
274 VERIFY( string.find_first_of(String(c, "xyz")) == String::npos );
275 VERIFY( string.find_first_not_of(String(c, "Tst")) == 1 );
276 VERIFY( string.find_first_not_of(String(c, "Tse"), 2) == 3 );
277 VERIFY( string.find_first_not_of(String(c, "abc")) == 0 );
278 VERIFY( string.find_first_not_of(String(c, "abc"), 20) == String::npos );