File: patcher_spec.rb

package info (click to toggle)
pry 0.15.2-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 1,772 kB
  • sloc: ruby: 21,692; sh: 17; makefile: 11
file content (37 lines) | stat: -rw-r--r-- 1,192 bytes parent folder | download | duplicates (3)
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
# frozen_string_literal: true

describe Pry::Method::Patcher do
  # rubocop:disable Style/SingleLineMethods
  before do
    @x = Object.new
    def @x.test; :before; end
    @method = Pry::Method(@x.method(:test))
  end
  # rubocop:enable Style/SingleLineMethods

  it "should change the behaviour of the method" do
    expect(@x.test).to eq :before
    @method.redefine "def @x.test; :after; end\n"
    expect(@x.test).to eq :after
  end

  it "should return a new method with new source" do
    expect(@method.source.strip).to eq "def @x.test; :before; end"
    expect(@method.redefine("def @x.test; :after; end\n")
      .source.strip).to eq "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"
    expect(Pry::Method(@x.method(:test)).source.strip).to eq "def @x.test; :after; end"
  end

  it "should preserve visibility" do
    class << @x
      private :test # rubocop:disable Style/AccessModifierDeclarations
    end
    expect(@method.visibility).to eq :private
    @method.redefine "def @x.test; :after; end\n"
    expect(Pry::Method(@x.method(:test)).visibility).to eq :private
  end
end