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

added tests for module candidate API

This commit is contained in:
John Mair 2012-06-23 20:14:10 +12:00
parent 0ee3c4af14
commit e23eba98b5
6 changed files with 114 additions and 4 deletions

View file

@ -68,6 +68,10 @@ class Pry
# Attempt to extract the source code for module (or class) `mod`.
#
# @param [Module, Class] mod The module (or class) of interest.
# @param [Fixnum, nil] start_line The line number to start on, or nil to use the
# method's original line numbers.
# @param [Fixnum] candidate_rank The module candidate (by rank)
# to use (see `Pry::WrappedModule::Candidate` for more information).
# @return [Code]
def from_module(mod, start_line=nil, candidate_rank=0)
candidate = Pry::WrappedModule(mod).candidate(candidate_rank)

View file

@ -6,8 +6,7 @@ class Pry
# This class represents a single candidate for a module/class definition.
# It provides access to the source, documentation, line and file
# for a monkeypatch (reopening) of a class/module. All candidates
# are
# for a monkeypatch (reopening) of a class/module.
class Candidate
include Pry::Helpers::DocumentationHelpers
extend Forwardable
@ -25,6 +24,7 @@ class Pry
def_delegators :@wrapper, *to_delegate
private *to_delegate
# @raise [Pry::CommandError] If `rank` is out of bounds.
# @param [Pry::WrappedModule] wrapper The associated
# `Pry::WrappedModule` instance that owns the candidates.
# @param [Fixnum] rank The rank of the candidate to
@ -35,7 +35,9 @@ class Pry
def initialize(wrapper, rank)
@wrapper = wrapper
if rank > (number_of_candidates - 1)
if number_of_candidates <= 0
raise CommandError, "Cannot find a definition for #{name} module!"
elsif rank > (number_of_candidates - 1)
raise CommandError, "No such module candidate. Allowed candidates range is from 0 to #{number_of_candidates - 1}"
end

View file

@ -175,7 +175,7 @@ class Pry
# the candidate API gives you access to the module definition
# representing each of those reopenings.
# @raise [Pry::CommandError] If the `rank` is out of range. That
# is greater than `number_of_candidates - 1`.
# is greater than `number_of_candidates - 1`.
# @param [Fixnum] rank
# @return [Pry::WrappedModule::Candidate]
def candidate(rank)

11
test/candidate_helper1.rb Normal file
View file

@ -0,0 +1,11 @@
# rank 0
class CandidateTest
def test1
end
def test2
end
def test3
end
end

View file

@ -0,0 +1,8 @@
# rank 1
class CandidateTest
def test4
end
def test5
end
end

View file

@ -8,6 +8,91 @@ describe Pry::WrappedModule do
end
end
describe "candidates" do
before do
class Host
source_files = [File.join(File.dirname(__FILE__), "candidate_helper1.rb"),
File.join(File.dirname(__FILE__), "candidate_helper2.rb")]
source_files.each do |file|
binding.eval File.read(file), file, 1
end
# rank 2
class CandidateTest
def test6
end
end
end
end
describe "number_of_candidates" do
it 'should return the correct number of candidates' do
Pry::WrappedModule(Host::CandidateTest).number_of_candidates.should == 3
end
end
describe "ordering of candidates" do
it 'should return class with largest number of methods as primary candidate' do
Pry::WrappedModule(Host::CandidateTest).candidate(0).file.should =~ /helper1/
end
it 'should return class with second largest number of methods as second ranked candidate' do
Pry::WrappedModule(Host::CandidateTest).candidate(1).file.should =~ /helper2/
end
it 'should return class with third largest number of methods as third ranked candidate' do
Pry::WrappedModule(Host::CandidateTest).candidate(2).file.should =~ /#{__FILE__}/
end
it 'should raise when trying to access non-existent candidate' do
lambda { Pry::WrappedModule(Host::CandidateTest).candidate(3) }.should.raise Pry::CommandError
end
end
describe "source" do
it 'should return primary candidates source by default' do
wm = Pry::WrappedModule(Host::CandidateTest)
wm.source.should == wm.candidate(0).source
end
it 'should return source for highest ranked candidate' do
Pry::WrappedModule(Host::CandidateTest).candidate(0).source.should =~ /test1/
end
it 'should return source for second ranked candidate' do
Pry::WrappedModule(Host::CandidateTest).candidate(1).source.should =~ /test4/
end
it 'should return source for third ranked candidate' do
Pry::WrappedModule(Host::CandidateTest).candidate(2).source.should =~ /test6/
end
end
describe "doc" do
it 'should return primary candidates doc by default' do
wm = Pry::WrappedModule(Host::CandidateTest)
wm.doc.should == wm.candidate(0).doc
end
it 'should return doc for highest ranked candidate' do
Pry::WrappedModule(Host::CandidateTest).candidate(0).doc.should =~ /rank 0/
end
it 'should return doc for second ranked candidate' do
Pry::WrappedModule(Host::CandidateTest).candidate(1).doc.should =~ /rank 1/
end
it 'should return doc for third ranked candidate' do
Pry::WrappedModule(Host::CandidateTest).candidate(2).doc.should =~ /rank 2/
end
end
after do
Object.remove_const(:Host)
end
end
describe ".method_prefix" do
before do
Foo = Class.new