summaryrefslogtreecommitdiff
path: root/test/irb
diff options
context:
space:
mode:
authork0kubun <k0kubun@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2017-11-24 03:53:27 +0000
committerk0kubun <k0kubun@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2017-11-24 03:53:27 +0000
commit106c9883e11673fa31e3854d7e24b97f4c1ba9fd (patch)
treeec8528ed8bf060ad891770220295422bbd8a62da /test/irb
parentf8523d35bf3e8ec4a705b1dd45648e1b4352199a (diff)
irb.rb: show source around binding.irb on start
[Feature #14124] [ruby-dev:50319] [close GH-1764] git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@60888 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
Diffstat (limited to 'test/irb')
-rw-r--r--test/irb/test_workspace.rb78
1 files changed, 78 insertions, 0 deletions
diff --git a/test/irb/test_workspace.rb b/test/irb/test_workspace.rb
new file mode 100644
index 0000000000..f725c55c06
--- /dev/null
+++ b/test/irb/test_workspace.rb
@@ -0,0 +1,78 @@
+# frozen_string_literal: false
+require 'test/unit'
+require 'tempfile'
+require 'irb/workspace'
+
+module TestIRB
+ class TestWorkSpace < Test::Unit::TestCase
+ def test_code_around_bindinig
+ Tempfile.create do |f|
+ code = <<~RUBY
+ # 1
+ # 2
+ IRB::WorkSpace.new(binding) # 3
+ # 4
+ # 5
+ RUBY
+ f.print(code)
+ f.close
+
+ workspace = eval(code, binding, f.path)
+ assert_equal(<<~EOS, workspace.code_around_binding)
+
+ From: #{f.path} @ line 3 :
+
+ 1: # 1
+ 2: # 2
+ => 3: IRB::WorkSpace.new(binding) # 3
+ 4: # 4
+ 5: # 5
+
+ EOS
+ end
+ end
+
+ def test_code_around_bindinig_with_script_lines__
+ with_script_lines do |script_lines|
+ Tempfile.create do |f|
+ code = "IRB::WorkSpace.new(binding)\n"
+ script_lines[f.path] = code
+
+ workspace = eval(code, binding, f.path)
+ assert_equal(<<~EOS, workspace.code_around_binding)
+
+ From: #{f.path} @ line 1 :
+
+ => 1: IRB::WorkSpace.new(binding)
+
+ EOS
+ end
+ end
+ end
+
+ def test_code_around_bindinig_on_irb
+ workspace = eval("IRB::WorkSpace.new(binding)", binding, "(irb)")
+ assert_equal(nil, workspace.code_around_binding)
+ end
+
+ private
+
+ def with_script_lines
+ script_lines = nil
+ debug_lines = {}
+ Object.class_eval do
+ if defined?(SCRIPT_LINES__)
+ script_lines = SCRIPT_LINES__
+ remove_const :SCRIPT_LINES__
+ end
+ const_set(:SCRIPT_LINES__, debug_lines)
+ end
+ yield debug_lines
+ ensure
+ Object.class_eval do
+ remove_const :SCRIPT_LINES__
+ const_set(:SCRIPT_LINES__, script_lines) if script_lines
+ end
+ end
+ end
+end