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
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
|
require_relative 'helper'
class Reline::Config::Test < Reline::TestCase
def setup
@pwd = Dir.pwd
@tmpdir = File.join(Dir.tmpdir, "test_reline_config_#{$$}")
begin
Dir.mkdir(@tmpdir)
rescue Errno::EEXIST
FileUtils.rm_rf(@tmpdir)
Dir.mkdir(@tmpdir)
end
Dir.chdir(@tmpdir)
Reline.test_mode
@config = Reline::Config.new
@inputrc_backup = ENV['INPUTRC']
end
def teardown
Dir.chdir(@pwd)
FileUtils.rm_rf(@tmpdir)
Reline.test_reset
@config.reset
ENV['INPUTRC'] = @inputrc_backup
end
def additional_key_bindings(keymap_label)
@config.instance_variable_get(:@additional_key_bindings)[keymap_label].instance_variable_get(:@key_bindings)
end
def registered_key_bindings(keys)
key_bindings = @config.key_bindings
keys.to_h { |key| [key, key_bindings.get(key)] }
end
def test_read_lines
@config.read_lines(<<~LINES.lines)
set show-mode-in-prompt on
LINES
assert_equal true, @config.instance_variable_get(:@show_mode_in_prompt)
end
def test_read_lines_with_variable
@config.read_lines(<<~LINES.lines)
set disable-completion on
LINES
assert_equal true, @config.instance_variable_get(:@disable_completion)
end
def test_string_value
@config.read_lines(<<~LINES.lines)
set show-mode-in-prompt on
set emacs-mode-string Emacs
LINES
assert_equal 'Emacs', @config.instance_variable_get(:@emacs_mode_string)
end
def test_string_value_with_brackets
@config.read_lines(<<~LINES.lines)
set show-mode-in-prompt on
set emacs-mode-string [Emacs]
LINES
assert_equal '[Emacs]', @config.instance_variable_get(:@emacs_mode_string)
end
def test_string_value_with_brackets_and_quotes
@config.read_lines(<<~LINES.lines)
set show-mode-in-prompt on
set emacs-mode-string "[Emacs]"
LINES
assert_equal '[Emacs]', @config.instance_variable_get(:@emacs_mode_string)
end
def test_string_value_with_parens
@config.read_lines(<<~LINES.lines)
set show-mode-in-prompt on
set emacs-mode-string (Emacs)
LINES
assert_equal '(Emacs)', @config.instance_variable_get(:@emacs_mode_string)
end
def test_string_value_with_parens_and_quotes
@config.read_lines(<<~LINES.lines)
set show-mode-in-prompt on
set emacs-mode-string "(Emacs)"
LINES
assert_equal '(Emacs)', @config.instance_variable_get(:@emacs_mode_string)
end
def test_encoding_is_ascii
@config.reset
Reline.core.io_gate.instance_variable_set(:@encoding, Encoding::US_ASCII)
@config = Reline::Config.new
assert_equal true, @config.convert_meta
end
def test_encoding_is_not_ascii
@config = Reline::Config.new
assert_equal nil, @config.convert_meta
end
def test_invalid_keystroke
@config.read_lines(<<~LINES.lines)
#"a": comment
a: error
"b": no-error
LINES
key_bindings = additional_key_bindings(:emacs)
assert_not_include key_bindings, 'a'.bytes
assert_not_include key_bindings, nil
assert_not_include key_bindings, []
assert_include key_bindings, 'b'.bytes
end
def test_bind_key
assert_equal ['input'.bytes, 'abcde'.bytes], @config.bind_key('"input"', '"abcde"')
end
def test_bind_key_with_macro
assert_equal ['input'.bytes, :abcde], @config.bind_key('"input"', 'abcde')
end
def test_bind_key_with_escaped_chars
assert_equal ['input'.bytes, "\e \\ \" ' \a \b \d \f \n \r \t \v".bytes], @config.bind_key('"input"', '"\\e \\\\ \\" \\\' \\a \\b \\d \\f \\n \\r \\t \\v"')
end
def test_bind_key_with_ctrl_chars
assert_equal ['input'.bytes, "\C-h\C-h".bytes], @config.bind_key('"input"', '"\C-h\C-H"')
assert_equal ['input'.bytes, "\C-h\C-h".bytes], @config.bind_key('"input"', '"\Control-h\Control-H"')
end
def test_bind_key_with_meta_chars
assert_equal ['input'.bytes, "\M-h\M-H".bytes], @config.bind_key('"input"', '"\M-h\M-H"')
assert_equal ['input'.bytes, "\M-h\M-H".bytes], @config.bind_key('"input"', '"\Meta-h\Meta-H"')
end
def test_bind_key_with_octal_number
input = %w{i n p u t}.map(&:ord)
assert_equal [input, "\1".bytes], @config.bind_key('"input"', '"\1"')
assert_equal [input, "\12".bytes], @config.bind_key('"input"', '"\12"')
assert_equal [input, "\123".bytes], @config.bind_key('"input"', '"\123"')
assert_equal [input, "\123".bytes + '4'.bytes], @config.bind_key('"input"', '"\1234"')
end
def test_bind_key_with_hexadecimal_number
input = %w{i n p u t}.map(&:ord)
assert_equal [input, "\x4".bytes], @config.bind_key('"input"', '"\x4"')
assert_equal [input, "\x45".bytes], @config.bind_key('"input"', '"\x45"')
assert_equal [input, "\x45".bytes + '6'.bytes], @config.bind_key('"input"', '"\x456"')
end
def test_include
File.open('included_partial', 'wt') do |f|
f.write(<<~PARTIAL_LINES)
set show-mode-in-prompt on
PARTIAL_LINES
end
@config.read_lines(<<~LINES.lines)
$include included_partial
LINES
assert_equal true, @config.instance_variable_get(:@show_mode_in_prompt)
end
def test_include_expand_path
home_backup = ENV['HOME']
File.open('included_partial', 'wt') do |f|
f.write(<<~PARTIAL_LINES)
set show-mode-in-prompt on
PARTIAL_LINES
end
ENV['HOME'] = Dir.pwd
@config.read_lines(<<~LINES.lines)
$include ~/included_partial
LINES
assert_equal true, @config.instance_variable_get(:@show_mode_in_prompt)
ensure
ENV['HOME'] = home_backup
end
def test_if
@config.read_lines(<<~LINES.lines)
$if Ruby
set vi-cmd-mode-string (cmd)
$else
set vi-cmd-mode-string [cmd]
$endif
LINES
assert_equal '(cmd)', @config.instance_variable_get(:@vi_cmd_mode_string)
end
def test_if_with_false
@config.read_lines(<<~LINES.lines)
$if Python
set vi-cmd-mode-string (cmd)
$else
set vi-cmd-mode-string [cmd]
$endif
LINES
assert_equal '[cmd]', @config.instance_variable_get(:@vi_cmd_mode_string)
end
def test_if_with_indent
%w[Ruby Reline].each do |cond|
@config.read_lines(<<~LINES.lines)
set vi-cmd-mode-string {cmd}
$if #{cond}
set vi-cmd-mode-string (cmd)
$else
set vi-cmd-mode-string [cmd]
$endif
LINES
assert_equal '(cmd)', @config.instance_variable_get(:@vi_cmd_mode_string)
end
end
def test_nested_if_else
@config.read_lines(<<~LINES.lines)
$if Ruby
"\x1": "O"
$if NotRuby
"\x2": "X"
$else
"\x3": "O"
$if Ruby
"\x4": "O"
$else
"\x5": "X"
$endif
"\x6": "O"
$endif
"\x7": "O"
$else
"\x8": "X"
$if NotRuby
"\x9": "X"
$else
"\xA": "X"
$endif
"\xB": "X"
$endif
"\xC": "O"
LINES
keys = [0x1, 0x3, 0x4, 0x6, 0x7, 0xC]
key_bindings = keys.to_h { |k| [[k], ['O'.ord]] }
assert_equal(key_bindings, additional_key_bindings(:emacs))
end
def test_unclosed_if
e = assert_raise(Reline::Config::InvalidInputrc) do
@config.read_lines(<<~LINES.lines, "INPUTRC")
$if Ruby
LINES
end
assert_equal "INPUTRC:1: unclosed if", e.message
end
def test_unmatched_else
e = assert_raise(Reline::Config::InvalidInputrc) do
@config.read_lines(<<~LINES.lines, "INPUTRC")
$else
LINES
end
assert_equal "INPUTRC:1: unmatched else", e.message
end
def test_unmatched_endif
e = assert_raise(Reline::Config::InvalidInputrc) do
@config.read_lines(<<~LINES.lines, "INPUTRC")
$endif
LINES
end
assert_equal "INPUTRC:1: unmatched endif", e.message
end
def test_if_with_mode
@config.read_lines(<<~LINES.lines)
$if mode=emacs
"\C-e": history-search-backward # comment
$else
"\C-f": history-search-forward
$endif
LINES
assert_equal({[5] => :history_search_backward}, additional_key_bindings(:emacs))
assert_equal({}, additional_key_bindings(:vi_insert))
assert_equal({}, additional_key_bindings(:vi_command))
end
def test_else
@config.read_lines(<<~LINES.lines)
$if mode=vi
"\C-e": history-search-backward # comment
$else
"\C-f": history-search-forward
$endif
LINES
assert_equal({[6] => :history_search_forward}, additional_key_bindings(:emacs))
assert_equal({}, additional_key_bindings(:vi_insert))
assert_equal({}, additional_key_bindings(:vi_command))
end
def test_if_with_invalid_mode
@config.read_lines(<<~LINES.lines)
$if mode=vim
"\C-e": history-search-backward
$else
"\C-f": history-search-forward # comment
$endif
LINES
assert_equal({[6] => :history_search_forward}, additional_key_bindings(:emacs))
assert_equal({}, additional_key_bindings(:vi_insert))
assert_equal({}, additional_key_bindings(:vi_command))
end
def test_mode_label_differs_from_keymap_label
@config.read_lines(<<~LINES.lines)
# Sets mode_label and keymap_label to vi
set editing-mode vi
# Change keymap_label to emacs. mode_label is still vi.
set keymap emacs
# condition=true because current mode_label is vi
$if mode=vi
# sets keybinding to current keymap_label=emacs
"\C-e": history-search-backward
$endif
LINES
assert_equal({[5] => :history_search_backward}, additional_key_bindings(:emacs))
assert_equal({}, additional_key_bindings(:vi_insert))
assert_equal({}, additional_key_bindings(:vi_command))
end
def test_if_without_else_condition
@config.read_lines(<<~LINES.lines)
set editing-mode vi
$if mode=vi
"\C-e": history-search-backward
$endif
LINES
assert_equal({}, additional_key_bindings(:emacs))
assert_equal({[5] => :history_search_backward}, additional_key_bindings(:vi_insert))
assert_equal({}, additional_key_bindings(:vi_command))
end
def test_default_key_bindings
@config.add_default_key_binding('abcd'.bytes, 'EFGH'.bytes)
@config.read_lines(<<~'LINES'.lines)
"abcd": "ABCD"
"ijkl": "IJKL"
LINES
expected = { 'abcd'.bytes => 'ABCD'.bytes, 'ijkl'.bytes => 'IJKL'.bytes }
assert_equal expected, registered_key_bindings(expected.keys)
end
def test_additional_key_bindings
@config.read_lines(<<~'LINES'.lines)
"ef": "EF"
"gh": "GH"
LINES
expected = { 'ef'.bytes => 'EF'.bytes, 'gh'.bytes => 'GH'.bytes }
assert_equal expected, registered_key_bindings(expected.keys)
end
def test_additional_key_bindings_with_nesting_and_comment_out
@config.read_lines(<<~'LINES'.lines)
#"ab": "AB"
#"cd": "cd"
"ef": "EF"
"gh": "GH"
LINES
expected = { 'ef'.bytes => 'EF'.bytes, 'gh'.bytes => 'GH'.bytes }
assert_equal expected, registered_key_bindings(expected.keys)
end
def test_additional_key_bindings_for_other_keymap
@config.read_lines(<<~'LINES'.lines)
set keymap vi-command
"ab": "AB"
set keymap vi-insert
"cd": "CD"
set keymap emacs
"ef": "EF"
set editing-mode vi # keymap changes to be vi-insert
LINES
expected = { 'cd'.bytes => 'CD'.bytes }
assert_equal expected, registered_key_bindings(expected.keys)
end
def test_additional_key_bindings_for_auxiliary_emacs_keymaps
@config.read_lines(<<~'LINES'.lines)
set keymap emacs
"ab": "AB"
set keymap emacs-standard
"cd": "CD"
set keymap emacs-ctlx
"ef": "EF"
set keymap emacs-meta
"gh": "GH"
set editing-mode emacs # keymap changes to be emacs
LINES
expected = {
'ab'.bytes => 'AB'.bytes,
'cd'.bytes => 'CD'.bytes,
"\C-xef".bytes => 'EF'.bytes,
"\egh".bytes => 'GH'.bytes,
}
assert_equal expected, registered_key_bindings(expected.keys)
end
def test_key_bindings_with_reset
# @config.reset is called after each readline.
# inputrc file is read once, so key binding shouldn't be cleared by @config.reset
@config.add_default_key_binding('default'.bytes, 'DEFAULT'.bytes)
@config.read_lines(<<~'LINES'.lines)
"additional": "ADDITIONAL"
LINES
@config.reset
expected = { 'default'.bytes => 'DEFAULT'.bytes, 'additional'.bytes => 'ADDITIONAL'.bytes }
assert_equal expected, registered_key_bindings(expected.keys)
end
def test_history_size
@config.read_lines(<<~LINES.lines)
set history-size 5000
LINES
assert_equal 5000, @config.instance_variable_get(:@history_size)
history = Reline::History.new(@config)
history << "a\n"
assert_equal 1, history.size
end
def test_empty_inputrc_env
inputrc_backup = ENV['INPUTRC']
ENV['INPUTRC'] = ''
assert_nothing_raised do
@config.read
end
ensure
ENV['INPUTRC'] = inputrc_backup
end
def test_inputrc
inputrc_backup = ENV['INPUTRC']
expected = "#{@tmpdir}/abcde"
ENV['INPUTRC'] = expected
assert_equal expected, @config.inputrc_path
ensure
ENV['INPUTRC'] = inputrc_backup
end
def test_inputrc_raw_value
@config.read_lines(<<~'LINES'.lines)
set editing-mode vi ignored-string
set vi-ins-mode-string aaa aaa
set vi-cmd-mode-string bbb ccc # comment
LINES
assert_equal :vi_insert, @config.instance_variable_get(:@editing_mode_label)
assert_equal 'aaa aaa', @config.vi_ins_mode_string
assert_equal 'bbb ccc # comment', @config.vi_cmd_mode_string
end
def test_inputrc_with_utf8
# This file is encoded by UTF-8 so this heredoc string is also UTF-8.
@config.read_lines(<<~'LINES'.lines)
set editing-mode vi
set vi-cmd-mode-string 🍸
set vi-ins-mode-string 🍶
LINES
assert_equal '🍸', @config.vi_cmd_mode_string
assert_equal '🍶', @config.vi_ins_mode_string
rescue Reline::ConfigEncodingConversionError
# do nothing
end
def test_inputrc_with_eucjp
@config.read_lines(<<~"LINES".encode(Encoding::EUC_JP).lines)
set editing-mode vi
set vi-cmd-mode-string ォャッ
set vi-ins-mode-string 能
LINES
assert_equal 'ォャッ'.encode(Reline.encoding_system_needs), @config.vi_cmd_mode_string
assert_equal '能'.encode(Reline.encoding_system_needs), @config.vi_ins_mode_string
rescue Reline::ConfigEncodingConversionError
# do nothing
end
def test_empty_inputrc
assert_nothing_raised do
@config.read_lines([])
end
end
def test_xdg_config_home
home_backup = ENV['HOME']
xdg_config_home_backup = ENV['XDG_CONFIG_HOME']
xdg_config_home = File.expand_path("#{@tmpdir}/.config/example_dir")
expected = File.expand_path("#{xdg_config_home}/readline/inputrc")
FileUtils.mkdir_p(File.dirname(expected))
FileUtils.touch(expected)
ENV['HOME'] = @tmpdir
ENV['XDG_CONFIG_HOME'] = xdg_config_home
assert_equal expected, @config.inputrc_path
ensure
FileUtils.rm(expected)
ENV['XDG_CONFIG_HOME'] = xdg_config_home_backup
ENV['HOME'] = home_backup
end
def test_empty_xdg_config_home
home_backup = ENV['HOME']
xdg_config_home_backup = ENV['XDG_CONFIG_HOME']
ENV['HOME'] = @tmpdir
ENV['XDG_CONFIG_HOME'] = ''
expected = File.expand_path('~/.config/readline/inputrc')
FileUtils.mkdir_p(File.dirname(expected))
FileUtils.touch(expected)
assert_equal expected, @config.inputrc_path
ensure
FileUtils.rm(expected)
ENV['XDG_CONFIG_HOME'] = xdg_config_home_backup
ENV['HOME'] = home_backup
end
def test_relative_xdg_config_home
home_backup = ENV['HOME']
xdg_config_home_backup = ENV['XDG_CONFIG_HOME']
ENV['HOME'] = @tmpdir
expected = File.expand_path('~/.config/readline/inputrc')
FileUtils.mkdir_p(File.dirname(expected))
FileUtils.touch(expected)
result = Dir.chdir(@tmpdir) do
xdg_config_home = ".config/example_dir"
ENV['XDG_CONFIG_HOME'] = xdg_config_home
inputrc = "#{xdg_config_home}/readline/inputrc"
FileUtils.mkdir_p(File.dirname(inputrc))
FileUtils.touch(inputrc)
@config.inputrc_path
end
assert_equal expected, result
FileUtils.rm(expected)
ENV['XDG_CONFIG_HOME'] = xdg_config_home_backup
ENV['HOME'] = home_backup
end
def test_reload
inputrc = "#{@tmpdir}/inputrc"
ENV['INPUTRC'] = inputrc
File.write(inputrc, "set emacs-mode-string !")
@config.read
assert_equal '!', @config.emacs_mode_string
File.write(inputrc, "set emacs-mode-string ?")
@config.reload
assert_equal '?', @config.emacs_mode_string
end
end
|