<commit_before>require "rspec/parameterized/version"
require 'parser'
require 'unparser'
require 'proc_to_ast'
require 'rspec/parameterized/helper_methods'
require 'rspec/parameterized/example_helper_methods'

module RSpec
  module Parameterized
    autoload :TableSyntax, 'rspec/parameterized/table_syntax'

    module ExampleGroupMethods
      # capsulize parameter attributes
      class Parameter
        attr_reader :arg_names, :block

        def initialize(arg_names, &block)
          @arg_names, @block = arg_names, block
        end
      end

      # Set parameters to be bound in specs under this example group.
      #
      # ## Example1
      #
      #     where(:a, :b, :answer) do
      #       [
      #         [1 , 2 , 3],
      #         [5 , 8 , 13],
      #         [0 , 0 , 0]
      #       ]
      #     end
      #
      # ## Example2
      #     using RSpec::Parameterized::TableSyntax
      #     where(:a, :b, :answer) do
      #       1 | 2 | 3 >
      #       5 | 8 | 13 >
      #       0 | 0 | 0
      #     end
      #
      def where(*args, &b)

        if args.size == 1 && args[0].instance_of?(Hash)
          naming_func = args.first.delete(:case_names)
          params = args[0]
          first, *rest = params.values
          arg_names = params.keys
          arg_values = first.product(*rest)

          if naming_func && naming_func.respond_to?(:call)
            arg_names << :case_name
            arg_values.map! { |row| row << naming_func.call(*row) }
          end

          set_parameters(arg_names) {
            arg_values
          }
        elsif args.size == 0
          set_verbose_parameters(&b)
        else
          set_parameters(args, &b)
        end
      end

      # Use parameters to execute the block.
      # The given block is converted into +describe+s for each parameter set.
      #
      # ## Example
      #     with_them do
      #       it "should do additions" do
      #         (a + b).should == answer
      #       end
      #     end
      #
      def with_them(*args, &b)
        opts = args.last.is_a?(Hash) ? args.pop : {}
        opts[:caller] = caller unless opts[:caller]
        args.push(opts)

        if @parameter.nil?
          @parameterized_pending_cases ||= []
          @parameterized_pending_cases << [args, b]
        else
          define_cases(@parameter, *args, &b)
        end
      end

      private
      def set_parameters(arg_names, &b)
        @parameter = Parameter.new(arg_names, &b)

        if @parameterized_pending_cases
          @parameterized_pending_cases.each { |e|
            define_cases(@parameter, *e[0], &e[1])
          }
        end
      end

      def define_cases(parameter, *args, &block)<commit_msg>Fix "warning: instance variable @parameter not initialized" raised when `--warnings` are enabled<commit_after>require "rspec/parameterized/version"
require 'parser'
require 'unparser'
require 'proc_to_ast'
require 'rspec/parameterized/helper_methods'
require 'rspec/parameterized/example_helper_methods'

module RSpec
  module Parameterized
    autoload :TableSyntax, 'rspec/parameterized/table_syntax'

    module ExampleGroupMethods
      # capsulize parameter attributes
      class Parameter
        attr_reader :arg_names, :block

        def initialize(arg_names, &block)
          @arg_names, @block = arg_names, block
        end
      end

      # Set parameters to be bound in specs under this example group.
      #
      # ## Example1
      #
      #     where(:a, :b, :answer) do
      #       [
      #         [1 , 2 , 3],
      #         [5 , 8 , 13],
      #         [0 , 0 , 0]
      #       ]
      #     end
      #
      # ## Example2
      #     using RSpec::Parameterized::TableSyntax
      #     where(:a, :b, :answer) do
      #       1 | 2 | 3 >
      #       5 | 8 | 13 >
      #       0 | 0 | 0
      #     end
      #
      def where(*args, &b)

        if args.size == 1 && args[0].instance_of?(Hash)
          naming_func = args.first.delete(:case_names)
          params = args[0]
          first, *rest = params.values
          arg_names = params.keys
          arg_values = first.product(*rest)

          if naming_func && naming_func.respond_to?(:call)
            arg_names << :case_name
            arg_values.map! { |row| row << naming_func.call(*row) }
          end

          set_parameters(arg_names) {
            arg_values
          }
        elsif args.size == 0
          set_verbose_parameters(&b)
        else
          set_parameters(args, &b)
        end
      end

      # Use parameters to execute the block.
      # The given block is converted into +describe+s for each parameter set.
      #
      # ## Example
      #     with_them do
      #       it "should do additions" do
      #         (a + b).should == answer
      #       end
      #     end
      #
      def with_them(*args, &b)
        opts = args.last.is_a?(Hash) ? args.pop : {}
        opts[:caller] = caller unless opts[:caller]
        args.push(opts)
        @parameter ||= nil

        if @parameter.nil?
          @parameterized_pending_cases ||= []
          @parameterized_pending_cases << [args, b]
        else
          define_cases(@parameter, *args, &b)
        end
      end

      private
      def set_parameters(arg_names, &b)
        @parameter = Parameter.new(arg_names, &b)

        if @parameterized_pending_cases
          @parameterized_pending_cases.each { |e|
            define_cases(@parameter, *e[0], &e[1])
          }
        end
      end

      def define_cases(parameter, *args, &block)
{'fraction_non_alphanumeric': 0.10473457675753228, 'fraction_numerical': 0.010043041606886656, 'mean_word_length': 1.7085964060223409, 'pattern_counts': {'":': 0, '<': 15, '<?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': '2820561', 'n_tokens_mistral': 1725, 'n_tokens_neox': 1655, 'n_words': 398}