From d0ff758488c8337b9017ceb8a80c2c04b3efe2f8 Mon Sep 17 00:00:00 2001 From: Brian Malinconico Date: Thu, 1 Sep 2022 14:51:23 -0400 Subject: [PATCH] Adding call through to existing defiend handler --- lib/rspec/sorbet/doubles.rb | 13 +++++++++++-- spec/lib/rspec/sorbet_spec.rb | 27 +++++++++++++++++++++++++++ 2 files changed, 38 insertions(+), 2 deletions(-) diff --git a/lib/rspec/sorbet/doubles.rb b/lib/rspec/sorbet/doubles.rb index e412736..18c81c3 100644 --- a/lib/rspec/sorbet/doubles.rb +++ b/lib/rspec/sorbet/doubles.rb @@ -11,6 +11,8 @@ def allow_doubles! inline_type_error_handler(error) end + @existing_handler = T::Configuration.instance_variable_get(:@call_validation_error_handler) + T::Configuration.call_validation_error_handler = proc do |signature, opts| call_validation_error_handler(signature, opts) end @@ -23,6 +25,13 @@ def allow_doubles! INLINE_DOUBLE_REGEX = /T.(?:let|cast): Expected type (?:T.(?any|nilable|class_of)\()?(?[a-zA-Z0-9:: ,]*)(\))?, got (?:type .* with value )?#<(?Instance|Class|Object)?Double([\(]|[ ])(?[a-zA-Z0-9:: ,]*)(\))?/.freeze + + def handle_call_validation_error(signature, opts) + raise TypeError, opts[:pretty_message] unless @existing_handler + + @existing_handler.call(signature, opts) + end + def inline_type_error_handler(error) case error when TypeError @@ -75,7 +84,7 @@ def typed_array_message?(message) message.match?(TYPED_ARRAY_MESSAGE) end - def call_validation_error_handler(_signature, opts) + def call_validation_error_handler(signature, opts) should_raise = true message = opts.fetch(:pretty_message, opts.fetch(:message, '')) @@ -108,7 +117,7 @@ def call_validation_error_handler(_signature, opts) end end - raise TypeError, opts[:pretty_message] if should_raise + handle_call_validation_error(signature, opts) if should_raise end end end diff --git a/spec/lib/rspec/sorbet_spec.rb b/spec/lib/rspec/sorbet_spec.rb index eef5c4e..93ec4e0 100644 --- a/spec/lib/rspec/sorbet_spec.rb +++ b/spec/lib/rspec/sorbet_spec.rb @@ -131,6 +131,33 @@ class Animal; end it_should_behave_like 'it allows an instance double' end + describe 'with an existing error handler' do + let(:handler) { proc {|_,_| } } + + before do + T::Configuration.call_validation_error_handler = handler + described_class.allow_doubles! + end + + + class PassthroughSig + extend T::Sig + + sig { params(message: String).void } + def initialize(message) + @message = message + end + end + + specify do + expect(handler).to receive(:call) + + # Error is not rasied becasue handler is no-op + expect { PassthroughSig.new(123) }.not_to raise_error + end + + end + describe 'class doubles' do extend T::Sig