comparison OrthancFramework/UnitTestsSources/LuaTests.cpp @ 4059:e241e5f3f088 framework

moved LuaTests and MemoryCacheTests from OrthancServer to OrthancFramework
author Sebastien Jodogne <s.jodogne@gmail.com>
date Thu, 11 Jun 2020 14:12:07 +0200
parents OrthancServer/UnitTestsSources/LuaTests.cpp@05b8fd21089c
children 0953b3dc3261
comparison
equal deleted inserted replaced
4058:2a8bf0991be0 4059:e241e5f3f088
1 /**
2 * Orthanc - A Lightweight, RESTful DICOM Store
3 * Copyright (C) 2012-2016 Sebastien Jodogne, Medical Physics
4 * Department, University Hospital of Liege, Belgium
5 * Copyright (C) 2017-2020 Osimis S.A., Belgium
6 *
7 * This program is free software: you can redistribute it and/or
8 * modify it under the terms of the GNU General Public License as
9 * published by the Free Software Foundation, either version 3 of the
10 * License, or (at your option) any later version.
11 *
12 * In addition, as a special exception, the copyright holders of this
13 * program give permission to link the code of its release with the
14 * OpenSSL project's "OpenSSL" library (or with modified versions of it
15 * that use the same license as the "OpenSSL" library), and distribute
16 * the linked executables. You must obey the GNU General Public License
17 * in all respects for all of the code used other than "OpenSSL". If you
18 * modify file(s) with this exception, you may extend this exception to
19 * your version of the file(s), but you are not obligated to do so. If
20 * you do not wish to do so, delete this exception statement from your
21 * version. If you delete this exception statement from all source files
22 * in the program, then also delete it here.
23 *
24 * This program is distributed in the hope that it will be useful, but
25 * WITHOUT ANY WARRANTY; without even the implied warranty of
26 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
27 * General Public License for more details.
28 *
29 * You should have received a copy of the GNU General Public License
30 * along with this program. If not, see <http://www.gnu.org/licenses/>.
31 **/
32
33
34 #if ORTHANC_UNIT_TESTS_LINK_FRAMEWORK == 1
35 # include <OrthancFramework.h>
36 #endif
37
38 #include "gtest/gtest.h"
39
40 #include "../Sources/OrthancException.h"
41 #include "../Sources/Toolbox.h"
42 #include "../Sources/Lua/LuaFunctionCall.h"
43
44 #include <boost/lexical_cast.hpp>
45
46
47 TEST(Lua, Existing)
48 {
49 Orthanc::LuaContext lua;
50 lua.Execute("a={}");
51 lua.Execute("function f() end");
52
53 ASSERT_TRUE(lua.IsExistingFunction("f"));
54 ASSERT_FALSE(lua.IsExistingFunction("a"));
55 ASSERT_FALSE(lua.IsExistingFunction("Dummy"));
56 }
57
58
59 TEST(Lua, ReturnJson)
60 {
61 Json::Value b = Json::objectValue;
62 b["a"] = 42;
63 b["b"] = 44.37;
64 b["c"] = -43;
65
66 Json::Value c = Json::arrayValue;
67 c.append("test3");
68 c.append("test1");
69 c.append("test2");
70
71 Json::Value a = Json::objectValue;
72 a["Hello"] = "World";
73 a["List"] = Json::arrayValue;
74 a["List"].append(b);
75 a["List"].append(c);
76
77 Orthanc::LuaContext lua;
78
79 // This is the identity function (it simply returns its input)
80 lua.Execute("function identity(a) return a end");
81
82 {
83 Orthanc::LuaFunctionCall f(lua, "identity");
84 f.PushJson("hello");
85 Json::Value v;
86 f.ExecuteToJson(v, false);
87 ASSERT_EQ("hello", v.asString());
88 }
89
90 {
91 Orthanc::LuaFunctionCall f(lua, "identity");
92 f.PushJson(42.25);
93 Json::Value v;
94 f.ExecuteToJson(v, false);
95 ASSERT_FLOAT_EQ(42.25f, v.asFloat());
96 }
97
98 {
99 Orthanc::LuaFunctionCall f(lua, "identity");
100 f.PushJson(-42);
101 Json::Value v;
102 f.ExecuteToJson(v, false);
103 ASSERT_EQ(-42, v.asInt());
104 }
105
106 {
107 Orthanc::LuaFunctionCall f(lua, "identity");
108 Json::Value vv = Json::arrayValue;
109 f.PushJson(vv);
110 Json::Value v;
111 f.ExecuteToJson(v, false);
112 ASSERT_EQ(Json::arrayValue, v.type());
113 }
114
115 {
116 Orthanc::LuaFunctionCall f(lua, "identity");
117 Json::Value vv = Json::objectValue;
118 f.PushJson(vv);
119 Json::Value v;
120 f.ExecuteToJson(v, false);
121 // Lua does not make the distinction between empty lists and empty objects
122 ASSERT_EQ(Json::arrayValue, v.type());
123 }
124
125 {
126 Orthanc::LuaFunctionCall f(lua, "identity");
127 f.PushJson(b);
128 Json::Value v;
129 f.ExecuteToJson(v, false);
130 ASSERT_EQ(Json::objectValue, v.type());
131 ASSERT_FLOAT_EQ(42.0f, v["a"].asFloat());
132 ASSERT_FLOAT_EQ(44.37f, v["b"].asFloat());
133 ASSERT_FLOAT_EQ(-43.0f, v["c"].asFloat());
134 }
135
136 {
137 Orthanc::LuaFunctionCall f(lua, "identity");
138 f.PushJson(c);
139 Json::Value v;
140 f.ExecuteToJson(v, false);
141 ASSERT_EQ(Json::arrayValue, v.type());
142 ASSERT_EQ("test3", v[0].asString());
143 ASSERT_EQ("test1", v[1].asString());
144 ASSERT_EQ("test2", v[2].asString());
145 }
146
147 {
148 Orthanc::LuaFunctionCall f(lua, "identity");
149 f.PushJson(a);
150 Json::Value v;
151 f.ExecuteToJson(v, false);
152 ASSERT_EQ("World", v["Hello"].asString());
153 ASSERT_EQ(Json::intValue, v["List"][0]["a"].type());
154 ASSERT_EQ(Json::realValue, v["List"][0]["b"].type());
155 ASSERT_EQ(Json::intValue, v["List"][0]["c"].type());
156 ASSERT_EQ(42, v["List"][0]["a"].asInt());
157 ASSERT_FLOAT_EQ(44.37f, v["List"][0]["b"].asFloat());
158 ASSERT_EQ(44, v["List"][0]["b"].asInt());
159 ASSERT_EQ(-43, v["List"][0]["c"].asInt());
160 ASSERT_EQ("test3", v["List"][1][0].asString());
161 ASSERT_EQ("test1", v["List"][1][1].asString());
162 ASSERT_EQ("test2", v["List"][1][2].asString());
163 }
164
165 {
166 Orthanc::LuaFunctionCall f(lua, "identity");
167 f.PushJson(a);
168 Json::Value v;
169 f.ExecuteToJson(v, true);
170 ASSERT_EQ("World", v["Hello"].asString());
171 ASSERT_EQ(Json::stringValue, v["List"][0]["a"].type());
172 ASSERT_EQ(Json::stringValue, v["List"][0]["b"].type());
173 ASSERT_EQ(Json::stringValue, v["List"][0]["c"].type());
174 ASSERT_FLOAT_EQ(42.0f, boost::lexical_cast<float>(v["List"][0]["a"].asString()));
175 ASSERT_FLOAT_EQ(44.37f, boost::lexical_cast<float>(v["List"][0]["b"].asString()));
176 ASSERT_FLOAT_EQ(-43.0f, boost::lexical_cast<float>(v["List"][0]["c"].asString()));
177 ASSERT_EQ("test3", v["List"][1][0].asString());
178 ASSERT_EQ("test1", v["List"][1][1].asString());
179 ASSERT_EQ("test2", v["List"][1][2].asString());
180 }
181
182 {
183 Orthanc::LuaFunctionCall f(lua, "DumpJson");
184 f.PushJson(a);
185 std::string s;
186 f.ExecuteToString(s);
187
188 Json::FastWriter writer;
189 std::string t = writer.write(a);
190
191 ASSERT_EQ(s, t);
192 }
193 }