summaryrefslogtreecommitdiff
path: root/test/test.rb
diff options
context:
space:
mode:
authorAdam Sanderson <netghost@gmail.com>2010-12-17 10:55:16 -0800
committerJohn Mair <jrmair@gmail.com>2010-12-18 19:28:42 +1300
commite61e36e4f937b18e258fe0f8f2733528d132cc5c (patch)
tree5a6eb4d0e9ab2d570ac4d23200059ce80c889cff /test/test.rb
parent32e6e8495c36782e6362676a32d7a7e808226437 (diff)
downloadmethod_source-e61e36e4f937b18e258fe0f8f2733528d132cc5c.tar.gz
version 0.2.0, support for method comments (and a few minor changes), thanks to Adam Sanderson
* Merged in Adam's changes * Refactored source_helper to reflect comment_helper style * Made some adjustments to Adam's code: removed buffer.strip, now doing an lstrip on each line before adding to buffer * No longer including blank lines in comment buffer (blank lines ignored) * Added large number of comment-related tests
Diffstat (limited to 'test/test.rb')
-rw-r--r--test/test.rb75
1 files changed, 60 insertions, 15 deletions
diff --git a/test/test.rb b/test/test.rb
index 05a711d..48f3ed8 100644
--- a/test/test.rb
+++ b/test/test.rb
@@ -2,17 +2,18 @@ direc = File.dirname(__FILE__)
require 'bacon'
require "#{direc}/../lib/method_source"
-
-hello_source = "def hello; :hello; end\n"
-lambda_source = "MyLambda = lambda { :lambda }\n"
-proc_source = "MyProc = Proc.new { :proc }\n"
-
-def hello; :hello; end
-
-MyLambda = lambda { :lambda }
-MyProc = Proc.new { :proc }
+require "#{direc}/test_helper"
describe MethodSource do
+
+ before do
+ @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
@@ -22,7 +23,11 @@ describe MethodSource do
describe "Methods" do
if RUBY_VERSION =~ /1.9/
it 'should return source for method' do
- method(:hello).source.should == hello_source
+ method(:hello).source.should == @hello_source
+ end
+
+ it 'should return a comment for method' do
+ method(:hello).comment.should == @hello_comment
end
it 'should raise for C methods' do
@@ -30,7 +35,7 @@ describe MethodSource do
end
else
- it 'should raise on #source' do
+ it 'should raise on #source for 1.8' do
lambda { method(:hello).source }.should.raise RuntimeError
end
end
@@ -39,16 +44,56 @@ describe MethodSource do
describe "Lambdas and Procs" do
if RUBY_VERSION =~ /1.9/
it 'should return source for proc' do
- MyProc.source.should == proc_source
+ 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
+ MyLambda.source.should == @lambda_source
+ end
+
+ it 'should return comment for lambda' do
+ MyLambda.comment.should == @lambda_comment
end
else
- it 'should raise on #source' do
+ it 'should raise on #source for 1.8' do
lambda { method(:hello).source }.should.raise RuntimeError
end
end
end
+
+ if RUBY_VERSION =~ /1.9/
+ 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
end