comparison UnitTestsSources/LuaTests.cpp @ 997:1b1d51e9f1a2 lua-scripting

return Json from Lua
author Sebastien Jodogne <s.jodogne@gmail.com>
date Thu, 03 Jul 2014 18:12:50 +0200
parents dfc076546821
children 92f4bf2c5d73
comparison
equal deleted inserted replaced
996:cf52f3bcb2b3 997:1b1d51e9f1a2
33 #include "PrecompiledHeadersUnitTests.h" 33 #include "PrecompiledHeadersUnitTests.h"
34 #include "gtest/gtest.h" 34 #include "gtest/gtest.h"
35 35
36 #include "../Core/Lua/LuaFunctionCall.h" 36 #include "../Core/Lua/LuaFunctionCall.h"
37 37
38 #include <boost/lexical_cast.hpp>
39
38 40
39 TEST(Lua, Json) 41 TEST(Lua, Json)
40 { 42 {
41 Orthanc::LuaContext lua; 43 Orthanc::LuaContext lua;
42 lua.Execute(Orthanc::EmbeddedResources::LUA_TOOLBOX); 44 lua.Execute(Orthanc::EmbeddedResources::LUA_TOOLBOX);
63 o["z"] = 20.5f; 65 o["z"] = 20.5f;
64 v.append(o); 66 v.append(o);
65 67
66 { 68 {
67 Orthanc::LuaFunctionCall f(lua, "PrintRecursive"); 69 Orthanc::LuaFunctionCall f(lua, "PrintRecursive");
68 f.PushJSON(v); 70 f.PushJson(v);
69 f.Execute(); 71 f.Execute();
70 } 72 }
71 73
72 { 74 {
73 Orthanc::LuaFunctionCall f(lua, "f"); 75 Orthanc::LuaFunctionCall f(lua, "f");
74 f.PushJSON(o); 76 f.PushJson(o);
75 ASSERT_THROW(f.ExecutePredicate(), Orthanc::LuaException); 77 ASSERT_THROW(f.ExecutePredicate(), Orthanc::LuaException);
76 } 78 }
77 79
78 o["bool"] = false; 80 o["bool"] = false;
79 81
80 { 82 {
81 Orthanc::LuaFunctionCall f(lua, "f"); 83 Orthanc::LuaFunctionCall f(lua, "f");
82 f.PushJSON(o); 84 f.PushJson(o);
83 ASSERT_FALSE(f.ExecutePredicate()); 85 ASSERT_FALSE(f.ExecutePredicate());
84 } 86 }
85 87
86 o["bool"] = true; 88 o["bool"] = true;
87 89
88 { 90 {
89 Orthanc::LuaFunctionCall f(lua, "f"); 91 Orthanc::LuaFunctionCall f(lua, "f");
90 f.PushJSON(o); 92 f.PushJson(o);
91 ASSERT_TRUE(f.ExecutePredicate()); 93 ASSERT_TRUE(f.ExecutePredicate());
92 } 94 }
93 } 95 }
94 96
95 97
132 Orthanc::LuaFunctionCall f(lua, "PrintRecursive"); 134 Orthanc::LuaFunctionCall f(lua, "PrintRecursive");
133 f.PushDouble(3.1415); 135 f.PushDouble(3.1415);
134 f.Execute(); 136 f.Execute();
135 } 137 }
136 } 138 }
139
140
141 TEST(Lua, ReturnJson)
142 {
143 Json::Value b = Json::objectValue;
144 b["a"] = 42;
145 b["b"] = 44;
146 b["c"] = 43;
147
148 Json::Value c = Json::arrayValue;
149 c.append("test3");
150 c.append("test1");
151 c.append("test2");
152
153 Json::Value a = Json::objectValue;
154 a["Hello"] = "World";
155 a["List"] = Json::arrayValue;
156 a["List"].append(b);
157 a["List"].append(c);
158
159 Orthanc::LuaContext lua;
160
161 // This is the identity function (it simply returns its input)
162 lua.Execute("function identity(a) return a end");
163
164 {
165 Orthanc::LuaFunctionCall f(lua, "identity");
166 f.PushJson("hello");
167 Json::Value v;
168 f.ExecuteToJson(v);
169 ASSERT_EQ("hello", v.asString());
170 }
171
172 {
173 Orthanc::LuaFunctionCall f(lua, "identity");
174 f.PushJson(42.25);
175 Json::Value v;
176 f.ExecuteToJson(v);
177 ASSERT_FLOAT_EQ(42.25f, v.asFloat());
178 }
179
180 {
181 Orthanc::LuaFunctionCall f(lua, "identity");
182 Json::Value vv = Json::arrayValue;
183 f.PushJson(vv);
184 Json::Value v;
185 f.ExecuteToJson(v);
186 ASSERT_EQ(Json::arrayValue, v.type());
187 }
188
189 {
190 Orthanc::LuaFunctionCall f(lua, "identity");
191 Json::Value vv = Json::objectValue;
192 f.PushJson(vv);
193 Json::Value v;
194 f.ExecuteToJson(v);
195 // Lua does not make the distinction between empty lists and empty objects
196 ASSERT_EQ(Json::arrayValue, v.type());
197 }
198
199 {
200 Orthanc::LuaFunctionCall f(lua, "identity");
201 f.PushJson(b);
202 Json::Value v;
203 f.ExecuteToJson(v);
204 ASSERT_EQ(Json::objectValue, v.type());
205 ASSERT_FLOAT_EQ(42.0f, v["a"].asFloat());
206 ASSERT_FLOAT_EQ(44.0f, v["b"].asFloat());
207 ASSERT_FLOAT_EQ(43.0f, v["c"].asFloat());
208 }
209
210 {
211 Orthanc::LuaFunctionCall f(lua, "identity");
212 f.PushJson(c);
213 Json::Value v;
214 f.ExecuteToJson(v);
215 ASSERT_EQ(Json::arrayValue, v.type());
216 ASSERT_EQ("test3", v[0].asString());
217 ASSERT_EQ("test1", v[1].asString());
218 ASSERT_EQ("test2", v[2].asString());
219 }
220
221 {
222 Orthanc::LuaFunctionCall f(lua, "identity");
223 f.PushJson(a);
224 Json::Value v;
225 f.ExecuteToJson(v);
226 ASSERT_EQ("World", v["Hello"].asString());
227 ASSERT_EQ(42, v["List"][0]["a"].asInt());
228 ASSERT_EQ(44, v["List"][0]["b"].asInt());
229 ASSERT_EQ(43, v["List"][0]["c"].asInt());
230 ASSERT_EQ("test3", v["List"][1][0].asString());
231 ASSERT_EQ("test1", v["List"][1][1].asString());
232 ASSERT_EQ("test2", v["List"][1][2].asString());
233 }
234 }