comparison UnitTestsSources/LuaTests.cpp @ 1448:3f7722179467

refactoring: GetJson in Lua
author Sebastien Jodogne <s.jodogne@gmail.com>
date Thu, 02 Jul 2015 09:10:25 +0200
parents 354640db5a7f
children b5eb5210af91
comparison
equal deleted inserted replaced
1447:5ba7471780ae 1448:3f7722179467
145 145
146 TEST(Lua, ReturnJson) 146 TEST(Lua, ReturnJson)
147 { 147 {
148 Json::Value b = Json::objectValue; 148 Json::Value b = Json::objectValue;
149 b["a"] = 42; 149 b["a"] = 42;
150 b["b"] = 44; 150 b["b"] = 44.37;
151 b["c"] = 43; 151 b["c"] = -43;
152 152
153 Json::Value c = Json::arrayValue; 153 Json::Value c = Json::arrayValue;
154 c.append("test3"); 154 c.append("test3");
155 c.append("test1"); 155 c.append("test1");
156 c.append("test2"); 156 c.append("test2");
182 ASSERT_FLOAT_EQ(42.25f, v.asFloat()); 182 ASSERT_FLOAT_EQ(42.25f, v.asFloat());
183 } 183 }
184 184
185 { 185 {
186 Orthanc::LuaFunctionCall f(lua, "identity"); 186 Orthanc::LuaFunctionCall f(lua, "identity");
187 f.PushJson(-42);
188 Json::Value v;
189 f.ExecuteToJson(v);
190 ASSERT_EQ(-42, v.asInt());
191 }
192
193 {
194 Orthanc::LuaFunctionCall f(lua, "identity");
187 Json::Value vv = Json::arrayValue; 195 Json::Value vv = Json::arrayValue;
188 f.PushJson(vv); 196 f.PushJson(vv);
189 Json::Value v; 197 Json::Value v;
190 f.ExecuteToJson(v); 198 f.ExecuteToJson(v);
191 ASSERT_EQ(Json::arrayValue, v.type()); 199 ASSERT_EQ(Json::arrayValue, v.type());
206 f.PushJson(b); 214 f.PushJson(b);
207 Json::Value v; 215 Json::Value v;
208 f.ExecuteToJson(v); 216 f.ExecuteToJson(v);
209 ASSERT_EQ(Json::objectValue, v.type()); 217 ASSERT_EQ(Json::objectValue, v.type());
210 ASSERT_FLOAT_EQ(42.0f, v["a"].asFloat()); 218 ASSERT_FLOAT_EQ(42.0f, v["a"].asFloat());
211 ASSERT_FLOAT_EQ(44.0f, v["b"].asFloat()); 219 ASSERT_FLOAT_EQ(44.37f, v["b"].asFloat());
212 ASSERT_FLOAT_EQ(43.0f, v["c"].asFloat()); 220 ASSERT_FLOAT_EQ(-43.0f, v["c"].asFloat());
213 } 221 }
214 222
215 { 223 {
216 Orthanc::LuaFunctionCall f(lua, "identity"); 224 Orthanc::LuaFunctionCall f(lua, "identity");
217 f.PushJson(c); 225 f.PushJson(c);
229 Json::Value v; 237 Json::Value v;
230 f.ExecuteToJson(v); 238 f.ExecuteToJson(v);
231 ASSERT_EQ("World", v["Hello"].asString()); 239 ASSERT_EQ("World", v["Hello"].asString());
232 ASSERT_EQ(42, v["List"][0]["a"].asInt()); 240 ASSERT_EQ(42, v["List"][0]["a"].asInt());
233 ASSERT_EQ(44, v["List"][0]["b"].asInt()); 241 ASSERT_EQ(44, v["List"][0]["b"].asInt());
234 ASSERT_EQ(43, v["List"][0]["c"].asInt()); 242 ASSERT_EQ(-43, v["List"][0]["c"].asInt());
235 ASSERT_EQ("test3", v["List"][1][0].asString()); 243 ASSERT_EQ("test3", v["List"][1][0].asString());
236 ASSERT_EQ("test1", v["List"][1][1].asString()); 244 ASSERT_EQ("test1", v["List"][1][1].asString());
237 ASSERT_EQ("test2", v["List"][1][2].asString()); 245 ASSERT_EQ("test2", v["List"][1][2].asString());
238 } 246 }
239 } 247
248 {
249 Orthanc::LuaFunctionCall f(lua, "DumpJson");
250 f.PushJson(a);
251 std::string s;
252 f.ExecuteToString(s);
253
254 Json::FastWriter writer;
255 std::string t = writer.write(a);
256
257 ASSERT_EQ(s, t);
258 }
259 }
260
261
240 262
241 TEST(Lua, Http) 263 TEST(Lua, Http)
242 { 264 {
243 Orthanc::LuaContext lua; 265 Orthanc::LuaContext lua;
244 266