<commit_before># frozen_string_literal: true

module RuboCop
  module RSpec
    # Mixin for `expect_offense` and `expect_no_offenses`
    #
    # This mixin makes it easier to specify strict offense expectations
    # and a declarative and visual fashion. Just type out the code that
    # should generate a offense, annotate code by writing '^'s
    # underneath each character that should be highlighted, and follow
    # the carets with a string (separated by a space) that is the
    # message of the offense. You can include multiple offenses in
    # one code snippet.
    #
    # @example Usage
    #
    #     expect_offense(<<-RUBY.strip_indent)
    #       a do
    #         b
    #       end.c
    #       ^^^^^ Avoid chaining a method call on a do...end block.
    #     RUBY
    #
    # @example Equivalent assertion without `expect_offense`
    #
    #     inspect_source(cop, <<-RUBY.strip_indent)
    #       a do
    #         b
    #       end.c
    #     RUBY
    #
    #     expect(cop.offenses.size).to be(1)
    #
    #     offense = cop.offenses.first
    #     expect(offense.line).to be(3)
    #     expect(offense.column_range).to be(0...5)
    #     expect(offense.message).to eql(
    #       'Avoid chaining a method call on a do...end block.'
    #     )
    #
    # If you do not want to specify an offense then use the
    # companion method `expect_no_offenses`. This method is a much
    # simpler assertion since it just inspects the source and checks
    # that there were no offenses. The `expect_offenses` method has
    # to do more work by parsing out lines that contain carets.
    module ExpectOffense
      DEFAULT_FILENAME = 'example.rb'.freeze

      def expect_offense(source, filename = DEFAULT_FILENAME)
        expected_annotations = AnnotatedSource.parse(source)

        if expected_annotations.plain_source == source
          raise 'Use expect_no_offenses to assert that no offenses are found'
        end

        inspect_source(cop, expected_annotations.plain_source, filename)
        actual_annotations =
          expected_annotations.with_offense_annotations(cop.offenses)
        expect(expected_annotations.to_s).to eq(actual_annotations.to_s)
      end

      def expect_no_offenses(source, filename = DEFAULT_FILENAME)
        inspect_source(cop, source, filename)

        expect(cop.offenses).to be_empty
      end

      # Parsed representation of code annotated with the `^^^ Message` style
      class AnnotatedSource
        ANNOTATION_PATTERN = /\A\s*\^+ /<commit_msg>Swap `expected` and `got` in `expect_offense` spec helper<commit_after># frozen_string_literal: true

module RuboCop
  module RSpec
    # Mixin for `expect_offense` and `expect_no_offenses`
    #
    # This mixin makes it easier to specify strict offense expectations
    # and a declarative and visual fashion. Just type out the code that
    # should generate a offense, annotate code by writing '^'s
    # underneath each character that should be highlighted, and follow
    # the carets with a string (separated by a space) that is the
    # message of the offense. You can include multiple offenses in
    # one code snippet.
    #
    # @example Usage
    #
    #     expect_offense(<<-RUBY.strip_indent)
    #       a do
    #         b
    #       end.c
    #       ^^^^^ Avoid chaining a method call on a do...end block.
    #     RUBY
    #
    # @example Equivalent assertion without `expect_offense`
    #
    #     inspect_source(cop, <<-RUBY.strip_indent)
    #       a do
    #         b
    #       end.c
    #     RUBY
    #
    #     expect(cop.offenses.size).to be(1)
    #
    #     offense = cop.offenses.first
    #     expect(offense.line).to be(3)
    #     expect(offense.column_range).to be(0...5)
    #     expect(offense.message).to eql(
    #       'Avoid chaining a method call on a do...end block.'
    #     )
    #
    # If you do not want to specify an offense then use the
    # companion method `expect_no_offenses`. This method is a much
    # simpler assertion since it just inspects the source and checks
    # that there were no offenses. The `expect_offenses` method has
    # to do more work by parsing out lines that contain carets.
    module ExpectOffense
      DEFAULT_FILENAME = 'example.rb'.freeze

      def expect_offense(source, filename = DEFAULT_FILENAME)
        expected_annotations = AnnotatedSource.parse(source)

        if expected_annotations.plain_source == source
          raise 'Use expect_no_offenses to assert that no offenses are found'
        end

        inspect_source(cop, expected_annotations.plain_source, filename)
        actual_annotations =
          expected_annotations.with_offense_annotations(cop.offenses)
        expect(actual_annotations.to_s).to eq(expected_annotations.to_s)
      end

      def expect_no_offenses(source, filename = DEFAULT_FILENAME)
        inspect_source(cop, source, filename)

        expect(cop.offenses).to be_empty
      end

      # Parsed representation of code annotated with the `^^^ Message` style
      class AnnotatedSource
        ANNOTATION_PATTERN = /\A\s*\^+ /
{'fraction_non_alphanumeric': 0.07692307692307693, 'fraction_numerical': 0.0015618898867629833, 'mean_word_length': 2.4568151147098516, 'pattern_counts': {'":': 0, '<': 11, '<?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': '2958870', 'n_tokens_mistral': 1520, 'n_tokens_neox': 1473, 'n_words': 467}