File: next_test.rb

package info (click to toggle)
ruby-byebug 11.1.3-5
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 1,252 kB
  • sloc: ruby: 8,835; ansic: 1,662; sh: 6; makefile: 4
file content (290 lines) | stat: -rw-r--r-- 6,920 bytes parent folder | download
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
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
# frozen_string_literal: true

require "test_helper"

module Byebug
  #
  # Tests basic stepping behaviour.
  #
  class BasicNextTest < TestCase
    def program
      strip_line_numbers <<-RUBY
         1:  module Byebug
         2:    #
         3:    # Toy class to test stepping.
         4:    #
         5:    class #{example_class}
         6:      def self.add_four(num)
         7:        byebug
         8:        num += 4
         9:        num += 2
        10:        num
        11:      end
        12:    end
        13:
        14:    res = #{example_class}.add_four(7)
        15:
        16:    res + 1
        17:  end
      RUBY
    end

    def test_next_goes_to_the_next_line
      enter "next"

      debug_code(program) { assert_location example_path, 9 }
    end

    def test_n_goes_to_the_next_line
      enter "n"

      debug_code(program) { assert_location example_path, 9 }
    end

    def test_next_stays_in_current_frame_while_not_finished
      enter "next 2"

      debug_code(program) { assert_location example_path, 10 }
    end

    def test_next_goes_up_a_frame_when_current_frame_finishes
      enter "next 3"

      debug_code(program) { assert_equal 16, frame.line }
    end

    def test_next_does_not_stop_at_byebug_internal_frames
      enter "next 4"

      debug_code(program) { assert_program_finished }
    end
  end

  #
  # Test for [#103](https://github.com/deivid-rodriguez/byebug/issues/103)
  #
  class NextWhenReturnInsideLoopInsideInitializeTest < TestCase
    def program
      strip_line_numbers <<-RUBY
         1:  byebug
         2:
         3:  module Byebug
         4:    #
         5:    # Toy class to test next.
         6:    #
         7:    class #{example_class}
         8:      def initialize
         9:        loop { return }
        10:      end
        11:    end
        12:
        13:    #{example_class}.new
        14:
        15:    "Bye!"
        16:  end
      RUBY
    end

    def test_next_works_return_inside_loop_inside_initialize
      enter "cont 13", "next"

      debug_code(program) { assert_location example_path, 15 }
    end
  end

  #
  # Test for: https://bugs.ruby-lang.org/issues/11492
  #
  class NextAndDefineMethodTest < TestCase
    def program
      strip_line_numbers <<-RUBY
         1:  module Byebug
         2:    #
         3:    # Toy class to test cases where next should not stay in frame
         4:    #
         5:    class #{example_class}
         6:      define_method "method1" do
         7:        return 1
         8:      end
         9:
        10:      def foo
        11:        method1
        12:        "bye foo!"
        13:      end
        14:    end
        15:
        16:    byebug
        17:
        18:    #{example_class}.new.foo
        19:
        20:    "bye!"
        21:  end
      RUBY
    end

    def test_next_works_as_expected_with_define_method
      enter "next"

      debug_code(program) { assert_equal 20, frame.line }
    end
  end

  #
  # Tests next behaviour in rescue clauses.
  #
  class NextRescueTest < TestCase
    def program
      strip_line_numbers <<-RUBY
         1:  module Byebug
         2:    #
         3:    # Toy class to test stepping and rescue interaction.
         4:    #
         5:    class #{example_class}
         6:      def self.raise_from_c
         7:        unknown
         8:      rescue NameError
         9:        1
        10:      end
        11:
        12:      def self.raise_from_ruby
        13:        fails_badly
        14:      rescue
        15:        1
        16:      end
        17:
        18:      def self.fails_badly
        19:        fail "booooooooooooooom"
        20:      end
        21:    end
        22:
        23:    byebug
        24:
        25:    #{example_class}.raise_from_c
        26:    #{example_class}.raise_from_ruby
        27:  end
      RUBY
    end

    def test_next_steps_over_rescue_when_raising_from_c_method
      enter "break Byebug::#{example_class}.raise_from_c", "cont", "next 2"

      debug_code(program) { assert_equal 9, frame.line }
    end

    def test_next_steps_over_rescue_when_raising_from_ruby_method
      enter "break Byebug::#{example_class}.raise_from_ruby", "cont", "next 2"

      debug_code(program) { assert_equal 15, frame.line }
    end
  end

  #
  # Tests next behaviour in combination with backtrace commands.
  #
  class NextBacktracesTest < TestCase
    def program
      strip_line_numbers <<-RUBY
         1:  module Byebug
         2:    #
         3:    # Toy class to test the combination of "up" and "next" commands.
         4:    #
         5:    class #{example_class}
         6:      def a
         7:        byebug
         8:        r = b(c)
         9:        r + 1
        10:      end
        11:
        12:      def b(p)
        13:        r = 2
        14:        p + r
        15:      end
        16:
        17:      def c
        18:        s = 3
        19:        s + 2
        20:      end
        21:    end
        22:
        23:    #{example_class}.new.a
        24:  end
      RUBY
    end

    def test_step_then_up_then_next_advances_in_the_upper_frame
      enter "step", "up", "next"

      debug_code(program) { assert_equal 9, frame.line }
    end
  end

  #
  # Tests next when execution should not stop at the same "stack size level"
  #
  class NextGoingUpFramesTest < TestCase
    def program
      strip_line_numbers <<-RUBY
         1:  module Byebug
         2:    #
         3:    # Toy class to test cases where next should not stay in frame
         4:    #
         5:    class #{example_class}
         6:      def finite_loop
         7:        n = 0
         8:        loop do
         9:          n = inc(n)
        10:          break if n == 2
        11:          n = inc(n)
        12:        end
        13:      end
        14:
        15:      def inc(n)
        16:        if n == 2
        17:          n
        18:        else
        19:          n + 1
        20:        end
        21:      end
        22:    end
        23:
        24:    byebug
        25:
        26:    #{example_class}.new.finite_loop
        27:  end
      RUBY
    end

    def test_next_goes_up_a_frame_if_current_frame_finishes
      enter "cont 19", "next"

      debug_code(program) { assert_equal 10, frame.line }
    end

    def _test_next_does_not_enter_other_frames_of_the_same_size
      enter "b 19", "cont", "cont", "next"

      debug_code(program) { assert_equal 9, frame.line }
    end
  end

  #
  # Test top-level block events are properly handled
  #
  class TopLevelBlockEventsTest < TestCase
    def program
      strip_line_numbers <<-RUBY
        1:  byebug
        2:
        3:  1.times {}
        4:
        5:  sleep 0
      RUBY
    end

    def test_top_level_b_call_event
      enter "next"

      debug_code(program) { assert_equal 5, frame.line }
    end
  end
end