File: test_rl.lua

package info (click to toggle)
lua-readline 3.3-3
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 124 kB
  • sloc: ansic: 251; makefile: 4
file content (263 lines) | stat: -rw-r--r-- 8,470 bytes parent folder | download | duplicates (3)
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
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
#! /usr/local/bin/lua
local RL = require 'readline'
local poll = require 'posix.poll'.poll
-- local TC = require 'testcases'
-- luarocks install http://pjb.com.au/comp/lua/testcases-0.1-0.rockspec

--------------------------- infrastructure -----------------------
local eps = .000000001
function equal(x, y)  -- unused here
    if #x ~= #y then return false end
    local i; for i in pairs(x) do
        if math.abs(x[i]-y[i]) > eps then return false end
    end
    return true
end
-- use Test::Simple tests => 6;
local Test = 73 ; local i_test = 0; local Failed = 0;
function ok(b,s)
    i_test = i_test + 1
    if b then
        io.write('ok '..i_test..' - '..s.."\n")
		return true
    else
        io.write('not ok '..i_test..' - '..s.."\n")
        Failed = Failed + 1
		return false
    end
end

local function qw(s)  -- t = qw([[ foo  bar  baz ]])
    local t = {}
	for x in s:gmatch("([-%s]+)") do t[#t+1] = x end
	return t
end

local function uname_minus_s()
    local pipe = assert(io.popen('uname -s'))
    local uname_output = pipe:read('*all')
    pipe:close()
    return string.gsub(uname_output, '%s$', '')
end

-- strict.lua    checks uses of undeclared global variables
-- All global variables must be 'declared' through a regular assignment
-- (even assigning nil will do) in a main chunk before being used
-- anywhere or assigned to inside a function.
local mt = getmetatable(_G)
if mt == nil then
  mt = {}
  setmetatable(_G, mt)
end
mt.__declared = {}
mt.__newindex = function (t, n, v)
  if not mt.__declared[n] then
    local w = debug.getinfo(2, "S").what
    if w ~= "main" and w ~= "C" then
      error("assign to undeclared variable '"..n.."'", 2)
    end
    mt.__declared[n] = true
  end
  rawset(t, n, v)
end
mt.__index = function (t, n)
  if not mt.__declared[n] and debug.getinfo(2, "S").what ~= "C" then
    error("variable '"..n.."' is not declared", 2)
  end
  return rawget(t, n)
end

----------------------- here we go... -------------------------

print('Testing readline.lua '..RL.Version..', '..RL.VersionDate..
 ' on '..uname_minus_s())

if not ok(type(RL) == 'table', 'type of RL is table') then
	print('type was '..type(RL))
end

RL.set_completion_append_character('')
RL.set_completion_append_character(' ')
RL.set_completion_append_character('X')

-- for k,v in pairs(RL) do print(k,tostring(v)) end
local filename = '/tmp/test_rl_history'
os.remove(filename)
RL.set_options{histfile=filename , ignoredups=false}

print('About to test the Alternative Interface ...')
local s0 = nil
RL.handler_install("Tab-completion should work: ", function(s)
	s0 = s
	RL.handler_remove()
end)
local fds = {[0] = {events={IN={true}}}}
while true do
    poll(fds, -1)
    if fds[0].revents.IN then -- read only if there's something to be read
        RL.read_char()
    else
        -- do some useful background task
    end
    if s0 then break end   -- don't add to the history this time...
end

print('About to test the standard interface ...')
print('Please make all answers longer than two characters !')
local s1 = RL.readline('Please enter something: ')
if not ok(type(s1)=='string', "readline returned "..s1) then
	print('xc='..tostring(xc)..' xv='..tostring(xv))
end
local s2 = RL.readline('this time Up-arrow should work: ')
local s3 = RL.readline('enter a filename and test Tab-completion: ')
local save = RL.set_options{completion=false}
local s4 = RL.readline('now Tab-completion should be disabled: ')
RL.set_options(save)
local s5 = RL.readline('now it should be re-enabled :-) ')
RL.set_options{auto_add=false}
local s6 = RL.readline('this answer should not get added into the history: ')
RL.set_options(save)
local s7 = RL.readline('now it should be re-enabled :-) ')

print('About to test the Alternative Interface again ...')
line = nil
local linehandler = function(s)
    RL.handler_remove()
    RL.add_history(s)
    line = s
end
RL.handler_install("Please enter something: ", linehandler)
fds = {[0] = {events={IN={true}}}}
while true do
    poll(fds, -1)
    if fds[0].revents.IN then -- read only if there's something to be read
        RL.read_char()
    else
        -- do some useful background task
    end
    if line then s8 = line ; break end
end

local reserved_words = {
  'and', 'assert', 'break', 'do', 'else', 'elseif', 'end',
  'false', 'for', 'function', 'if', 'ipairs(',
  'io.flush(', 'io.input(', 'io.lines(', 'io.open(', 'io.output(',
  'io.popen(', 'io.read(', 'io.seek(', 'io.setvbuf(', 'io.stderr',
  'io.stdin', 'io.stdout', 'io.tmpfile(', 'io.write(',
  'local',
  'math.asin(', 'math.ceil(', 'math.cos(', 'math.deg(', 'math.floor(',
  'math.huge(', 'math.max(', 'math.maxinteger(', 'math.mod(', 'math.rad(',
  'math.random(', 'math.randomseed(', 'math.sin(', 'math.tan(',
  'math.tointeger(', 'math.type(', 'math.ult(',
  'nil', 'not', 'print', 'require', 'return',
  'os.clock(', 'os.date(', 'os.difftime(', 'os.execute(', 'os.exit(',
  'os.getenv(', 'os.remove(', 'os.rename(', 'os.time(',
  'string.byte(', 'string.char(', 'string.dump(', 'string.find(',
  'string.format(', 'string.gmatch(', 'string.gsub(', 'string.len(',
  'string.lower(', 'string.match(', 'string.pack(', 'string.rep(',
  'string.sub(', 'string.unpack(', 'string.unpack(', 'string.upper(',
  'table.concat(', 'table.insert(', 'table.move(', 'table.pack(',
  'table.remove(', 'table.sort(', 'table.unpack(',
  'then', 'tostring(', 'tonumber(', 'true', 'type', 'while', 'pairs(', 'print',
}
RL.set_complete_list(reserved_words)
local s9 = RL.readline('now it expands lua reserved words: ')

line = nil
RL.handler_install("the same but with the Alternate Interface: ", linehandler)
-- fds = {[0] = {events={IN={true}}}}
-- RL.set_complete_list(reserved_words)
while true do
    poll(fds, -1)
    if fds[0].revents.IN then -- read only if there's something to be read
        RL.read_char()
    else
        -- do some useful background task
    end
    if line then sA = line ; break end
end
-- print (sA)

-- print(type(TC.empty_the_stack))
--[[
local comp_func = function ()
	TC.empty_the_stack()
	return {'gloop'}
end
RL.set_complete_function(comp_func)
local sB = RL.readline("now the complete_function empties the stack: ")
local reg = debug.getregistry()
for k,v in pairs(reg) do print(k,v) end

comp_func = function ()
	TC.dump_the_stack('dummy-string', nil, 42)
	return {'gleep'}
end
RL.set_complete_function(comp_func)
local sC = RL.readline("now the complete_function dumps the stack: ")
]]

comp_func_2 = function ()
	RL.set_completion_append_character('')
	return {'glurp'}
end
RL.set_complete_function(comp_func_2)
local sD = RL.readline("now the completion_append_character is \\0: ")

comp_func_3 = function ()
	return {'glork'}
end
RL.set_complete_function(comp_func_3)
local sD = RL.readline("the completion_append_character is ' ' again: ")

RL.save_history()

print('Now checking the saved histfile:')
local F = assert(io.open(filename))
local lines = {}
for line in F:lines() do lines[#lines+1] = line end
F:close()
-- os.remove(filename)
if not ok(lines[1] == s1, 'line 1 was '..s1) then
	print('lines[1]='..tostring(lines[1])..' s1='..tostring(s1))
end
if not ok(lines[2] == s2, 'line 2 was '..s2) then
	print('lines[2]='..tostring(lines[2])..' s2='..tostring(s2))
end
if not ok(lines[3] == s3, 'line 3 was '..s3) then
	print('lines[3]='..tostring(lines[3])..' s3='..tostring(s3))
end
if not ok(lines[4] == s4, 'line 4 was '..s4) then
	print('lines[4]='..tostring(lines[4])..' s4='..tostring(s4))
end
if not ok(lines[5] == s5, 'line 5 was '..s5) then
	print('lines[5]='..tostring(lines[5])..' s5='..tostring(s5))
end
if not ok(lines[6] == s7, 'line 6 was '..s7) then
	print('lines[6]='..tostring(lines[6])..' s7='..tostring(s7))
end
if not ok(lines[7] == s8, 'line 7 was '..s8) then
	print('lines[7]='..tostring(lines[7])..' s8='..tostring(s8))
end
if not ok(lines[8] == s9, 'line 8 was '..s9) then
	print('lines[8]="'..tostring(lines[8])..'" s9="'..tostring(s9)..'"')
end
if not ok(lines[9] == sA, 'line 9 was '..sA) then
	print('lines[9]="'..tostring(lines[9])..'" sA="'..tostring(sA)..'"')
end
--[[
if not ok(lines[10] == sB, 'line 10 was '..sB) then
	print('lines[10]="'..tostring(lines[10])..'" sB="'..tostring(sB)..'"')
end
if not ok(lines[11] == sC, 'line 11 was '..sC) then
	print('lines[11]="'..tostring(lines[11])..'" sC="'..tostring(sC)..'"')
end
]]
if Failed == 0 then
	print('Passed all '..i_test..' tests :-)')
else
	print('Failed '..Failed..' tests out of '..i_test)
end


os.exit()