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 derivedFreeMember(Derived&, const nasal::CallContext&) { return naNil(); }
68 naRef f_derivedGetX(naContext c, const Derived& d)
70 return nasal::to_nasal(c, d.getX());
72 naRef f_freeFunction(nasal::CallContext) { return naNil(); }
74 int main(int argc, char* argv[])
76 naContext c = naNewContext();
79 using namespace nasal;
81 r = to_nasal(c, "Test");
82 VERIFY( strncmp("Test", naStr_data(r), naStr_len(r)) == 0 );
83 VERIFY( from_nasal<std::string>(c, r) == "Test" );
85 r = to_nasal(c, std::string("Test"));
86 VERIFY( strncmp("Test", naStr_data(r), naStr_len(r)) == 0 );
87 VERIFY( from_nasal<std::string>(c, r) == "Test" );
90 VERIFY( naNumValue(r).num == 42 );
91 VERIFY( from_nasal<int>(c, r) == 42 );
93 r = to_nasal(c, 4.2f);
94 VERIFY( naNumValue(r).num == 4.2f );
95 VERIFY( from_nasal<float>(c, r) == 4.2f );
97 float test_data[3] = {0, 4, 2};
98 r = to_nasal(c, test_data);
101 r = to_nasal(c, vec);
102 VERIFY( from_nasal<SGVec2f>(c, r) == vec );
104 std::vector<int> std_vec;
105 r = to_nasal(c, std_vec);
107 r = to_nasal(c, "string");
110 from_nasal<int>(c, r);
112 std::cerr << "failed: Expected bad_nasal_cast to be thrown" << std::endl;
115 catch(nasal::bad_nasal_cast&)
120 hash.set("vec2", vec);
121 hash.set("name", "my-name");
122 hash.set("string", std::string("blub"));
123 hash.set("func", &f_freeFunction);
125 r = to_nasal(c, hash);
126 VERIFY( naIsHash(r) );
128 VERIFY( hash.get<std::string>("name") == "my-name" );
129 VERIFY( naIsString(hash.get("name")) );
131 Hash mod = hash.createHash("mod");
132 mod.set("parent", hash);
134 //----------------------------------------------------------------------------
135 // Test exposing classes to Nasal
136 //----------------------------------------------------------------------------
138 Ghost<BasePtr>::init("BasePtr")
139 .method("member", &Base::member)
140 .method("strlen", &Base::test1Arg)
141 .member("str", &Base::getString, &Base::setString)
142 .method("str_m", &Base::getString)
143 .method("void", &Base::constVoidFunc)
144 .member("var_r", &Base::getVar)
145 .member("var_w", &Base::setVar)
146 .member("var", &Base::getVar, &Base::setVar)
147 .method("void", &baseVoidFunc)
148 .method("void_c", &baseConstVoidFunc)
149 .method("int2args", &baseFunc2Args)
150 .method("bool2args", &Base::test2Args)
151 .method("str_ptr", &testPtr);
152 Ghost<DerivedPtr>::init("DerivedPtr")
154 .member("x", &Derived::getX, &Derived::setX)
155 .member("x_alternate", &f_derivedGetX)
156 .method("free_fn", &derivedFreeMember)
157 .method("free_member", &derivedFreeMember)
158 .method("baseDoIt", &baseFuncCallContext);
159 Ghost<DoubleDerivedPtr>::init("DoubleDerivedPtr")
160 .bases<DerivedPtr>();
161 Ghost<DoubleDerived2Ptr>::init("DoubleDerived2Ptr")
162 .bases< Ghost<DerivedPtr> >()
163 .member("base", &DoubleDerived2::getBase);
165 nasal::to_nasal(c, DoubleDerived2Ptr());
167 BasePtr d( new Derived );
168 naRef derived = Ghost<BasePtr>::create(c, d);
169 VERIFY( naIsGhost(derived) );
170 VERIFY( std::string("DerivedPtr") == naGhost_type(derived)->name );
172 BasePtr d2( new DoubleDerived );
173 derived = Ghost<BasePtr>::create(c, d2);
174 VERIFY( naIsGhost(derived) );
175 VERIFY( std::string("DoubleDerivedPtr") == naGhost_type(derived)->name );
177 BasePtr d3( new DoubleDerived2 );
178 derived = Ghost<BasePtr>::create(c, d3);
179 VERIFY( naIsGhost(derived) );
180 VERIFY( std::string("DoubleDerived2Ptr") == naGhost_type(derived)->name );
182 VERIFY( Ghost<BasePtr>::isBaseOf(derived) );
183 VERIFY( Ghost<DerivedPtr>::isBaseOf(derived) );
184 VERIFY( Ghost<DoubleDerived2Ptr>::isBaseOf(derived) );
186 VERIFY( Ghost<BasePtr>::fromNasal(c, derived) == d3 );
187 VERIFY( Ghost<BasePtr>::fromNasal(c, derived) != d2 );
188 VERIFY( Ghost<DerivedPtr>::fromNasal(c, derived)
189 == boost::dynamic_pointer_cast<Derived>(d3) );
190 VERIFY( Ghost<DoubleDerived2Ptr>::fromNasal(c, derived)
191 == boost::dynamic_pointer_cast<DoubleDerived2>(d3) );
192 VERIFY( !Ghost<DoubleDerivedPtr>::fromNasal(c, derived) );
194 std::map<std::string, BasePtr> instances;
195 VERIFY( naIsHash(to_nasal(c, instances)) );
197 std::map<std::string, DerivedPtr> instances_d;
198 VERIFY( naIsHash(to_nasal(c, instances_d)) );
200 std::map<std::string, int> int_map;
201 VERIFY( naIsHash(to_nasal(c, int_map)) );
203 std::map<std::string, std::vector<int> > int_vector_map;
204 VERIFY( naIsHash(to_nasal(c, int_vector_map)) );
206 // Check converting to Ghost if using Nasal hashes with actual ghost inside
207 // the hashes parents vector
208 std::vector<naRef> parents;
209 parents.push_back(hash.get_naRef());
210 parents.push_back(derived);
213 obj.set("parents", parents);
214 VERIFY( Ghost<BasePtr>::fromNasal(c, obj.get_naRef()) == d3 );
216 // Check recursive parents (aka parent-of-parent)
217 std::vector<naRef> parents2;
218 parents2.push_back(obj.get_naRef());
220 derived_obj.set("parents", parents2);
221 VERIFY( Ghost<BasePtr>::fromNasal(c, derived_obj.get_naRef()) == d3 );
223 // TODO actually do something with the ghosts...
225 //----------------------------------------------------------------------------
226 // Test nasal::CallContext
227 //----------------------------------------------------------------------------
230 int int_vec[] = {1,2,3};
231 std::map<std::string, std::string> map;
233 to_nasal(c, std::string("test-arg")),
235 to_nasal(c, int_vec),
238 CallContext cc(c, sizeof(args)/sizeof(args[0]), args);
239 VERIFY( cc.requireArg<std::string>(0) == "test-arg" );
240 VERIFY( cc.getArg<std::string>(0) == "test-arg" );
241 VERIFY( cc.getArg<std::string>(10) == "" );
242 VERIFY( cc.isString(0) );
243 VERIFY( !cc.isNumeric(0) );
244 VERIFY( !cc.isVector(0) );
245 VERIFY( !cc.isHash(0) );
246 VERIFY( !cc.isGhost(0) );
247 VERIFY( cc.isNumeric(1) );
248 VERIFY( cc.isVector(2) );
249 VERIFY( cc.isHash(3) );
251 naRef args_vec = nasal::to_nasal(c, args);
252 VERIFY( naIsVector(args_vec) );
254 //----------------------------------------------------------------------------
255 // Test nasal::String
256 //----------------------------------------------------------------------------
258 String string( to_nasal(c, "Test") );
259 VERIFY( from_nasal<std::string>(c, string.get_naRef()) == "Test" );
260 VERIFY( string.c_str() == std::string("Test") );
261 VERIFY( string.starts_with(string) );
262 VERIFY( string.starts_with(String(c, "T")) );
263 VERIFY( string.starts_with(String(c, "Te")) );
264 VERIFY( string.starts_with(String(c, "Tes")) );
265 VERIFY( string.starts_with(String(c, "Test")) );
266 VERIFY( !string.starts_with(String(c, "Test1")) );
267 VERIFY( !string.starts_with(String(c, "bb")) );
268 VERIFY( !string.starts_with(String(c, "bbasdasdafasd")) );
269 VERIFY( string.find('e') == 1 );
270 VERIFY( string.find('9') == String::npos );
271 VERIFY( string.find_first_of(String(c, "st")) == 2 );
272 VERIFY( string.find_first_of(String(c, "st"), 3) == 3 );
273 VERIFY( string.find_first_of(String(c, "xyz")) == String::npos );
274 VERIFY( string.find_first_not_of(String(c, "Tst")) == 1 );
275 VERIFY( string.find_first_not_of(String(c, "Tse"), 2) == 3 );
276 VERIFY( string.find_first_not_of(String(c, "abc")) == 0 );
277 VERIFY( string.find_first_not_of(String(c, "abc"), 20) == String::npos );