annotate UnitTests/Lua.cpp @ 386:7dec4f3c922c lua-scripting

lua wrapper
author Sebastien Jodogne <s.jodogne@gmail.com>
date Mon, 29 Apr 2013 17:14:10 +0200
parents
children 941ea46e9e26
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
7dec4f3c922c lua wrapper
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
6 TEST(Lua, Simple)
7dec4f3c922c lua wrapper
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
7 {
7dec4f3c922c lua wrapper
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
8 try
7dec4f3c922c lua wrapper
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
9 {
7dec4f3c922c lua wrapper
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
10 Orthanc::LuaContext lua;
7dec4f3c922c lua wrapper
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
11 lua.Execute(Orthanc::EmbeddedResources::LUA_TOOLBOX);
7dec4f3c922c lua wrapper
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
12 lua.Execute("a={}");
7dec4f3c922c lua wrapper
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
13 lua.Execute("a['x'] = 10");
7dec4f3c922c lua wrapper
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
14 lua.Execute("a['y'] = {}");
7dec4f3c922c lua wrapper
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
15 lua.Execute("a['y'][1] = 20");
7dec4f3c922c lua wrapper
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
16 lua.Execute("a['y'][2] = 20");
7dec4f3c922c lua wrapper
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
17 lua.Execute("rPrint(a)");
7dec4f3c922c lua wrapper
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
18
7dec4f3c922c lua wrapper
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
19 lua.Execute("function f(a) print(a.bool) return a.bool,20,30,40,50,60 end");
7dec4f3c922c lua wrapper
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
20
7dec4f3c922c lua wrapper
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
21 Json::Value v, vv, o;
7dec4f3c922c lua wrapper
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
22 //v["a"] = "b";
7dec4f3c922c lua wrapper
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
23 v.append("hello");
7dec4f3c922c lua wrapper
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
24 v.append("world");
7dec4f3c922c lua wrapper
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
25 v.append("42");
7dec4f3c922c lua wrapper
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
26 vv.append("coucou");
7dec4f3c922c lua wrapper
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
27 vv.append("toi");
7dec4f3c922c lua wrapper
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
28 v.append(vv);
7dec4f3c922c lua wrapper
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
29 o = Json::objectValue;
7dec4f3c922c lua wrapper
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
30 o["x"] = 10;
7dec4f3c922c lua wrapper
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
31 o["y"] = 20;
7dec4f3c922c lua wrapper
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
32 o["z"] = 20.5f;
7dec4f3c922c lua wrapper
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
33 v.append(o);
7dec4f3c922c lua wrapper
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
34
7dec4f3c922c lua wrapper
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
35 {
7dec4f3c922c lua wrapper
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
36 Orthanc::LuaFunctionCall f(lua, "rPrint");
7dec4f3c922c lua wrapper
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
37 f.PushJSON(v);
7dec4f3c922c lua wrapper
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
38 f.Execute();
7dec4f3c922c lua wrapper
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
39 }
7dec4f3c922c lua wrapper
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
40
7dec4f3c922c lua wrapper
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
41 {
7dec4f3c922c lua wrapper
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
42 Orthanc::LuaFunctionCall f(lua, "f");
7dec4f3c922c lua wrapper
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
43 f.PushJSON(o);
7dec4f3c922c lua wrapper
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
44 ASSERT_THROW(f.ExecutePredicate(), Orthanc::LuaException);
7dec4f3c922c lua wrapper
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
45 }
7dec4f3c922c lua wrapper
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
46
7dec4f3c922c lua wrapper
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
47 o["bool"] = false;
7dec4f3c922c lua wrapper
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
48
7dec4f3c922c lua wrapper
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
49 {
7dec4f3c922c lua wrapper
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
50 Orthanc::LuaFunctionCall f(lua, "f");
7dec4f3c922c lua wrapper
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
51 f.PushJSON(o);
7dec4f3c922c lua wrapper
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
52 ASSERT_FALSE(f.ExecutePredicate());
7dec4f3c922c lua wrapper
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
53 }
7dec4f3c922c lua wrapper
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
54
7dec4f3c922c lua wrapper
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
55 o["bool"] = true;
7dec4f3c922c lua wrapper
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
56
7dec4f3c922c lua wrapper
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
57 {
7dec4f3c922c lua wrapper
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
58 Orthanc::LuaFunctionCall f(lua, "f");
7dec4f3c922c lua wrapper
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
59 f.PushJSON(o);
7dec4f3c922c lua wrapper
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
60 ASSERT_TRUE(f.ExecutePredicate());
7dec4f3c922c lua wrapper
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
61 }
7dec4f3c922c lua wrapper
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
62
7dec4f3c922c lua wrapper
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
63 }
7dec4f3c922c lua wrapper
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
64 catch (Orthanc::LuaException e)
7dec4f3c922c lua wrapper
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
65 {
7dec4f3c922c lua wrapper
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
66 std::cerr << "EXCEPTION: [" << e.What() << "]" << std::endl;
7dec4f3c922c lua wrapper
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
67 }
7dec4f3c922c lua wrapper
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
68 }