<commit_before>require 'test_helper'

#
# Tests for pry-byebug breakpoints.
#
class BreakpointsTestGeneral < MiniTest::Spec
  #
  # Minimal dummy example class.
  #
  class Tester
    def self.class_method; end
    def instance_method; end
  end

  def breakpoints_class
    Pry::Byebug::Breakpoints
  end

  def test_add_file_raises_argument_error
    Pry.stubs eval_path: 'something'
    File.stubs :exist?
    assert_raises(ArgumentError) do
      breakpoints_class.add_file('file', 1)
    end
  end

  def test_add_method_adds_instance_method_breakpoint
    Pry.stub :processor, Byebug::PryProcessor.new do
      breakpoints_class.add_method 'BreakpointsTest::Tester#instance_method'
      bp = Byebug.breakpoints.last
      assert_equal 'BreakpointsTest::Tester', bp.source
      assert_equal 'instance_method', bp.pos
    end
  end

  def test_add_method_adds_class_method_breakpoint
    Pry.stub :processor, Byebug::PryProcessor.new do
      breakpoints_class.add_method 'BreakpointsTest::Tester.class_method'
      bp = Byebug.breakpoints.last
      assert_equal 'BreakpointsTest::Tester', bp.source
      assert_equal 'class_method', bp.pos
    end
  end
end

#
# Some common specs for breakpoints
#
module BreakpointSpecs
  def test_shows_breakpoint_enabled
    @output.string.must_match @regexp
  end

  def test_shows_breakpoint_hit
    result = @output.string
    result.must_match(@regexp)
    match = result.match(@regexp)
    result.must_match(/^  Breakpoint #{match[:id]}\. First hit/)
  end

  def test_shows_breakpoint_line
    @output.string.must_match(/\=> \s*#{@line}:/)
  end
end

#
# Tests for breakpoint commands
#
class BreakpointsTestCommands < Minitest::Spec
  let(:break_first_file) { test_file('break1') }
  let(:break_second_file) { test_file('break2') }

  before do
    Pry.color, Pry.pager, Pry.hooks = false, false, Pry::DEFAULT_HOOKS
    @input = InputTester.new 'break --delete-all'
    redirect_pry_io(@input, StringIO.new) { binding.pry }
    Object.send :remove_const, :Break1Example if defined? Break1Example
    Object.send :remove_const, :Break2Example if defined? Break2Example
    @output = StringIO.new
  end

  describe 'Set Breakpoints' do
    describe 'set by line number' do
      before do
        @input = InputTester.new('break 8')
        redirect_pry_io(@input, @output) { load break_first_file }
        @line = 8
        @regexp = /^  Breakpoint (?<id>\d+): #{break_first_file} @ 8 \(Enabled\)/
      end

      include BreakpointSpecs
    end

    describe 'set by method_id' do
      before do
        @input = InputTester.new('break Break1Example#a')<commit_msg>Silence RuboCop Lint/Debugger warning<commit_after>require 'test_helper'

#
# Tests for pry-byebug breakpoints.
#
class BreakpointsTestGeneral < MiniTest::Spec
  #
  # Minimal dummy example class.
  #
  class Tester
    def self.class_method; end
    def instance_method; end
  end

  def breakpoints_class
    Pry::Byebug::Breakpoints
  end

  def test_add_file_raises_argument_error
    Pry.stubs eval_path: 'something'
    File.stubs :exist?
    assert_raises(ArgumentError) do
      breakpoints_class.add_file('file', 1)
    end
  end

  def test_add_method_adds_instance_method_breakpoint
    Pry.stub :processor, Byebug::PryProcessor.new do
      breakpoints_class.add_method 'BreakpointsTest::Tester#instance_method'
      bp = Byebug.breakpoints.last
      assert_equal 'BreakpointsTest::Tester', bp.source
      assert_equal 'instance_method', bp.pos
    end
  end

  def test_add_method_adds_class_method_breakpoint
    Pry.stub :processor, Byebug::PryProcessor.new do
      breakpoints_class.add_method 'BreakpointsTest::Tester.class_method'
      bp = Byebug.breakpoints.last
      assert_equal 'BreakpointsTest::Tester', bp.source
      assert_equal 'class_method', bp.pos
    end
  end
end

#
# Some common specs for breakpoints
#
module BreakpointSpecs
  def test_shows_breakpoint_enabled
    @output.string.must_match @regexp
  end

  def test_shows_breakpoint_hit
    result = @output.string
    result.must_match(@regexp)
    match = result.match(@regexp)
    result.must_match(/^  Breakpoint #{match[:id]}\. First hit/)
  end

  def test_shows_breakpoint_line
    @output.string.must_match(/\=> \s*#{@line}:/)
  end
end

#
# Tests for breakpoint commands
#
class BreakpointsTestCommands < Minitest::Spec
  let(:break_first_file) { test_file('break1') }
  let(:break_second_file) { test_file('break2') }

  before do
    Pry.color, Pry.pager, Pry.hooks = false, false, Pry::DEFAULT_HOOKS
    @input = InputTester.new 'break --delete-all'
    redirect_pry_io(@input, StringIO.new) do
      binding.pry # rubocop:disable Lint/Debugger
    end
    Object.send :remove_const, :Break1Example if defined? Break1Example
    Object.send :remove_const, :Break2Example if defined? Break2Example
    @output = StringIO.new
  end

  describe 'Set Breakpoints' do
    describe 'set by line number' do
      before do
        @input = InputTester.new('break 8')
        redirect_pry_io(@input, @output) { load break_first_file }
        @line = 8
        @regexp = /^  Breakpoint (?<id>\d+): #{break_first_file} @ 8 \(Enabled\)/
      end

      include BreakpointSpecs
    end

    describe 'set by method_id' do
      before do
        @input = InputTester.new('break Break1Example#a')
{'fraction_non_alphanumeric': 0.09561453039713909, 'fraction_numerical': 0.004140786749482402, 'mean_word_length': 3.6248912097476067, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3592919', 'n_tokens_mistral': 1796, 'n_tokens_neox': 1775, 'n_words': 384}