PryTestHelpers methods are now module_functions, included into Bacon::Context by default

This commit is contained in:
John Mair 2012-12-07 23:08:49 +01:00
parent 64f402e216
commit 9a279461c1
13 changed files with 72 additions and 65 deletions

View File

@ -1,6 +1,10 @@
# Colorize output (based on greeneggs (c) 2009 Michael Fleet) # Colorize output (based on greeneggs (c) 2009 Michael Fleet)
# TODO: Make own gem (assigned to rking) # TODO: Make own gem (assigned to rking)
module Bacon module Bacon
class Context
include PryTestHelpers
end
COLORS = {'F' => 31, 'E' => 35, 'M' => 33, '.' => 32} COLORS = {'F' => 31, 'E' => 35, 'M' => 33, '.' => 32}
USE_COLOR = !(ENV['NO_PRY_COLORED_BACON'] == 'true') && Pry::Helpers::BaseHelpers.use_ansi_codes? USE_COLOR = !(ENV['NO_PRY_COLORED_BACON'] == 'true') && Pry::Helpers::BaseHelpers.use_ansi_codes?

View File

@ -2,10 +2,6 @@ require 'pry'
puts "Ruby v#{RUBY_VERSION} (#{defined?(RUBY_ENGINE) ? RUBY_ENGINE : "ruby"}), Pry v#{Pry::VERSION}, method_source v#{MethodSource::VERSION}, CodeRay v#{CodeRay::VERSION}, Slop v#{Slop::VERSION}" puts "Ruby v#{RUBY_VERSION} (#{defined?(RUBY_ENGINE) ? RUBY_ENGINE : "ruby"}), Pry v#{Pry::VERSION}, method_source v#{MethodSource::VERSION}, CodeRay v#{CodeRay::VERSION}, Slop v#{Slop::VERSION}"
if defined?(Bacon)
require File.join(File.expand_path(File.dirname(__FILE__)), 'bacon_helper')
end
# in case the tests call reset_defaults, ensure we reset them to # in case the tests call reset_defaults, ensure we reset them to
# amended (test friendly) values # amended (test friendly) values
class << Pry class << Pry
@ -34,15 +30,18 @@ def Pad.clear
end end
module PryTestHelpers module PryTestHelpers
module_function
# inject a variable into a binding # inject a variable into a binding
def self.inject_var(name, value, b) def inject_var(name, value, b)
Thread.current[:__pry_local__] = value Thread.current[:__pry_local__] = value
b.eval("#{name} = Thread.current[:__pry_local__]") b.eval("#{name} = Thread.current[:__pry_local__]")
ensure ensure
Thread.current[:__pry_local__] = nil Thread.current[:__pry_local__] = nil
end end
def self.constant_scope(*names) def constant_scope(*names)
names.each do |name| names.each do |name|
Object.remove_const name if Object.const_defined?(name) Object.remove_const name if Object.const_defined?(name)
end end
@ -54,13 +53,13 @@ module PryTestHelpers
end end
end end
def self.mri18_and_no_real_source_location? def mri18_and_no_real_source_location?
Pry::Helpers::BaseHelpers.mri_18? && !(Method.instance_method(:source_location).owner == Method) Pry::Helpers::BaseHelpers.mri_18? && !(Method.instance_method(:source_location).owner == Method)
end end
# Open a temp file and yield it to the block, closing it after # Open a temp file and yield it to the block, closing it after
# @return [String] The path of the temp file # @return [String] The path of the temp file
def self.temp_file(ext='.rb') def temp_file(ext='.rb')
file = Tempfile.new(['pry', ext]) file = Tempfile.new(['pry', ext])
yield file yield file
ensure ensure
@ -68,7 +67,7 @@ module PryTestHelpers
File.unlink("#{file.path}c") if File.exists?("#{file.path}c") # rbx File.unlink("#{file.path}c") if File.exists?("#{file.path}c") # rbx
end end
def self.unindent(*args) def unindent(*args)
Pry::Helpers::CommandHelpers.unindent(*args) Pry::Helpers::CommandHelpers.unindent(*args)
end end
end end
@ -204,3 +203,7 @@ class PryTester
@pry.output = @out @pry.output = @out
end end
end end
if defined?(Bacon)
require File.join(File.expand_path(File.dirname(__FILE__)), 'bacon_helper')
end

View File

@ -12,19 +12,19 @@ describe Pry::Code do
end end
should 'default to Ruby' do should 'default to Ruby' do
PryTestHelpers.temp_file('') do |f| temp_file('') do |f|
Pry::Code.from_file(f.path).code_type.should == :ruby Pry::Code.from_file(f.path).code_type.should == :ruby
end end
end end
should 'check the extension' do should 'check the extension' do
PryTestHelpers.temp_file('.c') do |f| temp_file('.c') do |f|
Pry::Code.from_file(f.path).code_type.should == :c Pry::Code.from_file(f.path).code_type.should == :c
end end
end end
should 'use the provided extension' do should 'use the provided extension' do
PryTestHelpers.temp_file('.c') do |f| temp_file('.c') do |f|
Pry::Code.from_file(f.path, :ruby).code_type.should == :ruby Pry::Code.from_file(f.path, :ruby).code_type.should == :ruby
end end
end end

View File

@ -719,7 +719,7 @@ describe "Pry::Command" do
pry_eval("my---test").should =~ /my-testmy-test/ pry_eval("my---test").should =~ /my-testmy-test/
end end
if !PryTestHelpers.mri18_and_no_real_source_location? if !mri18_and_no_real_source_location?
it "should show the source of the process method" do it "should show the source of the process method" do
pry_eval("show-source my-test").should =~ /output.puts command_name/ pry_eval("show-source my-test").should =~ /output.puts command_name/
end end

View File

@ -6,21 +6,21 @@ describe "amend-line" do
end end
it 'should amend the last line of input when no line number specified' do it 'should amend the last line of input when no line number specified' do
eval_str = PryTestHelpers.unindent(<<-STR) eval_str = unindent(<<-STR)
def hello def hello
puts :bing puts :bing
STR STR
@t.process_command 'amend-line puts :blah', eval_str @t.process_command 'amend-line puts :blah', eval_str
eval_str.should == PryTestHelpers.unindent(<<-STR) eval_str.should == unindent(<<-STR)
def hello def hello
puts :blah puts :blah
STR STR
end end
it 'should amend the specified line of input when line number given' do it 'should amend the specified line of input when line number given' do
eval_str = PryTestHelpers.unindent(<<-STR) eval_str = unindent(<<-STR)
def hello def hello
puts :bing puts :bing
puts :bang puts :bang
@ -28,7 +28,7 @@ describe "amend-line" do
@t.process_command 'amend-line 1 def goodbye', eval_str @t.process_command 'amend-line 1 def goodbye', eval_str
eval_str.should == PryTestHelpers.unindent(<<-STR) eval_str.should == unindent(<<-STR)
def goodbye def goodbye
puts :bing puts :bing
puts :bang puts :bang
@ -36,7 +36,7 @@ describe "amend-line" do
end end
it 'should amend the first line of input when 0 given as line number' do it 'should amend the first line of input when 0 given as line number' do
eval_str = PryTestHelpers.unindent(<<-STR) eval_str = unindent(<<-STR)
def hello def hello
puts :bing puts :bing
puts :bang puts :bang
@ -44,7 +44,7 @@ describe "amend-line" do
@t.process_command 'amend-line 0 def goodbye', eval_str @t.process_command 'amend-line 0 def goodbye', eval_str
eval_str.should == PryTestHelpers.unindent(<<-STR) eval_str.should == unindent(<<-STR)
def goodbye def goodbye
puts :bing puts :bing
puts :bang puts :bang
@ -52,7 +52,7 @@ describe "amend-line" do
end end
it 'should amend a specified line when negative number given' do it 'should amend a specified line when negative number given' do
eval_str = PryTestHelpers.unindent(<<-STR) eval_str = unindent(<<-STR)
def hello def hello
puts :bing puts :bing
puts :bang puts :bang
@ -60,7 +60,7 @@ describe "amend-line" do
@t.process_command 'amend-line -1 puts :bink', eval_str @t.process_command 'amend-line -1 puts :bink', eval_str
eval_str.should == PryTestHelpers.unindent(<<-STR) eval_str.should == unindent(<<-STR)
def hello def hello
puts :bing puts :bing
puts :bink puts :bink
@ -68,7 +68,7 @@ describe "amend-line" do
@t.process_command 'amend-line -2 puts :bink', eval_str @t.process_command 'amend-line -2 puts :bink', eval_str
eval_str.should == PryTestHelpers.unindent(<<-STR) eval_str.should == unindent(<<-STR)
def hello def hello
puts :bink puts :bink
puts :bink puts :bink
@ -76,7 +76,7 @@ describe "amend-line" do
end end
it 'should amend a range of lines of input when negative numbers given' do it 'should amend a range of lines of input when negative numbers given' do
eval_str = PryTestHelpers.unindent(<<-STR) eval_str = unindent(<<-STR)
def hello def hello
puts :bing puts :bing
puts :bang puts :bang
@ -85,7 +85,7 @@ describe "amend-line" do
@t.process_command 'amend-line -3..-2 puts :bink', eval_str @t.process_command 'amend-line -3..-2 puts :bink', eval_str
eval_str.should == PryTestHelpers.unindent(<<-STR) eval_str.should == unindent(<<-STR)
def hello def hello
puts :bink puts :bink
puts :boat puts :boat
@ -93,7 +93,7 @@ describe "amend-line" do
end end
it 'should correctly amend the specified line with interpolated text' do it 'should correctly amend the specified line with interpolated text' do
eval_str = PryTestHelpers.unindent(<<-STR) eval_str = unindent(<<-STR)
def hello def hello
puts :bing puts :bing
puts :bang puts :bang
@ -101,7 +101,7 @@ describe "amend-line" do
@t.process_command 'amend-line puts "#{goodbye}"', eval_str @t.process_command 'amend-line puts "#{goodbye}"', eval_str
eval_str.should == PryTestHelpers.unindent(<<-'STR') eval_str.should == unindent(<<-'STR')
def hello def hello
puts :bing puts :bing
puts "#{goodbye}" puts "#{goodbye}"
@ -122,7 +122,7 @@ describe "amend-line" do
end end
it 'should correctly amend the specified range of lines' do it 'should correctly amend the specified range of lines' do
eval_str = PryTestHelpers.unindent(<<-STR) eval_str = unindent(<<-STR)
def hello def hello
puts :bing puts :bing
puts :bang puts :bang
@ -131,7 +131,7 @@ describe "amend-line" do
@t.process_command 'amend-line 2..3 puts :bong', eval_str @t.process_command 'amend-line 2..3 puts :bong', eval_str
eval_str.should == PryTestHelpers.unindent(<<-STR) eval_str.should == unindent(<<-STR)
def hello def hello
puts :bong puts :bong
puts :heart puts :heart
@ -139,7 +139,7 @@ describe "amend-line" do
end end
it 'should correctly delete a specific line using the ! for content' do it 'should correctly delete a specific line using the ! for content' do
eval_str = PryTestHelpers.unindent(<<-STR) eval_str = unindent(<<-STR)
def hello def hello
puts :bing puts :bing
puts :bang puts :bang
@ -149,7 +149,7 @@ describe "amend-line" do
@t.process_command 'amend-line 3 !', eval_str @t.process_command 'amend-line 3 !', eval_str
eval_str.should == PryTestHelpers.unindent(<<-STR) eval_str.should == unindent(<<-STR)
def hello def hello
puts :bing puts :bing
puts :boast puts :boast
@ -158,7 +158,7 @@ describe "amend-line" do
end end
it 'should correctly delete a range of lines using the ! for content' do it 'should correctly delete a range of lines using the ! for content' do
eval_str = PryTestHelpers.unindent(<<-STR) eval_str = unindent(<<-STR)
def hello def hello
puts :bing puts :bing
puts :bang puts :bang
@ -168,14 +168,14 @@ describe "amend-line" do
@t.process_command 'amend-line 2..4 !', eval_str @t.process_command 'amend-line 2..4 !', eval_str
eval_str.should == PryTestHelpers.unindent(<<-STR) eval_str.should == unindent(<<-STR)
def hello def hello
puts :heart puts :heart
STR STR
end end
it 'should correctly delete the previous line using the ! for content' do it 'should correctly delete the previous line using the ! for content' do
eval_str = PryTestHelpers.unindent(<<-STR) eval_str = unindent(<<-STR)
def hello def hello
puts :bing puts :bing
puts :bang puts :bang
@ -185,7 +185,7 @@ describe "amend-line" do
@t.process_command 'amend-line !', eval_str @t.process_command 'amend-line !', eval_str
eval_str.should == PryTestHelpers.unindent(<<-STR) eval_str.should == unindent(<<-STR)
def hello def hello
puts :bing puts :bing
puts :bang puts :bang
@ -194,7 +194,7 @@ describe "amend-line" do
end end
it 'should amend the specified range of lines, with numbers < 0 in range' do it 'should amend the specified range of lines, with numbers < 0 in range' do
eval_str = PryTestHelpers.unindent(<<-STR) eval_str = unindent(<<-STR)
def hello def hello
puts :bing puts :bing
puts :bang puts :bang
@ -204,7 +204,7 @@ describe "amend-line" do
@t.process_command 'amend-line 2..-2 puts :bong', eval_str @t.process_command 'amend-line 2..-2 puts :bong', eval_str
eval_str.should == PryTestHelpers.unindent(<<-STR) eval_str.should == unindent(<<-STR)
def hello def hello
puts :bong puts :bong
puts :heart puts :heart
@ -212,7 +212,7 @@ describe "amend-line" do
end end
it 'should correctly insert a line before a specified line using >' do it 'should correctly insert a line before a specified line using >' do
eval_str = PryTestHelpers.unindent(<<-STR) eval_str = unindent(<<-STR)
def hello def hello
puts :bing puts :bing
puts :bang puts :bang
@ -220,7 +220,7 @@ describe "amend-line" do
@t.process_command 'amend-line 2 > puts :inserted', eval_str @t.process_command 'amend-line 2 > puts :inserted', eval_str
eval_str.should == PryTestHelpers.unindent(<<-STR) eval_str.should == unindent(<<-STR)
def hello def hello
puts :inserted puts :inserted
puts :bing puts :bing
@ -229,7 +229,7 @@ describe "amend-line" do
end end
it 'should ignore second value of range with > syntax' do it 'should ignore second value of range with > syntax' do
eval_str = PryTestHelpers.unindent(<<-STR) eval_str = unindent(<<-STR)
def hello def hello
puts :bing puts :bing
puts :bang puts :bang
@ -237,7 +237,7 @@ describe "amend-line" do
@t.process_command 'amend-line 2..21 > puts :inserted', eval_str @t.process_command 'amend-line 2..21 > puts :inserted', eval_str
eval_str.should == PryTestHelpers.unindent(<<-STR) eval_str.should == unindent(<<-STR)
def hello def hello
puts :inserted puts :inserted
puts :bing puts :bing

View File

@ -6,7 +6,7 @@ describe "!" do
end end
it 'should correctly clear the input buffer ' do it 'should correctly clear the input buffer ' do
eval_str = PryTestHelpers.unindent(<<-STR) eval_str = unindent(<<-STR)
def hello def hello
puts :bing puts :bing
STR STR

View File

@ -25,7 +25,7 @@ describe "cat" do
describe "with --in" do describe "with --in" do
it 'should display the last few expressions with indices' do it 'should display the last few expressions with indices' do
@t.eval('10', '20', 'cat --in').should == PryTestHelpers.unindent(<<-STR) @t.eval('10', '20', 'cat --in').should == unindent(<<-STR)
1: 1:
10 10
2: 2:
@ -52,7 +52,7 @@ describe "cat" do
@t.insert_nil_input # normally happens when a command is executed @t.insert_nil_input # normally happens when a command is executed
@t.eval ':hello' @t.eval ':hello'
@t.eval('cat --in 1..3').should == PryTestHelpers.unindent(<<-EOS) @t.eval('cat --in 1..3').should == unindent(<<-EOS)
1: 1:
10 10
3: 3:
@ -75,7 +75,7 @@ describe "cat" do
if !Pry::Helpers::BaseHelpers.rbx? if !Pry::Helpers::BaseHelpers.rbx?
it 'cat --ex should display repl code that generated exception' do it 'cat --ex should display repl code that generated exception' do
@t.eval PryTestHelpers.unindent(<<-EOS) @t.eval unindent(<<-EOS)
begin begin
this raises error this raises error
rescue => e rescue => e
@ -98,7 +98,7 @@ describe "cat" do
describe "with --ex N" do describe "with --ex N" do
it 'should cat first level of backtrace when --ex used with no argument ' do it 'should cat first level of backtrace when --ex used with no argument ' do
PryTestHelpers.temp_file do |f| temp_file do |f|
f << "bt number 1" f << "bt number 1"
f.flush f.flush
@t.last_exception = mock_exception("#{f.path}:1", 'x', 'x') @t.last_exception = mock_exception("#{f.path}:1", 'x', 'x')
@ -107,7 +107,7 @@ describe "cat" do
end end
it 'should cat first level of backtrace when --ex 0 used ' do it 'should cat first level of backtrace when --ex 0 used ' do
PryTestHelpers.temp_file do |f| temp_file do |f|
f << "bt number 1" f << "bt number 1"
f.flush f.flush
@t.last_exception = mock_exception("#{f.path}:1", 'x', 'x') @t.last_exception = mock_exception("#{f.path}:1", 'x', 'x')
@ -116,7 +116,7 @@ describe "cat" do
end end
it 'should cat second level of backtrace when --ex 1 used ' do it 'should cat second level of backtrace when --ex 1 used ' do
PryTestHelpers.temp_file do |f| temp_file do |f|
f << "bt number 2" f << "bt number 2"
f.flush f.flush
@t.last_exception = mock_exception('x', "#{f.path}:1", 'x') @t.last_exception = mock_exception('x', "#{f.path}:1", 'x')
@ -125,7 +125,7 @@ describe "cat" do
end end
it 'should cat third level of backtrace when --ex 2 used' do it 'should cat third level of backtrace when --ex 2 used' do
PryTestHelpers.temp_file do |f| temp_file do |f|
f << "bt number 3" f << "bt number 3"
f.flush f.flush
@t.last_exception = mock_exception('x', 'x', "#{f.path}:1") @t.last_exception = mock_exception('x', 'x', "#{f.path}:1")

View File

@ -49,7 +49,7 @@ describe "edit" do
end end
it "should reload the file if it is a ruby file" do it "should reload the file if it is a ruby file" do
PryTestHelpers.temp_file do |tf| temp_file do |tf|
counter = Pad.counter counter = Pad.counter
path = tf.path path = tf.path
@ -60,7 +60,7 @@ describe "edit" do
end end
it "should not reload the file if it is not a ruby file" do it "should not reload the file if it is not a ruby file" do
PryTestHelpers.temp_file('.py') do |tf| temp_file('.py') do |tf|
counter = Pad.counter counter = Pad.counter
path = tf.path path = tf.path
@ -71,7 +71,7 @@ describe "edit" do
end end
it "should not reload a ruby file if -n is given" do it "should not reload a ruby file if -n is given" do
PryTestHelpers.temp_file do |tf| temp_file do |tf|
counter = Pad.counter counter = Pad.counter
path = tf.path path = tf.path
@ -80,7 +80,7 @@ describe "edit" do
end end
it "should reload a non-ruby file if -r is given" do it "should reload a non-ruby file if -r is given" do
PryTestHelpers.temp_file('.pryrc') do |tf| temp_file('.pryrc') do |tf|
counter = Pad.counter counter = Pad.counter
path = tf.path path = tf.path

View File

@ -81,7 +81,7 @@ describe "play" do
pry_tester(@o).process_command 'play -d test_method', @eval_str pry_tester(@o).process_command 'play -d test_method', @eval_str
@eval_str.should == PryTestHelpers.unindent(<<-STR) @eval_str.should == unindent(<<-STR)
@v = 10 @v = 10
@y = 20 @y = 20
STR STR
@ -98,7 +98,7 @@ describe "play" do
pry_tester(@o).process_command 'play -d test_method --lines 2..3', @eval_str pry_tester(@o).process_command 'play -d test_method --lines 2..3', @eval_str
@eval_str.should == PryTestHelpers.unindent(<<-STR) @eval_str.should == unindent(<<-STR)
@v = 10 @v = 10
@y = 20 @y = 20
STR STR
@ -110,13 +110,13 @@ describe "play" do
end end
it 'should APPEND to the input buffer when playing a line with play -m, not replace it' do it 'should APPEND to the input buffer when playing a line with play -m, not replace it' do
@eval_str = PryTestHelpers.unindent(<<-STR) @eval_str = unindent(<<-STR)
def another_test_method def another_test_method
STR STR
pry_tester(@o).process_command 'play -m test_method --lines 2', @eval_str pry_tester(@o).process_command 'play -m test_method --lines 2', @eval_str
@eval_str.should == PryTestHelpers.unindent(<<-STR) @eval_str.should == unindent(<<-STR)
def another_test_method def another_test_method
:test_method_content :test_method_content
STR STR
@ -132,7 +132,7 @@ describe "play" do
pry_tester(@o).process_command 'play -m test_method --lines 3..4', @eval_str pry_tester(@o).process_command 'play -m test_method --lines 3..4', @eval_str
@eval_str.should == PryTestHelpers.unindent(<<-STR, 2) @eval_str.should == unindent(<<-STR, 2)
@var1 = 20 @var1 = 20
@var2 = 30 @var2 = 30
STR STR

View File

@ -13,7 +13,7 @@ describe "save-file" do
describe "-f" do describe "-f" do
it 'should save a file to a file' do it 'should save a file to a file' do
PryTestHelpers.temp_file do |f| temp_file do |f|
path = f.path path = f.path
f.puts ":cute_horse" f.puts ":cute_horse"
f.flush f.flush

View File

@ -209,7 +209,7 @@ if !PryTestHelpers.mri18_and_no_real_source_location?
end end
it 'should lookup module name with respect to current context' do it 'should lookup module name with respect to current context' do
PryTestHelpers.constant_scope(:AlphaClass, :BetaClass) do constant_scope(:AlphaClass, :BetaClass) do
# top-level beta # top-level beta
class BetaClass class BetaClass
def alpha def alpha
@ -229,7 +229,7 @@ if !PryTestHelpers.mri18_and_no_real_source_location?
end end
it 'should look up nested modules' do it 'should look up nested modules' do
PryTestHelpers.constant_scope(:AlphaClass) do constant_scope(:AlphaClass) do
class AlphaClass class AlphaClass
# nested beta # nested beta
class BetaClass class BetaClass

View File

@ -6,7 +6,7 @@ describe "show-input" do
end end
it 'should correctly show the current lines in the input buffer' do it 'should correctly show the current lines in the input buffer' do
eval_str = PryTestHelpers.unindent(<<-STR) eval_str = unindent(<<-STR)
def hello def hello
puts :bing puts :bing
STR STR

View File

@ -201,7 +201,7 @@ if !PryTestHelpers.mri18_and_no_real_source_location?
describe "on variables that shadow methods" do describe "on variables that shadow methods" do
before do before do
@t = pry_tester.eval PryTestHelpers.unindent(<<-EOS) @t = pry_tester.eval unindent(<<-EOS)
class ::TestHost class ::TestHost
def hello def hello
hello = proc { ' smile ' } hello = proc { ' smile ' }
@ -339,7 +339,7 @@ if !PryTestHelpers.mri18_and_no_real_source_location?
if !Pry::Helpers::BaseHelpers.mri_18? if !Pry::Helpers::BaseHelpers.mri_18?
before do before do
pry_eval PryTestHelpers.unindent(<<-EOS) pry_eval unindent(<<-EOS)
class Dog class Dog
def woof def woof
end end
@ -369,7 +369,7 @@ if !PryTestHelpers.mri18_and_no_real_source_location?
it 'should lookup module name with respect to current context' do it 'should lookup module name with respect to current context' do
PryTestHelpers.constant_scope(:AlphaClass, :BetaClass) do constant_scope(:AlphaClass, :BetaClass) do
class BetaClass class BetaClass
def alpha def alpha
end end
@ -387,7 +387,7 @@ if !PryTestHelpers.mri18_and_no_real_source_location?
end end
it 'should lookup nested modules' do it 'should lookup nested modules' do
PryTestHelpers.constant_scope(:AlphaClass) do constant_scope(:AlphaClass) do
class AlphaClass class AlphaClass
class BetaClass class BetaClass
def beta def beta