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 291 292 293 294 295 296
|
# frozen_string_literal: true
require "test_helper"
require "timeout"
module Byebug
#
# Tests generic input evaluation
#
class ProcessorBaseTest < TestCase
def program
strip_line_numbers <<-RUBY
1: module Byebug
2: byebug
3:
4: d = 1
5: d += 1
6: d
7: end
RUBY
end
def test_syntax_error_gives_a_prompt_back
enter "d."
debug_code(program) { assert_equal 4, frame.line }
end
def test_empty_command_repeats_last_command
enter "n", ""
debug_code(program) { assert_equal 6, frame.line }
end
def test_multiple_commands_are_executed_sequentially
enter "n ; n"
debug_code(program) { assert_equal 6, frame.line }
end
def test_semicolon_can_be_escaped_to_prevent_multiple_command_behaviour
enter 'n \; n'
debug_code(program) { assert_equal 4, frame.line }
end
def test_shows_an_error_for_unknown_subcommands_by_default
enter "info unknown_subcmd"
debug_code(minimal_program)
check_error_includes \
"Unknown command 'info unknown_subcmd'. Try 'help info'"
end
end
#
# Test evaluation of unknown input introduced by the user. Basically, the
# REPL behavior.
#
class ProcessorUnknownInputTest < TestCase
def program
strip_line_numbers <<-RUBY
1: module Byebug
2: #
3: # Toy class to test evaluation of unknown input
4: #
5: class #{example_class}
6: def inspect
7: "A very cool string representation"
8: end
9:
10: def to_s
11: "A not so cool string representation"
12: end
13: end
14:
15: byebug
16:
17: "Bye!"
18: end
RUBY
end
def test_arithmetic_expressions_are_evaluated_on_unknown_input
enter "3 + 2"
debug_code(minimal_program)
check_output_includes "5"
end
def test_ruby_code_is_evaluated_on_unknown_input
enter "[5, 6, 7].inject(&:+)"
debug_code(minimal_program)
check_output_includes "18"
end
def test_arrays_are_properly_printed_after_evaluation_of_unknown_input
enter "(1..3).to_a"
debug_code(minimal_program)
check_output_includes "[1, 2, 3]"
end
def test_eval_evaluates_just_like_without_it
enter 's = "something"', 'eval "s is #{s}"'
debug_code(minimal_program)
check_output_includes '"s is something"'
end
def test_evaluation_results_on_unknown_input_prefer_inspect_over_to_s
enter "#{example_class}.new"
debug_code(program)
check_output_includes "A very cool string representation"
end
def test_shows_backtrace_on_error_if_stack_on_error_enabled
enter "set stack_on_error", "2 / 0"
debug_code(minimal_program)
check_error_includes(/\s*from \S+:in \`eval\'/)
check_error_doesnt_include "ZeroDivisionError Exception: divided by 0"
end
def test_shows_only_exception_if_stack_on_error_disabled
enter "set stack_on_error off", "2 / 0"
debug_code(minimal_program)
check_error_includes "ZeroDivisionError Exception: divided by 0"
check_error_doesnt_include(/\S+:\d+:in `eval':divided by 0/)
end
end
#
# Tests processor evaluation and breakpoints working together
#
class ProcessorEvaluationAndBreakpointsTest < TestCase
def program
strip_line_numbers <<-RUBY
1: module Byebug
2: #
3: # Toy class to test subdebuggers inside evaluation prompt
4: #
5: class #{example_class}
6: def self.m1
7: m2
8: end
9:
10: def self.m2
11: "m2"
12: end
13: end
14:
15: byebug
16:
17: #{example_class}.m1
18:
19: "Bye!"
20: end
RUBY
end
def test_does_not_show_incorrect_info_about_having_stopped_at_breakpoint
enter "b 7", "cont", "m2"
debug_code(program)
# Regular breakpoint: OK
check_output_includes(/Stopped by breakpoint \d/)
# Incorrect info when evaluating something from command prompt
check_output_doesnt_include(/Stopped by breakpoint \d/,
/Stopped by breakpoint \d/)
end
end
#
# Tests commands automatically run when control is returned back to user
#
class ProcessorAutocommandsTest < TestCase
def program
strip_line_numbers <<-RUBY
1: module Byebug
2: #
3: # Toy class to test subdebuggers inside evaluation prompt
4: #
5: class #{example_class}
6: class_eval "def self.a; 1 end"
7: end
8:
9: byebug
10:
11: #{example_class}.a
12:
13: "Bye!"
14: end
RUBY
end
def test_autolists_lists_source_before_stopping
debug_code(program)
check_output_includes "[5, 14] in #{example_path}"
end
def _test_shows_error_when_current_source_location_is_unknown
enter "step"
debug_code(program) { assert_equal "(eval)", frame.file }
check_error_includes "No sourcefile available for (eval)"
end
end
#
# Tests evaluation in threaded programs.
#
class ProcessorEvaluationAndThreadsTest < TestCase
def program
<<-RUBY
module Byebug
#
# Toy class to test evaluation in Byebug's prompt
#
class #{example_class}
attr_accessor :thread
def initialize
@thread = Thread.new do
loop do
sleep 0.01
next if numbers.empty?
squares << (numbers.pop)**2
end
end
end
def numbers
@numbers ||= Queue.new
end
def squares
@squares ||= []
end
def calc(number)
numbers.push(number)
loop do
next if squares.empty?
return squares.pop
end
end
end
worker = #{example_class}.new
byebug
worker.thread.kill
end
RUBY
end
def test_properly_evaluates_expressions_using_threads
enter "Timeout::timeout(60) { 1 }"
debug_code(minimal_program)
check_output_includes "1"
end
def test_does_not_hang_when_evaluating_expressions_using_new_threads
enter "Thread.new {}.join"
debug_code(minimal_program)
check_output_includes(/#<Thread:0x.*>/)
end
def test_does_not_hang_when_evaluating_expressions_using_old_threads
enter "worker.calc(10)"
debug_code(program)
check_output_includes "100"
end
def test_thread_context_is_kept
enter 'Thread.current[:greeting] = "hi!"', "Thread.current[:greeting]"
debug_code(minimal_program)
check_output_includes '"hi!"', # After set
'"hi!"' # After get
end
end
end
|