<commit_before># frozen_string_literal: true

RSpec.describe RuboCop::Formatter::TapFormatter do
  subject(:formatter) { described_class.new(output) }

  let(:output) { StringIO.new }

  let(:files) do
    %w[lib/rubocop.rb spec/spec_helper.rb exe/rubocop].map do |path|
      File.expand_path(path)
    end
  end

  describe '#file_finished' do
    before do
      formatter.started(files)
      formatter.file_started(files.first, {})
      formatter.file_finished(files.first, offenses)
    end

    context 'when no offenses are detected' do
      let(:offenses) { [] }

      it 'prints "ok"' do
        expect(output.string).to include('ok 1')
      end
    end

    context 'when any offenses are detected' do
      let(:offenses) { [double('offense').as_null_object] }

      it 'prints "not ok"' do
        expect(output.string).to include('not ok 1')
      end
    end
  end

  describe '#finished' do
    before do
      formatter.started(files)
    end

    context 'when any offenses are detected' do
      before do
        source_buffer = Parser::Source::Buffer.new('test', 1)
        source = Array.new(9) do |index|
          "This is line #{index + 1}."
        end
        source_buffer.source = source.join("\n")
        line_length = source[0].length + 1

        formatter.file_started(files[0], {})
        formatter.file_finished(
          files[0],<commit_msg>Use fixtures in TapFormatter spec<commit_after># frozen_string_literal: true

RSpec.describe RuboCop::Formatter::TapFormatter do
  subject(:formatter) { described_class.new(output) }

  let(:output) { StringIO.new }

  let(:files) do
    %w[lib/rubocop.rb spec/spec_helper.rb exe/rubocop].map do |path|
      File.expand_path(path)
    end
  end

  describe '#file_finished' do
    before do
      formatter.started(files)
      formatter.file_started(files.first, {})
      formatter.file_finished(files.first, offenses)
    end

    context 'when no offenses are detected' do
      let(:offenses) { [] }

      it 'prints "ok"' do
        expect(output.string).to include('ok 1')
      end
    end

    context 'when any offenses are detected' do
      let(:offenses) do
        source_buffer = Parser::Source::Buffer.new('test', 1)
        source = Array.new(9) do |index|
          "This is line #{index + 1}."
        end
        source_buffer.source = source.join("\n")
        line_length = source[0].length + 1

        [
          RuboCop::Cop::Offense.new(
            :convention,
            Parser::Source::Range.new(source_buffer,
                                      line_length + 2,
                                      line_length + 3),
            'foo',
            'Cop'
          )
        ]
      end

      it 'prints "not ok"' do
        expect(output.string).to include('not ok 1')
      end
    end
  end

  describe '#finished' do
    before do
      formatter.started(files)
    end

    context 'when any offenses are detected' do
      before do
        source_buffer = Parser::Source::Buffer.new('test', 1)
        source = Array.new(9) do |index|
          "This is line #{index + 1}."
        end
        source_buffer.source = source.join("\n")
        line_length = source[0].length + 1

        formatter.file_started(files[0], {})
        formatter.file_finished(
          files[0],
{'fraction_non_alphanumeric': 0.11608222490931076, 'fraction_numerical': 0.007557436517533253, 'mean_word_length': 2.197101449275362, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3738817', 'n_tokens_mistral': 1058, 'n_tokens_neox': 984, 'n_words': 241}