summaryrefslogtreecommitdiff
path: root/spec/method/patcher_spec.rb
blob: 95b5f3e9757e8794493a4d6d584c416d023161bb (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
require_relative '../helper'

describe Pry::Method::Patcher do

  before do
    @x = Object.new
    def @x.test; :before; end
    @method = Pry::Method(@x.method(:test))
  end

  it "should change the behaviour of the method" do
    @x.test.should == :before
    @method.redefine "def @x.test; :after; end\n"
    @x.test.should == :after
  end

  it "should return a new method with new source" do
    @method.source.strip.should == "def @x.test; :before; end"
    @method.redefine("def @x.test; :after; end\n").
      source.strip.should == "def @x.test; :after; end"
  end

  it "should change the source of new Pry::Method objects" do
    @method.redefine "def @x.test; :after; end\n"
    Pry::Method(@x.method(:test)).source.strip.should == "def @x.test; :after; end"
  end

  it "should preserve visibility" do
    class << @x; private :test; end
    @method.visibility.should == :private
    @method.redefine "def @x.test; :after; end\n"
    Pry::Method(@x.method(:test)).visibility.should == :private
  end
end