summaryrefslogtreecommitdiff
path: root/spec/commands/play_spec.rb
blob: 000f6fe583bf9d4cc67aafed7a46de49994d7efe (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
# frozen_string_literal: true

# This command needs a TONNE more tests for it, but i can't figure out
# how to do them yet, and i really want to release. Sorry. Someone
# come along and do a better job.
describe "play" do
  before do
    @o = Object.new
    @t = pry_tester(@o)
  end

  describe "with an argument" do
    # can't think of a f*cking way to test this!!
    describe "implied file" do
      # it 'should play from the file associated with the current binding' do
      #   # require 'fixtures/play_helper'
      # end

      # describe "integer" do
      #   it "should process one line from pry_instance.last_file" do
      #     @t.process_command 'play --lines 1', @eval_str
      #     @eval_str.should =~ /bing = :bing\n/
      #   end
      # end

      # describe "range" do
      #   it "should process multiple lines at once from pry_instance.last_file" do
      #     @t.process_command 'play --lines 1..3', @eval_str
      #     [/bing = :bing\n/, /bang = :bang\n/, /bong = :bong\n/].each { |str|
      #       @eval_str.should =~ str
      #     }
      #   end
    end
  end

  describe "playing a file" do
    it 'should play a file' do
      @t.process_command 'play spec/fixtures/whereami_helper.rb'
      expect(@t.eval_string).to eq unindent(<<-STR)
        # rubocop:disable Layout/EmptyLineBetweenDefs
        class Cor
          def a; end
          def b; end
          def c; end
          def d; end
        end
        # rubocop:enable Layout/EmptyLineBetweenDefs
      STR
    end

    it 'should output file contents with print option' do
      @t.process_command 'play --print spec/fixtures/whereami_helper.rb'
      expect(@t.last_output).to eq unindent(<<-STR)
        1: # rubocop:disable Layout/EmptyLineBetweenDefs
        2: class Cor
        3:   def a; end
        4:   def b; end
        5:   def c; end
        6:   def d; end
        7: end
        8: # rubocop:enable Layout/EmptyLineBetweenDefs
      STR
    end
  end

  describe "whatever" do
    before do
      def @o.test_method
        :test_method_content
      end
    end

    it 'should play documentation with the -d switch' do
      @o.singleton_class.send :remove_method, :test_method

      # @v = 10
      # @y = 20
      def @o.test_method
        :test_method_content
      end

      @t.process_command 'play -d test_method'
      expect(@t.eval_string).to eq unindent(<<-STR)
        @v = 10
        @y = 20
      STR
    end

    it 'should restrict -d switch with --lines' do
      @o.singleton_class.send :remove_method, :test_method

      # @x = 0
      # @v = 10
      # @y = 20
      # @z = 30
      def @o.test_method
        :test_method_content
      end

      @t.process_command 'play -d test_method --lines 2..3'
      expect(@t.eval_string).to eq unindent(<<-STR)
        @v = 10
        @y = 20
      STR
    end

    it 'has pretty error messages when -d cant find object' do
      expect { @t.process_command "play -d sdfsdf" }
        .to raise_error(Pry::CommandError, /Cannot locate/)
    end

    it 'should play a method (a single line)' do
      @t.process_command 'play test_method --lines 2'
      expect(@t.eval_string).to eq ":test_method_content\n"
    end

    it 'should properly reindent lines' do
      @o.singleton_class.send :remove_method, :test_method

      def @o.test_method
        'hello world'
      end

      @t.process_command 'play test_method --lines 2'
      expect(@t.eval_string).to eq "'hello world'\n"
    end

    it 'should APPEND to the input buffer when playing a method line, not replace it' do
      @t.eval_string = unindent(<<-STR)
        def another_test_method
      STR

      @t.process_command 'play test_method --lines 2'

      expect(@t.eval_string).to eq unindent(<<-STR)
        def another_test_method
          :test_method_content
      STR
    end

    it 'should play a method (multiple lines)' do
      @o.singleton_class.send :remove_method, :test_method

      def @o.test_method
        @var0 = 10
        @var1 = 20
        @var2 = 30
        @var3 = 40
      end

      @t.process_command 'play test_method --lines 3..4'
      expect(@t.eval_string).to eq unindent(<<-STR, 0)
        @var1 = 20
        @var2 = 30
      STR
    end

    describe "play -i" do
      it 'should play multi-ranged input expressions' do
        a = b = c = d = e = 0
        redirect_pry_io(
          InputTester.new(
            'a += 1', 'b += 1', 'c += 1', 'd += 1', 'e += 1', 'play -i 1..3'
          ),
          StringIO.new
        ) do
          binding.pry # rubocop:disable Lint/Debugger
        end

        [a, b, c].all? { |v| expect(v).to eq 2 }
        expect(d).to eq 1
        expect(e).to eq 1
      end
    end

    describe "play -e" do
      it 'should run an expression from given line number' do
        @o.singleton_class.send :remove_method, :test_method

        def @o.test_method
          @s = [
            1, 2, 3,
            4, 5, 6
          ]
        end

        @t.process_command 'play test_method -e 2'
        expect(@t.eval_string).to eq unindent(<<-STR, 0)
          @s = [
            1, 2, 3,
            4, 5, 6
          ]
        STR
      end
    end
  end
end