FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
lua/luaApp/test/luaShellTest.cpp at master · epics-modules/lua · GitHub
Uh oh!
There was an error while loading.
Please reload this page
.
epics-modules
/
lua
Public
Notifications
You must be signed in to change notification settings
Fork
7
Star
7
Code
Issues
1
Pull requests
0
Actions
Projects
Wiki
Security and quality
0
Insights
Additional navigation options
Code
Issues
Pull requests
Actions
Projects
Wiki
Security and quality
Insights
Expand file tree
Breadcrumbs
lua
/
luaApp
/
test
/
luaShellTest.cpp
Copy path
More file actions
More file actions
Latest commit
History
History
History
241 lines (183 loc) · 6.72 KB
Breadcrumbs
lua
/
luaApp
/
test
/
luaShellTest.cpp
Copy path
File metadata and controls
241 lines (183 loc) · 6.72 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
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
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
/*
* Tests for the Lua shell integration
*
* Exercises luaCmd (single command execution),
* luaCreateState / luaNamedState API, and
* the iocsh command bridge.
*/
#
include
<
string.h
>
#
include
<
dbUnitTest.h
>
#
include
<
epicsUnitTest.h
>
#
include
<
testMain.h
>
#
include
<
dbAccess.h
>
#
include
<
errlog.h
>
#
include
<
iocsh.h
>
#
include
"
luaEpics.h
"
#
include
"
luaShell.h
"
extern
"
C
"
{
void
luaTest_registerRecordDeviceDriver
(
struct
dbBase
*);
}
static
void
testCreateState
(
void
)
{
testDiag
(
"
===== Lua shell: luaCreateState =====
"
);
lua_State* state =
luaCreateState
();
testOk
(state !=
NULL
,
"
luaCreateState returns non-NULL
"
);
if
(state)
{
/*
Verify the state is functional by running simple Lua
*/
int
status =
luaL_dostring
(state,
"
x = 1 + 1
"
);
testOk
(status ==
0
,
"
luaL_dostring succeeds in new state
"
);
lua_getglobal
(state,
"
x
"
);
int
val =
lua_tointeger
(state, -
1
);
testOk
(val ==
2
,
"
Lua arithmetic works: 1+1 = %d
"
, val);
lua_pop
(state,
1
);
lua_close
(state);
}
}
static
void
testNamedState
(
void
)
{
testDiag
(
"
===== Lua shell: luaNamedState =====
"
);
lua_State* state1 =
luaNamedState
(
"
test_shared
"
);
testOk
(state1 !=
NULL
,
"
luaNamedState returns non-NULL
"
);
lua_State* state2 =
luaNamedState
(
"
test_shared
"
);
testOk
(state1 == state2,
"
Same name returns same state pointer
"
);
lua_State* state3 =
luaNamedState
(
"
test_other
"
);
testOk
(state3 !=
NULL
,
"
Different name returns non-NULL
"
);
testOk
(state3 != state1,
"
Different name returns different state
"
);
/*
Verify data persists in named state
*/
if
(state1)
{
luaL_dostring
(state1,
"
shared_var = 99
"
);
lua_getglobal
(state2,
"
shared_var
"
);
int
val =
lua_tointeger
(state2, -
1
);
testOk
(val ==
99
,
"
Variable persists in named state: shared_var = %d
"
, val);
lua_pop
(state2,
1
);
}
}
static
void
testLuaCmd
(
void
)
{
testDiag
(
"
===== Lua shell: luaCmd =====
"
);
/*
luaCmd runs a Lua string through iocsh registration
*/
int
status =
iocshCmd
(
"
luaCmd
\"
epicsEnvSet('LUA_TEST_VAR', 'success')
\"
"
);
testOk
(status ==
0
,
"
luaCmd via iocshCmd returns 0
"
);
}
static
void
testLoadParams
(
void
)
{
testDiag
(
"
===== Lua shell: luaLoadParams =====
"
);
lua_State* state =
luaCreateState
();
testOk
(state !=
NULL
,
"
State created for param test
"
);
if
(state)
{
int
count =
luaLoadParams
(state,
"
10, 'hello', 3.14
"
);
testOk
(count ==
3
,
"
luaLoadParams returns 3 for three params, got %d
"
, count);
/*
Check types and values (they're on the stack in order)
*/
testOk
(
lua_isnumber
(state, -
3
),
"
First param is a number
"
);
testOk
(
lua_isstring
(state, -
2
),
"
Second param is a string
"
);
testOk
(
lua_isnumber
(state, -
1
),
"
Third param is a number
"
);
double
p1 =
lua_tonumber
(state, -
3
);
const
char
* p2 =
lua_tostring
(state, -
2
);
double
p3 =
lua_tonumber
(state, -
1
);
testOk
(p1 ==
10.0
,
"
First param value: %g
"
, p1);
testOk
(p2 &&
strcmp
(p2,
"
hello
"
) ==
0
,
"
Second param value: '%s'
"
, p2 ? p2 :
"
(null)
"
);
testOk
(p3 >
3.13
&& p3 <
3.15
,
"
Third param value: %g
"
, p3);
lua_pop
(state, count);
lua_close
(state);
}
}
static
void
testNullNamedState
(
void
)
{
testDiag
(
"
===== Lua shell: luaNamedState(NULL) =====
"
);
lua_State* state =
luaNamedState
(
NULL
);
testOk
(state ==
NULL
,
"
luaNamedState(NULL) returns NULL
"
);
}
static
void
testRegisterState
(
void
)
{
testDiag
(
"
===== Lua shell: luaRegisterState =====
"
);
/*
Use luaNamedState to create a persistent state (it won't be
* closed during test shutdown since named states persist)
*/
lua_State* state =
luaNamedState
(
"
test_reg
"
);
testOk
(state !=
NULL
,
"
Named state created
"
);
/*
luaNamedState registers it, so it should be findable
*/
testOk
(
luaFindNamedState
(
"
test_reg
"
) == state,
"
luaFindNamedState returns the state
"
);
/*
luaRegisterState with a different name
*/
luaRegisterState
(state,
"
test_reg_alias
"
);
testOk
(
luaFindNamedState
(
"
test_reg_alias
"
) == state,
"
State accessible under alias name
"
);
/*
luaStateIsRegistered should find it
*/
testOk
(
luaStateIsRegistered
(state) !=
0
,
"
State is registered
"
);
/*
A fresh unregistered state should not be registered
*/
lua_State* fresh =
luaL_newstate
();
testOk
(
luaStateIsRegistered
(fresh) ==
0
,
"
Fresh state is not registered
"
);
lua_close
(fresh);
}
static
void
testFindNamedStateNotFound
(
void
)
{
testDiag
(
"
===== Lua shell: luaFindNamedState not found =====
"
);
testOk
(
luaFindNamedState
(
"
nonexistent_state
"
) ==
NULL
,
"
luaFindNamedState returns NULL for unknown name
"
);
}
static
void
testLuaCmdTableMacros
(
void
)
{
testDiag
(
"
===== Lua shell: luaMacrosFromTable =====
"
);
/*
Use a bare Lua state (not luaCreateState) to avoid
* any EPICS library interactions during shutdown
*/
lua_State* state =
luaL_newstate
();
testOk
(state !=
NULL
,
"
State created for macro test
"
);
if
(state)
{
lua_newtable
(state);
lua_pushstring
(state,
"
dev1:
"
);
lua_setfield
(state, -
2
,
"
P
"
);
lua_pushstring
(state,
"
sensor
"
);
lua_setfield
(state, -
2
,
"
R
"
);
std::string macros =
luaMacrosFromTable
(state,
lua_gettop
(state));
lua_pop
(state,
1
);
/*
Order of keys in Lua tables is not guaranteed, check both
*/
testOk
(macros.
find
(
"
P=dev1:
"
) != std::string::npos,
"
macros contains P=dev1: : '%s'
"
, macros.
c_str
());
testOk
(macros.
find
(
"
R=sensor
"
) != std::string::npos,
"
macros contains R=sensor : '%s'
"
, macros.
c_str
());
lua_close
(state);
}
}
/*
--- info() function tests ---
*/
static
void
testInfoNoArgs
(
void
)
{
testDiag
(
"
===== Lua shell: info() no args =====
"
);
lua_State* state =
luaCreateState
();
testOk
(state !=
NULL
,
"
State created for info test
"
);
int
status =
luaL_dostring
(state,
"
info()
"
);
testOk
(status ==
0
,
"
info() with no args succeeds
"
);
lua_close
(state);
}
static
void
testInfoNilInput
(
void
)
{
testDiag
(
"
===== Lua shell: info(nil) =====
"
);
lua_State* state =
luaCreateState
();
int
status =
luaL_dostring
(state,
"
info(nil)
"
);
testOk
(status ==
0
,
"
info(nil) succeeds
"
);
lua_close
(state);
}
MAIN
(luaShellTest)
{
testPlan
(
0
);
testdbPrepare
();
testdbReadDatabase
(
"
luaTest.dbd
"
,
NULL
,
NULL
);
luaTest_registerRecordDeviceDriver
(pdbbase);
eltc
(
0
);
testIocInitOk
();
eltc
(
1
);
testCreateState
();
testNamedState
();
testLuaCmd
();
testLoadParams
();
testNullNamedState
();
testRegisterState
();
testFindNamedStateNotFound
();
testLuaCmdTableMacros
();
/*
info() function
*/
testInfoNoArgs
();
testInfoNilInput
();
testIocShutdownOk
();
testdbCleanup
();
return
testDone
();
}
Back
|
FazBrowse Home
|
New Git URL