Skip to content

Commit 7163128

Browse files
committed
Regroup completion tests
1 parent 2e12fac commit 7163128

File tree

1 file changed

+117
-113
lines changed

1 file changed

+117
-113
lines changed

test/irb/test_completion.rb

Lines changed: 117 additions & 113 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,123 @@ def test_complete_class
7777
end
7878
end
7979

80+
class TestRequireComepletion < TestCompletion
81+
def test_complete_require
82+
candidates = IRB::InputCompletor::CompletionProc.("'irb", "require ", "")
83+
%w['irb/init 'irb/ruby-lex].each do |word|
84+
assert_include candidates, word
85+
end
86+
# Test cache
87+
candidates = IRB::InputCompletor::CompletionProc.("'irb", "require ", "")
88+
%w['irb/init 'irb/ruby-lex].each do |word|
89+
assert_include candidates, word
90+
end
91+
end
92+
93+
def test_complete_require_with_pathname_in_load_path
94+
temp_dir = Dir.mktmpdir
95+
File.write(File.join(temp_dir, "foo.rb"), "test")
96+
test_path = Pathname.new(temp_dir)
97+
$LOAD_PATH << test_path
98+
99+
candidates = IRB::InputCompletor::CompletionProc.("'foo", "require ", "")
100+
assert_include candidates, "'foo"
101+
ensure
102+
$LOAD_PATH.pop if test_path
103+
FileUtils.remove_entry(temp_dir) if temp_dir
104+
end
105+
106+
def test_complete_require_with_string_convertable_in_load_path
107+
temp_dir = Dir.mktmpdir
108+
File.write(File.join(temp_dir, "foo.rb"), "test")
109+
object = Object.new
110+
object.define_singleton_method(:to_s) { temp_dir }
111+
$LOAD_PATH << object
112+
113+
candidates = IRB::InputCompletor::CompletionProc.("'foo", "require ", "")
114+
assert_include candidates, "'foo"
115+
ensure
116+
$LOAD_PATH.pop if object
117+
FileUtils.remove_entry(temp_dir) if temp_dir
118+
end
119+
120+
def test_complete_require_with_malformed_object_in_load_path
121+
object = Object.new
122+
def object.to_s; raise; end
123+
$LOAD_PATH << object
124+
125+
assert_nothing_raised do
126+
IRB::InputCompletor::CompletionProc.("'foo", "require ", "")
127+
end
128+
ensure
129+
$LOAD_PATH.pop if object
130+
end
131+
132+
def test_complete_require_library_name_first
133+
pend 'Need to use virtual library paths'
134+
candidates = IRB::InputCompletor::CompletionProc.("'csv", "require ", "")
135+
assert_equal "'csv", candidates.first
136+
end
137+
138+
def test_complete_require_relative
139+
candidates = Dir.chdir(__dir__ + "/../..") do
140+
IRB::InputCompletor::CompletionProc.("'lib/irb", "require_relative ", "")
141+
end
142+
%w['lib/irb/init 'lib/irb/ruby-lex].each do |word|
143+
assert_include candidates, word
144+
end
145+
# Test cache
146+
candidates = Dir.chdir(__dir__ + "/../..") do
147+
IRB::InputCompletor::CompletionProc.("'lib/irb", "require_relative ", "")
148+
end
149+
%w['lib/irb/init 'lib/irb/ruby-lex].each do |word|
150+
assert_include candidates, word
151+
end
152+
end
153+
end
154+
155+
class TestVariableCompletion < TestCompletion
156+
def test_complete_variable
157+
# Bug fix issues https://github.com/ruby/irb/issues/368
158+
# Variables other than `str_example` and `@str_example` are defined to ensure that irb completion does not cause unintended behavior
159+
str_example = ''
160+
@str_example = ''
161+
private_methods = ''
162+
methods = ''
163+
global_variables = ''
164+
local_variables = ''
165+
instance_variables = ''
166+
167+
# suppress "assigned but unused variable" warning
168+
str_example.clear
169+
@str_example.clear
170+
private_methods.clear
171+
methods.clear
172+
global_variables.clear
173+
local_variables.clear
174+
instance_variables.clear
175+
176+
assert_include(IRB::InputCompletor.retrieve_completion_data("str_examp", bind: binding), "str_example")
177+
assert_equal(IRB::InputCompletor.retrieve_completion_data("str_example", bind: binding, doc_namespace: true), "String")
178+
assert_equal(IRB::InputCompletor.retrieve_completion_data("str_example.to_s", bind: binding, doc_namespace: true), "String.to_s")
179+
180+
assert_include(IRB::InputCompletor.retrieve_completion_data("@str_examp", bind: binding), "@str_example")
181+
assert_equal(IRB::InputCompletor.retrieve_completion_data("@str_example", bind: binding, doc_namespace: true), "String")
182+
assert_equal(IRB::InputCompletor.retrieve_completion_data("@str_example.to_s", bind: binding, doc_namespace: true), "String.to_s")
183+
end
184+
185+
def test_complete_sort_variables
186+
xzy, xzy_1, xzy2 = '', '', ''
187+
188+
xzy.clear
189+
xzy_1.clear
190+
xzy2.clear
191+
192+
candidates = IRB::InputCompletor.retrieve_completion_data("xz", bind: binding, doc_namespace: false)
193+
assert_equal(candidates, %w[xzy xzy2 xzy_1])
194+
end
195+
end
196+
80197
def test_complete_symbol
81198
%w"UTF-16LE UTF-7".each do |enc|
82199
"K".force_encoding(enc).to_sym
@@ -115,108 +232,6 @@ def test_complete_reserved_words
115232
end
116233
end
117234

118-
def test_complete_require
119-
candidates = IRB::InputCompletor::CompletionProc.("'irb", "require ", "")
120-
%w['irb/init 'irb/ruby-lex].each do |word|
121-
assert_include candidates, word
122-
end
123-
# Test cache
124-
candidates = IRB::InputCompletor::CompletionProc.("'irb", "require ", "")
125-
%w['irb/init 'irb/ruby-lex].each do |word|
126-
assert_include candidates, word
127-
end
128-
end
129-
130-
def test_complete_require_with_pathname_in_load_path
131-
temp_dir = Dir.mktmpdir
132-
File.write(File.join(temp_dir, "foo.rb"), "test")
133-
test_path = Pathname.new(temp_dir)
134-
$LOAD_PATH << test_path
135-
136-
candidates = IRB::InputCompletor::CompletionProc.("'foo", "require ", "")
137-
assert_include candidates, "'foo"
138-
ensure
139-
$LOAD_PATH.pop if test_path
140-
FileUtils.remove_entry(temp_dir) if temp_dir
141-
end
142-
143-
def test_complete_require_with_string_convertable_in_load_path
144-
temp_dir = Dir.mktmpdir
145-
File.write(File.join(temp_dir, "foo.rb"), "test")
146-
object = Object.new
147-
object.define_singleton_method(:to_s) { temp_dir }
148-
$LOAD_PATH << object
149-
150-
candidates = IRB::InputCompletor::CompletionProc.("'foo", "require ", "")
151-
assert_include candidates, "'foo"
152-
ensure
153-
$LOAD_PATH.pop if object
154-
FileUtils.remove_entry(temp_dir) if temp_dir
155-
end
156-
157-
def test_complete_require_with_malformed_object_in_load_path
158-
object = Object.new
159-
def object.to_s; raise; end
160-
$LOAD_PATH << object
161-
162-
assert_nothing_raised do
163-
IRB::InputCompletor::CompletionProc.("'foo", "require ", "")
164-
end
165-
ensure
166-
$LOAD_PATH.pop if object
167-
end
168-
169-
def test_complete_require_library_name_first
170-
pend 'Need to use virtual library paths'
171-
candidates = IRB::InputCompletor::CompletionProc.("'csv", "require ", "")
172-
assert_equal "'csv", candidates.first
173-
end
174-
175-
def test_complete_require_relative
176-
candidates = Dir.chdir(__dir__ + "/../..") do
177-
IRB::InputCompletor::CompletionProc.("'lib/irb", "require_relative ", "")
178-
end
179-
%w['lib/irb/init 'lib/irb/ruby-lex].each do |word|
180-
assert_include candidates, word
181-
end
182-
# Test cache
183-
candidates = Dir.chdir(__dir__ + "/../..") do
184-
IRB::InputCompletor::CompletionProc.("'lib/irb", "require_relative ", "")
185-
end
186-
%w['lib/irb/init 'lib/irb/ruby-lex].each do |word|
187-
assert_include candidates, word
188-
end
189-
end
190-
191-
def test_complete_variable
192-
# Bug fix issues https://github.com/ruby/irb/issues/368
193-
# Variables other than `str_example` and `@str_example` are defined to ensure that irb completion does not cause unintended behavior
194-
str_example = ''
195-
@str_example = ''
196-
private_methods = ''
197-
methods = ''
198-
global_variables = ''
199-
local_variables = ''
200-
instance_variables = ''
201-
202-
# suppress "assigned but unused variable" warning
203-
str_example.clear
204-
@str_example.clear
205-
private_methods.clear
206-
methods.clear
207-
global_variables.clear
208-
local_variables.clear
209-
instance_variables.clear
210-
211-
assert_include(IRB::InputCompletor.retrieve_completion_data("str_examp", bind: binding), "str_example")
212-
assert_equal(IRB::InputCompletor.retrieve_completion_data("str_example", bind: binding, doc_namespace: true), "String")
213-
assert_equal(IRB::InputCompletor.retrieve_completion_data("str_example.to_s", bind: binding, doc_namespace: true), "String.to_s")
214-
215-
assert_include(IRB::InputCompletor.retrieve_completion_data("@str_examp", bind: binding), "@str_example")
216-
assert_equal(IRB::InputCompletor.retrieve_completion_data("@str_example", bind: binding, doc_namespace: true), "String")
217-
assert_equal(IRB::InputCompletor.retrieve_completion_data("@str_example.to_s", bind: binding, doc_namespace: true), "String.to_s")
218-
end
219-
220235
def test_complete_methods
221236
obj = Object.new
222237
obj.singleton_class.class_eval {
@@ -240,16 +255,5 @@ def instance_variables; end
240255
assert_include(IRB::InputCompletor.retrieve_completion_data("private_hoge.to_s", bind: bind), "private_hoge.to_s")
241256
assert_include(IRB::InputCompletor.retrieve_completion_data("private_hoge", bind: bind, doc_namespace: true), "private_hoge")
242257
end
243-
244-
def test_complete_sort_variables
245-
xzy, xzy_1, xzy2 = '', '', ''
246-
247-
xzy.clear
248-
xzy_1.clear
249-
xzy2.clear
250-
251-
candidates = IRB::InputCompletor.retrieve_completion_data("xz", bind: binding, doc_namespace: false)
252-
assert_equal(candidates, %w[xzy xzy2 xzy_1])
253-
end
254258
end
255259
end

0 commit comments

Comments
 (0)