From cdfa4cb6eb5cb969693262f39fedab155660eef3 Mon Sep 17 00:00:00 2001 From: Nikita Shilnikov Date: Sun, 12 Nov 2023 14:51:17 +0100 Subject: [PATCH] Recognize more pass-through constructor signatures (fix #91) --- lib/dry/auto_inject/method_parameters.rb | 9 ++++-- spec/unit/method_parameters_spec.rb | 38 ++++++++++++++++++++++++ 2 files changed, 45 insertions(+), 2 deletions(-) diff --git a/lib/dry/auto_inject/method_parameters.rb b/lib/dry/auto_inject/method_parameters.rb index 56b4a1a..9e6121d 100644 --- a/lib/dry/auto_inject/method_parameters.rb +++ b/lib/dry/auto_inject/method_parameters.rb @@ -10,8 +10,13 @@ class MethodParameters [%i[rest]], [%i[rest], %i[keyrest]], [%i[rest *]], - [%i[rest *], %i[keyrest **]] - ].freeze + [%i[rest *], %i[keyrest **]], + [%i[rest *], %i[keyrest **], %i[block &]], + [%i[rest *], %i[block &]], + [%i[keyrest **]], + [%i[keyrest **], %i[block &]], + [%i[block &]] + ].to_set.freeze def self.of(obj, name) Enumerator.new do |y| diff --git a/spec/unit/method_parameters_spec.rb b/spec/unit/method_parameters_spec.rb index 2aac0ab..302da73 100644 --- a/spec/unit/method_parameters_spec.rb +++ b/spec/unit/method_parameters_spec.rb @@ -45,6 +45,7 @@ def initialize(*) all_parameters = parameters.of(klass, :initialize).to_a expect(all_parameters.size).to eq 2 + if RUBY_VERSION >= "3.2" expect(all_parameters[0].parameters).to eql([[:rest, :*]]) else @@ -53,4 +54,41 @@ def initialize(*) expect(all_parameters[1]).to be_empty end end + + describe "#pass_through?" do + klass = Class.new { + def arg_kwarg(*, **) = super + + def arg(*) = super + + def kwarg(**) = super + + def ellipsis(...) = super + + if RUBY_VERSION >= "3.1" + class_eval(<<-RUBY, __FILE__, __LINE__ + 1) + def arg_kwarg_block(*, **, &) = super + + def arg_block(*, &) = super + + def kwarg_block(**, &) = super + + RUBY + else + alias_method :arg_kwarg_block, :arg + alias_method :arg_block, :arg + alias_method :kwarg_block, :kwarg + end + } + + it "returns true for pass-through methods" do + expect(parameters.of(klass, :arg_kwarg).first).to be_pass_through + expect(parameters.of(klass, :arg).first).to be_pass_through + expect(parameters.of(klass, :kwarg).first).to be_pass_through + expect(parameters.of(klass, :arg_kwarg_block).first).to be_pass_through + expect(parameters.of(klass, :arg_block).first).to be_pass_through + expect(parameters.of(klass, :kwarg_block).first).to be_pass_through + expect(parameters.of(klass, :ellipsis).first).to be_pass_through + end + end end