<commit_before># encoding: utf-8
# frozen_string_literal: true

require 'spec_helper'

describe RuboCop::Cop::Performance::TimesMap do
  subject(:cop) { described_class.new }

  before do
    inspect_source(cop, source)
  end

  context '.times.map' do
    context 'with a block' do
      let(:source) { '4.times.map { |i| i.to_s }' }

      it 'registers an offense' do
        expect(cop.offenses.size).to eq(1)
        expect(cop.offenses.first.message).to eq(
          'Use `Array.new` with a block instead of `.times.map`.'
        )
        expect(cop.highlights).to eq(['4.times.map { |i| i.to_s }'])
      end
    end

    context 'with an explicitly passed block' do
      let(:source) { '4.times.map(&method(:foo))' }

      it 'registers an offense' do
        expect(cop.offenses.size).to eq(1)
        expect(cop.offenses.first.message).to eq(
          'Use `Array.new` with a block instead of `.times.map`.'
        )
        expect(cop.highlights).to eq(['4.times.map(&method(:foo))'])
      end
    end

    context 'without a block' do
      let(:source) { '4.times.map' }

      it "doesn't register an offense" do
        expect(cop.offenses).to be_empty
      end
    end

    context 'called on nothing' do
      let(:source) { 'times.map { |i| i.to_s }' }

      it "doesn't register an offense" do
        expect(cop.offenses).to be_empty
      end
    end
  end

  context '.times.collect' do
    context 'with a block' do
      let(:source) { '4.times.collect { |i| i.to_s }' }

      it 'registers an offense' do
        expect(cop.offenses.size).to eq(1)
        expect(cop.offenses.first.message).to eq(
          'Use `Array.new` with a block instead of `.times.collect`.'
        )
        expect(cop.highlights).to eq(['4.times.collect { |i| i.to_s }'])
      end
    end

    context 'with an explicitly passed block' do
      let(:source) { '4.times.collect(&method(:foo))' }

      it 'registers an offense' do
        expect(cop.offenses.size).to eq(1)
        expect(cop.offenses.first.message).to eq(
          'Use `Array.new` with a block instead of `.times.collect`.'
        )
        expect(cop.highlights).to eq(['4.times.collect(&method(:foo))'])
      end
    end

    context 'without a block' do
      let(:source) { '4.times.collect' }

      it "doesn't register an offense" do
        expect(cop.offenses).to be_empty
      end
    end

    context 'called on nothing' do
      let(:source) { 'times.collect { |i| i.to_s }' }

      it "doesn't register an offense" do
        expect(cop.offenses).to be_empty
      end
    end
  end
end<commit_msg>Use shared_examples in TimesMap tests<commit_after># encoding: utf-8
# frozen_string_literal: true

require 'spec_helper'

describe RuboCop::Cop::Performance::TimesMap do
  subject(:cop) { described_class.new }

  before do
    inspect_source(cop, source)
  end

  shared_examples 'map_or_collect' do |method|
    context ".times.#{method}" do
      context 'with a block' do
        let(:source) { "4.times.#{method} { |i| i.to_s }" }

        it 'registers an offense' do
          expect(cop.offenses.size).to eq(1)
          expect(cop.offenses.first.message).to eq(
            "Use `Array.new` with a block instead of `.times.#{method}`."
          )
          expect(cop.highlights).to eq(["4.times.#{method} { |i| i.to_s }"])
        end
      end

      context 'with an explicitly passed block' do
        let(:source) { "4.times.#{method}(&method(:foo))" }

        it 'registers an offense' do
          expect(cop.offenses.size).to eq(1)
          expect(cop.offenses.first.message).to eq(
            "Use `Array.new` with a block instead of `.times.#{method}`."
          )
          expect(cop.highlights).to eq(["4.times.#{method}(&method(:foo))"])
        end
      end

      context 'without a block' do
        let(:source) { "4.times.#{method}" }

        it "doesn't register an offense" do
          expect(cop.offenses).to be_empty
        end
      end

      context 'called on nothing' do
        let(:source) { "times.#{method} { |i| i.to_s }" }

        it "doesn't register an offense" do
          expect(cop.offenses).to be_empty
        end
      end
    end
  end

  it_behaves_like 'map_or_collect', 'map'
  it_behaves_like 'map_or_collect', 'collect'
end
{'fraction_non_alphanumeric': 0.13723205964585275, 'fraction_numerical': 0.005358807082945014, 'mean_word_length': 2.2947045280122795, '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': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '362557', 'n_tokens_mistral': 1444, 'n_tokens_neox': 1409, 'n_words': 356}