-
Notifications
You must be signed in to change notification settings - Fork 45
Expand file tree
/
Copy pathListTests.cpp
More file actions
174 lines (134 loc) · 4.2 KB
/
ListTests.cpp
File metadata and controls
174 lines (134 loc) · 4.2 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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
// https://github.com/kunitoki/LuaBridge3
// Copyright 2019, Dmitry Tarakanov
// SPDX-License-Identifier: MIT
#include "TestBase.h"
#include "TestTypes.h"
#include "LuaBridge/List.h"
#include <list>
namespace {
template <class T>
std::list<T> toList(const std::vector<T>& vector)
{
return {vector.begin(), vector.end()};
}
template <class T>
void checkEquals(const std::list<T>& expected, const std::list<T>& actual)
{
using U = std::decay_t<T>;
if constexpr (std::is_same_v<U, float>)
{
for (std::size_t i = 0; i < expected.size(); ++i)
ASSERT_FLOAT_EQ((*std::next(expected.begin(), i)), (*std::next(actual.begin(), i)));
}
else if constexpr (std::is_same_v<U, double> || std::is_same_v<U, long double>)
{
for (std::size_t i = 0; i < expected.size(); ++i)
ASSERT_DOUBLE_EQ((*std::next(expected.begin(), i)), (*std::next(actual.begin(), i)));
}
else if constexpr (std::is_same_v<U, const char*>)
{
for (std::size_t i = 0; i < expected.size(); ++i)
ASSERT_STREQ((*std::next(expected.begin(), i)), (*std::next(actual.begin(), i)));
}
else
{
ASSERT_EQ(expected, actual);
}
}
} // namespace
template<class T>
struct ListTest : TestBase
{
};
TYPED_TEST_SUITE_P(ListTest);
TYPED_TEST_P(ListTest, LuaRef)
{
using Traits = TypeTraits<TypeParam>;
this->runLua("result = {" + Traits::list() + "}");
std::list<TypeParam> expected = toList(Traits::values());
std::list<TypeParam> actual = this->result();
checkEquals(expected, actual);
}
REGISTER_TYPED_TEST_SUITE_P(ListTest, LuaRef);
INSTANTIATE_TYPED_TEST_SUITE_P(ListTest, ListTest, TestTypes);
struct ListTests : TestBase
{
};
TEST_F(ListTests, GetNonTable)
{
lua_pushnumber(L, 42.0);
auto result = luabridge::Stack<std::list<int>>::get(L, -1);
ASSERT_FALSE(result);
EXPECT_EQ(luabridge::ErrorCode::InvalidTypeCast, result.error());
}
TEST_F(ListTests, GetWithInvalidItem)
{
lua_createtable(L, 2, 0);
lua_pushinteger(L, 1);
lua_pushstring(L, "not_an_int");
lua_settable(L, -3);
lua_pushinteger(L, 2);
lua_pushstring(L, "also_not_an_int");
lua_settable(L, -3);
auto result = luabridge::Stack<std::list<int>>::get(L, -1);
ASSERT_FALSE(result);
EXPECT_EQ(luabridge::ErrorCode::InvalidTypeCast, result.error());
}
TEST_F(ListTests, PassToFunction)
{
runLua("function foo (list) "
" result = list "
"end");
auto foo = luabridge::getGlobal(L, "foo");
resetResult();
std::list<int> lvalue{ 10, 20, 30 };
ASSERT_TRUE(foo.call(lvalue));
ASSERT_TRUE(result().isTable());
ASSERT_EQ(lvalue, result<std::list<int>>());
resetResult();
const std::list<int> constLvalue = lvalue;
ASSERT_TRUE(foo.call(constLvalue));
ASSERT_TRUE(result().isTable());
ASSERT_EQ(lvalue, result<std::list<int>>());
}
TEST_F(ListTests, UnregisteredClass)
{
struct Unregistered {};
#if LUABRIDGE_HAS_EXCEPTIONS
[[maybe_unused]] luabridge::Result r;
ASSERT_THROW((r = luabridge::push(L, std::list<Unregistered>{ Unregistered() })), std::exception);
#else
ASSERT_FALSE((luabridge::push(L, std::list<Unregistered>{ Unregistered() })));
#endif
}
TEST_F(ListTests, IsInstance)
{
ASSERT_TRUE((luabridge::push(L, std::list<int>{ 1, 2, 3 })));
EXPECT_TRUE(luabridge::isInstance<std::list<int>>(L, -1));
lua_pop(L, 1);
ASSERT_TRUE((luabridge::push(L, 1)));
EXPECT_FALSE(luabridge::isInstance<std::list<int>>(L, -1));
}
TEST_F(ListTests, StackOverflow)
{
exhaustStackSpace();
std::list<int> value = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
ASSERT_FALSE(luabridge::push(L, value));
}
#if !LUABRIDGE_HAS_EXCEPTIONS
TEST_F(ListTests, PushUnregisteredWithNoExceptionsShouldFailButRestoreStack)
{
class Unregistered {};
const int initialStackSize = lua_gettop(L);
lua_pushnumber(L, 1);
EXPECT_EQ(1, lua_gettop(L) - initialStackSize);
std::list<Unregistered> v;
v.emplace_back();
v.emplace_back();
auto result = luabridge::Stack<decltype(v)>::push(L, v);
EXPECT_FALSE(result);
EXPECT_EQ(1, lua_gettop(L) - initialStackSize);
lua_pop(L, 1);
EXPECT_EQ(0, lua_gettop(L) - initialStackSize);
}
#endif