2014-03-14 00:31:24 -04:00
|
|
|
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
|
2012-12-07 17:08:49 -05:00
|
|
|
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
|
2012-12-07 17:08:49 -05:00
|
|
|
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
|
Switch test suite to RSpec
Removes Bacon and Mocha
Reasoning explained in this comment: https://github.com/pry/pry/issues/277#issuecomment-51708712
Mostly this went smoothly. There were a few errors that I fixed along
the way, e.g. tests that were failing but for various reasons still
passed. Should have documented them, but didn't think about it until
very near the end. But generaly, I remember 2 reasons this would happen:
`lambda { raise "omg" }.should.raise(RuntimeError, /not-omg/)` will pass
because the second argument is ignored by Bacon. And `1.should == 2`
will return false instead of raising an error when it is not in an it
block (e.g. if stuck in a describe block, that would just return false)
The only one that I felt unsure about was spec/helpers/table_spec.rb
`Pry::Helpers.tablify_or_one_line('head', %w(ing)).should == 'head: ing'`
This is wrong, but was not failing because it was in a describe block
instead of an it block. In reality, it returns `"head: ing\n"`,
I updated the test to reflect this, though I don't know for sure
this is the right thing to do
This will fail on master until https://github.com/pry/pry/pull/1281 is merged.
This makes https://github.com/pry/pry/pull/1278 unnecessary.
2014-08-10 18:26:47 -04:00
|
|
|
expect do
|
2012-01-15 00:19:30 -05:00
|
|
|
Pry::Code.from_file('/knalkjsdnalsd/alkjdlkq')
|
Switch test suite to RSpec
Removes Bacon and Mocha
Reasoning explained in this comment: https://github.com/pry/pry/issues/277#issuecomment-51708712
Mostly this went smoothly. There were a few errors that I fixed along
the way, e.g. tests that were failing but for various reasons still
passed. Should have documented them, but didn't think about it until
very near the end. But generaly, I remember 2 reasons this would happen:
`lambda { raise "omg" }.should.raise(RuntimeError, /not-omg/)` will pass
because the second argument is ignored by Bacon. And `1.should == 2`
will return false instead of raising an error when it is not in an it
block (e.g. if stuck in a describe block, that would just return false)
The only one that I felt unsure about was spec/helpers/table_spec.rb
`Pry::Helpers.tablify_or_one_line('head', %w(ing)).should == 'head: ing'`
This is wrong, but was not failing because it was in a describe block
instead of an it block. In reality, it returns `"head: ing\n"`,
I updated the test to reflect this, though I don't know for sure
this is the right thing to do
This will fail on master until https://github.com/pry/pry/pull/1281 is merged.
This makes https://github.com/pry/pry/pull/1278 unnecessary.
2014-08-10 18:26:47 -04:00
|
|
|
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
|
2013-11-14 01:47:56 -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
|
2013-11-14 01:47:56 -05:00
|
|
|
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
|
2013-11-13 23:25:11 -05:00
|
|
|
|
2013-11-23 15:08:53 -05:00
|
|
|
describe 'find Ruby files relative to $LOAD_PATH' do
|
2013-11-13 23:25:11 -05:00
|
|
|
before do
|
2013-11-23 15:08:53 -05:00
|
|
|
$LOAD_PATH << 'spec/fixtures'
|
2013-11-13 23:25:11 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
after do
|
2013-11-23 15:08:53 -05:00
|
|
|
$LOAD_PATH.delete 'spec/fixtures'
|
2013-11-13 23:25:11 -05:00
|
|
|
end
|
|
|
|
|
2013-11-23 15:08:53 -05:00
|
|
|
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
|
2013-11-13 23:25:11 -05:00
|
|
|
end
|
|
|
|
|
2013-11-23 15:08:53 -05:00
|
|
|
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
|
2013-11-14 01:47:56 -05:00
|
|
|
end
|
|
|
|
|
2013-11-23 15:08:53 -05:00
|
|
|
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
|
2013-11-13 23:25:11 -05:00
|
|
|
end
|
2013-11-14 01:47:56 -05:00
|
|
|
|
2013-11-23 15:08:53 -05:00
|
|
|
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
|
2013-11-14 01:47:56 -05:00
|
|
|
end
|
2013-11-23 15:13:54 -05:00
|
|
|
|
|
|
|
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
|
2013-11-23 15:13:54 -05:00
|
|
|
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
|
2013-11-23 15:13:54 -05:00
|
|
|
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
|
2013-11-12 01:54:31 -05:00
|
|
|
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
|
2012-01-15 17:16:18 -05:00
|
|
|
|
2014-09-12 11:09:18 -04:00
|
|
|
specify 'use real line numbers for positive indices' do
|
2012-01-15 17:16:18 -05:00
|
|
|
@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/)
|
2012-01-15 17:16:18 -05:00
|
|
|
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
|