summaryrefslogtreecommitdiff
path: root/spec/commands/find_method_spec.rb
blob: f584f3b58bbfcd215a8546191cde4d25f8376284 (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
describe "find-method" do
  MyKlass = Class.new do
    def hello
      "timothy"
    end

    def goodbye
      "jenny"
    end

    def tea_tim?
      "timothy"
    end

    def tea_time?
      "polly"
    end
  end

  describe "find matching methods by name regex (-n option)" do
    it "should find a method by regex" do
      expect(pry_eval(binding, "find-method hell MyKlass")).to match(
        /MyKlass.*?hello/m
      )
    end

    it "should NOT match a method that does not match the regex" do
      expect(pry_eval(binding, "find-method hell MyKlass")).not_to match(
        /MyKlass.*?goodbye/m
      )
    end
  end

  describe "find matching methods by content regex (-c option)" do
    it "should find a method by regex" do
      expect(pry_eval(binding, "find-method -c timothy MyKlass")).to match(
        /MyKlass.*?hello/m
      )
    end

    it "should NOT match a method that does not match the regex" do
      expect(pry_eval(binding, "find-method timothy MyKlass")).not_to match(
        /MyKlass.*?goodbye/m
      )
    end
  end

  it "should work with badly behaved constants" do
    MyKlass::X = Object.new
    def (MyKlass::X).hash
      raise "mooo"
    end

    expect(pry_eval(binding, "find-method -c timothy MyKlass")).to match(
      /MyKlass.*?hello/m
    )
  end

  it "should escape regexes correctly" do
    good = /tea_time\?/
    bad  = /tea_tim\?/
    expect(pry_eval(binding, 'find-method tea_time? MyKlass')).to match(good)
    expect(pry_eval(binding, 'find-method tea_time? MyKlass')).to match(good)
    expect(pry_eval(binding, 'find-method tea_time\? MyKlass')).not_to match(bad)
    expect(pry_eval(binding, 'find-method tea_time\? MyKlass')).to match(good)
  end
end