Skip to content
This repository has been archived by the owner on May 4, 2024. It is now read-only.

Commit

Permalink
Merge pull request #65 from st0012/impl-#64
Browse files Browse the repository at this point in the history
Add tag option
  • Loading branch information
st0012 committed Sep 6, 2020
2 parents c72cbcf + dff87f1 commit 0cf582e
Show file tree
Hide file tree
Showing 5 changed files with 55 additions and 19 deletions.
1 change: 1 addition & 0 deletions lib/tapping_device.rb
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,7 @@ def build_payload(tp:, filepath:, line_number:)
defined_class: tp.defined_class,
trace: get_traces(tp),
is_private_call?: tp.defined_class.private_method_defined?(tp.callee_id),
tag: options[:tag],
tp: tp
})
end
Expand Down
12 changes: 7 additions & 5 deletions lib/tapping_device/output/payload.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,18 @@ class TappingDevice
module Output
class Payload < Payload
UNDEFINED = "[undefined]"
PRIVATE_MARK = " (private)"

alias :raw_arguments :arguments
alias :raw_return_value :return_value

def method_name(options = {})
if is_private_call?
":#{super(options)} (private)"
else
":#{super(options)}"
end
name = ":#{super(options)}"

name += " [#{tag}]" if tag
name += PRIVATE_MARK if is_private_call?

name
end

def arguments(options = {})
Expand Down
2 changes: 1 addition & 1 deletion lib/tapping_device/payload.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ class TappingDevice
class Payload < Hash
ATTRS = [
:target, :receiver, :method_name, :method_object, :arguments, :return_value, :filepath, :line_number,
:defined_class, :trace, :tp, :ivar_changes, :is_private_call?
:defined_class, :trace, :tag, :tp, :ivar_changes, :is_private_call?
]

ATTRS.each do |attr|
Expand Down
1 change: 1 addition & 0 deletions spec/payload_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
:line_number,
:defined_class,
:trace,
:tag,
:tp,
:is_private_call?
]
Expand Down
58 changes: 45 additions & 13 deletions spec/trackable/output_helpers_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
RSpec.describe TappingDevice::Trackable do
let(:cart) { Cart.new }
let(:service) { OrderCreationService.new }
let(:options) { { colorize: false } }

shared_examples "output calls examples" do
let(:expected_output) do
Expand Down Expand Up @@ -34,6 +35,37 @@
expect { service.perform(cart) }.to produce_expected_output(expected_output)
end

context "with tag: option" do
let(:options) { { colorize: false, tag: "service" } }
let(:expected_output) do
/:validate_cart \[service\] # OrderCreationService
from: .*:.*
<= {cart: #<Cart:.*>}
=> #<Cart:.*>
:apply_discount \[service\] # OrderCreationService
from: .*:.*
<= {cart: #<Cart:.*>}
=> #<Cart:.*>
:create_order \[service\] # OrderCreationService
from: .*:.*
<= {cart: #<Cart:.*>}
=> #<Order:.*>
:perform \[service\] # OrderCreationService
from: .*:.*
<= {cart: #<Cart:.*>}
=> #<Order:.*>/
end

it "prints out target's calls in detail" do
tap_action

expect { service.perform(cart) }.to produce_expected_output(expected_output)
end
end

context "with '.with' chained" do
let(:expected_output) do
/:create_order # OrderCreationService
Expand Down Expand Up @@ -177,7 +209,7 @@ def produce_expected_output(expected_output)
end

describe "#print_calls" do
let(:tap_action) { print_calls(service, colorize: false) }
let(:tap_action) { print_calls(service, options) }

include_context "order creation"
it_behaves_like "output calls examples" do
Expand All @@ -192,7 +224,7 @@ def produce_expected_output(expected_output)
end

describe "#print_traces" do
let(:tap_action) { print_traces(cart, colorize: false) }
let(:tap_action) { print_traces(cart, options) }

include_context "order creation"
it_behaves_like "output traces examples" do
Expand All @@ -208,7 +240,7 @@ def produce_expected_output(expected_output)

describe "#print_mutations" do
let(:student) { Student.new("Stan", 26) }
let(:tap_action) { print_mutations(student, colorize: false) }
let(:tap_action) { print_mutations(student, options) }

it_behaves_like "output mutations examples"
end
Expand All @@ -220,22 +252,22 @@ def produce_expected_output(expected_output)
end

describe "#print_instance_calls" do
let(:tap_action) { print_instance_calls(OrderCreationService, colorize: false) }
let(:tap_action) { print_instance_calls(OrderCreationService, options) }

include_context "order creation"
it_behaves_like "output calls examples"
end

describe "#print_instance_traces" do
let(:tap_action) { print_instance_traces(Cart, colorize: false) }
let(:tap_action) { print_instance_traces(Cart, options) }

include_context "order creation"
it_behaves_like "output traces examples"
end

describe "#print_instance_mutations" do
let(:student) { Student.new("Stan", 26) }
let(:tap_action) { print_instance_mutations(Student, colorize: false) }
let(:tap_action) { print_instance_mutations(Student, options) }

it_behaves_like "output mutations examples"
end
Expand All @@ -247,7 +279,7 @@ def produce_expected_output(log_file = output_log_file, expected_output)
end

describe "#write_calls" do
let(:tap_action) { write_calls(service, colorize: false) }
let(:tap_action) { write_calls(service, options) }

include_context "order creation"
it_behaves_like "output calls examples" do
Expand All @@ -262,7 +294,7 @@ def produce_expected_output(log_file = output_log_file, expected_output)
end

describe "#write_traces" do
let(:tap_action) { write_traces(cart, colorize: false) }
let(:tap_action) { write_traces(cart, options) }

include_context "order creation"
it_behaves_like "output traces examples" do
Expand All @@ -278,7 +310,7 @@ def produce_expected_output(log_file = output_log_file, expected_output)

describe "#write_mutations" do
let(:student) { Student.new("Stan", 26) }
let(:tap_action) { write_mutations(student, colorize: false) }
let(:tap_action) { write_mutations(student, options) }

it_behaves_like "output mutations examples"
end
Expand All @@ -294,7 +326,7 @@ def produce_expected_output(log_file = output_log_file, expected_output)
changes:
@name: "Stan" => "Sean"/

write_mutations(student, log_file: log_file, colorize: false)
write_mutations(student, options.merge(log_file: log_file))

expect { student.name = "Sean" }.to produce_expected_output(log_file, expected_output)

Expand All @@ -310,22 +342,22 @@ def produce_expected_output(log_file = output_log_file, expected_output)
end

describe "#write_instance_calls" do
let(:tap_action) { write_instance_calls(OrderCreationService, colorize: false) }
let(:tap_action) { write_instance_calls(OrderCreationService, options) }

include_context "order creation"
it_behaves_like "output calls examples"
end

describe "#write_instance_traces" do
let(:tap_action) { write_instance_traces(Cart, colorize: false) }
let(:tap_action) { write_instance_traces(Cart, options) }

include_context "order creation"
it_behaves_like "output traces examples"
end

describe "#write_instance_mutations" do
let(:student) { Student.new("Stan", 26) }
let(:tap_action) { write_instance_mutations(Student, colorize: false) }
let(:tap_action) { write_instance_mutations(Student, options) }

it_behaves_like "output mutations examples"
end
Expand Down

0 comments on commit 0cf582e

Please sign in to comment.