<commit_before>require 'spec_helper'

describe ActionArgs::ParamsHandler do
  # ActionArgs::ParamsHandler.extract_method_arguments_from_params(method_parameters, params)
  describe 'extract_method_arguments_from_params' do
    let(:params) { {a: '1', b: '2'} }
    subject { ActionArgs::ParamsHandler.extract_method_arguments_from_params method(:m).parameters, params }
    context 'no parameters' do
      before do
        def m() end
      end
      it { should == [] }
    end

    context '1 req' do
      before do
        def m(a) end
      end
      it { should == ['1'] }
    end

    context '2 reqs' do
      before do
        def m(a, b) end
      end
      it { should == ['1', '2'] }
    end

    context '1 opt with value' do
      before do
        def m(a = 'a') end
      end
      it { should == ['1'] }
    end

    context '1 opt without value' do
      before do
        def m(x = 'x') end
      end
      it { should == [] }
    end

    context 'req, opt with value' do
      before do
        def m(a, b = 'b') end
      end
      it { should == ['1', '2'] }
    end

    context 'req, opt without value' do
      before do
        def m(a, x = 'x') end
      end
      it { should == ['1'] }
    end

    context 'opt with value, opt with value' do
      before do
        def m(a = 'a', b = 'b') end
      end
      it { should == ['1', '2'] }
    end

    context 'opt with value, opt without value' do
      before do
        def m(a = 'a', x = 'x') end
      end
      it { should == ['1'] }
    end

    context 'opt without value, opt with value' do
      before do
        def m(x = 'x', a = 'a') end
      end
      it { should == [nil, '1'] }
    end

    context 'opt without value, opt without value' do
      before do
        def m(x = 'x', y = 'y') end
      end
      it { should == [] }
    end

    context 'opt with value, req' do
      before do
        def m(a = 'a', b) end
      end
      it { should == ['1', '2'] }
    end

    context 'opt without value, req' do
      before do
        def m(x = 'x', a) end
      end
      it { should == ['1'] }
    end

    context 'opt without value, opt with value, req' do
      before do
        def m(x = 'x', b = 'b', a) end
      end
      it { should == [nil, '2', '1'] }
    end

    context 'opt with value, opt without value, req' do
      before do
        def m(b = 'b', x = 'x', a) end
      end
      it { should == ['2', '1'] }
    end

    if RUBY_VERSION >= '2'
      eval <<-KWARGS_TEST
        context 'key' do
          before do
            def m(a: nil) end
          end
          it { should == [a: '1'] }
        end<commit_msg>Test that missing :req param raises AC::BadRequest<commit_after>require 'spec_helper'

describe ActionArgs::ParamsHandler do
  # ActionArgs::ParamsHandler.extract_method_arguments_from_params(method_parameters, params)
  describe 'extract_method_arguments_from_params' do
    let(:params) { {a: '1', b: '2'} }
    subject { ActionArgs::ParamsHandler.extract_method_arguments_from_params method(:m).parameters, params }
    context 'no parameters' do
      before do
        def m() end
      end
      it { should == [] }
    end

    context '1 req' do
      before do
        def m(a) end
      end
      it { should == ['1'] }
    end

    context '2 reqs' do
      before do
        def m(a, b) end
      end
      it { should == ['1', '2'] }
    end

    context '1 opt with value' do
      before do
        def m(a = 'a') end
      end
      it { should == ['1'] }
    end

    context '1 opt without value' do
      before do
        def m(x = 'x') end
      end
      it { should == [] }
    end

    context 'req, opt with value' do
      before do
        def m(a, b = 'b') end
      end
      it { should == ['1', '2'] }
    end

    context 'req, opt without value' do
      before do
        def m(a, x = 'x') end
      end
      it { should == ['1'] }
    end

    context 'opt with value, opt with value' do
      before do
        def m(a = 'a', b = 'b') end
      end
      it { should == ['1', '2'] }
    end

    context 'opt with value, opt without value' do
      before do
        def m(a = 'a', x = 'x') end
      end
      it { should == ['1'] }
    end

    context 'opt without value, opt with value' do
      before do
        def m(x = 'x', a = 'a') end
      end
      it { should == [nil, '1'] }
    end

    context 'opt without value, opt without value' do
      before do
        def m(x = 'x', y = 'y') end
      end
      it { should == [] }
    end

    context 'opt with value, req' do
      before do
        def m(a = 'a', b) end
      end
      it { should == ['1', '2'] }
    end

    context 'opt without value, req' do
      before do
        def m(x = 'x', a) end
      end
      it { should == ['1'] }
    end

    context 'opt without value, opt with value, req' do
      before do
        def m(x = 'x', b = 'b', a) end
      end
      it { should == [nil, '2', '1'] }
    end

    context 'opt with value, opt without value, req' do
      before do
        def m(b = 'b', x = 'x', a) end
      end
      it { should == ['2', '1'] }
    end

    if Rails::VERSION::MAJOR >= 4
      context 'req without a value' do
        before do
          def m(x) end
        end
        it { expect { subject }.to raise_error ActionController::BadRequest }
      end
    end

    if RUBY_VERSION >= '2'
      eval <<-KWARGS_TEST
        context 'key' do
          before do
            def m(a: nil) end
          end
          it { should == [a: '1'] }
        end
{'fraction_non_alphanumeric': 0.1240981240981241, 'fraction_numerical': 0.00955988455988456, 'mean_word_length': 1.6354562737642586, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 6, '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': '3619638', 'n_tokens_mistral': 1824, 'n_tokens_neox': 1786, 'n_words': 674}