Mercurial > hg > orthanc
annotate UnitTestsSources/LuaTests.cpp @ 1536:1b03676d68c2
fix windows build
author | Sebastien Jodogne <s.jodogne@gmail.com> |
---|---|
date | Thu, 13 Aug 2015 14:16:11 +0200 |
parents | b5eb5210af91 |
children | 9ea3d082b064 |
rev | line source |
---|---|
827
3d6f9b7d0add
precompiled headers in unit tests
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
712
diff
changeset
|
1 /** |
3d6f9b7d0add
precompiled headers in unit tests
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
712
diff
changeset
|
2 * Orthanc - A Lightweight, RESTful DICOM Store |
1288
6e7e5ed91c2d
upgrade to year 2015
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1056
diff
changeset
|
3 * Copyright (C) 2012-2015 Sebastien Jodogne, Medical Physics |
6e7e5ed91c2d
upgrade to year 2015
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1056
diff
changeset
|
4 * Department, University Hospital of Liege, Belgium |
827
3d6f9b7d0add
precompiled headers in unit tests
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
712
diff
changeset
|
5 * |
3d6f9b7d0add
precompiled headers in unit tests
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
712
diff
changeset
|
6 * This program is free software: you can redistribute it and/or |
3d6f9b7d0add
precompiled headers in unit tests
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
712
diff
changeset
|
7 * modify it under the terms of the GNU General Public License as |
3d6f9b7d0add
precompiled headers in unit tests
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
712
diff
changeset
|
8 * published by the Free Software Foundation, either version 3 of the |
3d6f9b7d0add
precompiled headers in unit tests
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
712
diff
changeset
|
9 * License, or (at your option) any later version. |
3d6f9b7d0add
precompiled headers in unit tests
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
712
diff
changeset
|
10 * |
3d6f9b7d0add
precompiled headers in unit tests
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
712
diff
changeset
|
11 * In addition, as a special exception, the copyright holders of this |
3d6f9b7d0add
precompiled headers in unit tests
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
712
diff
changeset
|
12 * program give permission to link the code of its release with the |
3d6f9b7d0add
precompiled headers in unit tests
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
712
diff
changeset
|
13 * OpenSSL project's "OpenSSL" library (or with modified versions of it |
3d6f9b7d0add
precompiled headers in unit tests
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
712
diff
changeset
|
14 * that use the same license as the "OpenSSL" library), and distribute |
3d6f9b7d0add
precompiled headers in unit tests
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
712
diff
changeset
|
15 * the linked executables. You must obey the GNU General Public License |
3d6f9b7d0add
precompiled headers in unit tests
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
712
diff
changeset
|
16 * in all respects for all of the code used other than "OpenSSL". If you |
3d6f9b7d0add
precompiled headers in unit tests
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
712
diff
changeset
|
17 * modify file(s) with this exception, you may extend this exception to |
3d6f9b7d0add
precompiled headers in unit tests
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
712
diff
changeset
|
18 * your version of the file(s), but you are not obligated to do so. If |
3d6f9b7d0add
precompiled headers in unit tests
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
712
diff
changeset
|
19 * you do not wish to do so, delete this exception statement from your |
3d6f9b7d0add
precompiled headers in unit tests
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
712
diff
changeset
|
20 * version. If you delete this exception statement from all source files |
3d6f9b7d0add
precompiled headers in unit tests
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
712
diff
changeset
|
21 * in the program, then also delete it here. |
3d6f9b7d0add
precompiled headers in unit tests
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
712
diff
changeset
|
22 * |
3d6f9b7d0add
precompiled headers in unit tests
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
712
diff
changeset
|
23 * This program is distributed in the hope that it will be useful, but |
3d6f9b7d0add
precompiled headers in unit tests
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
712
diff
changeset
|
24 * WITHOUT ANY WARRANTY; without even the implied warranty of |
3d6f9b7d0add
precompiled headers in unit tests
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
712
diff
changeset
|
25 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
3d6f9b7d0add
precompiled headers in unit tests
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
712
diff
changeset
|
26 * General Public License for more details. |
3d6f9b7d0add
precompiled headers in unit tests
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
712
diff
changeset
|
27 * |
3d6f9b7d0add
precompiled headers in unit tests
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
712
diff
changeset
|
28 * You should have received a copy of the GNU General Public License |
3d6f9b7d0add
precompiled headers in unit tests
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
712
diff
changeset
|
29 * along with this program. If not, see <http://www.gnu.org/licenses/>. |
3d6f9b7d0add
precompiled headers in unit tests
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
712
diff
changeset
|
30 **/ |
3d6f9b7d0add
precompiled headers in unit tests
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
712
diff
changeset
|
31 |
3d6f9b7d0add
precompiled headers in unit tests
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
712
diff
changeset
|
32 |
831
84513f2ee1f3
pch for unit tests and server
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
827
diff
changeset
|
33 #include "PrecompiledHeadersUnitTests.h" |
386 | 34 #include "gtest/gtest.h" |
35 | |
1051 | 36 #include "../Core/Toolbox.h" |
386 | 37 #include "../Core/Lua/LuaFunctionCall.h" |
38 | |
997
1b1d51e9f1a2
return Json from Lua
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
967
diff
changeset
|
39 #include <boost/lexical_cast.hpp> |
1b1d51e9f1a2
return Json from Lua
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
967
diff
changeset
|
40 |
1051 | 41 #if !defined(UNIT_TESTS_WITH_HTTP_CONNEXIONS) |
1532 | 42 #error "Please set UNIT_TESTS_WITH_HTTP_CONNEXIONS to 0 or 1" |
1051 | 43 #endif |
44 | |
386 | 45 |
712 | 46 TEST(Lua, Json) |
386 | 47 { |
397
941ea46e9e26
lua filter of new instances
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
386
diff
changeset
|
48 Orthanc::LuaContext lua; |
941ea46e9e26
lua filter of new instances
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
386
diff
changeset
|
49 lua.Execute(Orthanc::EmbeddedResources::LUA_TOOLBOX); |
941ea46e9e26
lua filter of new instances
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
386
diff
changeset
|
50 lua.Execute("a={}"); |
941ea46e9e26
lua filter of new instances
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
386
diff
changeset
|
51 lua.Execute("a['x'] = 10"); |
941ea46e9e26
lua filter of new instances
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
386
diff
changeset
|
52 lua.Execute("a['y'] = {}"); |
941ea46e9e26
lua filter of new instances
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
386
diff
changeset
|
53 lua.Execute("a['y'][1] = 20"); |
941ea46e9e26
lua filter of new instances
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
386
diff
changeset
|
54 lua.Execute("a['y'][2] = 20"); |
941ea46e9e26
lua filter of new instances
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
386
diff
changeset
|
55 lua.Execute("PrintRecursive(a)"); |
386 | 56 |
397
941ea46e9e26
lua filter of new instances
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
386
diff
changeset
|
57 lua.Execute("function f(a) print(a.bool) return a.bool,20,30,40,50,60 end"); |
386 | 58 |
397
941ea46e9e26
lua filter of new instances
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
386
diff
changeset
|
59 Json::Value v, vv, o; |
941ea46e9e26
lua filter of new instances
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
386
diff
changeset
|
60 //v["a"] = "b"; |
941ea46e9e26
lua filter of new instances
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
386
diff
changeset
|
61 v.append("hello"); |
941ea46e9e26
lua filter of new instances
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
386
diff
changeset
|
62 v.append("world"); |
941ea46e9e26
lua filter of new instances
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
386
diff
changeset
|
63 v.append("42"); |
941ea46e9e26
lua filter of new instances
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
386
diff
changeset
|
64 vv.append("sub"); |
941ea46e9e26
lua filter of new instances
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
386
diff
changeset
|
65 vv.append("set"); |
941ea46e9e26
lua filter of new instances
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
386
diff
changeset
|
66 v.append(vv); |
941ea46e9e26
lua filter of new instances
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
386
diff
changeset
|
67 o = Json::objectValue; |
941ea46e9e26
lua filter of new instances
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
386
diff
changeset
|
68 o["x"] = 10; |
941ea46e9e26
lua filter of new instances
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
386
diff
changeset
|
69 o["y"] = 20; |
941ea46e9e26
lua filter of new instances
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
386
diff
changeset
|
70 o["z"] = 20.5f; |
941ea46e9e26
lua filter of new instances
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
386
diff
changeset
|
71 v.append(o); |
386 | 72 |
397
941ea46e9e26
lua filter of new instances
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
386
diff
changeset
|
73 { |
941ea46e9e26
lua filter of new instances
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
386
diff
changeset
|
74 Orthanc::LuaFunctionCall f(lua, "PrintRecursive"); |
997
1b1d51e9f1a2
return Json from Lua
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
967
diff
changeset
|
75 f.PushJson(v); |
397
941ea46e9e26
lua filter of new instances
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
386
diff
changeset
|
76 f.Execute(); |
941ea46e9e26
lua filter of new instances
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
386
diff
changeset
|
77 } |
386 | 78 |
397
941ea46e9e26
lua filter of new instances
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
386
diff
changeset
|
79 { |
941ea46e9e26
lua filter of new instances
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
386
diff
changeset
|
80 Orthanc::LuaFunctionCall f(lua, "f"); |
997
1b1d51e9f1a2
return Json from Lua
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
967
diff
changeset
|
81 f.PushJson(o); |
397
941ea46e9e26
lua filter of new instances
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
386
diff
changeset
|
82 ASSERT_THROW(f.ExecutePredicate(), Orthanc::LuaException); |
941ea46e9e26
lua filter of new instances
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
386
diff
changeset
|
83 } |
386 | 84 |
397
941ea46e9e26
lua filter of new instances
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
386
diff
changeset
|
85 o["bool"] = false; |
386 | 86 |
397
941ea46e9e26
lua filter of new instances
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
386
diff
changeset
|
87 { |
941ea46e9e26
lua filter of new instances
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
386
diff
changeset
|
88 Orthanc::LuaFunctionCall f(lua, "f"); |
997
1b1d51e9f1a2
return Json from Lua
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
967
diff
changeset
|
89 f.PushJson(o); |
397
941ea46e9e26
lua filter of new instances
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
386
diff
changeset
|
90 ASSERT_FALSE(f.ExecutePredicate()); |
941ea46e9e26
lua filter of new instances
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
386
diff
changeset
|
91 } |
386 | 92 |
397
941ea46e9e26
lua filter of new instances
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
386
diff
changeset
|
93 o["bool"] = true; |
941ea46e9e26
lua filter of new instances
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
386
diff
changeset
|
94 |
386 | 95 { |
397
941ea46e9e26
lua filter of new instances
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
386
diff
changeset
|
96 Orthanc::LuaFunctionCall f(lua, "f"); |
997
1b1d51e9f1a2
return Json from Lua
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
967
diff
changeset
|
97 f.PushJson(o); |
397
941ea46e9e26
lua filter of new instances
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
386
diff
changeset
|
98 ASSERT_TRUE(f.ExecutePredicate()); |
386 | 99 } |
100 } | |
397
941ea46e9e26
lua filter of new instances
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
386
diff
changeset
|
101 |
941ea46e9e26
lua filter of new instances
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
386
diff
changeset
|
102 |
941ea46e9e26
lua filter of new instances
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
386
diff
changeset
|
103 TEST(Lua, Existing) |
941ea46e9e26
lua filter of new instances
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
386
diff
changeset
|
104 { |
941ea46e9e26
lua filter of new instances
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
386
diff
changeset
|
105 Orthanc::LuaContext lua; |
941ea46e9e26
lua filter of new instances
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
386
diff
changeset
|
106 lua.Execute("a={}"); |
941ea46e9e26
lua filter of new instances
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
386
diff
changeset
|
107 lua.Execute("function f() end"); |
941ea46e9e26
lua filter of new instances
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
386
diff
changeset
|
108 |
941ea46e9e26
lua filter of new instances
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
386
diff
changeset
|
109 ASSERT_TRUE(lua.IsExistingFunction("f")); |
941ea46e9e26
lua filter of new instances
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
386
diff
changeset
|
110 ASSERT_FALSE(lua.IsExistingFunction("a")); |
941ea46e9e26
lua filter of new instances
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
386
diff
changeset
|
111 ASSERT_FALSE(lua.IsExistingFunction("Dummy")); |
941ea46e9e26
lua filter of new instances
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
386
diff
changeset
|
112 } |
712 | 113 |
114 | |
115 TEST(Lua, Simple) | |
116 { | |
117 Orthanc::LuaContext lua; | |
118 lua.Execute(Orthanc::EmbeddedResources::LUA_TOOLBOX); | |
119 | |
120 { | |
121 Orthanc::LuaFunctionCall f(lua, "PrintRecursive"); | |
122 f.PushString("hello"); | |
123 f.Execute(); | |
124 } | |
125 | |
126 { | |
127 Orthanc::LuaFunctionCall f(lua, "PrintRecursive"); | |
128 f.PushBoolean(true); | |
129 f.Execute(); | |
130 } | |
131 | |
132 { | |
133 Orthanc::LuaFunctionCall f(lua, "PrintRecursive"); | |
134 f.PushInteger(42); | |
135 f.Execute(); | |
136 } | |
137 | |
138 { | |
139 Orthanc::LuaFunctionCall f(lua, "PrintRecursive"); | |
140 f.PushDouble(3.1415); | |
141 f.Execute(); | |
142 } | |
143 } | |
997
1b1d51e9f1a2
return Json from Lua
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
967
diff
changeset
|
144 |
1b1d51e9f1a2
return Json from Lua
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
967
diff
changeset
|
145 |
1b1d51e9f1a2
return Json from Lua
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
967
diff
changeset
|
146 TEST(Lua, ReturnJson) |
1b1d51e9f1a2
return Json from Lua
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
967
diff
changeset
|
147 { |
1b1d51e9f1a2
return Json from Lua
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
967
diff
changeset
|
148 Json::Value b = Json::objectValue; |
1b1d51e9f1a2
return Json from Lua
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
967
diff
changeset
|
149 b["a"] = 42; |
1448
3f7722179467
refactoring: GetJson in Lua
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1439
diff
changeset
|
150 b["b"] = 44.37; |
3f7722179467
refactoring: GetJson in Lua
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1439
diff
changeset
|
151 b["c"] = -43; |
997
1b1d51e9f1a2
return Json from Lua
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
967
diff
changeset
|
152 |
1b1d51e9f1a2
return Json from Lua
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
967
diff
changeset
|
153 Json::Value c = Json::arrayValue; |
1b1d51e9f1a2
return Json from Lua
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
967
diff
changeset
|
154 c.append("test3"); |
1b1d51e9f1a2
return Json from Lua
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
967
diff
changeset
|
155 c.append("test1"); |
1b1d51e9f1a2
return Json from Lua
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
967
diff
changeset
|
156 c.append("test2"); |
1b1d51e9f1a2
return Json from Lua
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
967
diff
changeset
|
157 |
1b1d51e9f1a2
return Json from Lua
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
967
diff
changeset
|
158 Json::Value a = Json::objectValue; |
1b1d51e9f1a2
return Json from Lua
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
967
diff
changeset
|
159 a["Hello"] = "World"; |
1b1d51e9f1a2
return Json from Lua
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
967
diff
changeset
|
160 a["List"] = Json::arrayValue; |
1b1d51e9f1a2
return Json from Lua
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
967
diff
changeset
|
161 a["List"].append(b); |
1b1d51e9f1a2
return Json from Lua
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
967
diff
changeset
|
162 a["List"].append(c); |
1b1d51e9f1a2
return Json from Lua
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
967
diff
changeset
|
163 |
1b1d51e9f1a2
return Json from Lua
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
967
diff
changeset
|
164 Orthanc::LuaContext lua; |
1b1d51e9f1a2
return Json from Lua
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
967
diff
changeset
|
165 |
1b1d51e9f1a2
return Json from Lua
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
967
diff
changeset
|
166 // This is the identity function (it simply returns its input) |
1b1d51e9f1a2
return Json from Lua
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
967
diff
changeset
|
167 lua.Execute("function identity(a) return a end"); |
1b1d51e9f1a2
return Json from Lua
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
967
diff
changeset
|
168 |
1b1d51e9f1a2
return Json from Lua
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
967
diff
changeset
|
169 { |
1b1d51e9f1a2
return Json from Lua
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
967
diff
changeset
|
170 Orthanc::LuaFunctionCall f(lua, "identity"); |
1b1d51e9f1a2
return Json from Lua
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
967
diff
changeset
|
171 f.PushJson("hello"); |
1b1d51e9f1a2
return Json from Lua
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
967
diff
changeset
|
172 Json::Value v; |
1b1d51e9f1a2
return Json from Lua
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
967
diff
changeset
|
173 f.ExecuteToJson(v); |
1b1d51e9f1a2
return Json from Lua
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
967
diff
changeset
|
174 ASSERT_EQ("hello", v.asString()); |
1b1d51e9f1a2
return Json from Lua
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
967
diff
changeset
|
175 } |
1b1d51e9f1a2
return Json from Lua
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
967
diff
changeset
|
176 |
1b1d51e9f1a2
return Json from Lua
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
967
diff
changeset
|
177 { |
1b1d51e9f1a2
return Json from Lua
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
967
diff
changeset
|
178 Orthanc::LuaFunctionCall f(lua, "identity"); |
1b1d51e9f1a2
return Json from Lua
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
967
diff
changeset
|
179 f.PushJson(42.25); |
1b1d51e9f1a2
return Json from Lua
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
967
diff
changeset
|
180 Json::Value v; |
1b1d51e9f1a2
return Json from Lua
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
967
diff
changeset
|
181 f.ExecuteToJson(v); |
1b1d51e9f1a2
return Json from Lua
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
967
diff
changeset
|
182 ASSERT_FLOAT_EQ(42.25f, v.asFloat()); |
1b1d51e9f1a2
return Json from Lua
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
967
diff
changeset
|
183 } |
1b1d51e9f1a2
return Json from Lua
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
967
diff
changeset
|
184 |
1b1d51e9f1a2
return Json from Lua
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
967
diff
changeset
|
185 { |
1b1d51e9f1a2
return Json from Lua
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
967
diff
changeset
|
186 Orthanc::LuaFunctionCall f(lua, "identity"); |
1448
3f7722179467
refactoring: GetJson in Lua
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1439
diff
changeset
|
187 f.PushJson(-42); |
3f7722179467
refactoring: GetJson in Lua
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1439
diff
changeset
|
188 Json::Value v; |
3f7722179467
refactoring: GetJson in Lua
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1439
diff
changeset
|
189 f.ExecuteToJson(v); |
3f7722179467
refactoring: GetJson in Lua
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1439
diff
changeset
|
190 ASSERT_EQ(-42, v.asInt()); |
3f7722179467
refactoring: GetJson in Lua
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1439
diff
changeset
|
191 } |
3f7722179467
refactoring: GetJson in Lua
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1439
diff
changeset
|
192 |
3f7722179467
refactoring: GetJson in Lua
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1439
diff
changeset
|
193 { |
3f7722179467
refactoring: GetJson in Lua
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1439
diff
changeset
|
194 Orthanc::LuaFunctionCall f(lua, "identity"); |
997
1b1d51e9f1a2
return Json from Lua
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
967
diff
changeset
|
195 Json::Value vv = Json::arrayValue; |
1b1d51e9f1a2
return Json from Lua
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
967
diff
changeset
|
196 f.PushJson(vv); |
1b1d51e9f1a2
return Json from Lua
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
967
diff
changeset
|
197 Json::Value v; |
1b1d51e9f1a2
return Json from Lua
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
967
diff
changeset
|
198 f.ExecuteToJson(v); |
1b1d51e9f1a2
return Json from Lua
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
967
diff
changeset
|
199 ASSERT_EQ(Json::arrayValue, v.type()); |
1b1d51e9f1a2
return Json from Lua
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
967
diff
changeset
|
200 } |
1b1d51e9f1a2
return Json from Lua
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
967
diff
changeset
|
201 |
1b1d51e9f1a2
return Json from Lua
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
967
diff
changeset
|
202 { |
1b1d51e9f1a2
return Json from Lua
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
967
diff
changeset
|
203 Orthanc::LuaFunctionCall f(lua, "identity"); |
1b1d51e9f1a2
return Json from Lua
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
967
diff
changeset
|
204 Json::Value vv = Json::objectValue; |
1b1d51e9f1a2
return Json from Lua
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
967
diff
changeset
|
205 f.PushJson(vv); |
1b1d51e9f1a2
return Json from Lua
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
967
diff
changeset
|
206 Json::Value v; |
1b1d51e9f1a2
return Json from Lua
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
967
diff
changeset
|
207 f.ExecuteToJson(v); |
1b1d51e9f1a2
return Json from Lua
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
967
diff
changeset
|
208 // Lua does not make the distinction between empty lists and empty objects |
1b1d51e9f1a2
return Json from Lua
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
967
diff
changeset
|
209 ASSERT_EQ(Json::arrayValue, v.type()); |
1b1d51e9f1a2
return Json from Lua
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
967
diff
changeset
|
210 } |
1b1d51e9f1a2
return Json from Lua
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
967
diff
changeset
|
211 |
1b1d51e9f1a2
return Json from Lua
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
967
diff
changeset
|
212 { |
1b1d51e9f1a2
return Json from Lua
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
967
diff
changeset
|
213 Orthanc::LuaFunctionCall f(lua, "identity"); |
1b1d51e9f1a2
return Json from Lua
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
967
diff
changeset
|
214 f.PushJson(b); |
1b1d51e9f1a2
return Json from Lua
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
967
diff
changeset
|
215 Json::Value v; |
1b1d51e9f1a2
return Json from Lua
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
967
diff
changeset
|
216 f.ExecuteToJson(v); |
1b1d51e9f1a2
return Json from Lua
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
967
diff
changeset
|
217 ASSERT_EQ(Json::objectValue, v.type()); |
1b1d51e9f1a2
return Json from Lua
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
967
diff
changeset
|
218 ASSERT_FLOAT_EQ(42.0f, v["a"].asFloat()); |
1448
3f7722179467
refactoring: GetJson in Lua
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1439
diff
changeset
|
219 ASSERT_FLOAT_EQ(44.37f, v["b"].asFloat()); |
3f7722179467
refactoring: GetJson in Lua
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1439
diff
changeset
|
220 ASSERT_FLOAT_EQ(-43.0f, v["c"].asFloat()); |
997
1b1d51e9f1a2
return Json from Lua
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
967
diff
changeset
|
221 } |
1b1d51e9f1a2
return Json from Lua
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
967
diff
changeset
|
222 |
1b1d51e9f1a2
return Json from Lua
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
967
diff
changeset
|
223 { |
1b1d51e9f1a2
return Json from Lua
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
967
diff
changeset
|
224 Orthanc::LuaFunctionCall f(lua, "identity"); |
1b1d51e9f1a2
return Json from Lua
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
967
diff
changeset
|
225 f.PushJson(c); |
1b1d51e9f1a2
return Json from Lua
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
967
diff
changeset
|
226 Json::Value v; |
1b1d51e9f1a2
return Json from Lua
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
967
diff
changeset
|
227 f.ExecuteToJson(v); |
1b1d51e9f1a2
return Json from Lua
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
967
diff
changeset
|
228 ASSERT_EQ(Json::arrayValue, v.type()); |
1b1d51e9f1a2
return Json from Lua
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
967
diff
changeset
|
229 ASSERT_EQ("test3", v[0].asString()); |
1b1d51e9f1a2
return Json from Lua
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
967
diff
changeset
|
230 ASSERT_EQ("test1", v[1].asString()); |
1b1d51e9f1a2
return Json from Lua
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
967
diff
changeset
|
231 ASSERT_EQ("test2", v[2].asString()); |
1b1d51e9f1a2
return Json from Lua
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
967
diff
changeset
|
232 } |
1b1d51e9f1a2
return Json from Lua
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
967
diff
changeset
|
233 |
1b1d51e9f1a2
return Json from Lua
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
967
diff
changeset
|
234 { |
1b1d51e9f1a2
return Json from Lua
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
967
diff
changeset
|
235 Orthanc::LuaFunctionCall f(lua, "identity"); |
1b1d51e9f1a2
return Json from Lua
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
967
diff
changeset
|
236 f.PushJson(a); |
1b1d51e9f1a2
return Json from Lua
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
967
diff
changeset
|
237 Json::Value v; |
1b1d51e9f1a2
return Json from Lua
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
967
diff
changeset
|
238 f.ExecuteToJson(v); |
1b1d51e9f1a2
return Json from Lua
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
967
diff
changeset
|
239 ASSERT_EQ("World", v["Hello"].asString()); |
1b1d51e9f1a2
return Json from Lua
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
967
diff
changeset
|
240 ASSERT_EQ(42, v["List"][0]["a"].asInt()); |
1b1d51e9f1a2
return Json from Lua
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
967
diff
changeset
|
241 ASSERT_EQ(44, v["List"][0]["b"].asInt()); |
1448
3f7722179467
refactoring: GetJson in Lua
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1439
diff
changeset
|
242 ASSERT_EQ(-43, v["List"][0]["c"].asInt()); |
997
1b1d51e9f1a2
return Json from Lua
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
967
diff
changeset
|
243 ASSERT_EQ("test3", v["List"][1][0].asString()); |
1b1d51e9f1a2
return Json from Lua
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
967
diff
changeset
|
244 ASSERT_EQ("test1", v["List"][1][1].asString()); |
1b1d51e9f1a2
return Json from Lua
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
967
diff
changeset
|
245 ASSERT_EQ("test2", v["List"][1][2].asString()); |
1b1d51e9f1a2
return Json from Lua
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
967
diff
changeset
|
246 } |
1448
3f7722179467
refactoring: GetJson in Lua
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1439
diff
changeset
|
247 |
3f7722179467
refactoring: GetJson in Lua
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1439
diff
changeset
|
248 { |
3f7722179467
refactoring: GetJson in Lua
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1439
diff
changeset
|
249 Orthanc::LuaFunctionCall f(lua, "DumpJson"); |
3f7722179467
refactoring: GetJson in Lua
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1439
diff
changeset
|
250 f.PushJson(a); |
3f7722179467
refactoring: GetJson in Lua
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1439
diff
changeset
|
251 std::string s; |
3f7722179467
refactoring: GetJson in Lua
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1439
diff
changeset
|
252 f.ExecuteToString(s); |
3f7722179467
refactoring: GetJson in Lua
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1439
diff
changeset
|
253 |
3f7722179467
refactoring: GetJson in Lua
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1439
diff
changeset
|
254 Json::FastWriter writer; |
3f7722179467
refactoring: GetJson in Lua
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1439
diff
changeset
|
255 std::string t = writer.write(a); |
3f7722179467
refactoring: GetJson in Lua
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1439
diff
changeset
|
256 |
3f7722179467
refactoring: GetJson in Lua
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1439
diff
changeset
|
257 ASSERT_EQ(s, t); |
3f7722179467
refactoring: GetJson in Lua
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1439
diff
changeset
|
258 } |
997
1b1d51e9f1a2
return Json from Lua
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
967
diff
changeset
|
259 } |
1051 | 260 |
1448
3f7722179467
refactoring: GetJson in Lua
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1439
diff
changeset
|
261 |
3f7722179467
refactoring: GetJson in Lua
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1439
diff
changeset
|
262 |
1051 | 263 TEST(Lua, Http) |
264 { | |
1055
6f923d52a46c
call Web services from Lua
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1053
diff
changeset
|
265 Orthanc::LuaContext lua; |
1051 | 266 |
1055
6f923d52a46c
call Web services from Lua
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1053
diff
changeset
|
267 #if UNIT_TESTS_WITH_HTTP_CONNEXIONS == 1 |
6f923d52a46c
call Web services from Lua
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1053
diff
changeset
|
268 lua.Execute("JSON = loadstring(HttpGet('http://www.montefiore.ulg.ac.be/~jodogne/Orthanc/ThirdPartyDownloads/JSON.lua')) ()"); |
1396
ac4efabeb80c
Migration of the orthanc-client as a separate project
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1288
diff
changeset
|
269 const std::string url("http://www.montefiore.ulg.ac.be/~jodogne/Orthanc/ThirdPartyDownloads/Product.json"); |
1055
6f923d52a46c
call Web services from Lua
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1053
diff
changeset
|
270 #endif |
6f923d52a46c
call Web services from Lua
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1053
diff
changeset
|
271 |
1051 | 272 std::string s; |
273 lua.Execute(s, "print(HttpGet({}))"); | |
1439 | 274 ASSERT_EQ("nil", Orthanc::Toolbox::StripSpaces(s)); |
1051 | 275 |
276 #if UNIT_TESTS_WITH_HTTP_CONNEXIONS == 1 | |
277 lua.Execute(s, "print(string.len(HttpGet(\"" + url + "\")))"); | |
1055
6f923d52a46c
call Web services from Lua
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1053
diff
changeset
|
278 ASSERT_LE(100, boost::lexical_cast<int>(Orthanc::Toolbox::StripSpaces(s))); |
1051 | 279 |
280 // Parse a JSON file | |
1055
6f923d52a46c
call Web services from Lua
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1053
diff
changeset
|
281 lua.Execute(s, "print(JSON:decode(HttpGet(\"" + url + "\")) ['Product'])"); |
6f923d52a46c
call Web services from Lua
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1053
diff
changeset
|
282 ASSERT_EQ("OrthancClient", Orthanc::Toolbox::StripSpaces(s)); |
1051 | 283 |
1056 | 284 #if 0 |
1055
6f923d52a46c
call Web services from Lua
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1053
diff
changeset
|
285 // This part of the test can only be executed if one instance of |
6f923d52a46c
call Web services from Lua
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1053
diff
changeset
|
286 // Orthanc is running on the localhost |
6f923d52a46c
call Web services from Lua
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1053
diff
changeset
|
287 |
6f923d52a46c
call Web services from Lua
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1053
diff
changeset
|
288 lua.Execute("modality = {}"); |
6f923d52a46c
call Web services from Lua
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1053
diff
changeset
|
289 lua.Execute("table.insert(modality, 'ORTHANC')"); |
6f923d52a46c
call Web services from Lua
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1053
diff
changeset
|
290 lua.Execute("table.insert(modality, 'localhost')"); |
6f923d52a46c
call Web services from Lua
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1053
diff
changeset
|
291 lua.Execute("table.insert(modality, 4242)"); |
6f923d52a46c
call Web services from Lua
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1053
diff
changeset
|
292 |
1052
cc4ff680e2a0
http requests in lua
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1051
diff
changeset
|
293 lua.Execute(s, "print(HttpPost(\"http://localhost:8042/tools/execute-script\", \"print('hello world')\"))"); |
cc4ff680e2a0
http requests in lua
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1051
diff
changeset
|
294 ASSERT_EQ("hello world", Orthanc::Toolbox::StripSpaces(s)); |
cc4ff680e2a0
http requests in lua
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1051
diff
changeset
|
295 |
1055
6f923d52a46c
call Web services from Lua
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1053
diff
changeset
|
296 lua.Execute(s, "print(JSON:decode(HttpPost(\"http://localhost:8042/tools/execute-script\", \"print('[10,42,1000]')\")) [2])"); |
1052
cc4ff680e2a0
http requests in lua
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1051
diff
changeset
|
297 ASSERT_EQ("42", Orthanc::Toolbox::StripSpaces(s)); |
1055
6f923d52a46c
call Web services from Lua
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1053
diff
changeset
|
298 |
6f923d52a46c
call Web services from Lua
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1053
diff
changeset
|
299 // Add/remove a modality with Lua |
6f923d52a46c
call Web services from Lua
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1053
diff
changeset
|
300 Json::Value v; |
6f923d52a46c
call Web services from Lua
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1053
diff
changeset
|
301 lua.Execute(s, "print(HttpGet('http://localhost:8042/modalities/lua'))"); |
6f923d52a46c
call Web services from Lua
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1053
diff
changeset
|
302 ASSERT_EQ(0, Orthanc::Toolbox::StripSpaces(s).size()); |
6f923d52a46c
call Web services from Lua
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1053
diff
changeset
|
303 lua.Execute(s, "print(HttpPut('http://localhost:8042/modalities/lua', JSON:encode(modality)))"); |
6f923d52a46c
call Web services from Lua
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1053
diff
changeset
|
304 lua.Execute(v, "print(HttpGet('http://localhost:8042/modalities/lua'))"); |
6f923d52a46c
call Web services from Lua
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1053
diff
changeset
|
305 ASSERT_TRUE(v.type() == Json::arrayValue); |
6f923d52a46c
call Web services from Lua
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1053
diff
changeset
|
306 lua.Execute(s, "print(HttpDelete('http://localhost:8042/modalities/lua'))"); |
6f923d52a46c
call Web services from Lua
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1053
diff
changeset
|
307 lua.Execute(s, "print(HttpGet('http://localhost:8042/modalities/lua'))"); |
6f923d52a46c
call Web services from Lua
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1053
diff
changeset
|
308 ASSERT_EQ(0, Orthanc::Toolbox::StripSpaces(s).size()); |
1052
cc4ff680e2a0
http requests in lua
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1051
diff
changeset
|
309 #endif |
cc4ff680e2a0
http requests in lua
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1051
diff
changeset
|
310 |
cc4ff680e2a0
http requests in lua
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1051
diff
changeset
|
311 #endif |
1051 | 312 } |