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

258 lines
7.2 KiB
Ruby
Raw Normal View History

require_relative 'helper'
2012-01-15 00:19:30 -05:00
describe Pry::Code do
describe '.from_file' do
2014-09-12 11:09:18 -04:00
specify 'read lines from a file on disk' do
2015-03-10 16:49:29 -04:00
expect(Pry::Code.from_file('lib/pry.rb').length).to be > 0
2012-01-15 00:19:30 -05:00
end
2014-09-12 11:09:18 -04:00
specify 'read lines from Pry\'s line buffer' do
2012-09-09 01:29:00 -04:00
pry_eval ':hay_guys'
2015-03-10 16:49:29 -04:00
expect(Pry::Code.from_file('(pry)').grep(/:hay_guys/).length).to eq 1
2012-01-15 00:19:30 -05:00
end
2014-09-12 11:09:18 -04:00
specify 'default to unknown' do
temp_file('') do |f|
2015-03-10 16:49:29 -04:00
expect(Pry::Code.from_file(f.path).code_type).to eq :unknown
2012-01-15 01:06:24 -05:00
end
end
2014-09-12 11:09:18 -04:00
specify 'check the extension' do
temp_file('.c') do |f|
2015-03-10 16:49:29 -04:00
expect(Pry::Code.from_file(f.path).code_type).to eq :c
2012-01-15 01:06:24 -05:00
end
end
2014-09-12 11:09:18 -04:00
specify 'raise an error if the file doesn\'t exist' do
expect do
2012-01-15 00:19:30 -05:00
Pry::Code.from_file('/knalkjsdnalsd/alkjdlkq')
end.to raise_error MethodSource::SourceNotFoundError
2012-01-15 00:19:30 -05:00
end
2012-08-13 01:41:28 -04:00
2014-09-12 11:09:18 -04:00
specify 'check for files relative to origin pwd' do
2012-11-15 00:37:37 -05:00
Dir.chdir('spec') do |f|
2015-03-10 16:49:29 -04:00
expect(Pry::Code.from_file('spec/' + File.basename(__FILE__)).code_type).to eq :ruby
2012-08-13 01:41:28 -04:00
end
end
2014-09-12 11:09:18 -04:00
specify 'check for Ruby files relative to origin pwd with `.rb` omitted' do
Dir.chdir('spec') do |f|
2015-03-10 16:49:29 -04:00
expect(Pry::Code.from_file('spec/' + File.basename(__FILE__, '.*')).code_type).to eq :ruby
end
end
2014-09-12 11:09:18 -04:00
specify 'find files that are relative to the current working directory' do
2012-11-15 00:37:37 -05:00
Dir.chdir('spec') do |f|
2015-03-10 16:49:29 -04:00
expect(Pry::Code.from_file(File.basename(__FILE__)).code_type).to eq :ruby
2012-08-13 01:41:28 -04:00
end
end
describe 'find Ruby files relative to $LOAD_PATH' do
before do
$LOAD_PATH << 'spec/fixtures'
end
after do
$LOAD_PATH.delete 'spec/fixtures'
end
it 'finds files with `.rb` extension' do
2015-03-10 16:49:29 -04:00
expect(Pry::Code.from_file('slinky.rb').code_type).to eq :ruby
end
it 'finds files with `.rb` omitted' do
2015-03-10 16:49:29 -04:00
expect(Pry::Code.from_file('slinky').code_type).to eq :ruby
end
it 'finds files in a relative directory with `.rb` extension' do
2015-03-10 16:49:29 -04:00
expect(Pry::Code.from_file('../helper.rb').code_type).to eq :ruby
end
it 'finds files in a relative directory with `.rb` omitted' do
2015-03-10 16:49:29 -04:00
expect(Pry::Code.from_file('../helper').code_type).to eq :ruby
end
it "doesn't confuse files with the same name, but without an extension" do
2015-03-10 16:49:29 -04:00
expect(Pry::Code.from_file('cat_load_path').code_type).to eq :unknown
end
it "doesn't confuse files with the same name, but with an extension" do
2015-03-10 16:49:29 -04:00
expect(Pry::Code.from_file('cat_load_path.rb').code_type).to eq :ruby
end
2015-01-15 01:07:32 -05:00
it "recognizes special Ruby files without extensions" do
2015-03-10 16:49:29 -04:00
expect(Pry::Code.from_file('Gemfile').code_type).to eq :ruby
2015-01-15 01:07:32 -05:00
end
end
2012-01-15 00:19:30 -05:00
end
describe '.from_method' do
2014-09-12 11:09:18 -04:00
specify 'read lines from a method\'s definition' do
2012-01-15 00:19:30 -05:00
m = Pry::Method.from_obj(Pry, :load_history)
2015-03-10 16:49:29 -04:00
expect(Pry::Code.from_method(m).length).to be > 0
2012-01-15 00:19:30 -05:00
end
end
describe '#initialize' do
before do
@str = Pry::Helpers::CommandHelpers.unindent <<-CODE
def hay
:guys
end
CODE
@array = ['def hay', ' :guys', 'end']
end
2014-09-12 11:09:18 -04:00
specify 'break a string into lines' do
2015-03-10 16:49:29 -04:00
expect(Pry::Code.new(@str).length).to eq 3
2012-01-15 00:19:30 -05:00
end
2014-09-12 11:09:18 -04:00
specify 'accept an array' do
2015-03-10 16:49:29 -04:00
expect(Pry::Code.new(@array).length).to eq 3
2012-01-15 00:19:30 -05:00
end
2014-09-12 11:09:18 -04:00
it 'an array or string specify produce an equivalent object' do
2015-03-10 16:49:29 -04:00
expect(Pry::Code.new(@str)).to eq Pry::Code.new(@array)
2012-01-15 00:19:30 -05:00
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
2014-09-12 11:09:18 -04:00
specify 'work with an inclusive range' do
2012-01-15 00:19:30 -05:00
@code = @code.between(1..3)
2015-03-10 16:49:29 -04:00
expect(@code.length).to eq 3
expect(@code).to match(/\Aclass MyProgram/)
expect(@code).to match(/world!'\Z/)
2012-01-15 00:19:30 -05:00
end
2014-09-12 11:09:18 -04:00
specify 'default to an inclusive range' do
2012-01-15 00:19:30 -05:00
@code = @code.between(3, 5)
2015-03-10 16:49:29 -04:00
expect(@code.length).to eq 3
2012-01-15 00:19:30 -05:00
end
2014-09-12 11:09:18 -04:00
specify 'work with an exclusive range' do
2012-01-15 00:19:30 -05:00
@code = @code.between(2...4)
2015-03-10 16:49:29 -04:00
expect(@code.length).to eq 2
expect(@code).to match(/\A def self/)
expect(@code).to match(/world!'\Z/)
2012-01-15 00:19:30 -05:00
end
2014-09-12 11:09:18 -04:00
specify 'use real line numbers for positive indices' do
@code = @code.after(3, 3)
@code = @code.between(4, 4)
2015-03-10 16:49:29 -04:00
expect(@code.length).to eq 1
expect(@code).to match(/\A end\Z/)
end
2012-01-15 00:19:30 -05:00
end
describe '#before' do
2014-09-12 11:09:18 -04:00
specify 'work' do
2012-01-15 00:19:30 -05:00
@code = @code.before(3, 1)
2015-03-10 16:49:29 -04:00
expect(@code).to match(/\A def self\.main\Z/)
2012-01-15 00:19:30 -05:00
end
end
describe '#around' do
2014-09-12 11:09:18 -04:00
specify 'work' do
2012-01-15 00:19:30 -05:00
@code = @code.around(3, 1)
2015-03-10 16:49:29 -04:00
expect(@code.length).to eq 3
expect(@code).to match(/\A def self/)
expect(@code).to match(/ end\Z/)
2012-01-15 00:19:30 -05:00
end
end
describe '#after' do
2014-09-12 11:09:18 -04:00
specify 'work' do
2012-01-15 00:19:30 -05:00
@code = @code.after(3, 1)
2015-03-10 16:49:29 -04:00
expect(@code).to match(/\A end\Z/)
2012-01-15 00:19:30 -05:00
end
end
describe '#grep' do
2014-09-12 11:09:18 -04:00
specify 'work' do
2012-01-15 00:19:30 -05:00
@code = @code.grep(/end/)
2015-03-10 16:49:29 -04:00
expect(@code.length).to eq 2
2012-01-15 00:19:30 -05:00
end
end
end
describe 'formatters' do
describe '#with_line_numbers' do
2014-09-12 11:09:18 -04:00
specify 'show line numbers' do
2012-01-15 00:19:30 -05:00
@code = @code.with_line_numbers
2015-03-10 16:49:29 -04:00
expect(@code).to match(/1:/)
2012-01-15 00:19:30 -05:00
end
2014-09-12 11:09:18 -04:00
specify '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)
2015-03-10 16:49:29 -04:00
expect(@code).not_to match(/1:/)
2012-01-15 00:19:30 -05:00
end
end
describe '#with_marker' do
2014-09-12 11:09:18 -04:00
specify 'show a marker in the right place' do
2012-01-15 00:19:30 -05:00
@code = @code.with_marker(2)
2015-03-10 16:49:29 -04:00
expect(@code).to match(/^ => def self/)
2012-01-15 00:19:30 -05:00
end
2014-09-12 11:09:18 -04:00
specify 'disable the marker when falsy' do
2012-01-15 00:19:30 -05:00
@code = @code.with_marker(2)
@code = @code.with_marker(false)
2015-03-10 16:49:29 -04:00
expect(@code).to match(/^ def self/)
2012-01-15 00:19:30 -05:00
end
end
describe '#with_indentation' do
2014-09-12 11:09:18 -04:00
specify 'indent the text' do
2012-01-15 00:19:30 -05:00
@code = @code.with_indentation(2)
2015-03-10 16:49:29 -04:00
expect(@code).to match(/^ def self/)
2012-01-15 00:19:30 -05:00
end
2014-09-12 11:09:18 -04:00
specify 'disable the indentation when falsy' do
2012-01-15 00:19:30 -05:00
@code = @code.with_indentation(2)
@code = @code.with_indentation(false)
2015-03-10 16:49:29 -04:00
expect(@code).to match(/^ def self/)
2012-01-15 00:19:30 -05:00
end
end
end
2012-01-15 03:55:40 -05:00
describe 'composition' do
describe 'grep and with_line_numbers' do
2014-09-12 11:09:18 -04:00
specify 'work' do
2012-01-15 03:55:40 -05:00
@code = @code.grep(/end/).with_line_numbers
2015-03-10 16:49:29 -04:00
expect(@code).to match(/\A4: end/)
expect(@code).to match(/5: end\Z/)
2012-01-15 03:55:40 -05:00
end
end
describe 'grep and before and with_line_numbers' do
2014-09-12 11:09:18 -04:00
specify 'work' do
2012-01-15 03:55:40 -05:00
@code = @code.grep(/e/).before(5, 5).with_line_numbers
2015-03-10 16:49:29 -04:00
expect(@code).to match(/\A2: def self.main\n3:/)
expect(@code).to match(/4: end\Z/)
2012-01-15 03:55:40 -05:00
end
end
describe 'before and after' do
2014-09-12 11:09:18 -04:00
specify 'work' do
2012-01-15 03:55:40 -05:00
@code = @code.before(4, 2).after(2)
2015-03-10 16:49:29 -04:00
expect(@code).to eq " puts 'Hello, world!'"
2012-01-15 03:55:40 -05:00
end
end
end
2012-01-15 00:19:30 -05:00
end
end