Skip to content

Commit 5942949

Browse files
authored
Simplify info command's tests (#440)
1 parent b7556a2 commit 5942949

File tree

1 file changed

+136
-154
lines changed

1 file changed

+136
-154
lines changed

test/irb/test_cmd.rb

Lines changed: 136 additions & 154 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
require_relative "helper"
66

77
module TestIRB
8-
class ExtendCommand < TestCase
8+
class ExtendCommandTest < TestCase
99
class TestInputMethod < ::IRB::InputMethod
1010
attr_reader :list, :line_no
1111

@@ -58,164 +58,146 @@ def teardown
5858
restore_encodings
5959
end
6060

61-
def test_irb_info_multiline
62-
FileUtils.touch("#{@tmpdir}/.inputrc")
63-
FileUtils.touch("#{@tmpdir}/.irbrc")
64-
IRB.setup(__FILE__, argv: [])
65-
IRB.conf[:USE_MULTILINE] = true
66-
IRB.conf[:USE_SINGLELINE] = false
67-
IRB.conf[:VERBOSE] = false
68-
lang_backup = ENV.delete("LANG")
69-
lc_all_backup = ENV.delete("LC_ALL")
70-
workspace = IRB::WorkSpace.new(self)
71-
irb = IRB::Irb.new(workspace, TestInputMethod.new([]))
72-
IRB.conf[:MAIN_CONTEXT] = irb.context
73-
expected = %r{
74-
Ruby\sversion:\s.+\n
75-
IRB\sversion:\sirb\s.+\n
76-
InputMethod:\sAbstract\sInputMethod\n
77-
\.irbrc\spath:\s.+\n
78-
RUBY_PLATFORM:\s.+\n
79-
East\sAsian\sAmbiguous\sWidth:\s\d\n
80-
#{@is_win ? 'Code\spage:\s\d+\n' : ''}
81-
}x
82-
assert_match expected, irb.context.main.irb_info.to_s
83-
ensure
84-
ENV["LANG"] = lang_backup
85-
ENV["LC_ALL"] = lc_all_backup
86-
end
61+
class InfoCommandTest < ExtendCommandTest
62+
def setup
63+
super
64+
@locals_backup = ENV.delete("LANG"), ENV.delete("LC_ALL")
65+
end
8766

88-
def test_irb_info_singleline
89-
FileUtils.touch("#{@tmpdir}/.inputrc")
90-
FileUtils.touch("#{@tmpdir}/.irbrc")
91-
IRB.setup(__FILE__, argv: [])
92-
IRB.conf[:USE_MULTILINE] = false
93-
IRB.conf[:USE_SINGLELINE] = true
94-
IRB.conf[:VERBOSE] = false
95-
lang_backup = ENV.delete("LANG")
96-
lc_all_backup = ENV.delete("LC_ALL")
97-
workspace = IRB::WorkSpace.new(self)
98-
irb = IRB::Irb.new(workspace, TestInputMethod.new([]))
99-
IRB.conf[:MAIN_CONTEXT] = irb.context
100-
expected = %r{
101-
Ruby\sversion:\s.+\n
102-
IRB\sversion:\sirb\s.+\n
103-
InputMethod:\sAbstract\sInputMethod\n
104-
\.irbrc\spath:\s.+\n
105-
RUBY_PLATFORM:\s.+\n
106-
East\sAsian\sAmbiguous\sWidth:\s\d\n
107-
#{@is_win ? 'Code\spage:\s\d+\n' : ''}
108-
}x
109-
info = irb.context.main.irb_info
110-
capture_output do
111-
# Reline::Core#ambiguous_width may access STDOUT, not $stdout
112-
stdout = STDOUT.dup
113-
STDOUT.reopen(IO::NULL, "w")
114-
info = info.to_s
67+
def teardown
68+
super
69+
ENV["LANG"], ENV["LC_ALL"] = @locals_backup
70+
end
71+
72+
def test_irb_info_multiline
73+
FileUtils.touch("#{@tmpdir}/.inputrc")
74+
FileUtils.touch("#{@tmpdir}/.irbrc")
75+
76+
out, err = execute_lines(
77+
"irb_info",
78+
conf: { USE_MULTILINE: true, USE_SINGLELINE: false }
79+
)
80+
81+
expected = %r{
82+
Ruby\sversion:\s.+\n
83+
IRB\sversion:\sirb\s.+\n
84+
InputMethod:\sAbstract\sInputMethod\n
85+
\.irbrc\spath:\s.+\n
86+
RUBY_PLATFORM:\s.+\n
87+
East\sAsian\sAmbiguous\sWidth:\s\d\n
88+
#{@is_win ? 'Code\spage:\s\d+\n' : ''}
89+
}x
90+
91+
assert_empty err
92+
assert_match expected, out
93+
end
94+
95+
def test_irb_info_singleline
96+
FileUtils.touch("#{@tmpdir}/.inputrc")
97+
FileUtils.touch("#{@tmpdir}/.irbrc")
98+
99+
out, err = execute_lines(
100+
"irb_info",
101+
conf: { USE_MULTILINE: false, USE_SINGLELINE: true }
102+
)
103+
104+
expected = %r{
105+
Ruby\sversion:\s.+\n
106+
IRB\sversion:\sirb\s.+\n
107+
InputMethod:\sAbstract\sInputMethod\n
108+
\.irbrc\spath:\s.+\n
109+
RUBY_PLATFORM:\s.+\n
110+
East\sAsian\sAmbiguous\sWidth:\s\d\n
111+
#{@is_win ? 'Code\spage:\s\d+\n' : ''}
112+
}x
113+
114+
assert_empty err
115+
assert_match expected, out
116+
end
117+
118+
def test_irb_info_multiline_without_rc_files
119+
inputrc_backup = ENV["INPUTRC"]
120+
ENV["INPUTRC"] = "unknown_inpurc"
121+
ext_backup = IRB::IRBRC_EXT
122+
IRB.__send__(:remove_const, :IRBRC_EXT)
123+
IRB.const_set(:IRBRC_EXT, "unknown_ext")
124+
125+
out, err = execute_lines(
126+
"irb_info",
127+
conf: { USE_MULTILINE: true, USE_SINGLELINE: false }
128+
)
129+
130+
expected = %r{
131+
Ruby\sversion:\s.+\n
132+
IRB\sversion:\sirb\s.+\n
133+
InputMethod:\sAbstract\sInputMethod\n
134+
RUBY_PLATFORM:\s.+\n
135+
East\sAsian\sAmbiguous\sWidth:\s\d\n
136+
#{@is_win ? 'Code\spage:\s\d+\n' : ''}
137+
}x
138+
139+
assert_empty err
140+
assert_match expected, out
115141
ensure
116-
STDOUT.reopen(stdout)
117-
stdout.close
142+
ENV["INPUTRC"] = inputrc_backup
143+
IRB.__send__(:remove_const, :IRBRC_EXT)
144+
IRB.const_set(:IRBRC_EXT, ext_backup)
118145
end
119-
assert_match expected, info
120-
ensure
121-
ENV["LANG"] = lang_backup
122-
ENV["LC_ALL"] = lc_all_backup
123-
end
124146

125-
def test_irb_info_multiline_without_rc_files
126-
inputrc_backup = ENV["INPUTRC"]
127-
ENV["INPUTRC"] = "unknown_inpurc"
128-
ext_backup = IRB::IRBRC_EXT
129-
IRB.__send__(:remove_const, :IRBRC_EXT)
130-
IRB.const_set(:IRBRC_EXT, "unknown_ext")
131-
IRB.setup(__FILE__, argv: [])
132-
IRB.conf[:USE_MULTILINE] = true
133-
IRB.conf[:USE_SINGLELINE] = false
134-
IRB.conf[:VERBOSE] = false
135-
lang_backup = ENV.delete("LANG")
136-
lc_all_backup = ENV.delete("LC_ALL")
137-
workspace = IRB::WorkSpace.new(self)
138-
irb = IRB::Irb.new(workspace, TestInputMethod.new([]))
139-
IRB.conf[:MAIN_CONTEXT] = irb.context
140-
expected = %r{
141-
Ruby\sversion:\s.+\n
142-
IRB\sversion:\sirb\s.+\n
143-
InputMethod:\sAbstract\sInputMethod\n
144-
RUBY_PLATFORM:\s.+\n
145-
East\sAsian\sAmbiguous\sWidth:\s\d\n
146-
#{@is_win ? 'Code\spage:\s\d+\n' : ''}
147-
\z
148-
}x
149-
assert_match expected, irb.context.main.irb_info.to_s
150-
ensure
151-
ENV["INPUTRC"] = inputrc_backup
152-
IRB.__send__(:remove_const, :IRBRC_EXT)
153-
IRB.const_set(:IRBRC_EXT, ext_backup)
154-
ENV["LANG"] = lang_backup
155-
ENV["LC_ALL"] = lc_all_backup
156-
end
147+
def test_irb_info_singleline_without_rc_files
148+
inputrc_backup = ENV["INPUTRC"]
149+
ENV["INPUTRC"] = "unknown_inpurc"
150+
ext_backup = IRB::IRBRC_EXT
151+
IRB.__send__(:remove_const, :IRBRC_EXT)
152+
IRB.const_set(:IRBRC_EXT, "unknown_ext")
157153

158-
def test_irb_info_singleline_without_rc_files
159-
inputrc_backup = ENV["INPUTRC"]
160-
ENV["INPUTRC"] = "unknown_inpurc"
161-
ext_backup = IRB::IRBRC_EXT
162-
IRB.__send__(:remove_const, :IRBRC_EXT)
163-
IRB.const_set(:IRBRC_EXT, "unknown_ext")
164-
IRB.setup(__FILE__, argv: [])
165-
IRB.conf[:USE_MULTILINE] = false
166-
IRB.conf[:USE_SINGLELINE] = true
167-
IRB.conf[:VERBOSE] = false
168-
lang_backup = ENV.delete("LANG")
169-
lc_all_backup = ENV.delete("LC_ALL")
170-
workspace = IRB::WorkSpace.new(self)
171-
irb = IRB::Irb.new(workspace, TestInputMethod.new([]))
172-
IRB.conf[:MAIN_CONTEXT] = irb.context
173-
expected = %r{
174-
Ruby\sversion:\s.+\n
175-
IRB\sversion:\sirb\s.+\n
176-
InputMethod:\sAbstract\sInputMethod\n
177-
RUBY_PLATFORM:\s.+\n
178-
East\sAsian\sAmbiguous\sWidth:\s\d\n
179-
#{@is_win ? 'Code\spage:\s\d+\n' : ''}
180-
\z
181-
}x
182-
assert_match expected, irb.context.main.irb_info.to_s
183-
ensure
184-
ENV["INPUTRC"] = inputrc_backup
185-
IRB.__send__(:remove_const, :IRBRC_EXT)
186-
IRB.const_set(:IRBRC_EXT, ext_backup)
187-
ENV["LANG"] = lang_backup
188-
ENV["LC_ALL"] = lc_all_backup
189-
end
154+
out, err = execute_lines(
155+
"irb_info",
156+
conf: { USE_MULTILINE: false, USE_SINGLELINE: true }
157+
)
190158

191-
def test_irb_info_lang
192-
FileUtils.touch("#{@tmpdir}/.inputrc")
193-
FileUtils.touch("#{@tmpdir}/.irbrc")
194-
IRB.setup(__FILE__, argv: [])
195-
IRB.conf[:USE_MULTILINE] = true
196-
IRB.conf[:USE_SINGLELINE] = false
197-
IRB.conf[:VERBOSE] = false
198-
lang_backup = ENV.delete("LANG")
199-
lc_all_backup = ENV.delete("LC_ALL")
200-
ENV["LANG"] = "ja_JP.UTF-8"
201-
ENV["LC_ALL"] = "en_US.UTF-8"
202-
workspace = IRB::WorkSpace.new(self)
203-
irb = IRB::Irb.new(workspace, TestInputMethod.new([]))
204-
IRB.conf[:MAIN_CONTEXT] = irb.context
205-
expected = %r{
206-
Ruby\sversion: .+\n
207-
IRB\sversion:\sirb .+\n
208-
InputMethod:\sAbstract\sInputMethod\n
209-
\.irbrc\spath: .+\n
210-
RUBY_PLATFORM: .+\n
211-
LANG\senv:\sja_JP\.UTF-8\n
212-
LC_ALL\senv:\sen_US\.UTF-8\n
213-
East\sAsian\sAmbiguous\sWidth:\s\d\n
214-
}x
215-
assert_match expected, irb.context.main.irb_info.to_s
216-
ensure
217-
ENV["LANG"] = lang_backup
218-
ENV["LC_ALL"] = lc_all_backup
159+
expected = %r{
160+
Ruby\sversion:\s.+\n
161+
IRB\sversion:\sirb\s.+\n
162+
InputMethod:\sAbstract\sInputMethod\n
163+
RUBY_PLATFORM:\s.+\n
164+
East\sAsian\sAmbiguous\sWidth:\s\d\n
165+
#{@is_win ? 'Code\spage:\s\d+\n' : ''}
166+
}x
167+
168+
assert_empty err
169+
assert_match expected, out
170+
ensure
171+
ENV["INPUTRC"] = inputrc_backup
172+
IRB.__send__(:remove_const, :IRBRC_EXT)
173+
IRB.const_set(:IRBRC_EXT, ext_backup)
174+
end
175+
176+
def test_irb_info_lang
177+
FileUtils.touch("#{@tmpdir}/.inputrc")
178+
FileUtils.touch("#{@tmpdir}/.irbrc")
179+
ENV["LANG"] = "ja_JP.UTF-8"
180+
ENV["LC_ALL"] = "en_US.UTF-8"
181+
182+
out, err = execute_lines(
183+
"irb_info",
184+
conf: { USE_MULTILINE: true, USE_SINGLELINE: false }
185+
)
186+
187+
expected = %r{
188+
Ruby\sversion: .+\n
189+
IRB\sversion:\sirb .+\n
190+
InputMethod:\sAbstract\sInputMethod\n
191+
\.irbrc\spath: .+\n
192+
RUBY_PLATFORM: .+\n
193+
LANG\senv:\sja_JP\.UTF-8\n
194+
LC_ALL\senv:\sen_US\.UTF-8\n
195+
East\sAsian\sAmbiguous\sWidth:\s\d\n
196+
}x
197+
198+
assert_empty err
199+
assert_match expected, out
200+
end
219201
end
220202

221203
def test_measure
@@ -571,7 +553,7 @@ def show_source_test_method
571553
EOS
572554

573555
out, err = execute_lines(
574-
"show_source 'TestIRB::ExtendCommand#show_source_test_method'\n",
556+
"show_source 'TestIRB::ExtendCommandTest#show_source_test_method'\n",
575557
)
576558

577559
assert_empty err

0 commit comments

Comments
 (0)