rb-readline-r7 0.5.2.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,53 @@
1
+ require "fileutils"
2
+
3
+ module FilesystemCompletionHelper
4
+ SEP = File::SEPARATOR
5
+ COMP_TEST_DIR = "comp_test#{SEP}"
6
+ SUB_DIR = "#{COMP_TEST_DIR}a_sub_dir#{SEP}"
7
+ SUB_SUB_DIR = "#{SUB_DIR}another_sub_dir#{SEP}"
8
+ DIR_WITH_SPACES = "#{COMP_TEST_DIR}dir with spaces#{SEP}"
9
+ SUB_DIR_WITH_SPACES = "#{DIR_WITH_SPACES}sub dir with spaces#{SEP}"
10
+
11
+ # This creates:
12
+ #
13
+ # comp_test/
14
+ # abc
15
+ # aaa
16
+ # a_sub_dir/
17
+ # abc
18
+ # aaa
19
+ # another_sub_dir/
20
+ # aaa
21
+ # dir with spaces/
22
+ # filename with spaces
23
+ # sub dir with spaces/
24
+ # another filename with spaces
25
+ def setup_filesystem_for_completion
26
+ FileUtils.mkdir_p("#{SUB_SUB_DIR}")
27
+ FileUtils.mkdir_p("#{SUB_DIR_WITH_SPACES}")
28
+ @comp_test_dir = Dir.new COMP_TEST_DIR
29
+ @sub_dir = Dir.new SUB_DIR
30
+ @sub_sub_dir = Dir.new SUB_SUB_DIR
31
+ @dir_with_spaces = Dir.new DIR_WITH_SPACES
32
+ @sub_dir_with_spaces = Dir.new SUB_DIR_WITH_SPACES
33
+
34
+ FileUtils.touch("#{@comp_test_dir.path}abc")
35
+ FileUtils.touch("#{@comp_test_dir.path}aaa")
36
+ FileUtils.touch("#{@sub_dir.path}abc")
37
+ FileUtils.touch("#{@sub_dir.path}aaa")
38
+ FileUtils.touch("#{@sub_sub_dir.path}aaa")
39
+ FileUtils.touch("#{@dir_with_spaces.path}filename with spaces")
40
+ FileUtils.touch("#{@sub_dir_with_spaces.path}another filename with spaces")
41
+
42
+ # The previous Dir.new calls seem to cache the dir entries on Windows.
43
+ @comp_test_dir = Dir.new COMP_TEST_DIR
44
+ @sub_dir = Dir.new SUB_DIR
45
+ @sub_sub_dir = Dir.new SUB_SUB_DIR
46
+ @sub_dir_with_spaces = Dir.new SUB_DIR_WITH_SPACES
47
+ @dir_with_spaces = Dir.new DIR_WITH_SPACES
48
+ end
49
+
50
+ def teardown_filesystem_after_completion
51
+ FileUtils.rm_r(COMP_TEST_DIR)
52
+ end
53
+ end
@@ -0,0 +1,100 @@
1
+ require "minitest/autorun"
2
+ require "readline"
3
+
4
+ require 'timeout'
5
+ require "support/filesystem_completion_helper"
6
+
7
+ class TestCompletion < Minitest::Test
8
+ include RbReadline
9
+ include FilesystemCompletionHelper
10
+
11
+ def filename_quoting_function(filename, mtype, quote_char)
12
+ quoted_filename = filename.dup
13
+ @rl_filename_quote_characters.split("").each do |c|
14
+ quoted_filename.gsub!(c, "\\#{c}")
15
+ end
16
+ quoted_filename
17
+ end
18
+
19
+ def filename_dequoting_function(filename, quote_char = "\\")
20
+ filename.delete quote_char
21
+ end
22
+
23
+ def setup
24
+ @rl_completion_word_break_hook, @rl_char_is_quoted_p = nil
25
+ @rl_basic_quote_characters, @rl_special_prefixes = nil
26
+ @rl_completer_word_break_characters = Readline.basic_word_break_characters
27
+ @rl_completer_quote_characters = "\\"
28
+ @rl_completion_quote_character = "\\"
29
+ @rl_filename_quote_characters = " "
30
+ @rl_byte_oriented = true
31
+ @rl_filename_quoting_desired = true
32
+ @rl_filename_completion_desired = true
33
+ @rl_complete_with_tilde_expansion = true
34
+ @_rl_match_hidden_files = false
35
+ @rl_completion_found_quote = false
36
+ @_rl_completion_case_fold = false
37
+ @directory = nil
38
+
39
+ @rl_filename_quoting_function = :filename_quoting_function
40
+ @rl_filename_dequoting_function = :filename_dequoting_function
41
+ @rl_directory_completion_hook = nil
42
+
43
+ setup_filesystem_for_completion
44
+ end
45
+
46
+ def teardown
47
+ teardown_filesystem_after_completion
48
+ end
49
+
50
+ def set_line_buffer(text)
51
+ @rl_line_buffer = text
52
+ @rl_point = @rl_line_buffer.size
53
+ @rl_line_buffer << 0.chr
54
+ end
55
+
56
+ def test__find_completion_word_doesnt_hang_on_completer_quote_character
57
+ set_line_buffer "#{@dir_with_spaces.path}filename\\ w"
58
+
59
+ Timeout::timeout(3) do
60
+ assert_equal([ "\000", true, "\000" ], _rl_find_completion_word)
61
+ end
62
+ end
63
+
64
+ def test__find_completion_word_without_quote_characters
65
+ set_line_buffer "#{@comp_test_dir.path}a"
66
+ assert_equal([ "\000", false, "\000" ], _rl_find_completion_word)
67
+ end
68
+
69
+ def test_make_quoted_replacement_calls_filename_quoting_function
70
+ assert_equal "dir/with\\ space", make_quoted_replacement("dir/with space", RbReadline::SINGLE_MATCH, 0.chr)
71
+ end
72
+
73
+ def test_rl_filname_completion_function_calls_dequoting_function
74
+ @rl_completion_found_quote = true
75
+ dir = filename_quoting_function(@dir_with_spaces.path, nil, 0.chr)
76
+
77
+ # rl_filename_completion_function is called with an increasing state in
78
+ # order to iterate through directory entries.
79
+
80
+ entries = [ "#{@dir_with_spaces.path}sub dir with spaces", "#{@dir_with_spaces.path}filename with spaces" ]
81
+
82
+ assert entries.include?(rl_filename_completion_function(dir, 0))
83
+ assert entries.include?(rl_filename_completion_function(dir, 1))
84
+ assert_nil rl_filename_completion_function(dir, 2)
85
+ ensure
86
+ @rl_completion_found_quote = false
87
+ end
88
+
89
+ def test_completing_path_starting_dot_slash
90
+ assert_equal "./#{COMP_TEST_DIR.chop}", rl_filename_completion_function("./#{COMP_TEST_DIR.chop}", 0)
91
+ end
92
+
93
+ def test_completing_non_existant_directory
94
+ assert_nil rl_filename_completion_function("/this/dir/does/not/exist", 0)
95
+ end
96
+
97
+ def test_completing_a_file_as_a_directory
98
+ assert_nil rl_filename_completion_function("#{File.expand_path(__FILE__)}/", 0)
99
+ end
100
+ end
@@ -0,0 +1,88 @@
1
+ require 'minitest/autorun'
2
+ require 'readline'
3
+
4
+ require 'fileutils'
5
+ require "rbconfig"
6
+ require "support/filesystem_completion_helper"
7
+
8
+ class TestFilenameCompletionProc < Minitest::Test
9
+ include FilesystemCompletionHelper
10
+
11
+ def setup
12
+ FileUtils.mkdir_p("#{SUB_SUB_DIR}")
13
+ FileUtils.mkdir_p("#{SUB_DIR_WITH_SPACES}")
14
+ @comp_test_dir = Dir.new COMP_TEST_DIR
15
+ @sub_dir = Dir.new SUB_DIR
16
+ @sub_sub_dir = Dir.new SUB_SUB_DIR
17
+ @dir_with_spaces = Dir.new DIR_WITH_SPACES
18
+ @sub_dir_with_spaces = Dir.new SUB_DIR_WITH_SPACES
19
+
20
+ FileUtils.touch("#{@comp_test_dir.path}abc")
21
+ FileUtils.touch("#{@comp_test_dir.path}aaa")
22
+ FileUtils.touch("#{@sub_dir.path}abc")
23
+ FileUtils.touch("#{@sub_dir.path}aaa")
24
+ FileUtils.touch("#{@sub_sub_dir.path}aaa")
25
+ FileUtils.touch("#{@dir_with_spaces.path}filename with spaces")
26
+ FileUtils.touch("#{@sub_dir_with_spaces.path}another filename with spaces")
27
+
28
+ # The previous Dir.new calls seem to cache the dir entries on Windows.
29
+ @comp_test_dir = Dir.new COMP_TEST_DIR
30
+ @sub_dir = Dir.new SUB_DIR
31
+ @sub_sub_dir = Dir.new SUB_SUB_DIR
32
+ @dir_with_spaces = Dir.new DIR_WITH_SPACES
33
+ @sub_dir_with_spaces = Dir.new SUB_DIR_WITH_SPACES
34
+ setup_filesystem_for_completion
35
+ end
36
+
37
+ def teardown
38
+ teardown_filesystem_after_completion
39
+ end
40
+
41
+ def test_listing_files_in_cwd
42
+ Dir.chdir(COMP_TEST_DIR) do
43
+ entries = Dir.entries(".").select { |e| e[0,1] == "a" }
44
+ assert_equal entries, Readline::FILENAME_COMPLETION_PROC.call("a")
45
+ end
46
+ end
47
+
48
+ def test_list_files_in_sub_directories
49
+ entries = @sub_dir.entries.select { |e| e[0,1] == "a" }
50
+ entries.map! { |e| "#{@sub_dir.path}#{e}" }
51
+ assert_equal entries, Readline::FILENAME_COMPLETION_PROC.call("#{@sub_dir.path}a")
52
+
53
+ entries = @sub_sub_dir.entries - %w( . .. )
54
+ entries.map! { |e| "#{@sub_sub_dir.path}#{e}" }
55
+ assert_equal entries, Readline::FILENAME_COMPLETION_PROC.call("#{@sub_sub_dir.path}")
56
+ end
57
+
58
+ def test_list_files_and_directories_with_spaces
59
+ entries = @comp_test_dir.entries.select { |e| e[0,1] == "d" }
60
+ entries.map! { |e| @comp_test_dir.path + e }
61
+ assert_equal entries, Readline::FILENAME_COMPLETION_PROC.call("#{@comp_test_dir.path}d")
62
+
63
+ entries = @dir_with_spaces.entries - %w( . .. )
64
+ entries.map! { |e| @dir_with_spaces.path + e }
65
+ assert_equal entries, Readline::FILENAME_COMPLETION_PROC.call("#{@dir_with_spaces.path}")
66
+ end
67
+
68
+ def test_list_files_in_current_directory
69
+ assert_equal((Dir.entries(".") - %w( . .. )).sort, Readline::FILENAME_COMPLETION_PROC.call("").sort)
70
+ end
71
+
72
+ def test_listing_files_with_no_read_access
73
+ FileUtils.mkdir("test_no_access")
74
+ FileUtils.touch("test_no_access/123")
75
+
76
+ skip "chmod is noop in Windows" if windows?
77
+
78
+ FileUtils.chmod(0333, "test_no_access")
79
+ assert_nil Readline::FILENAME_COMPLETION_PROC.call("test_no_access/")
80
+ ensure
81
+ FileUtils.chmod(0775, "test_no_access")
82
+ FileUtils.rm_r("test_no_access")
83
+ end
84
+
85
+ def windows?
86
+ RbConfig::CONFIG["host_os"] =~ /mingw|mswin/
87
+ end
88
+ end
@@ -0,0 +1,29 @@
1
+ require "minitest/autorun"
2
+ require "readline"
3
+
4
+ class TestHistory < Minitest::Test
5
+
6
+ # RbReadline::HISTORY_WORD_DELIMITERS.inspect
7
+ # => " \t\n;&()|<>"
8
+ # RbReadline::HISTORY_QUOTE_CHARACTERS = "\"'`"
9
+ # => "\"'`"
10
+ def test_history_arg_extract
11
+ assert_raises(RuntimeError) { RbReadline.history_arg_extract("!", "$", "one two three") }
12
+ assert_raises(RuntimeError) { RbReadline.history_arg_extract("$", "!", "one two three") }
13
+
14
+ assert_equal "one", RbReadline.history_arg_extract("$", "$", "one")
15
+ assert_equal "three", RbReadline.history_arg_extract("$", "$", "one two three")
16
+ assert_equal "two\\ three", RbReadline.history_arg_extract("$", "$", "one two\\ three")
17
+ assert_equal "three", RbReadline.history_arg_extract("$", "$", "one two;three")
18
+ assert_equal "two\\;three", RbReadline.history_arg_extract("$", "$", "one two\\;three")
19
+
20
+ assert_equal "'two three'", RbReadline.history_arg_extract("$", "$", "one 'two three'")
21
+ assert_equal "`two three`", RbReadline.history_arg_extract("$", "$", "one `two three`")
22
+ assert_equal "three\\'", RbReadline.history_arg_extract("$", "$", "one \\'two three\\'")
23
+ assert_equal "`one`", RbReadline.history_arg_extract("$", "$", "`one`")
24
+
25
+ assert_equal "three'", RbReadline.history_arg_extract("$", "$", "one two three'")
26
+ assert_equal "three", RbReadline.history_arg_extract("$", "$", "one two' three")
27
+ assert_equal "'two three '", RbReadline.history_arg_extract("$", "$", "one 'two three '")
28
+ end
29
+ end
@@ -0,0 +1,23 @@
1
+ require 'minitest/autorun'
2
+ require 'rbreadline'
3
+
4
+ class TestRbReadline < Minitest::Test
5
+ def test_versions
6
+ assert_equal('5.2', RbReadline::RL_LIBRARY_VERSION)
7
+ assert_equal(0x0502, RbReadline::RL_READLINE_VERSION)
8
+ end
9
+
10
+ def test_rl_adjust_point
11
+ encoding_name = RbReadline.instance_variable_get(:@encoding_name)
12
+ RbReadline.instance_variable_set(:@encoding_name, Encoding.find('UTF-8'))
13
+
14
+ assert_equal(0, RbReadline._rl_adjust_point("a".force_encoding('ASCII-8BIT'), 0))
15
+ assert_equal(0, RbReadline._rl_adjust_point("a".force_encoding('ASCII-8BIT'), 1))
16
+ assert_equal(0, RbReadline._rl_adjust_point(("a" * 40).force_encoding('ASCII-8BIT'), 0))
17
+ assert_equal(0, RbReadline._rl_adjust_point(("a" * 40).force_encoding('ASCII-8BIT'), 40))
18
+ assert_equal(2, RbReadline._rl_adjust_point(("\u3042" * 10).force_encoding('ASCII-8BIT'), 1))
19
+ assert_equal(1, RbReadline._rl_adjust_point(("\u3042" * 15).force_encoding('ASCII-8BIT'), 38))
20
+ ensure
21
+ RbReadline.instance_variable_set(:@encoding_name, encoding_name)
22
+ end if defined?(Encoding)
23
+ end
@@ -0,0 +1,221 @@
1
+ require 'minitest/autorun'
2
+ require 'readline'
3
+
4
+ class TestReadline < Minitest::Test
5
+ def setup
6
+ @proc = proc{ |s| ['alpha', 'beta'].grep( /^#{Regexp.escape(s)}/) }
7
+ end
8
+
9
+ def test_version
10
+ assert_equal('5.2', Readline::VERSION)
11
+ end
12
+
13
+ def test_readline_basic
14
+ assert_respond_to(Readline, :readline)
15
+ end
16
+
17
+ def test_readline_with_default_parameters_does_not_error
18
+ thread = Thread.new { Readline.readline }
19
+ sleep 0.1
20
+ assert thread.alive?
21
+ ensure
22
+ thread.kill
23
+ end
24
+
25
+ def test_input_basic
26
+ assert_respond_to(Readline, :input=)
27
+ end
28
+
29
+ def test_input
30
+ Readline.input = $stdin
31
+ assert_equal $stdin, RbReadline.rl_instream
32
+ end
33
+
34
+ def test_output_basic
35
+ assert_respond_to(Readline, :output=)
36
+ end
37
+
38
+ def test_output
39
+ Readline.output = $stdout
40
+ assert_equal $stdout, RbReadline.rl_outstream
41
+ end
42
+
43
+ def test_completion_proc_get_basic
44
+ assert_respond_to(Readline, :completion_proc)
45
+ end
46
+
47
+ def test_completion_proc_set_basic
48
+ assert_respond_to(Readline, :completion_proc=)
49
+ end
50
+
51
+ def test_completion_proc
52
+ Readline.completion_proc = @proc
53
+ assert_equal @proc, Readline.completion_proc
54
+ end
55
+
56
+ def test_completion_case_fold_get_basic
57
+ assert_respond_to(Readline, :completion_case_fold)
58
+ end
59
+
60
+ def test_completion_case_fold_default
61
+ assert_equal(false, Readline.completion_case_fold) # default
62
+ end
63
+
64
+ def test_completion_case_fold_set_basic
65
+ assert_respond_to(Readline, :completion_case_fold=)
66
+ end
67
+
68
+ def test_completion_case_fold_changed
69
+ Readline.completion_case_fold = false
70
+ refute Readline.completion_case_fold
71
+ end
72
+
73
+ def test_completion_proc_expected_errors
74
+ assert_raises(ArgumentError) { Readline.completion_proc = 1 }
75
+ assert_raises(ArgumentError) { Readline.completion_proc = 'a' }
76
+ end
77
+
78
+ def test_vi_editing_mode_basic
79
+ assert_respond_to(Readline, :vi_editing_mode)
80
+ end
81
+
82
+ def test_emacs_editing_mode_basic
83
+ assert_respond_to(Readline, :emacs_editing_mode)
84
+ end
85
+
86
+ def test_completion_append_character_get_basic
87
+ assert_respond_to(Readline, :completion_append_character)
88
+ end
89
+
90
+ def test_completion_append_character_get
91
+ assert_equal(' ', Readline.completion_append_character) # default
92
+ end
93
+
94
+ def test_completion_append_character_set_basic
95
+ assert_respond_to(Readline, :completion_append_character=)
96
+ end
97
+
98
+ def test_completion_append_character_set
99
+ assert_equal " ", Readline.completion_append_character
100
+ end
101
+
102
+ def test_completion_append_character
103
+ orig_char = Readline.completion_append_character
104
+ begin
105
+ [
106
+ [ "x", "x" ],
107
+ [ "xyx", "x" ],
108
+ [ " ", " " ],
109
+ [ "\t", "\t" ],
110
+ [ "", nil ],
111
+ ].each do |data, expected|
112
+ Readline.completion_append_character = data
113
+ assert_equal(expected, Readline.completion_append_character,
114
+ "failed case: [#{data.inspect}, #{expected.inspect}]")
115
+ end
116
+ ensure
117
+ Readline.completion_append_character = orig_char
118
+ end
119
+ end
120
+
121
+ def test_basic_word_break_characters_get_basic
122
+ assert_respond_to(Readline, :basic_word_break_characters)
123
+ end
124
+
125
+ def test_basic_word_break_characters_get
126
+ assert_equal(" \t\n\"\\'`@$><=|&{(", Readline.basic_word_break_characters)
127
+ end
128
+
129
+ def test_basic_word_break_characters_set_basic
130
+ assert_respond_to(Readline, :basic_word_break_characters=)
131
+ end
132
+
133
+ def test_basic_word_break_characters_set
134
+ chars = " \t\n\"\\'`@$><=|&{("
135
+ Readline.basic_word_break_characters = chars
136
+ assert_equal chars, Readline.basic_word_break_characters
137
+ end
138
+
139
+ def test_basic_quote_characters_get_basic
140
+ assert_respond_to(Readline, :basic_quote_characters)
141
+ end
142
+
143
+ def test_basic_quote_characters_get
144
+ assert_equal "\"'", Readline.basic_quote_characters
145
+ end
146
+
147
+ def test_basic_quote_characters_set_basic
148
+ assert_respond_to(Readline, :basic_quote_characters=)
149
+ end
150
+
151
+ def test_basic_quote_characters_set
152
+ chars = "\"'"
153
+ Readline.basic_quote_characters = chars
154
+ assert_equal chars, Readline.basic_quote_characters
155
+ end
156
+
157
+ def test_some_character_methods
158
+ expecteds = [ " ", " .,|\t", "" ]
159
+ [
160
+ :basic_word_break_characters,
161
+ :completer_word_break_characters,
162
+ :basic_quote_characters,
163
+ :completer_quote_characters,
164
+ :filename_quote_characters,
165
+ ].each do |method|
166
+ begin
167
+ saved = Readline.send(method)
168
+ expecteds.each do |e|
169
+ Readline.send("#{method}=".to_sym, e)
170
+ assert_equal(e, Readline.send(method),
171
+ "failed case #{e.inspect} for method #{method}")
172
+ end
173
+ ensure
174
+ Readline.send("#{method}=".to_sym, saved) if saved
175
+ end
176
+ end
177
+ end
178
+
179
+ def test_attempted_comp_func_returns_nil_when_no_completion_proc_set
180
+ assert_equal nil, Readline.readline_attempted_completion_function("12", 0, 1)
181
+ end
182
+
183
+ def test_attempted_comp_func_case_folding
184
+ Readline.completion_proc = Proc.new do |word|
185
+ %w( 123456 123abc abc123 ).grep(/^#{word}/i)
186
+ end
187
+
188
+ Readline.completion_case_fold = true
189
+
190
+ assert_equal [ "123", "123456", "123abc", nil ], Readline.readline_attempted_completion_function("123", 0, 3)
191
+
192
+ assert_equal [ "123abc", nil, nil ], Readline.readline_attempted_completion_function("123A", 0, 3)
193
+
194
+ ensure
195
+ Readline.completion_case_fold = false
196
+ Readline.module_eval do
197
+ @completion_proc = nil
198
+ end
199
+ end
200
+
201
+ def test_attempted_comp_func_removes_replacement_from_possible_matches
202
+ Readline.completion_proc = Proc.new do |word|
203
+ %w( 123456 123abc abc123 ).grep(/^#{word}/)
204
+ end
205
+
206
+ assert_equal [ "123", "123456", "123abc", nil ], Readline.readline_attempted_completion_function("12", 0, 1)
207
+
208
+ assert_equal [ "123", "123456", "123abc", nil ], Readline.readline_attempted_completion_function("123", 0, 2)
209
+
210
+ assert_equal [ "123456", nil, nil ], Readline.readline_attempted_completion_function("1234", 0, 3)
211
+
212
+ ensure
213
+ Readline.module_eval do
214
+ @completion_proc = nil
215
+ end
216
+ end
217
+
218
+ def teardown
219
+ @proc = nil
220
+ end
221
+ end