This commit is contained in:
@@ -0,0 +1,43 @@
|
||||
require 'spec_helper'
|
||||
|
||||
describe MethodSource::CodeHelpers do
|
||||
before do
|
||||
@tester = Object.new.extend(MethodSource::CodeHelpers)
|
||||
end
|
||||
|
||||
[
|
||||
["p = '", "'"],
|
||||
["def", "a", "(); end"],
|
||||
["p = <<FOO", "lots", "and", "lots of", "foo", "FOO"],
|
||||
["[", ":lets,", "'list',", "[/nested/", "], things ]"],
|
||||
["abc =~ /hello", "/"],
|
||||
["issue = %W/", "343/"],
|
||||
["pouts(<<HI, 'foo", "bar", "HI", "baz')"],
|
||||
["=begin", "no-one uses this syntax anymore...", "=end"],
|
||||
["puts 1, 2,", "3"],
|
||||
["puts 'hello'\\", "'world'"]
|
||||
].each do |lines|
|
||||
it "should not raise an error on broken lines: #{lines.join("\\n")}" do
|
||||
1.upto(lines.size - 1) do |i|
|
||||
expect(@tester.complete_expression?(lines[0...i].join("\n") + "\n")).to be_falsy
|
||||
end
|
||||
expect(@tester.complete_expression?(lines.join("\n"))).to be_truthy
|
||||
end
|
||||
end
|
||||
|
||||
[
|
||||
["end"],
|
||||
["puts )("],
|
||||
["1 1"],
|
||||
["puts :"]
|
||||
] + (RbConfig::CONFIG['ruby_install_name'] == 'rbx' ? [] : [
|
||||
["def", "method(1"], # in this case the syntax error is "expecting ')'".
|
||||
["o = Object.new.tap{ def o.render;","'MEH'", "}"] # in this case the syntax error is "expecting keyword_end".
|
||||
]).compact.each do |foo|
|
||||
it "should raise an error on invalid syntax like #{foo.inspect}" do
|
||||
expect {
|
||||
@tester.complete_expression?(foo.join("\n"))
|
||||
}.to raise_error(SyntaxError)
|
||||
end
|
||||
end
|
||||
end
|
||||
@@ -0,0 +1,157 @@
|
||||
require 'spec_helper'
|
||||
|
||||
describe MethodSource do
|
||||
|
||||
describe "source_location (testing 1.8 implementation)" do
|
||||
it 'should return correct source_location for a method' do
|
||||
expect(method(:hello).source_location.first).to match(/spec_helper/)
|
||||
end
|
||||
|
||||
it 'should not raise for immediate instance methods' do
|
||||
[Symbol, Integer, TrueClass, FalseClass, NilClass].each do |immediate_class|
|
||||
expect do
|
||||
immediate_class.instance_method(:to_s).source_location
|
||||
end.not_to raise_error
|
||||
end
|
||||
end
|
||||
|
||||
it 'should not raise for immediate methods' do
|
||||
[:a, 1, true, false, nil].each do |immediate|
|
||||
expect do
|
||||
immediate.method(:to_s).source_location
|
||||
end.not_to raise_error
|
||||
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"
|
||||
@module_comment = "# This is a comment for module\n"
|
||||
@class_comment = "# This is a comment for class\n"
|
||||
@lambda_source = "MyLambda = lambda { :lambda }\n"
|
||||
@proc_source = "MyProc = Proc.new { :proc }\n"
|
||||
@hello_instance_evaled_source = " def hello_\#{name}(*args)\n send_mesg(:\#{name}, *args)\n end\n"
|
||||
@hello_instance_evaled_source_2 = " def \#{name}_two()\n if 40 + 4\n 45\n end\n end\n"
|
||||
@hello_class_evaled_source = " def hello_\#{name}(*args)\n send_mesg(:\#{name}, *args)\n end\n"
|
||||
@hi_module_evaled_source = " def hi_\#{name}\n @var = \#{name}\n end\n"
|
||||
end
|
||||
|
||||
it 'should define methods on Method and UnboundMethod and Proc' do
|
||||
expect(Method.method_defined?(:source)).to be_truthy
|
||||
expect(UnboundMethod.method_defined?(:source)).to be_truthy
|
||||
expect(Proc.method_defined?(:source)).to be_truthy
|
||||
end
|
||||
|
||||
describe "Methods" do
|
||||
it 'should return source for method' do
|
||||
expect(method(:hello).source).to eq(@hello_source)
|
||||
end
|
||||
|
||||
it 'should return source for a method defined in a module' do
|
||||
expect(M.instance_method(:hello).source).to eq(@hello_module_source)
|
||||
end
|
||||
|
||||
it 'should return source for a singleton method as an instance method' do
|
||||
expect(class << $o
|
||||
self
|
||||
end.instance_method(:hello).source).to eq(@hello_singleton_source)
|
||||
end
|
||||
|
||||
it 'should return source for a singleton method' do
|
||||
expect($o.method(:hello).source).to eq(@hello_singleton_source)
|
||||
end
|
||||
|
||||
it 'should return a comment for method' do
|
||||
expect(method(:hello).comment).to eq(@hello_comment)
|
||||
end
|
||||
|
||||
# These tests fail because of http://jira.codehaus.org/browse/JRUBY-4576
|
||||
unless defined?(RUBY_ENGINE) && RUBY_ENGINE == "jruby"
|
||||
it 'should return source for an *_evaled method' do
|
||||
expect(M.method(:hello_name).source).to eq(@hello_instance_evaled_source)
|
||||
expect(M.method(:name_two).source).to eq(@hello_instance_evaled_source_2)
|
||||
expect(M.instance_method(:hello_name).source).to eq(@hello_class_evaled_source)
|
||||
expect(M.instance_method(:hi_name).source).to eq(@hi_module_evaled_source)
|
||||
end
|
||||
end
|
||||
|
||||
it "should raise error for evaled methods that do not pass __FILE__ and __LINE__ + 1 as its arguments" do
|
||||
expect do
|
||||
M.instance_method(:name_three).source
|
||||
end.to raise_error(MethodSource::SourceNotFoundError)
|
||||
end
|
||||
|
||||
if !is_rbx?
|
||||
it 'should raise for C methods' do
|
||||
expect do
|
||||
method(:puts).source
|
||||
end.to raise_error(MethodSource::SourceNotFoundError)
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
# if RUBY_VERSION =~ /1.9/ || is_rbx?
|
||||
describe "Lambdas and Procs" do
|
||||
it 'should return source for proc' do
|
||||
expect(MyProc.source).to eq(@proc_source)
|
||||
end
|
||||
|
||||
it 'should return an empty string if there is no comment' do
|
||||
expect(MyProc.comment).to eq('')
|
||||
end
|
||||
|
||||
it 'should return source for lambda' do
|
||||
expect(MyLambda.source).to eq(@lambda_source)
|
||||
end
|
||||
|
||||
it 'should return comment for lambda' do
|
||||
expect(MyLambda.comment).to eq(@lambda_comment)
|
||||
end
|
||||
|
||||
it 'should return comment for module' do
|
||||
expect(M.instance_method(:hello).module_comment).to eq(@module_comment)
|
||||
end
|
||||
|
||||
it 'should return comment for class' do
|
||||
expect(C.method(:hello).class_comment).to eq(@class_comment)
|
||||
end
|
||||
|
||||
it 'should return comment for class instance' do
|
||||
expect(C.new.method(:hello).class_comment).to eq(@class_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
|
||||
expect(method(:comment_test1).comment).to eq(@comment1)
|
||||
end
|
||||
|
||||
it "should correctly strip leading whitespace before comments" do
|
||||
expect(method(:comment_test2).comment).to eq(@comment2)
|
||||
end
|
||||
|
||||
it "should keep empty comment lines" do
|
||||
expect(method(:comment_test3).comment).to eq(@comment3)
|
||||
end
|
||||
|
||||
it "should ignore blank lines between comments" do
|
||||
expect(method(:comment_test4).comment).to eq(@comment4)
|
||||
end
|
||||
|
||||
it "should align all comments to same indent level" do
|
||||
expect(method(:comment_test5).comment).to eq(@comment5)
|
||||
end
|
||||
end
|
||||
end
|
||||
@@ -0,0 +1,107 @@
|
||||
require 'method_source'
|
||||
require 'rspec'
|
||||
|
||||
def is_rbx?
|
||||
defined?(RUBY_ENGINE) && RUBY_ENGINE =~ /rbx/
|
||||
end
|
||||
|
||||
def jruby?
|
||||
defined?(RUBY_ENGINE) && RUBY_ENGINE =~ /jruby/
|
||||
end
|
||||
|
||||
|
||||
# This is a comment for module
|
||||
module M
|
||||
def hello; :hello_module; end
|
||||
end
|
||||
|
||||
# This is a comment for class
|
||||
class C
|
||||
include M
|
||||
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 }
|
||||
|
||||
|
||||
name = "name"
|
||||
|
||||
M.instance_eval <<-METHOD, __FILE__, __LINE__ + 1
|
||||
def hello_#{name}(*args)
|
||||
send_mesg(:#{name}, *args)
|
||||
end
|
||||
METHOD
|
||||
|
||||
M.class_eval <<-METHOD, __FILE__, __LINE__ + 1
|
||||
def hello_#{name}(*args)
|
||||
send_mesg(:#{name}, *args)
|
||||
end
|
||||
METHOD
|
||||
|
||||
# module_eval to DRY code up
|
||||
#
|
||||
M.module_eval <<-METHOD, __FILE__, __LINE__ + 1
|
||||
|
||||
# module_eval is used here
|
||||
#
|
||||
def hi_#{name}
|
||||
@var = #{name}
|
||||
end
|
||||
METHOD
|
||||
|
||||
# case where 2 methods are defined inside an _eval block
|
||||
#
|
||||
M.instance_eval <<EOF, __FILE__, __LINE__ + 1
|
||||
|
||||
def #{name}_one()
|
||||
if 40 + 3
|
||||
44
|
||||
end
|
||||
end
|
||||
|
||||
|
||||
def #{name}_two()
|
||||
if 40 + 4
|
||||
45
|
||||
end
|
||||
end
|
||||
EOF
|
||||
|
||||
# class_eval without filename and lineno + 1 parameter
|
||||
|
||||
M.class_eval "def #{name}_three; @tempfile.#{name}; end"
|
||||
|
||||
Reference in New Issue
Block a user