-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathLuaBridge.cpp
More file actions
146 lines (108 loc) · 2.96 KB
/
Copy pathLuaBridge.cpp
File metadata and controls
146 lines (108 loc) · 2.96 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
#include <cmath>
#include <string>
#include <gtest/gtest.h>
extern "C"
{
#include <lua/lauxlib.h>
#include <lua/lua.h>
#include <lua/lualib.h>
}
#include <LuaBridge/LuaBridge.h>
class LuaBridgeTest : public ::testing::Test
{
protected:
lua_State* L = nullptr;
void SetUp() override
{
// Create a new Lua state
L = luaL_newstate();
ASSERT_NE(L, nullptr) << "Failed to create Lua state";
// Open standard libraries
luaL_openlibs(L);
}
void TearDown() override
{
if (L)
{
lua_close(L);
L = nullptr;
}
}
};
namespace
{
int add(int a, int b)
{
return a + b;
}
struct Point
{
Point(double x_, double y_) : x(x_), y(y_) { }
double length() const { return std::sqrt(x * x + y * y); }
double x = 0.0;
double y = 0.0;
};
std::string greet(const std::string& name)
{
return std::string("Hello, ") + name;
}
} // namespace
TEST_F(LuaBridgeTest, CanCallBoundFunction)
{
using namespace luabridge;
getGlobalNamespace(L).addFunction("add", &add);
const char* script = "return add(2, 3)";
int status = luaL_loadstring(L, script);
ASSERT_EQ(status, LUA_OK) << "Failed to load Lua script";
status = lua_pcall(L, 0, 1, 0);
ASSERT_EQ(status, LUA_OK) << "Failed to execute Lua script";
ASSERT_TRUE(lua_isnumber(L, -1));
int result = static_cast<int>(lua_tointeger(L, -1));
EXPECT_EQ(result, 5);
lua_pop(L, 1);
}
TEST_F(LuaBridgeTest, CanUseBoundClass)
{
using namespace luabridge;
getGlobalNamespace(L)
.beginClass<Point>("Point")
.addConstructor<void (*)(double, double)>()
.addData("x", &Point::x)
.addData("y", &Point::y)
.addFunction("length", &Point::length)
.endClass();
const char* script = "p = Point(3.0, 4.0)\n"
"return p:length(), p.x, p.y";
int status = luaL_loadstring(L, script);
ASSERT_EQ(status, LUA_OK) << "Failed to load Lua script";
status = lua_pcall(L, 0, 3, 0);
ASSERT_EQ(status, LUA_OK) << "Failed to execute Lua script";
ASSERT_TRUE(lua_isnumber(L, -3));
ASSERT_TRUE(lua_isnumber(L, -2));
ASSERT_TRUE(lua_isnumber(L, -1));
double len = lua_tonumber(L, -3);
double x = lua_tonumber(L, -2);
double y = lua_tonumber(L, -1);
EXPECT_DOUBLE_EQ(len, 5.0);
EXPECT_DOUBLE_EQ(x, 3.0);
EXPECT_DOUBLE_EQ(y, 4.0);
lua_pop(L, 3);
}
TEST_F(LuaBridgeTest, CanUseNamespaceAndStdString)
{
using namespace luabridge;
getGlobalNamespace(L)
.beginNamespace("test")
.addFunction("greet", &greet)
.endNamespace();
const char* script = "return test.greet('LuaBridge')";
int status = luaL_loadstring(L, script);
ASSERT_EQ(status, LUA_OK) << "Failed to load Lua script";
status = lua_pcall(L, 0, 1, 0);
ASSERT_EQ(status, LUA_OK) << "Failed to execute Lua script";
luabridge::LuaRef result = luabridge::LuaRef::fromStack(L, -1);
ASSERT_TRUE(result.isString());
std::string str = result.cast<std::string>();
EXPECT_EQ(str, std::string("Hello, LuaBridge"));
lua_pop(L, 1);
}