diff options
| -rw-r--r-- | lib/method_source.rb | 14 | ||||
| -rw-r--r-- | test/test.rb | 4 |
2 files changed, 10 insertions, 8 deletions
diff --git a/lib/method_source.rb b/lib/method_source.rb index aa2b34a..0353144 100644 --- a/lib/method_source.rb +++ b/lib/method_source.rb @@ -7,6 +7,9 @@ require "#{direc}/method_source/version" require "#{direc}/method_source/source_location" module MethodSource + + class SourceNotFoundError < StandardError; end + # Determine if a string of code is a valid Ruby expression. # @param [String] code The code to validate. # @return [Boolean] Whether or not the code is a valid Ruby expression. @@ -51,8 +54,7 @@ module MethodSource code rescue Errno::ENOENT - # source_location[0] of evaled methods would return (eval) if __FILE__ and __LINE__ is not given. File.readlines "(eval)" would raise ENOENT - nil + raise SourceNotFoundError, "Cannot get source code located at file: #{source_location[0]}" end # @param [Array] source_location The array containing file_name [String], line [Fixnum] @@ -143,9 +145,9 @@ module MethodSource if respond_to?(:source_location) source = MethodSource.source_helper(source_location) - raise "Cannot locate source for this method: #{name}" if !source + raise SourceNotFoundError, "Cannot locate source for this method: #{name}" if !source else - raise "#{self.class}#source not supported by this Ruby version (#{RUBY_VERSION})" + raise SourceNotFoundError, "#{self.class}#source not supported by this Ruby version (#{RUBY_VERSION})" end source @@ -162,9 +164,9 @@ module MethodSource if respond_to?(:source_location) comment = MethodSource.comment_helper(source_location) - raise "Cannot locate source for this method: #{name}" if !comment + raise SourceNotFoundError, "Cannot locate source for this method: #{name}" if !comment else - raise "#{self.class}#comment not supported by this Ruby version (#{RUBY_VERSION})" + raise SourceNotFoundError, "#{self.class}#comment not supported by this Ruby version (#{RUBY_VERSION})" end comment diff --git a/test/test.rb b/test/test.rb index 3fd1597..4743a50 100644 --- a/test/test.rb +++ b/test/test.rb @@ -77,12 +77,12 @@ describe MethodSource do end it "should raise error for evaled methods that do not pass __FILE__ and __LINE__ + 1 as its arguments" do - lambda { M.instance_method(:name_three).source }.should.raise RuntimeError + lambda { M.instance_method(:name_three).source }.should.raise MethodSource::SourceNotFoundError end if !is_rbx? it 'should raise for C methods' do - lambda { method(:puts).source }.should.raise RuntimeError + lambda { method(:puts).source }.should.raise MethodSource::SourceNotFoundError end end end |
