1
0
Fork 0
mirror of https://github.com/pry/pry.git synced 2022-11-09 12:35:05 -05:00
pry--pry/spec/code_spec.rb

220 lines
5.7 KiB
Ruby
Raw Normal View History

2012-01-15 00:19:30 -05:00
require 'helper'
describe Pry::Code do
describe '.from_file' do
2012-01-15 03:55:40 -05:00
should 'read lines from a file on disk' do
2012-01-15 00:19:30 -05:00
Pry::Code.from_file('lib/pry.rb').length.should > 0
end
2012-01-15 03:55:40 -05:00
should 'read lines from Pry\'s line buffer' do
2012-09-09 01:29:00 -04:00
pry_eval ':hay_guys'
2012-01-15 00:19:30 -05:00
Pry::Code.from_file('(pry)').grep(/:hay_guys/).length.should == 1
end
2012-01-15 03:55:40 -05:00
should 'default to Ruby' do
temp_file('') do |f|
2012-01-15 01:06:24 -05:00
Pry::Code.from_file(f.path).code_type.should == :ruby
end
end
2012-01-15 03:55:40 -05:00
should 'check the extension' do
temp_file('.c') do |f|
2012-01-15 01:06:24 -05:00
Pry::Code.from_file(f.path).code_type.should == :c
end
end
should 'use the provided extension' do
temp_file('.c') do |f|
Pry::Code.from_file(f.path, :ruby).code_type.should == :ruby
end
end
2012-01-15 03:55:40 -05:00
should 'raise an error if the file doesn\'t exist' do
2012-01-15 00:19:30 -05:00
proc do
Pry::Code.from_file('/knalkjsdnalsd/alkjdlkq')
2012-06-11 02:37:47 -04:00
end.should.raise(MethodSource::SourceNotFoundError)
2012-01-15 00:19:30 -05:00
end
2012-08-13 01:41:28 -04:00
should 'check for files relative to origin pwd' do
2012-11-15 00:37:37 -05:00
Dir.chdir('spec') do |f|
Pry::Code.from_file('spec/' + File.basename(__FILE__)).code_type.should == :ruby
2012-08-13 01:41:28 -04:00
end
end
should 'find files that are relative to the current working directory' do
2012-11-15 00:37:37 -05:00
Dir.chdir('spec') do |f|
2012-08-13 01:41:28 -04:00
Pry::Code.from_file(File.basename(__FILE__)).code_type.should == :ruby
end
end
2012-01-15 00:19:30 -05:00
end
describe '.from_method' do
2012-01-15 03:55:40 -05:00
should 'read lines from a method\'s definition' do
2012-01-15 00:19:30 -05:00
m = Pry::Method.from_obj(Pry, :load_history)
Pry::Code.from_method(m).length.should > 0
end
end
describe '#initialize' do
before do
@str = Pry::Helpers::CommandHelpers.unindent <<-CODE
def hay
:guys
end
CODE
@array = ['def hay', ' :guys', 'end']
end
2012-01-15 03:55:40 -05:00
should 'break a string into lines' do
2012-01-15 00:19:30 -05:00
Pry::Code.new(@str).length.should == 3
end
2012-01-15 03:55:40 -05:00
should 'accept an array' do
2012-01-15 00:19:30 -05:00
Pry::Code.new(@array).length.should == 3
end
it 'an array or string should produce an equivalent object' do
Pry::Code.new(@str).should == Pry::Code.new(@array)
end
end
describe 'filters and formatters' do
before do
@code = Pry::Code(Pry::Helpers::CommandHelpers.unindent <<-STR)
class MyProgram
def self.main
puts 'Hello, world!'
end
end
STR
end
describe 'filters' do
describe '#between' do
2012-01-15 03:55:40 -05:00
should 'work with an inclusive range' do
2012-01-15 00:19:30 -05:00
@code = @code.between(1..3)
@code.length.should == 3
@code.should =~ /\Aclass MyProgram/
@code.should =~ /world!'\Z/
end
2012-01-15 03:55:40 -05:00
should 'default to an inclusive range' do
2012-01-15 00:19:30 -05:00
@code = @code.between(3, 5)
@code.length.should == 3
end
2012-01-15 03:55:40 -05:00
should 'work with an exclusive range' do
2012-01-15 00:19:30 -05:00
@code = @code.between(2...4)
@code.length.should == 2
@code.should =~ /\A def self/
@code.should =~ /world!'\Z/
end
should 'use real line numbers for positive indices' do
@code = @code.after(3, 3)
@code = @code.between(4, 4)
@code.length.should == 1
@code.should =~ /\A end\Z/
end
2012-01-15 00:19:30 -05:00
end
describe '#before' do
2012-01-15 03:55:40 -05:00
should 'work' do
2012-01-15 00:19:30 -05:00
@code = @code.before(3, 1)
@code.should =~ /\A def self\.main\Z/
end
end
describe '#around' do
2012-01-15 03:55:40 -05:00
should 'work' do
2012-01-15 00:19:30 -05:00
@code = @code.around(3, 1)
@code.length.should == 3
@code.should =~ /\A def self/
@code.should =~ / end\Z/
end
end
describe '#after' do
2012-01-15 03:55:40 -05:00
should 'work' do
2012-01-15 00:19:30 -05:00
@code = @code.after(3, 1)
@code.should =~ /\A end\Z/
end
end
describe '#grep' do
2012-01-15 03:55:40 -05:00
should 'work' do
2012-01-15 00:19:30 -05:00
@code = @code.grep(/end/)
@code.length.should == 2
end
end
end
describe 'formatters' do
describe '#with_line_numbers' do
2012-01-15 03:55:40 -05:00
should 'show line numbers' do
2012-01-15 00:19:30 -05:00
@code = @code.with_line_numbers
@code.should =~ /1:/
end
2012-01-15 03:55:40 -05:00
should 'disable line numbers when falsy' do
2012-01-15 00:19:30 -05:00
@code = @code.with_line_numbers
@code = @code.with_line_numbers(false)
@code.should.not =~ /1:/
end
end
describe '#with_marker' do
2012-01-15 03:55:40 -05:00
should 'show a marker in the right place' do
2012-01-15 00:19:30 -05:00
@code = @code.with_marker(2)
@code.should =~ /^ => def self/
end
2012-01-15 03:55:40 -05:00
should 'disable the marker when falsy' do
2012-01-15 00:19:30 -05:00
@code = @code.with_marker(2)
@code = @code.with_marker(false)
@code.should =~ /^ def self/
end
end
describe '#with_indentation' do
2012-01-15 03:55:40 -05:00
should 'indent the text' do
2012-01-15 00:19:30 -05:00
@code = @code.with_indentation(2)
@code.should =~ /^ def self/
end
2012-01-15 03:55:40 -05:00
should 'disable the indentation when falsy' do
2012-01-15 00:19:30 -05:00
@code = @code.with_indentation(2)
@code = @code.with_indentation(false)
@code.should =~ /^ def self/
end
end
end
2012-01-15 03:55:40 -05:00
describe 'composition' do
describe 'grep and with_line_numbers' do
should 'work' do
@code = @code.grep(/end/).with_line_numbers
@code.should =~ /\A4: end/
@code.should =~ /5: end\Z/
end
end
describe 'grep and before and with_line_numbers' do
should 'work' do
@code = @code.grep(/e/).before(5, 5).with_line_numbers
@code.should =~ /\A2: def self.main\n3:/
@code.should =~ /4: end\Z/
end
end
describe 'before and after' do
should 'work' do
@code = @code.before(4, 2).after(2)
@code.should == " puts 'Hello, world!'"
end
end
end
2012-01-15 00:19:30 -05:00
end
end