From 181b1020a989e4106c62db74618851fa559f40f7 Mon Sep 17 00:00:00 2001 From: Elliot Winkler Date: Wed, 31 Jan 2024 23:27:52 -0700 Subject: [PATCH] Remove instance_eval from InspectionTree (#210) The InspectionTree class defines methods that create nodes in the tree. Some of these methods take a block which is `instance_eval`'ed inside of a new subtree. This use of `instance_eval` may seem to create a very clean and easy to use API on the surface, but it quickly breaks down if, as you are defining your InspectionTree, you need to extract methods inside of your InspectionTreeBuilder and then reference them. _Not_ `instance_eval`'ing creates footguns, but it also removes the magic and mystery around how InspectionTree works. --- .../active_record_model.rb | 36 ++++++---- .../active_record_relation.rb | 21 ++++-- .../hash_with_indifferent_access.rb | 31 ++++++--- .../ordered_options.rb | 31 ++++++--- .../object_inspection/inspection_tree.rb | 45 +++++++------ .../inspection_tree_builders/array.rb | 43 ++++++++---- .../inspection_tree_builders/custom_object.rb | 28 +++++--- .../inspection_tree_builders/date_like.rb | 40 ++++++----- .../default_object.rb | 67 +++++++++++++------ .../inspection_tree_builders/hash.rb | 53 +++++++++++---- .../inspection_tree_builders/primitive.rb | 8 +-- .../inspection_tree_builders/time_like.rb | 51 ++++++++------ .../nodes/as_lines_when_rendering_to_lines.rb | 5 +- .../collection_containing_exactly.rb | 21 ++++-- .../collection_including.rb | 32 +++++---- .../inspection_tree_builders/double.rb | 56 +++++++++------- .../hash_including.rb | 33 +++++---- .../inspection_tree_builders/instance_of.rb | 19 +++--- .../inspection_tree_builders/kind_of.rb | 19 +++--- .../object_having_attributes.rb | 21 ++++-- .../inspection_tree_builders/rspec_matcher.rb | 4 +- .../inspection_tree_builders/value_within.rb | 21 +++--- 22 files changed, 432 insertions(+), 253 deletions(-) diff --git a/lib/super_diff/active_record/object_inspection/inspection_tree_builders/active_record_model.rb b/lib/super_diff/active_record/object_inspection/inspection_tree_builders/active_record_model.rb index 1f886a4a..a3b79766 100644 --- a/lib/super_diff/active_record/object_inspection/inspection_tree_builders/active_record_model.rb +++ b/lib/super_diff/active_record/object_inspection/inspection_tree_builders/active_record_model.rb @@ -8,27 +8,39 @@ def self.applies_to?(value) end def call - SuperDiff::ObjectInspection::InspectionTree.new do - as_lines_when_rendering_to_lines(collection_bookend: :open) do - add_text { |object| "#<#{object.class} " } + SuperDiff::ObjectInspection::InspectionTree.new do |t1| + t1.as_lines_when_rendering_to_lines( + collection_bookend: :open + ) do |t2| + t2.add_text "#<#{object.class} " - when_rendering_to_lines { add_text "{" } + # stree-ignore + t2.when_rendering_to_lines do |t3| + t3.add_text "{" + end end - nested do |object| - insert_separated_list( + t1.nested do |t2| + t2.insert_separated_list( ["id"] + (object.attributes.keys.sort - ["id"]) - ) do |name| - as_prefix_when_rendering_to_lines { add_text "#{name}: " } + ) do |t3, name| + t3.as_prefix_when_rendering_to_lines do |t4| + t4.add_text "#{name}: " + end - add_inspection_of object.read_attribute(name) + t3.add_inspection_of object.read_attribute(name) end end - as_lines_when_rendering_to_lines(collection_bookend: :close) do - when_rendering_to_lines { add_text "}" } + t1.as_lines_when_rendering_to_lines( + collection_bookend: :close + ) do |t2| + # stree-ignore + t2.when_rendering_to_lines do |t3| + t3.add_text "}" + end - add_text ">" + t2.add_text ">" end end end diff --git a/lib/super_diff/active_record/object_inspection/inspection_tree_builders/active_record_relation.rb b/lib/super_diff/active_record/object_inspection/inspection_tree_builders/active_record_relation.rb index decbf8ff..d85167e7 100644 --- a/lib/super_diff/active_record/object_inspection/inspection_tree_builders/active_record_relation.rb +++ b/lib/super_diff/active_record/object_inspection/inspection_tree_builders/active_record_relation.rb @@ -8,15 +8,24 @@ def self.applies_to?(value) end def call - SuperDiff::ObjectInspection::InspectionTree.new do - as_lines_when_rendering_to_lines(collection_bookend: :open) do - add_text "#" + # stree-ignore + t1.as_lines_when_rendering_to_lines( + collection_bookend: :close + ) do |t2| + t2.add_text "]>" end end end diff --git a/lib/super_diff/active_support/object_inspection/inspection_tree_builders/hash_with_indifferent_access.rb b/lib/super_diff/active_support/object_inspection/inspection_tree_builders/hash_with_indifferent_access.rb index c5933491..9af7448f 100644 --- a/lib/super_diff/active_support/object_inspection/inspection_tree_builders/hash_with_indifferent_access.rb +++ b/lib/super_diff/active_support/object_inspection/inspection_tree_builders/hash_with_indifferent_access.rb @@ -8,19 +8,34 @@ def self.applies_to?(value) end def call - SuperDiff::ObjectInspection::InspectionTree.new do - as_lines_when_rendering_to_lines(collection_bookend: :open) do - add_text "#" + # stree-ignore + t1.as_lines_when_rendering_to_lines( + collection_bookend: :close + ) do |t2| + t2.add_text "}>" end end end diff --git a/lib/super_diff/active_support/object_inspection/inspection_tree_builders/ordered_options.rb b/lib/super_diff/active_support/object_inspection/inspection_tree_builders/ordered_options.rb index 97e2270a..730d3817 100644 --- a/lib/super_diff/active_support/object_inspection/inspection_tree_builders/ordered_options.rb +++ b/lib/super_diff/active_support/object_inspection/inspection_tree_builders/ordered_options.rb @@ -8,21 +8,34 @@ def self.applies_to?(value) end def call - SuperDiff::ObjectInspection::InspectionTree.new do - as_lines_when_rendering_to_lines(collection_bookend: :open) do - add_text "#" + # stree-ignore + t1.as_lines_when_rendering_to_lines( + collection_bookend: :close + ) do |t2| + t2.add_text "}>" end end end diff --git a/lib/super_diff/object_inspection/inspection_tree.rb b/lib/super_diff/object_inspection/inspection_tree.rb index 664ece2e..2df63022 100644 --- a/lib/super_diff/object_inspection/inspection_tree.rb +++ b/lib/super_diff/object_inspection/inspection_tree.rb @@ -7,7 +7,7 @@ def initialize(disallowed_node_names: [], &block) @disallowed_node_names = disallowed_node_names @nodes = [] - instance_eval(&block) if block + evaluate_block(&block) if block end Nodes.registry.each do |node_class| @@ -26,8 +26,7 @@ def before_each_callbacks def render_to_string(object) nodes.reduce("") do |string, node| - result = node.render_to_string(object) - string + result + string + node.render_to_string(object) end end @@ -52,43 +51,45 @@ def render_to_lines(object, type:, indentation_level:) .first end - def evaluate_block(object, &block) - instance_exec(object, &block) + def evaluate_block(*args, &block) + block.call(self, *args) end def insert_array_inspection_of(array) - insert_separated_list(array) do |value| + insert_separated_list(array) do |t, value| # Have to do these shenanigans so that if value is a hash, Ruby # doesn't try to interpret it as keyword args if SuperDiff::Helpers.ruby_version_matches?(">= 2.7.1") - add_inspection_of(value, **{}) + t.add_inspection_of(value, **{}) else - add_inspection_of(*[value, {}]) + t.add_inspection_of(*[value, {}]) end end end def insert_hash_inspection_of(hash) keys = hash.keys - format_keys_as_kwargs = keys.all? { |key| key.is_a?(Symbol) } - insert_separated_list(keys) do |key| + insert_separated_list(keys) do |t1, key| if format_keys_as_kwargs - as_prefix_when_rendering_to_lines { add_text "#{key}: " } + # stree-ignore + t1.as_prefix_when_rendering_to_lines do |t2| + t2.add_text "#{key}: " + end else - as_prefix_when_rendering_to_lines do - add_inspection_of key, as_lines: false - add_text " => " + t1.as_prefix_when_rendering_to_lines do |t2| + t2.add_inspection_of key, as_lines: false + t2.add_text " => " end end # Have to do these shenanigans so that if hash[key] is a hash, Ruby # doesn't try to interpret it as keyword args if SuperDiff::Helpers.ruby_version_matches?(">= 2.7.1") - add_inspection_of(hash[key], **{}) + t1.add_inspection_of(hash[key], **{}) else - add_inspection_of(*[hash[key], {}]) + t1.add_inspection_of(*[hash[key], {}]) end end end @@ -97,10 +98,14 @@ def insert_separated_list(enumerable, &block) enumerable.each_with_index do |value, index| as_lines_when_rendering_to_lines( add_comma: index < enumerable.size - 1 - ) do - when_rendering_to_string { add_text " " } if index > 0 - - evaluate_block(value, &block) + ) do |t1| + if index > 0 + # stree-ignore + t1.when_rendering_to_string do |t2| + t2.add_text " " + end + end + t1.evaluate_block(value, &block) end end end diff --git a/lib/super_diff/object_inspection/inspection_tree_builders/array.rb b/lib/super_diff/object_inspection/inspection_tree_builders/array.rb index bee06a8e..1a6bfb2b 100644 --- a/lib/super_diff/object_inspection/inspection_tree_builders/array.rb +++ b/lib/super_diff/object_inspection/inspection_tree_builders/array.rb @@ -7,27 +7,46 @@ def self.applies_to?(value) end def call - empty = -> { object.empty? } - nonempty = -> { !object.empty? } - - InspectionTree.new do - only_when empty do - as_lines_when_rendering_to_lines { add_text "[]" } + InspectionTree.new do |t1| + t1.only_when empty do |t2| + # stree-ignore + t2.as_lines_when_rendering_to_lines do |t3| + t3.add_text "[]" + end end - only_when nonempty do - as_lines_when_rendering_to_lines(collection_bookend: :open) do - add_text "[" + t1.only_when nonempty do |t2| + # stree-ignore + t2.as_lines_when_rendering_to_lines( + collection_bookend: :open + ) do |t3| + t3.add_text "[" end - nested { |array| insert_array_inspection_of(array) } + # stree-ignore + t2.nested do |t3| + t3.insert_array_inspection_of(object) + end - as_lines_when_rendering_to_lines(collection_bookend: :close) do - add_text "]" + # stree-ignore + t2.as_lines_when_rendering_to_lines( + collection_bookend: :close + ) do |t3| + t3.add_text "]" end end end end + + private + + def empty + -> { object.empty? } + end + + def nonempty + -> { !object.empty? } + end end end end diff --git a/lib/super_diff/object_inspection/inspection_tree_builders/custom_object.rb b/lib/super_diff/object_inspection/inspection_tree_builders/custom_object.rb index 03a72aa5..741db24a 100644 --- a/lib/super_diff/object_inspection/inspection_tree_builders/custom_object.rb +++ b/lib/super_diff/object_inspection/inspection_tree_builders/custom_object.rb @@ -7,21 +7,31 @@ def self.applies_to?(value) end def call - InspectionTree.new do - as_lines_when_rendering_to_lines(collection_bookend: :open) do - add_text { |object| "#<#{object.class} " } + InspectionTree.new do |t1| + t1.as_lines_when_rendering_to_lines( + collection_bookend: :open + ) do |t2| + t2.add_text "#<#{object.class} " - when_rendering_to_lines { add_text "{" } + # stree-ignore + t2.when_rendering_to_lines do |t3| + t3.add_text "{" + end end - nested do |object| - insert_hash_inspection_of(object.attributes_for_super_diff) + t1.nested do |t2| + t2.insert_hash_inspection_of(object.attributes_for_super_diff) end - as_lines_when_rendering_to_lines(collection_bookend: :close) do - when_rendering_to_lines { add_text "}" } + t1.as_lines_when_rendering_to_lines( + collection_bookend: :close + ) do |t2| + # stree-ignore + t2.when_rendering_to_lines do |t3| + t3.add_text "}" + end - add_text ">" + t2.add_text ">" end end end diff --git a/lib/super_diff/object_inspection/inspection_tree_builders/date_like.rb b/lib/super_diff/object_inspection/inspection_tree_builders/date_like.rb index 0e5d7101..a8dc6402 100644 --- a/lib/super_diff/object_inspection/inspection_tree_builders/date_like.rb +++ b/lib/super_diff/object_inspection/inspection_tree_builders/date_like.rb @@ -7,31 +7,41 @@ def self.applies_to?(value) end def call - InspectionTree.new do - as_lines_when_rendering_to_lines(collection_bookend: :open) do - add_text { |date| "#<#{date.class} " } + InspectionTree.new do |t1| + t1.as_lines_when_rendering_to_lines( + collection_bookend: :open + ) do |t2| + t2.add_text "#<#{object.class} " - when_rendering_to_lines { add_text "{" } + # stree-ignore + t2.when_rendering_to_lines do |t3| + t3.add_text "{" + end end - when_rendering_to_string do - add_text { |date| date.strftime("%Y-%m-%d") } + t1.when_rendering_to_string do |t2| + t2.add_text object.strftime("%Y-%m-%d") end - when_rendering_to_lines do - nested do |date| - insert_separated_list(%i[year month day]) do |name| - add_text name.to_s - add_text ": " - add_inspection_of date.public_send(name) + t1.when_rendering_to_lines do |t2| + t2.nested do |t3| + t3.insert_separated_list(%i[year month day]) do |t4, name| + t4.add_text name.to_s + t4.add_text ": " + t4.add_inspection_of object.public_send(name) end end end - as_lines_when_rendering_to_lines(collection_bookend: :close) do - when_rendering_to_lines { add_text "}" } + t1.as_lines_when_rendering_to_lines( + collection_bookend: :close + ) do |t2| + # stree-ignore + t2.when_rendering_to_lines do |t3| + t3.add_text "}" + end - add_text ">" + t2.add_text ">" end end end diff --git a/lib/super_diff/object_inspection/inspection_tree_builders/default_object.rb b/lib/super_diff/object_inspection/inspection_tree_builders/default_object.rb index 81831c63..42a54d58 100644 --- a/lib/super_diff/object_inspection/inspection_tree_builders/default_object.rb +++ b/lib/super_diff/object_inspection/inspection_tree_builders/default_object.rb @@ -7,47 +7,72 @@ def self.applies_to?(_value) end def call - empty = -> { object.instance_variables.empty? } - nonempty = -> { !object.instance_variables.empty? } - - InspectionTree.new do - only_when empty do - as_lines_when_rendering_to_lines do - add_text do |object| + InspectionTree.new do |t1| + t1.only_when empty do |t2| + t2.as_lines_when_rendering_to_lines do |t3| + t3.add_text( "#<#{object.class.name}:" + SuperDiff::Helpers.object_address_for(object) + ">" - end + ) end end - only_when nonempty do - as_lines_when_rendering_to_lines(collection_bookend: :open) do - add_text do |object| + t1.only_when nonempty do |t2| + t2.as_lines_when_rendering_to_lines( + collection_bookend: :open + ) do |t3| + t3.add_text( "#<#{object.class.name}:" + SuperDiff::Helpers.object_address_for(object) - end + ) - when_rendering_to_lines { add_text " {" } + # stree-ignore + t3.when_rendering_to_lines do |t4| + t4.add_text " {" + end end - when_rendering_to_string { add_text " " } + # stree-ignore + t2.when_rendering_to_string do |t3| + t3.add_text " " + end - nested do |object| - insert_separated_list(object.instance_variables.sort) do |name| - as_prefix_when_rendering_to_lines { add_text "#{name}=" } + t2.nested do |t3| + t3.insert_separated_list( + object.instance_variables.sort + ) do |t4, name| + # stree-ignore + t4.as_prefix_when_rendering_to_lines do |t5| + t5.add_text "#{name}=" + end - add_inspection_of object.instance_variable_get(name) + t4.add_inspection_of object.instance_variable_get(name) end end - as_lines_when_rendering_to_lines(collection_bookend: :close) do - when_rendering_to_lines { add_text "}" } + t2.as_lines_when_rendering_to_lines( + collection_bookend: :close + ) do |t3| + # stree-ignore + t3.when_rendering_to_lines do |t4| + t4.add_text "}" + end - add_text ">" + t3.add_text ">" end end end end + + private + + def empty + -> { object.instance_variables.empty? } + end + + def nonempty + -> { !object.instance_variables.empty? } + end end end end diff --git a/lib/super_diff/object_inspection/inspection_tree_builders/hash.rb b/lib/super_diff/object_inspection/inspection_tree_builders/hash.rb index 45a43c31..ecce40bd 100644 --- a/lib/super_diff/object_inspection/inspection_tree_builders/hash.rb +++ b/lib/super_diff/object_inspection/inspection_tree_builders/hash.rb @@ -7,31 +7,56 @@ def self.applies_to?(value) end def call - empty = -> { object.empty? } - nonempty = -> { !object.empty? } - - InspectionTree.new do - only_when empty do - as_lines_when_rendering_to_lines { add_text "{}" } + InspectionTree.new do |t1| + t1.only_when empty do |t2| + # stree-ignore + t2.as_lines_when_rendering_to_lines do |t3| + t3.add_text "{}" + end end - only_when nonempty do - as_lines_when_rendering_to_lines(collection_bookend: :open) do - add_text "{" + t1.only_when nonempty do |t2| + # stree-ignore + t2.as_lines_when_rendering_to_lines( + collection_bookend: :open + ) do |t3| + t3.add_text "{" end - when_rendering_to_string { add_text " " } + # stree-ignore + t2.when_rendering_to_string do |t3| + t3.add_text " " + end - nested { |hash| insert_hash_inspection_of(hash) } + # stree-ignore + t2.nested do |t3| + t3.insert_hash_inspection_of(object) + end - when_rendering_to_string { add_text " " } + # stree-ignore + t2.when_rendering_to_string do |t3| + t3.add_text " " + end - as_lines_when_rendering_to_lines(collection_bookend: :close) do - add_text "}" + # stree-ignore + t2.as_lines_when_rendering_to_lines( + collection_bookend: :close + ) do |t3| + t3.add_text "}" end end end end + + private + + def empty + -> { object.empty? } + end + + def nonempty + -> { !object.empty? } + end end end end diff --git a/lib/super_diff/object_inspection/inspection_tree_builders/primitive.rb b/lib/super_diff/object_inspection/inspection_tree_builders/primitive.rb index 9374b21d..4a3b021e 100644 --- a/lib/super_diff/object_inspection/inspection_tree_builders/primitive.rb +++ b/lib/super_diff/object_inspection/inspection_tree_builders/primitive.rb @@ -7,11 +7,9 @@ def self.applies_to?(value) end def call - InspectionTree.new do - as_lines_when_rendering_to_lines do - # rubocop:disable Style/SymbolProc - add_text { |object| object.inspect } - # rubocop:enable Style/SymbolProc + InspectionTree.new do |t1| + t1.as_lines_when_rendering_to_lines do |t2| + t2.add_text object.inspect end end end diff --git a/lib/super_diff/object_inspection/inspection_tree_builders/time_like.rb b/lib/super_diff/object_inspection/inspection_tree_builders/time_like.rb index b31bf268..0739a2d6 100644 --- a/lib/super_diff/object_inspection/inspection_tree_builders/time_like.rb +++ b/lib/super_diff/object_inspection/inspection_tree_builders/time_like.rb @@ -7,37 +7,48 @@ def self.applies_to?(value) end def call - InspectionTree.new do - as_lines_when_rendering_to_lines(collection_bookend: :open) do - add_text { |time| "#<#{time.class} " } + InspectionTree.new do |t1| + t1.as_lines_when_rendering_to_lines( + collection_bookend: :open + ) do |t2| + t2.add_text "#<#{object.class} " - when_rendering_to_lines { add_text "{" } + # stree-ignore + t2.when_rendering_to_lines do |t3| + t3.add_text "{" + end end - when_rendering_to_string do - add_text do |time| - time.strftime("%Y-%m-%d %H:%M:%S") + - (time.subsec == 0 ? "" : "+#{time.subsec.inspect}") + " " + - time.strftime("%:z") + (time.zone ? " (#{time.zone})" : "") - end + t1.when_rendering_to_string do |t2| + t2.add_text( + object.strftime("%Y-%m-%d %H:%M:%S") + + (object.subsec == 0 ? "" : "+#{object.subsec.inspect}") + + " " + object.strftime("%:z") + + (object.zone ? " (#{object.zone})" : "") + ) end - when_rendering_to_lines do - nested do |time| - insert_separated_list( + t1.when_rendering_to_lines do |t2| + t2.nested do |t3| + t3.insert_separated_list( %i[year month day hour min sec subsec zone utc_offset] - ) do |name| - add_text name.to_s - add_text ": " - add_inspection_of time.public_send(name) + ) do |t4, name| + t4.add_text name.to_s + t4.add_text ": " + t4.add_inspection_of object.public_send(name) end end end - as_lines_when_rendering_to_lines(collection_bookend: :close) do - when_rendering_to_lines { add_text "}" } + t1.as_lines_when_rendering_to_lines( + collection_bookend: :close + ) do |t2| + # stree-ignore + t2.when_rendering_to_lines do |t3| + t3.add_text "}" + end - add_text ">" + t2.add_text ">" end end end diff --git a/lib/super_diff/object_inspection/nodes/as_lines_when_rendering_to_lines.rb b/lib/super_diff/object_inspection/nodes/as_lines_when_rendering_to_lines.rb index bf3ed75f..f8a13158 100644 --- a/lib/super_diff/object_inspection/nodes/as_lines_when_rendering_to_lines.rb +++ b/lib/super_diff/object_inspection/nodes/as_lines_when_rendering_to_lines.rb @@ -15,9 +15,10 @@ def initialize( *args, add_comma: false, collection_bookend: nil, - **rest + **rest, + &block ) - super(tree, *args, **rest) + super(tree, *args, **rest, &block) @add_comma = add_comma @collection_bookend = collection_bookend diff --git a/lib/super_diff/rspec/object_inspection/inspection_tree_builders/collection_containing_exactly.rb b/lib/super_diff/rspec/object_inspection/inspection_tree_builders/collection_containing_exactly.rb index 240a4836..b64325f1 100644 --- a/lib/super_diff/rspec/object_inspection/inspection_tree_builders/collection_containing_exactly.rb +++ b/lib/super_diff/rspec/object_inspection/inspection_tree_builders/collection_containing_exactly.rb @@ -8,17 +8,24 @@ def self.applies_to?(value) end def call - SuperDiff::ObjectInspection::InspectionTree.new do - as_lines_when_rendering_to_lines(collection_bookend: :open) do - add_text "#" + # stree-ignore + t1.as_lines_when_rendering_to_lines( + collection_bookend: :close + ) do |t2| + t2.add_text ")>" end end end diff --git a/lib/super_diff/rspec/object_inspection/inspection_tree_builders/collection_including.rb b/lib/super_diff/rspec/object_inspection/inspection_tree_builders/collection_including.rb index 5919fefa..cb4b6887 100644 --- a/lib/super_diff/rspec/object_inspection/inspection_tree_builders/collection_including.rb +++ b/lib/super_diff/rspec/object_inspection/inspection_tree_builders/collection_including.rb @@ -9,23 +9,29 @@ def self.applies_to?(value) end def call - SuperDiff::ObjectInspection::InspectionTree.new do - as_lines_when_rendering_to_lines(collection_bookend: :open) do - add_text "#" + # stree-ignore + t1.as_lines_when_rendering_to_lines( + collection_bookend: :close + ) do |t2| + t2.add_text ")>" end end end diff --git a/lib/super_diff/rspec/object_inspection/inspection_tree_builders/double.rb b/lib/super_diff/rspec/object_inspection/inspection_tree_builders/double.rb index 5dd7b1a4..8278db45 100644 --- a/lib/super_diff/rspec/object_inspection/inspection_tree_builders/double.rb +++ b/lib/super_diff/rspec/object_inspection/inspection_tree_builders/double.rb @@ -8,47 +8,51 @@ def self.applies_to?(value) end def call - builder = self - empty = -> { empty? } - nonempty = -> { !empty? } - - SuperDiff::ObjectInspection::InspectionTree.new do - only_when empty do - as_lines_when_rendering_to_lines do - add_text do |object| - inspected_class = builder.inspected_class - inspected_name = builder.inspected_name - "#<#{inspected_class} #{inspected_name}>" - end + SuperDiff::ObjectInspection::InspectionTree.new do |t1| + t1.only_when method(:empty?) do |t2| + t2.as_lines_when_rendering_to_lines do |t3| + t3.add_text("#<#{inspected_class} #{inspected_name}>") end end - only_when nonempty do - as_lines_when_rendering_to_lines(collection_bookend: :open) do - add_text do |object| - inspected_class = builder.inspected_class - inspected_name = builder.inspected_name - "#<#{inspected_class} #{inspected_name}" - end + t1.only_when method(:nonempty?) do |t2| + t2.as_lines_when_rendering_to_lines( + collection_bookend: :open + ) do |t3| + t3.add_text("#<#{inspected_class} #{inspected_name}") - when_rendering_to_lines { add_text " {" } + # stree-ignore + t3.when_rendering_to_lines do |t4| + t4.add_text " {" + end end - when_rendering_to_string { add_text " " } + # stree-ignore + t2.when_rendering_to_string do |t3| + t3.add_text " " + end - nested do |object| - insert_hash_inspection_of(builder.doubled_methods) + # stree-ignore + t2.nested do |t3| + t3.insert_hash_inspection_of doubled_methods end - as_lines_when_rendering_to_lines(collection_bookend: :close) do - when_rendering_to_lines { add_text "}" } + t2.as_lines_when_rendering_to_lines( + collection_bookend: :close + ) do |t3| + # stree-ignore + t3.when_rendering_to_lines do |t4| + t4.add_text "}" + end - add_text ">" + t3.add_text ">" end end end end + private + def empty? doubled_methods.empty? end diff --git a/lib/super_diff/rspec/object_inspection/inspection_tree_builders/hash_including.rb b/lib/super_diff/rspec/object_inspection/inspection_tree_builders/hash_including.rb index 7387db95..9cb977bf 100644 --- a/lib/super_diff/rspec/object_inspection/inspection_tree_builders/hash_including.rb +++ b/lib/super_diff/rspec/object_inspection/inspection_tree_builders/hash_including.rb @@ -9,24 +9,29 @@ def self.applies_to?(value) end def call - SuperDiff::ObjectInspection::InspectionTree.new do - as_lines_when_rendering_to_lines(collection_bookend: :open) do - add_text "#" + # stree-ignore + t1.as_lines_when_rendering_to_lines( + collection_bookend: :close + ) do |t2| + t2.add_text ")>" end end end diff --git a/lib/super_diff/rspec/object_inspection/inspection_tree_builders/instance_of.rb b/lib/super_diff/rspec/object_inspection/inspection_tree_builders/instance_of.rb index d62b2f0e..e666f5f6 100644 --- a/lib/super_diff/rspec/object_inspection/inspection_tree_builders/instance_of.rb +++ b/lib/super_diff/rspec/object_inspection/inspection_tree_builders/instance_of.rb @@ -9,16 +9,15 @@ def self.applies_to?(value) end def call - SuperDiff::ObjectInspection::InspectionTree.new do - add_text do |value| - klass = - if SuperDiff::RSpec.an_instance_of_something?(value) - value.expected - else - value.instance_variable_get(:@klass) - end - "#" - end + SuperDiff::ObjectInspection::InspectionTree.new do |t1| + klass = + if SuperDiff::RSpec.an_instance_of_something?(object) + object.expected + else + object.instance_variable_get(:@klass) + end + + t1.add_text "#" end end end diff --git a/lib/super_diff/rspec/object_inspection/inspection_tree_builders/kind_of.rb b/lib/super_diff/rspec/object_inspection/inspection_tree_builders/kind_of.rb index b38e65ae..00ec4168 100644 --- a/lib/super_diff/rspec/object_inspection/inspection_tree_builders/kind_of.rb +++ b/lib/super_diff/rspec/object_inspection/inspection_tree_builders/kind_of.rb @@ -9,16 +9,15 @@ def self.applies_to?(value) end def call - SuperDiff::ObjectInspection::InspectionTree.new do - add_text do |value| - klass = - if SuperDiff::RSpec.a_kind_of_something?(value) - value.expected - else - value.instance_variable_get(:@klass) - end - "#" - end + SuperDiff::ObjectInspection::InspectionTree.new do |t1| + klass = + if SuperDiff::RSpec.a_kind_of_something?(object) + object.expected + else + object.instance_variable_get(:@klass) + end + + t1.add_text "#" end end end diff --git a/lib/super_diff/rspec/object_inspection/inspection_tree_builders/object_having_attributes.rb b/lib/super_diff/rspec/object_inspection/inspection_tree_builders/object_having_attributes.rb index 564c1488..8696f07b 100644 --- a/lib/super_diff/rspec/object_inspection/inspection_tree_builders/object_having_attributes.rb +++ b/lib/super_diff/rspec/object_inspection/inspection_tree_builders/object_having_attributes.rb @@ -8,17 +8,24 @@ def self.applies_to?(value) end def call - SuperDiff::ObjectInspection::InspectionTree.new do - as_lines_when_rendering_to_lines(collection_bookend: :open) do - add_text "#" + # stree-ignore + t1.as_lines_when_rendering_to_lines( + collection_bookend: :close + ) do |t2| + t2.add_text ")>" end end end diff --git a/lib/super_diff/rspec/object_inspection/inspection_tree_builders/rspec_matcher.rb b/lib/super_diff/rspec/object_inspection/inspection_tree_builders/rspec_matcher.rb index aa53fd8a..afbdbe22 100644 --- a/lib/super_diff/rspec/object_inspection/inspection_tree_builders/rspec_matcher.rb +++ b/lib/super_diff/rspec/object_inspection/inspection_tree_builders/rspec_matcher.rb @@ -9,8 +9,8 @@ def self.applies_to?(value) end def call - SuperDiff::ObjectInspection::InspectionTree.new do - add_text { |object| "#<#{object.description}>" } + SuperDiff::ObjectInspection::InspectionTree.new do |t1| + t1.add_text "#<#{object.description}>" end end end diff --git a/lib/super_diff/rspec/object_inspection/inspection_tree_builders/value_within.rb b/lib/super_diff/rspec/object_inspection/inspection_tree_builders/value_within.rb index 25ca5789..c9fb9854 100644 --- a/lib/super_diff/rspec/object_inspection/inspection_tree_builders/value_within.rb +++ b/lib/super_diff/rspec/object_inspection/inspection_tree_builders/value_within.rb @@ -8,22 +8,21 @@ def self.applies_to?(value) end def call - SuperDiff::ObjectInspection::InspectionTree.new do - as_prelude_when_rendering_to_lines do - add_text "#" + t1.add_text ">" end end end