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&) {}
31 int getX() const { return _x; }
32 void setX(int x) { _x = x; }
39 struct DoubleDerived2:
45 typedef boost::shared_ptr<Base> BasePtr;
46 typedef boost::shared_ptr<Derived> DerivedPtr;
47 typedef boost::shared_ptr<DoubleDerived> DoubleDerivedPtr;
48 typedef boost::shared_ptr<DoubleDerived2> DoubleDerived2Ptr;
50 naRef member(Derived&, const nasal::CallContext&) { return naNil(); }
51 naRef f_derivedGetX(naContext c, const Derived& d)
53 return nasal::to_nasal(c, d.getX());
56 int main(int argc, char* argv[])
58 naContext c = naNewContext();
61 using namespace nasal;
63 r = to_nasal(c, "Test");
64 VERIFY( strncmp("Test", naStr_data(r), naStr_len(r)) == 0 );
65 VERIFY( from_nasal<std::string>(c, r) == "Test" );
67 r = to_nasal(c, std::string("Test"));
68 VERIFY( strncmp("Test", naStr_data(r), naStr_len(r)) == 0 );
69 VERIFY( from_nasal<std::string>(c, r) == "Test" );
72 VERIFY( naNumValue(r).num == 42 );
73 VERIFY( from_nasal<int>(c, r) == 42 );
75 r = to_nasal(c, 4.2f);
76 VERIFY( naNumValue(r).num == 4.2f );
77 VERIFY( from_nasal<float>(c, r) == 4.2f );
79 float test_data[3] = {0, 4, 2};
80 r = to_nasal(c, test_data);
84 VERIFY( from_nasal<SGVec2f>(c, r) == vec );
86 std::vector<int> std_vec;
87 r = to_nasal(c, std_vec);
89 r = to_nasal(c, "string");
92 from_nasal<int>(c, r);
94 std::cerr << "failed: Expected bad_nasal_cast to be thrown" << std::endl;
97 catch(nasal::bad_nasal_cast&)
102 hash.set("vec2", vec);
103 hash.set("name", "my-name");
104 hash.set("string", std::string("blub"));
106 r = to_nasal(c, hash);
107 VERIFY( naIsHash(r) );
109 VERIFY( hash.get<std::string>("name") == "my-name" );
110 VERIFY( naIsString(hash.get("name")) );
112 Hash mod = hash.createHash("mod");
113 mod.set("parent", hash);
115 String string( to_nasal(c, "Test") );
116 VERIFY( from_nasal<std::string>(c, string.get_naRef()) == "Test" );
117 VERIFY( string.c_str() == std::string("Test") );
118 VERIFY( string.starts_with(string) );
119 VERIFY( string.starts_with(String(c, "T")) );
120 VERIFY( string.starts_with(String(c, "Te")) );
121 VERIFY( string.starts_with(String(c, "Tes")) );
122 VERIFY( string.starts_with(String(c, "Test")) );
123 VERIFY( !string.starts_with(String(c, "Test1")) );
124 VERIFY( !string.starts_with(String(c, "bb")) );
125 VERIFY( !string.starts_with(String(c, "bbasdasdafasd")) );
126 VERIFY( string.find('e') == 1 );
127 VERIFY( string.find('9') == String::npos );
128 VERIFY( string.find_first_of(String(c, "st")) == 2 );
129 VERIFY( string.find_first_of(String(c, "st"), 3) == 3 );
130 VERIFY( string.find_first_of(String(c, "xyz")) == String::npos );
131 VERIFY( string.find_first_not_of(String(c, "Tst")) == 1 );
132 VERIFY( string.find_first_not_of(String(c, "Tse"), 2) == 3 );
133 VERIFY( string.find_first_not_of(String(c, "abc")) == 0 );
134 VERIFY( string.find_first_not_of(String(c, "abc"), 20) == String::npos );
136 Ghost<BasePtr>::init("BasePtr")
137 .method<&Base::member>("member")
138 .member("str", &Base::getString, &Base::setString);
139 Ghost<DerivedPtr>::init("DerivedPtr")
141 .member("x", &Derived::getX, &Derived::setX)
142 .member("x_alternate", &f_derivedGetX)
143 .method_func<&member>("free_member");
144 Ghost<DoubleDerivedPtr>::init("DoubleDerivedPtr")
145 .bases<DerivedPtr>();
146 Ghost<DoubleDerived2Ptr>::init("DoubleDerived2Ptr")
147 .bases< Ghost<DerivedPtr> >();
149 BasePtr d( new Derived );
150 naRef derived = Ghost<BasePtr>::create(c, d);
151 VERIFY( naIsGhost(derived) );
152 VERIFY( std::string("DerivedPtr") == naGhost_type(derived)->name );
154 BasePtr d2( new DoubleDerived );
155 derived = Ghost<BasePtr>::create(c, d2);
156 VERIFY( naIsGhost(derived) );
157 VERIFY( std::string("DoubleDerivedPtr") == naGhost_type(derived)->name );
159 BasePtr d3( new DoubleDerived2 );
160 derived = Ghost<BasePtr>::create(c, d3);
161 VERIFY( naIsGhost(derived) );
162 VERIFY( std::string("DoubleDerived2Ptr") == naGhost_type(derived)->name );
164 VERIFY( Ghost<BasePtr>::isBaseOf(derived) );
165 VERIFY( Ghost<DerivedPtr>::isBaseOf(derived) );
166 VERIFY( Ghost<DoubleDerived2Ptr>::isBaseOf(derived) );
168 VERIFY( Ghost<BasePtr>::fromNasal(c, derived) == d3 );
169 VERIFY( Ghost<BasePtr>::fromNasal(c, derived) != d2 );
170 VERIFY( Ghost<DerivedPtr>::fromNasal(c, derived)
171 == boost::dynamic_pointer_cast<Derived>(d3) );
172 VERIFY( Ghost<DoubleDerived2Ptr>::fromNasal(c, derived)
173 == boost::dynamic_pointer_cast<DoubleDerived2>(d3) );
174 VERIFY( !Ghost<DoubleDerivedPtr>::fromNasal(c, derived) );
176 // Check converting to Ghost if using Nasal hashes with actual ghost inside
177 // the hashes parents vector
178 std::vector<naRef> parents;
179 parents.push_back(hash.get_naRef());
180 parents.push_back(derived);
183 obj.set("parents", parents);
184 VERIFY( Ghost<BasePtr>::fromNasal(c, obj.get_naRef()) == d3 );
186 // Check recursive parents (aka parent-of-parent)
187 std::vector<naRef> parents2;
188 parents2.push_back(obj.get_naRef());
190 derived_obj.set("parents", parents2);
191 VERIFY( Ghost<BasePtr>::fromNasal(c, derived_obj.get_naRef()) == d3 );
194 to_nasal(c, std::string("test-arg"))
196 CallContext cc(c, sizeof(args)/sizeof(args[0]), args);
197 VERIFY( cc.requireArg<std::string>(0) == "test-arg" );
198 VERIFY( cc.getArg<std::string>(0) == "test-arg" );
199 VERIFY( cc.getArg<std::string>(1) == "" );
201 naRef args_vec = nasal::to_nasal(c, args);
202 VERIFY( naIsVector(args_vec) );
204 // TODO actually do something with the ghosts...