Skip to content

Commit

Permalink
PryTestHelpers methods are now module_functions, included into Bacon:…
Browse files Browse the repository at this point in the history
…:Context by default
  • Loading branch information
banister committed Dec 7, 2012
1 parent 64f402e commit 9a27946
Show file tree
Hide file tree
Showing 13 changed files with 72 additions and 65 deletions.
4 changes: 4 additions & 0 deletions lib/pry/test/bacon_helper.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
# Colorize output (based on greeneggs (c) 2009 Michael Fleet)
# TODO: Make own gem (assigned to rking)
module Bacon
class Context
include PryTestHelpers
end

COLORS = {'F' => 31, 'E' => 35, 'M' => 33, '.' => 32}
USE_COLOR = !(ENV['NO_PRY_COLORED_BACON'] == 'true') && Pry::Helpers::BaseHelpers.use_ansi_codes?

Expand Down
21 changes: 12 additions & 9 deletions lib/pry/test/helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,6 @@

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
# amended (test friendly) values
class << Pry
Expand Down Expand Up @@ -34,15 +30,18 @@ def Pad.clear
end

module PryTestHelpers

module_function

# inject a variable into a binding
def self.inject_var(name, value, b)
def inject_var(name, value, b)
Thread.current[:__pry_local__] = value
b.eval("#{name} = Thread.current[:__pry_local__]")
ensure
Thread.current[:__pry_local__] = nil
end

def self.constant_scope(*names)
def constant_scope(*names)
names.each do |name|
Object.remove_const name if Object.const_defined?(name)
end
Expand All @@ -54,21 +53,21 @@ def self.constant_scope(*names)
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)
end

# Open a temp file and yield it to the block, closing it after
# @return [String] The path of the temp file
def self.temp_file(ext='.rb')
def temp_file(ext='.rb')
file = Tempfile.new(['pry', ext])
yield file
ensure
file.close(true) if file
File.unlink("#{file.path}c") if File.exists?("#{file.path}c") # rbx
end

def self.unindent(*args)
def unindent(*args)
Pry::Helpers::CommandHelpers.unindent(*args)
end
end
Expand Down Expand Up @@ -204,3 +203,7 @@ def reset_output
@pry.output = @out
end
end

if defined?(Bacon)
require File.join(File.expand_path(File.dirname(__FILE__)), 'bacon_helper')
end
6 changes: 3 additions & 3 deletions spec/code_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,19 +12,19 @@
end

should 'default to Ruby' do
PryTestHelpers.temp_file('') do |f|
temp_file('') do |f|
Pry::Code.from_file(f.path).code_type.should == :ruby
end
end

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
end
end

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
end
end
Expand Down
2 changes: 1 addition & 1 deletion spec/command_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -719,7 +719,7 @@ def process
pry_eval("my---test").should =~ /my-testmy-test/
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
pry_eval("show-source my-test").should =~ /output.puts command_name/
end
Expand Down
54 changes: 27 additions & 27 deletions spec/commands/amend_line_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,77 +6,77 @@
end

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
puts :bing
STR

@t.process_command 'amend-line puts :blah', eval_str

eval_str.should == PryTestHelpers.unindent(<<-STR)
eval_str.should == unindent(<<-STR)
def hello
puts :blah
STR
end

it 'should amend the specified line of input when line number given' do
eval_str = PryTestHelpers.unindent(<<-STR)
eval_str = unindent(<<-STR)
def hello
puts :bing
puts :bang
STR

@t.process_command 'amend-line 1 def goodbye', eval_str

eval_str.should == PryTestHelpers.unindent(<<-STR)
eval_str.should == unindent(<<-STR)
def goodbye
puts :bing
puts :bang
STR
end

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
puts :bing
puts :bang
STR

@t.process_command 'amend-line 0 def goodbye', eval_str

eval_str.should == PryTestHelpers.unindent(<<-STR)
eval_str.should == unindent(<<-STR)
def goodbye
puts :bing
puts :bang
STR
end

it 'should amend a specified line when negative number given' do
eval_str = PryTestHelpers.unindent(<<-STR)
eval_str = unindent(<<-STR)
def hello
puts :bing
puts :bang
STR

@t.process_command 'amend-line -1 puts :bink', eval_str

eval_str.should == PryTestHelpers.unindent(<<-STR)
eval_str.should == unindent(<<-STR)
def hello
puts :bing
puts :bink
STR

@t.process_command 'amend-line -2 puts :bink', eval_str

eval_str.should == PryTestHelpers.unindent(<<-STR)
eval_str.should == unindent(<<-STR)
def hello
puts :bink
puts :bink
STR
end

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
puts :bing
puts :bang
Expand All @@ -85,23 +85,23 @@ def hello

@t.process_command 'amend-line -3..-2 puts :bink', eval_str

eval_str.should == PryTestHelpers.unindent(<<-STR)
eval_str.should == unindent(<<-STR)
def hello
puts :bink
puts :boat
STR
end

it 'should correctly amend the specified line with interpolated text' do
eval_str = PryTestHelpers.unindent(<<-STR)
eval_str = unindent(<<-STR)
def hello
puts :bing
puts :bang
STR

@t.process_command 'amend-line puts "#{goodbye}"', eval_str

eval_str.should == PryTestHelpers.unindent(<<-'STR')
eval_str.should == unindent(<<-'STR')
def hello
puts :bing
puts "#{goodbye}"
Expand All @@ -122,7 +122,7 @@ def hello
end

it 'should correctly amend the specified range of lines' do
eval_str = PryTestHelpers.unindent(<<-STR)
eval_str = unindent(<<-STR)
def hello
puts :bing
puts :bang
Expand All @@ -131,15 +131,15 @@ def hello

@t.process_command 'amend-line 2..3 puts :bong', eval_str

eval_str.should == PryTestHelpers.unindent(<<-STR)
eval_str.should == unindent(<<-STR)
def hello
puts :bong
puts :heart
STR
end

it 'should correctly delete a specific line using the ! for content' do
eval_str = PryTestHelpers.unindent(<<-STR)
eval_str = unindent(<<-STR)
def hello
puts :bing
puts :bang
Expand All @@ -149,7 +149,7 @@ def hello

@t.process_command 'amend-line 3 !', eval_str

eval_str.should == PryTestHelpers.unindent(<<-STR)
eval_str.should == unindent(<<-STR)
def hello
puts :bing
puts :boast
Expand All @@ -158,7 +158,7 @@ def hello
end

it 'should correctly delete a range of lines using the ! for content' do
eval_str = PryTestHelpers.unindent(<<-STR)
eval_str = unindent(<<-STR)
def hello
puts :bing
puts :bang
Expand All @@ -168,14 +168,14 @@ def hello

@t.process_command 'amend-line 2..4 !', eval_str

eval_str.should == PryTestHelpers.unindent(<<-STR)
eval_str.should == unindent(<<-STR)
def hello
puts :heart
STR
end

it 'should correctly delete the previous line using the ! for content' do
eval_str = PryTestHelpers.unindent(<<-STR)
eval_str = unindent(<<-STR)
def hello
puts :bing
puts :bang
Expand All @@ -185,7 +185,7 @@ def hello

@t.process_command 'amend-line !', eval_str

eval_str.should == PryTestHelpers.unindent(<<-STR)
eval_str.should == unindent(<<-STR)
def hello
puts :bing
puts :bang
Expand All @@ -194,7 +194,7 @@ def hello
end

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
puts :bing
puts :bang
Expand All @@ -204,23 +204,23 @@ def hello

@t.process_command 'amend-line 2..-2 puts :bong', eval_str

eval_str.should == PryTestHelpers.unindent(<<-STR)
eval_str.should == unindent(<<-STR)
def hello
puts :bong
puts :heart
STR
end

it 'should correctly insert a line before a specified line using >' do
eval_str = PryTestHelpers.unindent(<<-STR)
eval_str = unindent(<<-STR)
def hello
puts :bing
puts :bang
STR

@t.process_command 'amend-line 2 > puts :inserted', eval_str

eval_str.should == PryTestHelpers.unindent(<<-STR)
eval_str.should == unindent(<<-STR)
def hello
puts :inserted
puts :bing
Expand All @@ -229,15 +229,15 @@ def hello
end

it 'should ignore second value of range with > syntax' do
eval_str = PryTestHelpers.unindent(<<-STR)
eval_str = unindent(<<-STR)
def hello
puts :bing
puts :bang
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
puts :inserted
puts :bing
Expand Down
2 changes: 1 addition & 1 deletion spec/commands/bang_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
end

it 'should correctly clear the input buffer ' do
eval_str = PryTestHelpers.unindent(<<-STR)
eval_str = unindent(<<-STR)
def hello
puts :bing
STR
Expand Down
Loading

0 comments on commit 9a27946

Please sign in to comment.