summaryrefslogtreecommitdiff
path: root/spec/control_d_handler_spec.rb
blob: 63b7c7acbc1876949ab671c0670a429c67bfe5ea (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
require_relative 'helper'

describe Pry::DEFAULT_CONTROL_D_HANDLER do

  describe "control-d press" do

    before do
      # Simulates a ^D press.
      @control_d = "Pry::DEFAULT_CONTROL_D_HANDLER.call('', _pry_)"
    end

    describe "in an expression" do
      it "should clear out passed string" do
        str = 'hello world'
        Pry::DEFAULT_CONTROL_D_HANDLER.call(str, nil)
        str.should == ''
      end
    end

    describe 'at top-level session' do
      it 'should break out of a REPL loop' do
        instance = Pry.new
        instance.binding_stack.should.not.be.empty
        instance.eval(nil).should.be_false
        instance.binding_stack.should.be.empty
      end
    end

    describe 'in a nested session' do
      it 'should pop last binding from the binding stack' do
        t = pry_tester
        t.eval "cd Object.new"
        t.eval("_pry_.binding_stack.size").should == 2
        t.eval("_pry_.eval(nil)").should.be_true
        t.eval("_pry_.binding_stack.size").should == 1
      end

      it "breaks out of the parent session" do
        ReplTester.start do
          input  'Pry::REPL.new(_pry_, :target => 10).start'
          output ''
          prompt(/10.*> $/)

          input  'self'
          output '=> 10'

          input  nil # Ctrl-D
          output ''

          input  'self'
          output '=> main'

          input  nil # Ctrl-D
          output '=> nil' # Exit value of nested REPL.
          assert_exited
        end
      end
    end

  end

end