summaryrefslogtreecommitdiff
path: root/test/test.rb
blob: fd1113fd9faff042a4191bebee45dd2e927105fb (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
direc = File.dirname(__FILE__)

require 'rubygems'
require 'bacon'
require 'open4'
require "#{direc}/../lib/method_source"
require "#{direc}/test_helper"

describe MethodSource do

  describe "emitted warnings" do
    it 'should emit no warnings' do
      Open4.popen4 'ruby -I lib -rubygems -r"method_source" -W -e "exit"' do |pid,stdin,stdout,stderr|
        stderr.read.empty?.should == true
      end
    end
  end

  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