comparison UnitTestsSources/LuaTests.cpp @ 967:dfc076546821

add suffix Tests to unit test sources
author Sebastien Jodogne <s.jodogne@gmail.com>
date Fri, 27 Jun 2014 15:36:38 +0200
parents UnitTestsSources/Lua.cpp@84513f2ee1f3
children 1b1d51e9f1a2
comparison
equal deleted inserted replaced
966:886652370ff2 967:dfc076546821
1 /**
2 * Orthanc - A Lightweight, RESTful DICOM Store
3 * Copyright (C) 2012-2014 Medical Physics Department, CHU of Liege,
4 * Belgium
5 *
6 * This program is free software: you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License as
8 * published by the Free Software Foundation, either version 3 of the
9 * License, or (at your option) any later version.
10 *
11 * In addition, as a special exception, the copyright holders of this
12 * program give permission to link the code of its release with the
13 * OpenSSL project's "OpenSSL" library (or with modified versions of it
14 * that use the same license as the "OpenSSL" library), and distribute
15 * the linked executables. You must obey the GNU General Public License
16 * in all respects for all of the code used other than "OpenSSL". If you
17 * modify file(s) with this exception, you may extend this exception to
18 * your version of the file(s), but you are not obligated to do so. If
19 * you do not wish to do so, delete this exception statement from your
20 * version. If you delete this exception statement from all source files
21 * in the program, then also delete it here.
22 *
23 * This program is distributed in the hope that it will be useful, but
24 * WITHOUT ANY WARRANTY; without even the implied warranty of
25 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
26 * General Public License for more details.
27 *
28 * You should have received a copy of the GNU General Public License
29 * along with this program. If not, see <http://www.gnu.org/licenses/>.
30 **/
31
32
33 #include "PrecompiledHeadersUnitTests.h"
34 #include "gtest/gtest.h"
35
36 #include "../Core/Lua/LuaFunctionCall.h"
37
38
39 TEST(Lua, Json)
40 {
41 Orthanc::LuaContext lua;
42 lua.Execute(Orthanc::EmbeddedResources::LUA_TOOLBOX);
43 lua.Execute("a={}");
44 lua.Execute("a['x'] = 10");
45 lua.Execute("a['y'] = {}");
46 lua.Execute("a['y'][1] = 20");
47 lua.Execute("a['y'][2] = 20");
48 lua.Execute("PrintRecursive(a)");
49
50 lua.Execute("function f(a) print(a.bool) return a.bool,20,30,40,50,60 end");
51
52 Json::Value v, vv, o;
53 //v["a"] = "b";
54 v.append("hello");
55 v.append("world");
56 v.append("42");
57 vv.append("sub");
58 vv.append("set");
59 v.append(vv);
60 o = Json::objectValue;
61 o["x"] = 10;
62 o["y"] = 20;
63 o["z"] = 20.5f;
64 v.append(o);
65
66 {
67 Orthanc::LuaFunctionCall f(lua, "PrintRecursive");
68 f.PushJSON(v);
69 f.Execute();
70 }
71
72 {
73 Orthanc::LuaFunctionCall f(lua, "f");
74 f.PushJSON(o);
75 ASSERT_THROW(f.ExecutePredicate(), Orthanc::LuaException);
76 }
77
78 o["bool"] = false;
79
80 {
81 Orthanc::LuaFunctionCall f(lua, "f");
82 f.PushJSON(o);
83 ASSERT_FALSE(f.ExecutePredicate());
84 }
85
86 o["bool"] = true;
87
88 {
89 Orthanc::LuaFunctionCall f(lua, "f");
90 f.PushJSON(o);
91 ASSERT_TRUE(f.ExecutePredicate());
92 }
93 }
94
95
96 TEST(Lua, Existing)
97 {
98 Orthanc::LuaContext lua;
99 lua.Execute("a={}");
100 lua.Execute("function f() end");
101
102 ASSERT_TRUE(lua.IsExistingFunction("f"));
103 ASSERT_FALSE(lua.IsExistingFunction("a"));
104 ASSERT_FALSE(lua.IsExistingFunction("Dummy"));
105 }
106
107
108 TEST(Lua, Simple)
109 {
110 Orthanc::LuaContext lua;
111 lua.Execute(Orthanc::EmbeddedResources::LUA_TOOLBOX);
112
113 {
114 Orthanc::LuaFunctionCall f(lua, "PrintRecursive");
115 f.PushString("hello");
116 f.Execute();
117 }
118
119 {
120 Orthanc::LuaFunctionCall f(lua, "PrintRecursive");
121 f.PushBoolean(true);
122 f.Execute();
123 }
124
125 {
126 Orthanc::LuaFunctionCall f(lua, "PrintRecursive");
127 f.PushInteger(42);
128 f.Execute();
129 }
130
131 {
132 Orthanc::LuaFunctionCall f(lua, "PrintRecursive");
133 f.PushDouble(3.1415);
134 f.Execute();
135 }
136 }