Skip to content

Commit

Permalink
Format with a max line width of 100
Browse files Browse the repository at this point in the history
  • Loading branch information
jorgemanrubia committed Jun 29, 2019
1 parent 3be0934 commit 9e61bfd
Show file tree
Hide file tree
Showing 9 changed files with 46 additions and 22 deletions.
2 changes: 1 addition & 1 deletion .rubocop.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ Style/FrozenStringLiteralComment:
Enabled: false

Metrics/LineLength:
Enabled: false
Max: 105

Metrics/AbcSize:
Enabled: false
Expand Down
3 changes: 2 additions & 1 deletion impersonator.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@ Gem::Specification.new do |spec|
spec.email = ['jorge.manrubia@gmail.com']

spec.summary = 'Generate test stubs that replay recorded interactions'
spec.description = 'Record and replay object interactions. Ideal for mocking not-http services when testing (just because, for http, VCR is probably what you want)'
spec.description = 'Record and replay object interactions. Ideal for mocking not-http services'\
' when testing (just because, for http, VCR is probably what you want)'
spec.homepage = 'https://github.com/jorgemanrubia/impersonator'
spec.license = 'MIT'

Expand Down
24 changes: 16 additions & 8 deletions lib/impersonator/api.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
module Impersonator
module Api
def recording(label, disabled: false)
@current_recording = ::Impersonator::Recording.new(label, disabled: disabled, recordings_path: configuration.recordings_path)
@current_recording = ::Impersonator::Recording.new label,
disabled: disabled,
recordings_path: configuration.recordings_path
@current_recording.start
yield
@current_recording.finish
Expand Down Expand Up @@ -36,14 +38,16 @@ def reset
# impersonator = Impersonator.impersonate(:add, :subtract) { Calculator.new }
# impersonator.add(3, 4)
#
# Notice that the actual object won't be instantiated in record mode. For that reason, the impersonated
# object will only respond to the list of impersonated methods.
# Notice that the actual object won't be instantiated in record mode. For that reason, the
# impersonated object will only respond to the list of impersonated methods.
#
# If you need to invoke other (not impersonated) methods see #impersonate_method instead.
#
# @return [Object] the impersonated object
def impersonate(*methods)
raise ArgumentError, 'Provide a block to instantiate the object to impersonate in record mode' unless block_given?
unless block_given?
raise ArgumentError, 'Provide a block to instantiate the object to impersonate in record mode'
end

object_to_impersonate = if current_recording&.record_mode?
yield
Expand All @@ -55,14 +59,18 @@ def impersonate(*methods)

# Impersonates a list of methods of a given object
#
# The returned object will impersonate the list of methods and will delegate the rest of method calls
# to the actual object.
# The returned object will impersonate the list of methods and will delegate the rest of method
# calls to the actual object.
#
# @return [Object] the impersonated object
def impersonate_methods(actual_object, *methods)
raise Impersonator::Errors::ConfigurationError, 'You must start a recording to impersonate objects. Use Impersonator.recording {}' unless @current_recording
unless @current_recording
raise Impersonator::Errors::ConfigurationError, 'You must start a recording to impersonate'\
' objects. Use Impersonator.recording {}'
end

::Impersonator::Proxy.new(actual_object, recording: current_recording, impersonated_methods: methods)
::Impersonator::Proxy.new(actual_object, recording: current_recording,
impersonated_methods: methods)
end
end
end
3 changes: 2 additions & 1 deletion lib/impersonator/method.rb
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,8 @@ def ==(other_method)
other_arguments.delete_at(ignored_position)
end

name == other_method.name && my_arguments == other_arguments && !block_spy == !other_method.block_spy
name == other_method.name && my_arguments == other_arguments &&
!block_spy == !other_method.block_spy
end
end
end
12 changes: 9 additions & 3 deletions lib/impersonator/proxy.rb
Original file line number Diff line number Diff line change
Expand Up @@ -39,15 +39,21 @@ def validate_object_has_methods_to_impersonate!(object, methods_to_impersonate)
!object.respond_to?(method.to_sym)
end

raise Impersonator::Errors::ConfigurationError, "These methods to impersonate does not exist: #{missing_methods.inspect}" unless missing_methods.empty?
unless missing_methods.empty?
raise Impersonator::Errors::ConfigurationError, 'These methods to impersonate does not'\
"exist: #{missing_methods.inspect}"
end
end

def invoke_impersonated_method(method_name, *args, &block)
method = Method.new(name: method_name, arguments: args, block: block, matching_configuration: method_matching_configurations_by_method[method_name.to_sym])
matching_configuration = method_matching_configurations_by_method[method_name.to_sym]
method = Method.new(name: method_name, arguments: args, block: block,
matching_configuration: matching_configuration)
if recording.replay_mode?
recording.replay(method)
else
@impersonated_object.send(method_name, *args, &method&.block_spy&.block).tap do |return_value|
spiable_block = method&.block_spy&.block
@impersonated_object.send(method_name, *args, &spiable_block).tap do |return_value|
recording.record(method, return_value)
end
end
Expand Down
10 changes: 7 additions & 3 deletions lib/impersonator/recording.rb
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,10 @@ def record(method, return_value)

def replay(method)
method_invocation = @method_invocations.shift
raise Impersonator::Errors::MethodInvocationError, "Unexpected method invocation received: #{method}" unless method_invocation
unless method_invocation
raise Impersonator::Errors::MethodInvocationError, 'Unexpected method invocation received:'\
"#{method}"
end

validate_method_signature!(method, method_invocation.method_instance)
replay_block(method_invocation, method)
Expand Down Expand Up @@ -88,8 +91,9 @@ def finish_in_record_mode

def finish_in_replay_mode
unless @method_invocations.empty?
raise Impersonator::Errors::MethodInvocationError, "Expecting #{@method_invocations.length} method invocations"\
" that didn't happen: #{@method_invocations.inspect}"
raise Impersonator::Errors::MethodInvocationError,
"Expecting #{@method_invocations.length} method invocations"\
" that didn't happen: #{@method_invocations.inspect}"
end
end

Expand Down
9 changes: 6 additions & 3 deletions spec/errors_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,14 @@

describe Impersonator::Errors::ConfigurationError do
it 'raises an error when trying to impersonate without starting a recording' do
expect { Impersonator.impersonate_methods(actual_calculator, :next, :previous) }.to raise_error(Impersonator::Errors::ConfigurationError)
expect { Impersonator.impersonate_methods(actual_calculator, :next, :previous) }
.to raise_error(Impersonator::Errors::ConfigurationError)
end

it 'raises an error when the method to impersonate does not exist' do
Impersonator.recording('missing method') do
expect { Impersonator.impersonate_methods(actual_calculator, :some_missing_method) }.to raise_error(Impersonator::Errors::ConfigurationError)
expect { Impersonator.impersonate_methods(actual_calculator, :some_missing_method) }
.to raise_error(Impersonator::Errors::ConfigurationError)
end
end
end
Expand Down Expand Up @@ -70,7 +72,8 @@
impersonator = Impersonator.impersonate_methods(actual_calculator, :add, :lineal_sequence)

impersonator.add(1, 2, &block)
expect { impersonator.add(1, 2, &block) }.to raise_error(Impersonator::Errors::MethodInvocationError)
expect { impersonator.add(1, 2, &block) }
.to raise_error(Impersonator::Errors::MethodInvocationError)
end
end

Expand Down
2 changes: 1 addition & 1 deletion spec/impersonate_methods_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
end
end

it 'records and impersonates multiple invocations in a row of a method that returns a simple value' do
it 'records and impersonates multiple invocations in a row of a method that returns a value' do
test_impersonation do |impersonator|
expect(impersonator.next).to eq(1)
expect(impersonator.next).to eq(2)
Expand Down
3 changes: 2 additions & 1 deletion spec/recording_file_generation_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@
end

it 'eliminates symbols from label when generating file names' do
validate_fixture_was_generated for_label: '((my()#(example::))', expected_file_name: 'my-example.yml'
validate_fixture_was_generated for_label: '((my()#(example::))',
expected_file_name: 'my-example.yml'
end

def validate_fixture_was_generated(for_label:, expected_file_name:)
Expand Down

0 comments on commit 9e61bfd

Please sign in to comment.