annotate UnitTestsSources/Lua.cpp @ 793:145604a97914

refactoring
author Sebastien Jodogne <s.jodogne@gmail.com>
date Tue, 06 May 2014 10:28:04 +0200
parents 9e3f21441903
children 3d6f9b7d0add
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
386
7dec4f3c922c lua wrapper
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
1 #include "gtest/gtest.h"
7dec4f3c922c lua wrapper
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
2
7dec4f3c922c lua wrapper
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
3 #include "../Core/Lua/LuaFunctionCall.h"
7dec4f3c922c lua wrapper
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
4
7dec4f3c922c lua wrapper
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
5
712
9e3f21441903 lua tests
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 632
diff changeset
6 TEST(Lua, Json)
386
7dec4f3c922c lua wrapper
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
7 {
397
941ea46e9e26 lua filter of new instances
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 386
diff changeset
8 Orthanc::LuaContext lua;
941ea46e9e26 lua filter of new instances
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 386
diff changeset
9 lua.Execute(Orthanc::EmbeddedResources::LUA_TOOLBOX);
941ea46e9e26 lua filter of new instances
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 386
diff changeset
10 lua.Execute("a={}");
941ea46e9e26 lua filter of new instances
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 386
diff changeset
11 lua.Execute("a['x'] = 10");
941ea46e9e26 lua filter of new instances
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 386
diff changeset
12 lua.Execute("a['y'] = {}");
941ea46e9e26 lua filter of new instances
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 386
diff changeset
13 lua.Execute("a['y'][1] = 20");
941ea46e9e26 lua filter of new instances
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 386
diff changeset
14 lua.Execute("a['y'][2] = 20");
941ea46e9e26 lua filter of new instances
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 386
diff changeset
15 lua.Execute("PrintRecursive(a)");
386
7dec4f3c922c lua wrapper
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
16
397
941ea46e9e26 lua filter of new instances
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 386
diff changeset
17 lua.Execute("function f(a) print(a.bool) return a.bool,20,30,40,50,60 end");
386
7dec4f3c922c lua wrapper
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
18
397
941ea46e9e26 lua filter of new instances
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 386
diff changeset
19 Json::Value v, vv, o;
941ea46e9e26 lua filter of new instances
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 386
diff changeset
20 //v["a"] = "b";
941ea46e9e26 lua filter of new instances
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 386
diff changeset
21 v.append("hello");
941ea46e9e26 lua filter of new instances
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 386
diff changeset
22 v.append("world");
941ea46e9e26 lua filter of new instances
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 386
diff changeset
23 v.append("42");
941ea46e9e26 lua filter of new instances
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 386
diff changeset
24 vv.append("sub");
941ea46e9e26 lua filter of new instances
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 386
diff changeset
25 vv.append("set");
941ea46e9e26 lua filter of new instances
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 386
diff changeset
26 v.append(vv);
941ea46e9e26 lua filter of new instances
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 386
diff changeset
27 o = Json::objectValue;
941ea46e9e26 lua filter of new instances
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 386
diff changeset
28 o["x"] = 10;
941ea46e9e26 lua filter of new instances
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 386
diff changeset
29 o["y"] = 20;
941ea46e9e26 lua filter of new instances
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 386
diff changeset
30 o["z"] = 20.5f;
941ea46e9e26 lua filter of new instances
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 386
diff changeset
31 v.append(o);
386
7dec4f3c922c lua wrapper
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
32
397
941ea46e9e26 lua filter of new instances
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 386
diff changeset
33 {
941ea46e9e26 lua filter of new instances
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 386
diff changeset
34 Orthanc::LuaFunctionCall f(lua, "PrintRecursive");
941ea46e9e26 lua filter of new instances
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 386
diff changeset
35 f.PushJSON(v);
941ea46e9e26 lua filter of new instances
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 386
diff changeset
36 f.Execute();
941ea46e9e26 lua filter of new instances
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 386
diff changeset
37 }
386
7dec4f3c922c lua wrapper
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
38
397
941ea46e9e26 lua filter of new instances
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 386
diff changeset
39 {
941ea46e9e26 lua filter of new instances
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 386
diff changeset
40 Orthanc::LuaFunctionCall f(lua, "f");
941ea46e9e26 lua filter of new instances
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 386
diff changeset
41 f.PushJSON(o);
941ea46e9e26 lua filter of new instances
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 386
diff changeset
42 ASSERT_THROW(f.ExecutePredicate(), Orthanc::LuaException);
941ea46e9e26 lua filter of new instances
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 386
diff changeset
43 }
386
7dec4f3c922c lua wrapper
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
44
397
941ea46e9e26 lua filter of new instances
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 386
diff changeset
45 o["bool"] = false;
386
7dec4f3c922c lua wrapper
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
46
397
941ea46e9e26 lua filter of new instances
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 386
diff changeset
47 {
941ea46e9e26 lua filter of new instances
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 386
diff changeset
48 Orthanc::LuaFunctionCall f(lua, "f");
941ea46e9e26 lua filter of new instances
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 386
diff changeset
49 f.PushJSON(o);
941ea46e9e26 lua filter of new instances
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 386
diff changeset
50 ASSERT_FALSE(f.ExecutePredicate());
941ea46e9e26 lua filter of new instances
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 386
diff changeset
51 }
386
7dec4f3c922c lua wrapper
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
52
397
941ea46e9e26 lua filter of new instances
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 386
diff changeset
53 o["bool"] = true;
941ea46e9e26 lua filter of new instances
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 386
diff changeset
54
386
7dec4f3c922c lua wrapper
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
55 {
397
941ea46e9e26 lua filter of new instances
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 386
diff changeset
56 Orthanc::LuaFunctionCall f(lua, "f");
941ea46e9e26 lua filter of new instances
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 386
diff changeset
57 f.PushJSON(o);
941ea46e9e26 lua filter of new instances
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 386
diff changeset
58 ASSERT_TRUE(f.ExecutePredicate());
386
7dec4f3c922c lua wrapper
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
59 }
7dec4f3c922c lua wrapper
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
60 }
397
941ea46e9e26 lua filter of new instances
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 386
diff changeset
61
941ea46e9e26 lua filter of new instances
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 386
diff changeset
62
941ea46e9e26 lua filter of new instances
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 386
diff changeset
63 TEST(Lua, Existing)
941ea46e9e26 lua filter of new instances
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 386
diff changeset
64 {
941ea46e9e26 lua filter of new instances
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 386
diff changeset
65 Orthanc::LuaContext lua;
941ea46e9e26 lua filter of new instances
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 386
diff changeset
66 lua.Execute("a={}");
941ea46e9e26 lua filter of new instances
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 386
diff changeset
67 lua.Execute("function f() end");
941ea46e9e26 lua filter of new instances
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 386
diff changeset
68
941ea46e9e26 lua filter of new instances
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 386
diff changeset
69 ASSERT_TRUE(lua.IsExistingFunction("f"));
941ea46e9e26 lua filter of new instances
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 386
diff changeset
70 ASSERT_FALSE(lua.IsExistingFunction("a"));
941ea46e9e26 lua filter of new instances
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 386
diff changeset
71 ASSERT_FALSE(lua.IsExistingFunction("Dummy"));
941ea46e9e26 lua filter of new instances
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 386
diff changeset
72 }
712
9e3f21441903 lua tests
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 632
diff changeset
73
9e3f21441903 lua tests
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 632
diff changeset
74
9e3f21441903 lua tests
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 632
diff changeset
75 TEST(Lua, Simple)
9e3f21441903 lua tests
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 632
diff changeset
76 {
9e3f21441903 lua tests
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 632
diff changeset
77 Orthanc::LuaContext lua;
9e3f21441903 lua tests
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 632
diff changeset
78 lua.Execute(Orthanc::EmbeddedResources::LUA_TOOLBOX);
9e3f21441903 lua tests
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 632
diff changeset
79
9e3f21441903 lua tests
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 632
diff changeset
80 {
9e3f21441903 lua tests
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 632
diff changeset
81 Orthanc::LuaFunctionCall f(lua, "PrintRecursive");
9e3f21441903 lua tests
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 632
diff changeset
82 f.PushString("hello");
9e3f21441903 lua tests
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 632
diff changeset
83 f.Execute();
9e3f21441903 lua tests
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 632
diff changeset
84 }
9e3f21441903 lua tests
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 632
diff changeset
85
9e3f21441903 lua tests
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 632
diff changeset
86 {
9e3f21441903 lua tests
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 632
diff changeset
87 Orthanc::LuaFunctionCall f(lua, "PrintRecursive");
9e3f21441903 lua tests
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 632
diff changeset
88 f.PushBoolean(true);
9e3f21441903 lua tests
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 632
diff changeset
89 f.Execute();
9e3f21441903 lua tests
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 632
diff changeset
90 }
9e3f21441903 lua tests
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 632
diff changeset
91
9e3f21441903 lua tests
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 632
diff changeset
92 {
9e3f21441903 lua tests
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 632
diff changeset
93 Orthanc::LuaFunctionCall f(lua, "PrintRecursive");
9e3f21441903 lua tests
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 632
diff changeset
94 f.PushInteger(42);
9e3f21441903 lua tests
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 632
diff changeset
95 f.Execute();
9e3f21441903 lua tests
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 632
diff changeset
96 }
9e3f21441903 lua tests
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 632
diff changeset
97
9e3f21441903 lua tests
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 632
diff changeset
98 {
9e3f21441903 lua tests
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 632
diff changeset
99 Orthanc::LuaFunctionCall f(lua, "PrintRecursive");
9e3f21441903 lua tests
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 632
diff changeset
100 f.PushDouble(3.1415);
9e3f21441903 lua tests
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 632
diff changeset
101 f.Execute();
9e3f21441903 lua tests
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 632
diff changeset
102 }
9e3f21441903 lua tests
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 632
diff changeset
103 }