summaryrefslogtreecommitdiff
path: root/spec/commands/cd_spec.rb
blob: ae715a3a3349f30d6e82c421996fc8f273e90288 (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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
describe 'cd' do
  before do
    @o = Object.new
    @obj = Object.new
    @obj.instance_variable_set(:@x, 66)
    @obj.instance_variable_set(:@y, 79)
    @o.instance_variable_set(:@obj, @obj)

    @t = pry_tester(@o) do
      def mapped_binding_stack
        binding_stack.map { |b| b.eval('self') }
      end

      def binding_stack
        pry.binding_stack.dup
      end

      def command_state
        pry.commands['cd'].state
      end

      def old_stack
        pry.commands['cd'].state.old_stack.dup
      end
    end
  end

  after { Pry::CommandState.default.reset('cd') }

  describe 'old stack toggling with `cd -`' do
    describe 'in fresh pry instance' do
      it 'should not toggle when there is no old stack' do
        2.times do
          @t.eval 'cd -'
          expect(@t.mapped_binding_stack).to eq [@o]
        end
      end
    end

    describe 'when an error was raised' do
      it 'should not toggle and should keep correct stacks' do
        expect { @t.eval 'cd %' }.to raise_error Pry::CommandError

        expect(@t.old_stack).to eq []
        expect(@t.mapped_binding_stack).to eq [@o]

        @t.eval 'cd -'
        expect(@t.old_stack).to eq []
        expect(@t.mapped_binding_stack).to eq [@o]
      end
    end

    describe 'when using simple cd syntax' do
      it 'should toggle' do
        @t.eval 'cd :mon_dogg', 'cd -'
        expect(@t.mapped_binding_stack).to eq [@o]

        @t.eval 'cd -'
        expect(@t.mapped_binding_stack).to eq [@o, :mon_dogg]
      end
    end

    describe "when using complex cd syntax" do
      it 'should toggle with a complex path (simple case)' do
        @t.eval 'cd 1/2/3', 'cd -'
        expect(@t.mapped_binding_stack).to eq [@o]

        @t.eval 'cd -'
        expect(@t.mapped_binding_stack).to eq [@o, 1, 2, 3]
      end

      it 'should toggle with a complex path (more complex case)' do
        @t.eval 'cd 1/2/3', 'cd ../4', 'cd -'
        expect(@t.mapped_binding_stack).to eq [@o, 1, 2, 3]

        @t.eval 'cd -'
        expect(@t.mapped_binding_stack).to eq [@o, 1, 2, 4]
      end
    end

    describe 'series of cd calls' do
      it 'should toggle with fuzzy `cd -` calls' do
        @t.eval 'cd :mon_dogg', 'cd -', 'cd 42', 'cd -'
        expect(@t.mapped_binding_stack).to eq [@o]

        @t.eval 'cd -'
        expect(@t.mapped_binding_stack).to eq [@o, 42]
      end
    end

    describe 'when using cd ..' do
      it 'should toggle with a simple path' do
        @t.eval 'cd :john_dogg', 'cd ..'
        expect(@t.mapped_binding_stack).to eq [@o]

        @t.eval 'cd -'
        expect(@t.mapped_binding_stack).to eq [@o, :john_dogg]
      end

      it 'should toggle with a complex path' do
        @t.eval 'cd 1/2/3/../4', 'cd -'
        expect(@t.mapped_binding_stack).to eq [@o]

        @t.eval 'cd -'
        expect(@t.mapped_binding_stack).to eq [@o, 1, 2, 4]
      end
    end

    describe 'when using cd ::' do
      it 'should toggle' do
        @t.eval 'cd ::', 'cd -'
        expect(@t.mapped_binding_stack).to eq [@o]

        @t.eval 'cd -'
        expect(@t.mapped_binding_stack).to eq [@o, TOPLEVEL_BINDING.eval('self')]
      end
    end

    describe 'when using cd /' do
      it 'should toggle' do
        @t.eval 'cd /', 'cd -'
        expect(@t.mapped_binding_stack).to eq [@o]

        @t.eval 'cd :john_dogg', 'cd /', 'cd -'
        expect(@t.mapped_binding_stack).to eq [@o, :john_dogg]
      end
    end

    describe 'when using ^D (Control-D) key press' do
      it 'should keep correct old binding' do
        @t.eval 'cd :john_dogg', 'cd :mon_dogg', 'cd :kyr_dogg',
                'Pry.config.control_d_handler.call("", pry_instance)'
        expect(@t.mapped_binding_stack).to eq [@o, :john_dogg, :mon_dogg]

        @t.eval 'cd -'
        expect(@t.mapped_binding_stack).to eq [@o, :john_dogg, :mon_dogg, :kyr_dogg]

        @t.eval 'cd -'
        expect(@t.mapped_binding_stack).to eq [@o, :john_dogg, :mon_dogg]
      end
    end
  end

  it 'should cd into simple input' do
    @t.eval 'cd :mon_ouie'
    expect(@t.eval('self')).to eq :mon_ouie
  end

  it 'should break out of session with cd ..' do
    @t.eval 'cd :outer', 'cd :inner'
    expect(@t.eval('self')).to eq :inner

    @t.eval 'cd ..'
    expect(@t.eval('self')).to eq :outer
  end

  it "should not leave the REPL session when given 'cd ..'" do
    @t.eval 'cd ..'
    expect(@t.eval('self')).to eq @o
  end

  it 'should break out to outer-most session with cd /' do
    @t.eval 'cd :inner'
    expect(@t.eval('self')).to eq :inner

    @t.eval 'cd 5'
    expect(@t.eval('self')).to eq 5

    @t.eval 'cd /'
    expect(@t.eval('self')).to eq @o
  end

  it 'should break out to outer-most session with just cd (no args)' do
    @t.eval 'cd :inner'
    expect(@t.eval('self')).to eq :inner

    @t.eval 'cd 5'
    expect(@t.eval('self')).to eq 5

    @t.eval 'cd'
    expect(@t.eval('self')).to eq @o
  end

  it 'should cd into an object and its ivar using cd obj/@ivar syntax' do
    @t.eval 'cd @obj/@x'
    expect(@t.mapped_binding_stack).to eq [@o, @obj, 66]
  end

  it 'cds into an object and its ivar using cd obj/@ivar/ syntax (note following /)' do
    @t.eval 'cd @obj/@x/'
    expect(@t.mapped_binding_stack).to eq [@o, @obj, 66]
  end

  it 'should cd into previous object and its local using cd ../local syntax' do
    @t.eval 'cd @obj', 'local = :local', 'cd @x', 'cd ../local'
    expect(@t.mapped_binding_stack).to eq [@o, @obj, :local]
  end

  it 'cds into an object and its ivar and back again using cd obj/@ivar/.. syntax' do
    @t.eval 'cd @obj/@x/..'
    expect(@t.mapped_binding_stack).to eq [@o, @obj]
  end

  it(
    'cds into an object and its ivar and back and then into another ivar ' \
    'using cd obj/@ivar/../@y syntax'
  ) do
    @t.eval 'cd @obj/@x/../@y'
    expect(@t.mapped_binding_stack).to eq [@o, @obj, 79]
  end

  it 'should cd back to top-level and then into another ivar using cd /@ivar/ syntax' do
    @t.eval '@z = 20', 'cd @obj/@x/', 'cd /@z'
    expect(@t.mapped_binding_stack).to eq [@o, 20]
  end

  it 'should start a session on TOPLEVEL_BINDING with cd ::' do
    @t.eval 'cd ::'
    expect(@t.eval('self')).to eq TOPLEVEL_BINDING.eval('self')
  end

  it 'should cd into complex input (with spaces)' do
    def @o.hello(_x, _y, _z) # rubocop:disable Naming/UncommunicativeMethodParamName
      :mon_ouie
    end

    @t.eval 'cd hello 1, 2, 3'
    expect(@t.eval('self')).to eq :mon_ouie
  end

  it 'should not cd into complex input when it encounters an exception' do
    expect { @t.eval 'cd 1/2/swoop_a_doop/3' }.to raise_error Pry::CommandError

    expect(@t.mapped_binding_stack).to eq [@o]
  end

  it 'can cd into an expression containing a string with slashes in it' do
    @t.eval 'cd ["http://google.com"]'
    expect(@t.eval('self')).to eq ["http://google.com"]
  end

  it 'can cd into an expression with division in it' do
    @t.eval 'cd (10/2)/even?'
    expect(@t.eval('self')).to eq false
  end

  # Regression test for ticket #516.
  it 'should be able to cd into the Object BasicObject' do
    expect { @t.eval 'cd BasicObject.new' }.to_not raise_error
  end

  # https://github.com/pry/pry/issues/1596
  it "can cd into objects that redefine #respond_to? to return true" do
    expect { @t.eval('cd Class.new { def respond_to?(m) true end }.new') }
      .to_not raise_error
  end
end