-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathpcall_test.cpp
More file actions
117 lines (101 loc) · 3.08 KB
/
Copy pathpcall_test.cpp
File metadata and controls
117 lines (101 loc) · 3.08 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
#include "../lich/push_to_primitive.h"
#include "../lich/push_to_string.h"
#include "../lich/program.h"
#include "../lich/ref.h"
#include "must.h"
#include <algorithm>
using namespace std;
void pcall_test()
{
lua_State* L = luaL_newstate();
luaL_openlibs(L);
lich::enable_ref(L);
// 여러 인자, 여러 리턴
{
tuple<lich::ref> rv0;
auto result = lich::run_program(L,
"return function(a, b, c) return c, b, a end", "", rv0);
MUST_EQUAL(result, make_pair(true, string()));
tuple<string, string, string> rv1;
result = lich::pcall(get<0>(rv0), make_tuple("빗발친다", "정의가", "하늘에서"), rv1);
MUST_EQUAL(result, make_pair(true, string()));
MUST_EQUAL(get<0>(rv1), "하늘에서");
MUST_EQUAL(get<1>(rv1), "정의가");
MUST_EQUAL(get<2>(rv1), "빗발친다");
}
MUST_EQUAL(lua_gettop(L), 0);
// 글로벌 함수 실행
{
tuple<string> rv;
MUST_TRUE(lich::pcall(L, "type", make_tuple(1), rv).first);
MUST_EQUAL(get<0>(rv), "number");
// 원소 하나짜리 투플로 실행하면 그냥 글로벌 함수로 취급
MUST_TRUE(lich::pcall(L, make_tuple("type"), make_tuple(1), rv).first);
MUST_EQUAL(get<0>(rv), "number");
}
// 글로벌 함수 실행: 에러 처리
{
tuple<string> rv;
auto result = lich::pcall(L, "XXX", tuple<>(), rv);
MUST_TRUE(result.second.find("attempt to call a nil value") != string::npos);
}
// 모듈 함수 실행
{
// string.rep("아", 5)
tuple<string> rv;
MUST_TRUE(lich::pcall(L, make_tuple("string", "rep"), make_tuple("아", 5), rv).first);
MUST_EQUAL(get<0>(rv), "아아아아아");
// A.B.C("아", 5)
lich::run_program(L, "A = { B = { C = string.rep } }", "", rv);
MUST_TRUE(lich::pcall(L, make_tuple("A", "B", "C"), make_tuple("아", 6), rv).first);
MUST_EQUAL(get<0>(rv), "아아아아아아");
}
// 모듈 함수 실행: 에러 처리
{
// a.b.c.d()
tuple<> rv;
auto result = lich::pcall(L, make_tuple("a", "b", "c", "d"), tuple<>(), rv);
MUST_EQUAL(result.first, false);
MUST_TRUE(result.second.find("attempt to call a nil value") != string::npos);
// string.XXX.YYY()
lich::pcall(L, make_tuple("string", "XXX", "YYY"), tuple<>(), rv);
MUST_EQUAL(result.first, false);
MUST_TRUE(result.second.find("attempt to call a nil value") != string::npos);
}
// 메서드 실행
{
// s = "justive rains from above"
lich::push(L, "justive rains from above");
lich::ref s;
lich::to(L, -1, s);
lua_pop(L, 1);
// s:upper() == s.upper(s)
tuple<string> rv;
MUST_TRUE(lich::pcall(L, tie(s, "upper"), tie(s), rv).first);
MUST_EQUAL(get<0>(rv), "JUSTIVE RAINS FROM ABOVE");
}
// 스택 트레이스
{
tuple<lich::ref> fn;
lich::run_program(
L,
" local function R(n)"
"\n if n == 0 then"
"\n return 'a' + 'b'"
"\n else"
"\n return R(n - 1) .. '*'"
"\n end"
"\n end"
"\n return R",
"",
fn);
tuple<> rv;
auto callResult = lich::pcall(get<0>(fn), make_tuple(5), rv);
auto newlineCount = count_if(
callResult.second.begin(),
callResult.second.end(),
[](char c) { return c == '\n'; });
MUST_EQUAL(newlineCount, 7);
}
lua_close(L);
}