summaryrefslogtreecommitdiff
path: root/spec/control_d_handler_spec.rb
blob: 030cefd688210c5dd2ab8cf8fba4cf531c0318d6 (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
RSpec.describe Pry::ControlDHandler do
  context "when given eval string is non-empty" do
    let(:pry_instance) do
      Pry.new.tap do |p|
        p.eval_string = 'hello'
      end
    end

    it "clears input buffer" do
      described_class.default(pry_instance)
      expect(pry_instance.eval_string).to be_empty
    end
  end

  context "when given eval string is empty & pry instance has one binding" do
    let(:pry_instance) do
      Pry.new.tap do |p|
        p.eval_string = ''
        p.binding_stack = [binding]
      end
    end

    it "throws :breakout" do
      expect { described_class.default(pry_instance) }
        .to throw_symbol(:breakout)
    end

    it "clears binding stack" do
      expect { described_class.default(pry_instance) }
        .to throw_symbol
      expect(pry_instance.binding_stack).to be_empty
    end
  end

  context "when given eval string is empty & pry instance has 2+ bindings" do
    let(:binding1) { binding }
    let(:binding2) { binding }
    let(:binding_stack) { [binding1, binding2] }

    let(:pry_instance) do
      Pry.new.tap do |p|
        p.eval_string = ''
        p.binding_stack = binding_stack
      end
    end

    it "saves a dup of the current binding stack in the 'cd' command" do
      described_class.default(pry_instance)
      cd_state = pry_instance.commands['cd'].state
      expect(cd_state.old_stack).to eq([binding1, binding2])
    end

    it "pops the binding off the stack" do
      described_class.default(pry_instance)
      expect(pry_instance.binding_stack).to eq([binding1])
    end
  end
end