Skip to content

Commit

Permalink
Adding call through to existing defiend handler
Browse files Browse the repository at this point in the history
  • Loading branch information
bmalinconico committed Sep 1, 2022
1 parent e8d2288 commit d0ff758
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 2 deletions.
13 changes: 11 additions & 2 deletions lib/rspec/sorbet/doubles.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -23,6 +25,13 @@ def allow_doubles!
INLINE_DOUBLE_REGEX =
/T.(?:let|cast): Expected type (?:T.(?<t_method>any|nilable|class_of)\()?(?<expected_types>[a-zA-Z0-9:: ,]*)(\))?, got (?:type .* with value )?#<(?<double_type>Instance|Class|Object)?Double([\(]|[ ])(?<doubled_type>[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
Expand Down Expand Up @@ -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, ''))
Expand Down Expand Up @@ -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
Expand Down
27 changes: 27 additions & 0 deletions spec/lib/rspec/sorbet_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down

0 comments on commit d0ff758

Please sign in to comment.