put gemcache for methodsource back
parent
ac6fdf24a2
commit
6abbbeb3ca
|
@ -0,0 +1,17 @@
|
|||
rvm:
|
||||
- 1.8.7
|
||||
- 1.9.2
|
||||
- 1.9.3
|
||||
- ree
|
||||
- rbx-18mode
|
||||
- rbx-19mode
|
||||
- jruby
|
||||
|
||||
notifications:
|
||||
irc: "irc.freenode.org#pry"
|
||||
recipients:
|
||||
- jrmair@gmail.com
|
||||
|
||||
branches:
|
||||
only:
|
||||
- master
|
|
@ -0,0 +1 @@
|
|||
-m markdown
|
|
@ -0,0 +1,2 @@
|
|||
source :rubygems
|
||||
gemspec
|
|
@ -0,0 +1,25 @@
|
|||
License
|
||||
-------
|
||||
|
||||
(The MIT License)
|
||||
|
||||
Copyright (c) 2011 John Mair (banisterfiend)
|
||||
|
||||
Permission is hereby granted, free of charge, to any person obtaining
|
||||
a copy of this software and associated documentation files (the
|
||||
'Software'), to deal in the Software without restriction, including
|
||||
without limitation the rights to use, copy, modify, merge, publish,
|
||||
distribute, sublicense, and/or sell copies of the Software, and to
|
||||
permit persons to whom the Software is furnished to do so, subject to
|
||||
the following conditions:
|
||||
|
||||
The above copyright notice and this permission notice shall be
|
||||
included in all copies or substantial portions of the Software.
|
||||
|
||||
THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND,
|
||||
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
||||
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
|
||||
IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
|
||||
CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
|
||||
TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
|
||||
SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
|
@ -0,0 +1,91 @@
|
|||
method_source
|
||||
=============
|
||||
|
||||
(C) John Mair (banisterfiend) 2011
|
||||
|
||||
_retrieve the sourcecode for a method_
|
||||
|
||||
*NOTE:* This simply utilizes `Method#source_location`; it
|
||||
does not access the live AST.
|
||||
|
||||
`method_source` is a utility to return a method's sourcecode as a
|
||||
Ruby string. Also returns `Proc` and `Lambda` sourcecode.
|
||||
|
||||
Method comments can also be extracted using the `comment` method.
|
||||
|
||||
It is written in pure Ruby (no C).
|
||||
|
||||
* Some Ruby 1.8 support now available.
|
||||
* Support for MRI, RBX, JRuby, REE
|
||||
|
||||
`method_source` provides the `source` and `comment` methods to the `Method` and
|
||||
`UnboundMethod` and `Proc` classes.
|
||||
|
||||
* Install the [gem](https://rubygems.org/gems/method_source): `gem install method_source`
|
||||
* Read the [documentation](http://rdoc.info/github/banister/method_source/master/file/README.markdown)
|
||||
* See the [source code](http://github.com/banister/method_source)
|
||||
|
||||
Example: display method source
|
||||
------------------------------
|
||||
|
||||
Set.instance_method(:merge).source.display
|
||||
# =>
|
||||
def merge(enum)
|
||||
if enum.instance_of?(self.class)
|
||||
@hash.update(enum.instance_variable_get(:@hash))
|
||||
else
|
||||
do_with_enum(enum) { |o| add(o) }
|
||||
end
|
||||
|
||||
self
|
||||
end
|
||||
|
||||
Example: display method comments
|
||||
--------------------------------
|
||||
|
||||
Set.instance_method(:merge).comment.display
|
||||
# =>
|
||||
# Merges the elements of the given enumerable object to the set and
|
||||
# returns self.
|
||||
|
||||
Limitations:
|
||||
------------
|
||||
|
||||
* Occasional strange behaviour in Ruby 1.8
|
||||
* Cannot return source for C methods.
|
||||
* Cannot return source for dynamically defined methods.
|
||||
|
||||
Special Thanks
|
||||
--------------
|
||||
|
||||
[Adam Sanderson](https://github.com/adamsanderson) for `comment` functionality.
|
||||
|
||||
[Dmitry Elastic](https://github.com/dmitryelastic) for the brilliant Ruby 1.8 `source_location` hack.
|
||||
|
||||
[Samuel Kadolph](https://github.com/samuelkadolph) for the JRuby 1.8 `source_location`.
|
||||
|
||||
License
|
||||
-------
|
||||
|
||||
(The MIT License)
|
||||
|
||||
Copyright (c) 2011 John Mair (banisterfiend)
|
||||
|
||||
Permission is hereby granted, free of charge, to any person obtaining
|
||||
a copy of this software and associated documentation files (the
|
||||
'Software'), to deal in the Software without restriction, including
|
||||
without limitation the rights to use, copy, modify, merge, publish,
|
||||
distribute, sublicense, and/or sell copies of the Software, and to
|
||||
permit persons to whom the Software is furnished to do so, subject to
|
||||
the following conditions:
|
||||
|
||||
The above copyright notice and this permission notice shall be
|
||||
included in all copies or substantial portions of the Software.
|
||||
|
||||
THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND,
|
||||
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
||||
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
|
||||
IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
|
||||
CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
|
||||
TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
|
||||
SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
|
@ -0,0 +1,76 @@
|
|||
dlext = Config::CONFIG['DLEXT']
|
||||
direc = File.dirname(__FILE__)
|
||||
|
||||
require 'rake/clean'
|
||||
require 'rake/gempackagetask'
|
||||
require "#{direc}/lib/method_source/version"
|
||||
|
||||
CLOBBER.include("**/*.#{dlext}", "**/*~", "**/*#*", "**/*.log", "**/*.o")
|
||||
CLEAN.include("ext/**/*.#{dlext}", "ext/**/*.log", "ext/**/*.o",
|
||||
"ext/**/*~", "ext/**/*#*", "ext/**/*.obj", "**/*.rbc",
|
||||
"ext/**/*.def", "ext/**/*.pdb", "**/*_flymake*.*", "**/*_flymake")
|
||||
|
||||
def apply_spec_defaults(s)
|
||||
s.name = "method_source"
|
||||
s.summary = "retrieve the sourcecode for a method"
|
||||
s.version = MethodSource::VERSION
|
||||
s.date = Time.now.strftime '%Y-%m-%d'
|
||||
s.author = "John Mair (banisterfiend)"
|
||||
s.email = 'jrmair@gmail.com'
|
||||
s.description = s.summary
|
||||
s.require_path = 'lib'
|
||||
|
||||
s.add_development_dependency("bacon","~>1.1.0")
|
||||
s.add_development_dependency("rake", "~>0.9")
|
||||
s.homepage = "http://banisterfiend.wordpress.com"
|
||||
s.has_rdoc = 'yard'
|
||||
s.files = `git ls-files`.split("\n")
|
||||
s.test_files = `git ls-files -- test/*`.split("\n")
|
||||
end
|
||||
|
||||
task :test do
|
||||
sh "bacon -q #{direc}/test/test.rb"
|
||||
end
|
||||
|
||||
desc "reinstall gem"
|
||||
task :reinstall => :gems do
|
||||
sh "gem uninstall method_source" rescue nil
|
||||
sh "gem install #{direc}/pkg/method_source-#{MethodSource::VERSION}.gem"
|
||||
end
|
||||
|
||||
desc "Set up and run tests"
|
||||
task :default => [:test]
|
||||
|
||||
namespace :ruby do
|
||||
spec = Gem::Specification.new do |s|
|
||||
apply_spec_defaults(s)
|
||||
s.platform = Gem::Platform::RUBY
|
||||
end
|
||||
|
||||
Rake::GemPackageTask.new(spec) do |pkg|
|
||||
pkg.need_zip = false
|
||||
pkg.need_tar = false
|
||||
end
|
||||
|
||||
desc "Generate gemspec file"
|
||||
task :gemspec do
|
||||
File.open("#{spec.name}.gemspec", "w") do |f|
|
||||
f << spec.to_ruby
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
desc "build all platform gems at once"
|
||||
task :gems => [:rmgems, "ruby:gem"]
|
||||
|
||||
desc "remove all platform gems"
|
||||
task :rmgems => ["ruby:clobber_package"]
|
||||
|
||||
desc "build and push latest gems"
|
||||
task :pushgems => :gems do
|
||||
chdir("#{direc}/pkg") do
|
||||
Dir["*.gem"].each do |gemfile|
|
||||
sh "gem push #{gemfile}"
|
||||
end
|
||||
end
|
||||
end
|
|
@ -0,0 +1,163 @@
|
|||
# (C) John Mair (banisterfiend) 2011
|
||||
# MIT License
|
||||
|
||||
direc = File.dirname(__FILE__)
|
||||
|
||||
require "#{direc}/method_source/version"
|
||||
require "#{direc}/method_source/source_location"
|
||||
|
||||
module MethodSource
|
||||
# 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.
|
||||
# @example
|
||||
# valid_expression?("class Hello") #=> false
|
||||
# valid_expression?("class Hello; end") #=> true
|
||||
def self.valid_expression?(str)
|
||||
if defined?(Rubinius::Melbourne19) && RUBY_VERSION =~ /^1\.9/
|
||||
Rubinius::Melbourne19.parse_string(str)
|
||||
elsif defined?(Rubinius::Melbourne)
|
||||
Rubinius::Melbourne.parse_string(str)
|
||||
else
|
||||
catch(:valid) {
|
||||
eval("BEGIN{throw :valid}\n#{str}")
|
||||
}
|
||||
end
|
||||
true
|
||||
rescue SyntaxError
|
||||
false
|
||||
end
|
||||
|
||||
# Helper method responsible for extracting method body.
|
||||
# Defined here to avoid polluting `Method` class.
|
||||
# @param [Array] source_location The array returned by Method#source_location
|
||||
# @return [File] The opened source file
|
||||
def self.source_helper(source_location)
|
||||
return nil if !source_location.is_a?(Array)
|
||||
|
||||
file_name, line = source_location
|
||||
File.open(file_name) do |file|
|
||||
(line - 1).times { file.readline }
|
||||
|
||||
code = ""
|
||||
loop do
|
||||
val = file.readline
|
||||
code << val
|
||||
|
||||
return code if valid_expression?(code)
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
# Helper method responsible for opening source file and buffering up
|
||||
# the comments for a specified method. Defined here to avoid polluting
|
||||
# `Method` class.
|
||||
# @param [Array] source_location The array returned by Method#source_location
|
||||
# @return [String] The comments up to the point of the method.
|
||||
def self.comment_helper(source_location)
|
||||
return nil if !source_location.is_a?(Array)
|
||||
|
||||
file_name, line = source_location
|
||||
File.open(file_name) do |file|
|
||||
buffer = ""
|
||||
(line - 1).times do
|
||||
line = file.readline
|
||||
# Add any line that is a valid ruby comment,
|
||||
# but clear as soon as we hit a non comment line.
|
||||
if (line =~ /^\s*#/) || (line =~ /^\s*$/)
|
||||
buffer << line.lstrip
|
||||
else
|
||||
buffer.replace("")
|
||||
end
|
||||
end
|
||||
|
||||
buffer
|
||||
end
|
||||
end
|
||||
|
||||
# This module is to be included by `Method` and `UnboundMethod` and
|
||||
# provides the `#source` functionality
|
||||
module MethodExtensions
|
||||
|
||||
# We use the included hook to patch Method#source on rubinius.
|
||||
# We need to use the included hook as Rubinius defines a `source`
|
||||
# on Method so including a module will have no effect (as it's
|
||||
# higher up the MRO).
|
||||
# @param [Class] klass The class that includes the module.
|
||||
def self.included(klass)
|
||||
if klass.method_defined?(:source) && Object.const_defined?(:RUBY_ENGINE) &&
|
||||
RUBY_ENGINE =~ /rbx/
|
||||
|
||||
klass.class_eval do
|
||||
orig_source = instance_method(:source)
|
||||
|
||||
define_method(:source) do
|
||||
begin
|
||||
super
|
||||
rescue
|
||||
orig_source.bind(self).call
|
||||
end
|
||||
end
|
||||
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
# Return the sourcecode for the method as a string
|
||||
# (This functionality is only supported in Ruby 1.9 and above)
|
||||
# @return [String] The method sourcecode as a string
|
||||
# @example
|
||||
# Set.instance_method(:clear).source.display
|
||||
# =>
|
||||
# def clear
|
||||
# @hash.clear
|
||||
# self
|
||||
# end
|
||||
def source
|
||||
if respond_to?(:source_location)
|
||||
source = MethodSource.source_helper(source_location)
|
||||
|
||||
raise "Cannot locate source for this method: #{name}" if !source
|
||||
else
|
||||
raise "#{self.class}#source not supported by this Ruby version (#{RUBY_VERSION})"
|
||||
end
|
||||
|
||||
source
|
||||
end
|
||||
|
||||
# Return the comments associated with the method as a string.
|
||||
# (This functionality is only supported in Ruby 1.9 and above)
|
||||
# @return [String] The method's comments as a string
|
||||
# @example
|
||||
# Set.instance_method(:clear).comment.display
|
||||
# =>
|
||||
# # Removes all elements and returns self.
|
||||
def comment
|
||||
if respond_to?(:source_location)
|
||||
comment = MethodSource.comment_helper(source_location)
|
||||
|
||||
raise "Cannot locate source for this method: #{name}" if !comment
|
||||
else
|
||||
raise "#{self.class}#comment not supported by this Ruby version (#{RUBY_VERSION})"
|
||||
end
|
||||
|
||||
comment
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
class Method
|
||||
include MethodSource::SourceLocation::MethodExtensions
|
||||
include MethodSource::MethodExtensions
|
||||
end
|
||||
|
||||
class UnboundMethod
|
||||
include MethodSource::SourceLocation::UnboundMethodExtensions
|
||||
include MethodSource::MethodExtensions
|
||||
end
|
||||
|
||||
class Proc
|
||||
include MethodSource::SourceLocation::ProcExtensions
|
||||
include MethodSource::MethodExtensions
|
||||
end
|
||||
|
|
@ -0,0 +1,138 @@
|
|||
module MethodSource
|
||||
module ReeSourceLocation
|
||||
# Ruby enterprise edition provides all the information that's
|
||||
# needed, in a slightly different way.
|
||||
def source_location
|
||||
[__file__, __line__] rescue nil
|
||||
end
|
||||
end
|
||||
|
||||
module SourceLocation
|
||||
module MethodExtensions
|
||||
if Proc.method_defined? :__file__
|
||||
include ReeSourceLocation
|
||||
|
||||
elsif defined?(RUBY_ENGINE) && RUBY_ENGINE =~ /jruby/
|
||||
require 'java'
|
||||
|
||||
# JRuby version source_location hack
|
||||
# @return [Array] A two element array containing the source location of the method
|
||||
def source_location
|
||||
to_java.source_location(Thread.current.to_java.getContext())
|
||||
end
|
||||
else
|
||||
|
||||
|
||||
def trace_func(event, file, line, id, binding, classname)
|
||||
return unless event == 'call'
|
||||
set_trace_func nil
|
||||
|
||||
@file, @line = file, line
|
||||
raise :found
|
||||
end
|
||||
|
||||
private :trace_func
|
||||
|
||||
# Return the source location of a method for Ruby 1.8.
|
||||
# @return [Array] A two element array. First element is the
|
||||
# file, second element is the line in the file where the
|
||||
# method definition is found.
|
||||
def source_location
|
||||
if @file.nil?
|
||||
args =[*(1..(arity<-1 ? -arity-1 : arity ))]
|
||||
|
||||
set_trace_func method(:trace_func).to_proc
|
||||
call(*args) rescue nil
|
||||
set_trace_func nil
|
||||
@file = File.expand_path(@file) if @file && File.exist?(File.expand_path(@file))
|
||||
end
|
||||
return [@file, @line] if File.exist?(@file.to_s)
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
module ProcExtensions
|
||||
if Proc.method_defined? :__file__
|
||||
include ReeSourceLocation
|
||||
|
||||
elsif defined?(RUBY_ENGINE) && RUBY_ENGINE =~ /rbx/
|
||||
|
||||
# Return the source location for a Proc (Rubinius only)
|
||||
# @return [Array] A two element array. First element is the
|
||||
# file, second element is the line in the file where the
|
||||
# proc definition is found.
|
||||
def source_location
|
||||
[block.file.to_s, block.line]
|
||||
end
|
||||
else
|
||||
|
||||
# Return the source location for a Proc (in implementations
|
||||
# without Proc#source_location)
|
||||
# @return [Array] A two element array. First element is the
|
||||
# file, second element is the line in the file where the
|
||||
# proc definition is found.
|
||||
def source_location
|
||||
self.to_s =~ /@(.*):(\d+)/
|
||||
[$1, $2.to_i]
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
module UnboundMethodExtensions
|
||||
if Proc.method_defined? :__file__
|
||||
include ReeSourceLocation
|
||||
|
||||
elsif defined?(RUBY_ENGINE) && RUBY_ENGINE =~ /jruby/
|
||||
require 'java'
|
||||
|
||||
# JRuby version source_location hack
|
||||
# @return [Array] A two element array containing the source location of the method
|
||||
def source_location
|
||||
to_java.source_location(Thread.current.to_java.getContext())
|
||||
end
|
||||
|
||||
else
|
||||
|
||||
|
||||
# Return the source location of an instance method for Ruby 1.8.
|
||||
# @return [Array] A two element array. First element is the
|
||||
# file, second element is the line in the file where the
|
||||
# method definition is found.
|
||||
def source_location
|
||||
klass = case owner
|
||||
when Class
|
||||
owner
|
||||
when Module
|
||||
method_owner = owner
|
||||
Class.new { include(method_owner) }
|
||||
end
|
||||
|
||||
# deal with immediate values
|
||||
case
|
||||
when klass == Symbol
|
||||
return :a.method(name).source_location
|
||||
when klass == Fixnum
|
||||
return 0.method(name).source_location
|
||||
when klass == TrueClass
|
||||
return true.method(name).source_location
|
||||
when klass == FalseClass
|
||||
return false.method(name).source_location
|
||||
when klass == NilClass
|
||||
return nil.method(name).source_location
|
||||
end
|
||||
|
||||
begin
|
||||
Object.instance_method(:method).bind(klass.allocate).call(name).source_location
|
||||
rescue TypeError
|
||||
|
||||
# Assume we are dealing with a Singleton Class:
|
||||
# 1. Get the instance object
|
||||
# 2. Forward the source_location lookup to the instance
|
||||
instance ||= ObjectSpace.each_object(owner).first
|
||||
Object.instance_method(:method).bind(instance).call(name).source_location
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
|
@ -0,0 +1,3 @@
|
|||
module MethodSource
|
||||
VERSION = "0.7.1"
|
||||
end
|
|
@ -0,0 +1,33 @@
|
|||
# -*- encoding: utf-8 -*-
|
||||
|
||||
Gem::Specification.new do |s|
|
||||
s.name = "method_source"
|
||||
s.version = "0.7.0"
|
||||
|
||||
s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
|
||||
s.authors = ["John Mair (banisterfiend)"]
|
||||
s.date = "2012-01-01"
|
||||
s.description = "retrieve the sourcecode for a method"
|
||||
s.email = "jrmair@gmail.com"
|
||||
s.files = [".gemtest", ".travis.yml", ".yardopts", "Gemfile", "LICENSE", "README.markdown", "Rakefile", "lib/method_source.rb", "lib/method_source/source_location.rb", "lib/method_source/version.rb", "method_source.gemspec", "test/test.rb", "test/test_helper.rb"]
|
||||
s.homepage = "http://banisterfiend.wordpress.com"
|
||||
s.require_paths = ["lib"]
|
||||
s.rubygems_version = "1.8.10"
|
||||
s.summary = "retrieve the sourcecode for a method"
|
||||
s.test_files = ["test/test.rb", "test/test_helper.rb"]
|
||||
|
||||
if s.respond_to? :specification_version then
|
||||
s.specification_version = 3
|
||||
|
||||
if Gem::Version.new(Gem::VERSION) >= Gem::Version.new('1.2.0') then
|
||||
s.add_development_dependency(%q<bacon>, ["~> 1.1.0"])
|
||||
s.add_development_dependency(%q<rake>, ["~> 0.9"])
|
||||
else
|
||||
s.add_dependency(%q<bacon>, ["~> 1.1.0"])
|
||||
s.add_dependency(%q<rake>, ["~> 0.9"])
|
||||
end
|
||||
else
|
||||
s.add_dependency(%q<bacon>, ["~> 1.1.0"])
|
||||
s.add_dependency(%q<rake>, ["~> 0.9"])
|
||||
end
|
||||
end
|
|
@ -0,0 +1,122 @@
|
|||
direc = File.dirname(__FILE__)
|
||||
|
||||
require 'rubygems'
|
||||
require 'bacon'
|
||||
require "#{direc}/../lib/method_source"
|
||||
require "#{direc}/test_helper"
|
||||
|
||||
describe MethodSource do
|
||||
|
||||
describe "source_location (testing 1.8 implementation)" do
|
||||
it 'should return correct source_location for a method' do
|
||||
method(:hello).source_location.first.should =~ /test_helper/
|
||||
end
|
||||
|
||||
it 'should not raise for immediate instance methods' do
|
||||
[Symbol, Fixnum, TrueClass, FalseClass, NilClass].each do |immediate_class|
|
||||
lambda { immediate_class.instance_method(:to_s).source_location }.should.not.raise
|
||||
end
|
||||
end
|
||||
|
||||
it 'should not raise for immediate methods' do
|
||||
[:a, 1, true, false, nil].each do |immediate|
|
||||
lambda { immediate.method(:to_s).source_location }.should.not.raise
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
before do
|
||||
@hello_module_source = " def hello; :hello_module; end\n"
|
||||
@hello_singleton_source = "def $o.hello; :hello_singleton; end\n"
|
||||
@hello_source = "def hello; :hello; end\n"
|
||||
@hello_comment = "# A comment for hello\n# It spans two lines and is indented by 2 spaces\n"
|
||||
@lambda_comment = "# This is a comment for MyLambda\n"
|
||||
@lambda_source = "MyLambda = lambda { :lambda }\n"
|
||||
@proc_source = "MyProc = Proc.new { :proc }\n"
|
||||
end
|
||||
|
||||
it 'should define methods on Method and UnboundMethod and Proc' do
|
||||
Method.method_defined?(:source).should == true
|
||||
UnboundMethod.method_defined?(:source).should == true
|
||||
Proc.method_defined?(:source).should == true
|
||||
end
|
||||
|
||||
describe "Methods" do
|
||||
it 'should return source for method' do
|
||||
method(:hello).source.should == @hello_source
|
||||
end
|
||||
|
||||
it 'should return source for a method defined in a module' do
|
||||
M.instance_method(:hello).source.should == @hello_module_source
|
||||
end
|
||||
|
||||
it 'should return source for a singleton method as an instance method' do
|
||||
class << $o; self; end.instance_method(:hello).source.should == @hello_singleton_source
|
||||
end
|
||||
|
||||
it 'should return source for a singleton method' do
|
||||
$o.method(:hello).source.should == @hello_singleton_source
|
||||
end
|
||||
|
||||
|
||||
it 'should return a comment for method' do
|
||||
method(:hello).comment.should == @hello_comment
|
||||
end
|
||||
|
||||
|
||||
if !is_rbx?
|
||||
it 'should raise for C methods' do
|
||||
lambda { method(:puts).source }.should.raise RuntimeError
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
# if RUBY_VERSION =~ /1.9/ || is_rbx?
|
||||
describe "Lambdas and Procs" do
|
||||
it 'should return source for proc' do
|
||||
MyProc.source.should == @proc_source
|
||||
end
|
||||
|
||||
it 'should return an empty string if there is no comment' do
|
||||
MyProc.comment.should == ''
|
||||
end
|
||||
|
||||
it 'should return source for lambda' do
|
||||
MyLambda.source.should == @lambda_source
|
||||
end
|
||||
|
||||
it 'should return comment for lambda' do
|
||||
MyLambda.comment.should == @lambda_comment
|
||||
end
|
||||
end
|
||||
# end
|
||||
describe "Comment tests" do
|
||||
before do
|
||||
@comment1 = "# a\n# b\n"
|
||||
@comment2 = "# a\n# b\n"
|
||||
@comment3 = "# a\n#\n# b\n"
|
||||
@comment4 = "# a\n# b\n"
|
||||
@comment5 = "# a\n# b\n# c\n# d\n"
|
||||
end
|
||||
|
||||
it "should correctly extract multi-line comments" do
|
||||
method(:comment_test1).comment.should == @comment1
|
||||
end
|
||||
|
||||
it "should correctly strip leading whitespace before comments" do
|
||||
method(:comment_test2).comment.should == @comment2
|
||||
end
|
||||
|
||||
it "should keep empty comment lines" do
|
||||
method(:comment_test3).comment.should == @comment3
|
||||
end
|
||||
|
||||
it "should ignore blank lines between comments" do
|
||||
method(:comment_test4).comment.should == @comment4
|
||||
end
|
||||
|
||||
it "should align all comments to same indent level" do
|
||||
method(:comment_test5).comment.should == @comment5
|
||||
end
|
||||
end
|
||||
end
|
|
@ -0,0 +1,50 @@
|
|||
def is_rbx?
|
||||
defined?(RUBY_ENGINE) && RUBY_ENGINE =~ /rbx/
|
||||
end
|
||||
|
||||
def jruby?
|
||||
defined?(RUBY_ENGINE) && RUBY_ENGINE =~ /jruby/
|
||||
end
|
||||
|
||||
|
||||
module M
|
||||
def hello; :hello_module; end
|
||||
end
|
||||
|
||||
$o = Object.new
|
||||
def $o.hello; :hello_singleton; end
|
||||
|
||||
# A comment for hello
|
||||
|
||||
# It spans two lines and is indented by 2 spaces
|
||||
def hello; :hello; end
|
||||
|
||||
# a
|
||||
# b
|
||||
def comment_test1; end
|
||||
|
||||
# a
|
||||
# b
|
||||
def comment_test2; end
|
||||
|
||||
# a
|
||||
#
|
||||
# b
|
||||
def comment_test3; end
|
||||
|
||||
# a
|
||||
|
||||
# b
|
||||
def comment_test4; end
|
||||
|
||||
|
||||
# a
|
||||
# b
|
||||
# c
|
||||
# d
|
||||
def comment_test5; end
|
||||
|
||||
# This is a comment for MyLambda
|
||||
MyLambda = lambda { :lambda }
|
||||
MyProc = Proc.new { :proc }
|
||||
|
Loading…
Reference in New Issue