From 8845c20ac35e3295bd39cd902b8eeec639705ebd Mon Sep 17 00:00:00 2001 From: Elliot Winkler Date: Mon, 18 May 2020 23:17:49 -0600 Subject: [PATCH] Do some reorganizing * Rename "function" classes to Main, e.g., Differ -> Differs::Main. This makes it easier to Ctrl-P for the right files (if you were looking for differ.rb you might get differs/something_else.rb). * Change inspector classes so that what they return match what they're named after. That is, previously an Inspector actually was an InspectionTree object, and now the InspectionTree lives inside the Inspector. * Remove the inspector Map, as well as the MapExtensions which are used by extension modules such as `super_diff/rspec`, `super_diff/active_record`, etc., in favor of the same registry mechanism that differs, operational sequencers, etc. use. * Pluralize extension points in the Configuration object so that you can provide more than one custom differ, operational sequencer, etc., at a time. This reorganization is in preparation for introducing new classes in the future around eliding diff output. --- Gemfile | 5 ++ gemfiles/no_rails.gemfile | 1 + gemfiles/no_rails.gemfile.lock | 10 ++- gemfiles/rails_5_0.gemfile | 1 + gemfiles/rails_5_0.gemfile.lock | 10 ++- gemfiles/rails_5_1.gemfile | 1 + gemfiles/rails_5_1.gemfile.lock | 10 ++- gemfiles/rails_5_2.gemfile | 1 + gemfiles/rails_5_2.gemfile.lock | 10 ++- gemfiles/rails_6_0.gemfile | 1 + gemfiles/rails_6_0.gemfile.lock | 10 ++- lib/super_diff.rb | 22 ++---- lib/super_diff/active_record.rb | 36 ++++----- .../inspectors/active_record_model.rb | 54 ++++++++------ .../inspectors/active_record_relation.rb | 24 ++++-- .../object_inspection/map_extension.rb | 18 ----- lib/super_diff/active_support.rb | 31 ++++---- .../hash_with_indifferent_access.rb | 24 ++++-- .../object_inspection/map_extension.rb | 15 ---- lib/super_diff/configuration.rb | 44 ++++++++--- lib/super_diff/diff_formatter.rb | 40 ---------- lib/super_diff/diff_formatters.rb | 10 +-- lib/super_diff/diff_formatters/defaults.rb | 10 +++ lib/super_diff/diff_formatters/main.rb | 41 ++++++++++ lib/super_diff/differ.rb | 46 ------------ lib/super_diff/differs.rb | 15 +--- lib/super_diff/differs/default_object.rb | 2 +- lib/super_diff/differs/defaults.rb | 12 +++ lib/super_diff/differs/main.rb | 48 ++++++++++++ .../differs/{time.rb => time_like.rb} | 4 +- lib/super_diff/equality_matcher.rb | 19 ----- lib/super_diff/equality_matchers.rb | 12 +-- lib/super_diff/equality_matchers/default.rb | 2 +- lib/super_diff/equality_matchers/defaults.rb | 12 +++ lib/super_diff/equality_matchers/main.rb | 21 ++++++ lib/super_diff/errors.rb | 16 ++++ .../no_diff_formatter_available_error.rb | 21 ++++++ .../errors/no_differ_available_error.rb | 24 ++++++ ...o_operational_sequencer_available_error.rb | 22 ++++++ .../no_diff_formatter_available_error.rb | 19 ----- lib/super_diff/no_differ_available_error.rb | 22 ------ ...o_operational_sequencer_available_error.rb | 20 ----- lib/super_diff/object_inspection.rb | 11 +-- lib/super_diff/object_inspection/inspector.rb | 27 ------- .../object_inspection/inspectors.rb | 6 +- .../object_inspection/inspectors/array.rb | 30 +++++--- .../object_inspection/inspectors/base.rb | 36 +++++++++ .../inspectors/custom_object.rb | 38 ++++++---- .../inspectors/default_object.rb | 74 +++++++++++-------- .../object_inspection/inspectors/defaults.rb | 15 ++++ .../object_inspection/inspectors/hash.rb | 30 +++++--- .../object_inspection/inspectors/main.rb | 35 +++++++++ .../object_inspection/inspectors/primitive.rb | 25 +++++-- .../object_inspection/inspectors/string.rb | 20 +++-- .../object_inspection/inspectors/time.rb | 13 ---- .../object_inspection/inspectors/time_like.rb | 23 ++++++ lib/super_diff/object_inspection/map.rb | 30 -------- lib/super_diff/operation_sequence.rb | 34 --------- lib/super_diff/operation_sequences.rb | 5 +- .../operation_sequences/default_object.rb | 2 +- .../operation_sequences/defaults.rb | 5 ++ lib/super_diff/operation_sequences/main.rb | 35 +++++++++ lib/super_diff/operational_sequencer.rb | 40 ---------- lib/super_diff/operational_sequencers.rb | 5 +- lib/super_diff/operational_sequencers/base.rb | 7 +- .../operational_sequencers/default_object.rb | 2 +- .../operational_sequencers/defaults.rb | 5 ++ lib/super_diff/operational_sequencers/main.rb | 42 +++++++++++ .../operational_sequencers/time_like.rb | 20 +++-- lib/super_diff/rspec.rb | 39 +++++----- lib/super_diff/rspec/differ.rb | 8 +- lib/super_diff/rspec/matcher_text_template.rb | 2 +- lib/super_diff/rspec/object_inspection.rb | 1 - .../rspec/object_inspection/inspectors.rb | 4 + .../collection_containing_exactly.rb | 25 +++++-- .../inspectors/collection_including.rb | 24 +++--- .../inspectors/hash_including.rb | 30 +++++--- .../inspectors/instance_of.rb | 16 +++- .../object_inspection/inspectors/kind_of.rb | 16 +++- .../inspectors/object_having_attributes.rb | 31 +++++--- .../object_inspection/inspectors/primitive.rb | 13 ++++ .../inspectors/value_within.rb | 26 +++++-- .../rspec/object_inspection/map_extension.rb | 29 -------- .../main_spec.rb} | 2 +- super_diff.gemspec | 1 - 85 files changed, 957 insertions(+), 691 deletions(-) delete mode 100644 lib/super_diff/active_record/object_inspection/map_extension.rb delete mode 100644 lib/super_diff/active_support/object_inspection/map_extension.rb delete mode 100644 lib/super_diff/diff_formatter.rb create mode 100644 lib/super_diff/diff_formatters/defaults.rb create mode 100644 lib/super_diff/diff_formatters/main.rb delete mode 100644 lib/super_diff/differ.rb create mode 100644 lib/super_diff/differs/defaults.rb create mode 100644 lib/super_diff/differs/main.rb rename lib/super_diff/differs/{time.rb => time_like.rb} (68%) delete mode 100644 lib/super_diff/equality_matcher.rb create mode 100644 lib/super_diff/equality_matchers/defaults.rb create mode 100644 lib/super_diff/equality_matchers/main.rb create mode 100644 lib/super_diff/errors.rb create mode 100644 lib/super_diff/errors/no_diff_formatter_available_error.rb create mode 100644 lib/super_diff/errors/no_differ_available_error.rb create mode 100644 lib/super_diff/errors/no_operational_sequencer_available_error.rb delete mode 100644 lib/super_diff/no_diff_formatter_available_error.rb delete mode 100644 lib/super_diff/no_differ_available_error.rb delete mode 100644 lib/super_diff/no_operational_sequencer_available_error.rb delete mode 100644 lib/super_diff/object_inspection/inspector.rb create mode 100644 lib/super_diff/object_inspection/inspectors/base.rb create mode 100644 lib/super_diff/object_inspection/inspectors/defaults.rb create mode 100644 lib/super_diff/object_inspection/inspectors/main.rb delete mode 100644 lib/super_diff/object_inspection/inspectors/time.rb create mode 100644 lib/super_diff/object_inspection/inspectors/time_like.rb delete mode 100644 lib/super_diff/object_inspection/map.rb delete mode 100644 lib/super_diff/operation_sequence.rb create mode 100644 lib/super_diff/operation_sequences/defaults.rb create mode 100644 lib/super_diff/operation_sequences/main.rb delete mode 100644 lib/super_diff/operational_sequencer.rb create mode 100644 lib/super_diff/operational_sequencers/defaults.rb create mode 100644 lib/super_diff/operational_sequencers/main.rb create mode 100644 lib/super_diff/rspec/object_inspection/inspectors/primitive.rb delete mode 100644 lib/super_diff/rspec/object_inspection/map_extension.rb rename spec/unit/{equality_matcher_spec.rb => equality_matchers/main_spec.rb} (99%) diff --git a/Gemfile b/Gemfile index bc42e0c3..8ea3c9dc 100644 --- a/Gemfile +++ b/Gemfile @@ -5,6 +5,11 @@ source "https://rubygems.org" git_source(:github) { |repo_name| "https://github.com/#{repo_name}" } gem "appraisal" +gem( + "attr_extras", + github: "mcmire/attr_extras", + branch: "pass-kwargs-in-explicit-static-facade", +) gem "childprocess" gem "pry-byebug", platform: :mri gem "pry-nav", platform: :jruby diff --git a/gemfiles/no_rails.gemfile b/gemfiles/no_rails.gemfile index bc224a96..31201321 100644 --- a/gemfiles/no_rails.gemfile +++ b/gemfiles/no_rails.gemfile @@ -9,5 +9,6 @@ gem "pry-nav", platform: :jruby gem "rake" gem "rspec" gem "rubocop" +gem "attr_extras", branch: "pass-kwargs-in-explicit-static-facade", git: "https://github.com/mcmire/attr_extras" gemspec path: "../" diff --git a/gemfiles/no_rails.gemfile.lock b/gemfiles/no_rails.gemfile.lock index e0ae86c9..055c7829 100644 --- a/gemfiles/no_rails.gemfile.lock +++ b/gemfiles/no_rails.gemfile.lock @@ -1,8 +1,14 @@ +GIT + remote: https://github.com/mcmire/attr_extras + revision: 6073a1d6c7ac2d1b7331a34a09ed2779c4fec83c + branch: pass-kwargs-in-explicit-static-facade + specs: + attr_extras (6.2.3) + PATH remote: .. specs: super_diff (0.4.2) - attr_extras diff-lcs patience_diff @@ -14,7 +20,6 @@ GEM rake thor (>= 0.14.0) ast (2.4.0) - attr_extras (6.2.3) byebug (11.1.3) childprocess (3.0.0) coderay (1.1.2) @@ -64,6 +69,7 @@ PLATFORMS DEPENDENCIES appraisal + attr_extras! childprocess pry-byebug pry-nav diff --git a/gemfiles/rails_5_0.gemfile b/gemfiles/rails_5_0.gemfile index 37924470..2b94052e 100644 --- a/gemfiles/rails_5_0.gemfile +++ b/gemfiles/rails_5_0.gemfile @@ -9,6 +9,7 @@ gem "pry-nav", platform: :jruby gem "rake" gem "rspec" gem "rubocop" +gem "attr_extras", branch: "pass-kwargs-in-explicit-static-facade", git: "https://github.com/mcmire/attr_extras" gem "activerecord-jdbcsqlite3-adapter", platform: :jruby gem "jdbc-sqlite3", platform: :jruby gem "activerecord", "~> 5.0.0" diff --git a/gemfiles/rails_5_0.gemfile.lock b/gemfiles/rails_5_0.gemfile.lock index 03e89c2c..aa5c15dd 100644 --- a/gemfiles/rails_5_0.gemfile.lock +++ b/gemfiles/rails_5_0.gemfile.lock @@ -1,8 +1,14 @@ +GIT + remote: https://github.com/mcmire/attr_extras + revision: 6073a1d6c7ac2d1b7331a34a09ed2779c4fec83c + branch: pass-kwargs-in-explicit-static-facade + specs: + attr_extras (6.2.3) + PATH remote: .. specs: super_diff (0.4.2) - attr_extras diff-lcs patience_diff @@ -26,7 +32,6 @@ GEM thor (>= 0.14.0) arel (7.1.4) ast (2.4.0) - attr_extras (6.2.3) byebug (11.1.3) childprocess (3.0.0) coderay (1.1.2) @@ -86,6 +91,7 @@ DEPENDENCIES activerecord (~> 5.0.0) activerecord-jdbcsqlite3-adapter appraisal + attr_extras! childprocess jdbc-sqlite3 pry-byebug diff --git a/gemfiles/rails_5_1.gemfile b/gemfiles/rails_5_1.gemfile index b0fd7e7c..e153276d 100644 --- a/gemfiles/rails_5_1.gemfile +++ b/gemfiles/rails_5_1.gemfile @@ -9,6 +9,7 @@ gem "pry-nav", platform: :jruby gem "rake" gem "rspec" gem "rubocop" +gem "attr_extras", branch: "pass-kwargs-in-explicit-static-facade", git: "https://github.com/mcmire/attr_extras" gem "activerecord-jdbcsqlite3-adapter", platform: :jruby gem "jdbc-sqlite3", platform: :jruby gem "activerecord", "~> 5.1.0" diff --git a/gemfiles/rails_5_1.gemfile.lock b/gemfiles/rails_5_1.gemfile.lock index 291d511c..9a470d2e 100644 --- a/gemfiles/rails_5_1.gemfile.lock +++ b/gemfiles/rails_5_1.gemfile.lock @@ -1,8 +1,14 @@ +GIT + remote: https://github.com/mcmire/attr_extras + revision: 6073a1d6c7ac2d1b7331a34a09ed2779c4fec83c + branch: pass-kwargs-in-explicit-static-facade + specs: + attr_extras (6.2.3) + PATH remote: .. specs: super_diff (0.4.2) - attr_extras diff-lcs patience_diff @@ -26,7 +32,6 @@ GEM thor (>= 0.14.0) arel (8.0.0) ast (2.4.0) - attr_extras (6.2.3) byebug (11.1.3) childprocess (3.0.0) coderay (1.1.2) @@ -86,6 +91,7 @@ DEPENDENCIES activerecord (~> 5.1.0) activerecord-jdbcsqlite3-adapter appraisal + attr_extras! childprocess jdbc-sqlite3 pry-byebug diff --git a/gemfiles/rails_5_2.gemfile b/gemfiles/rails_5_2.gemfile index 3977f5ce..12255301 100644 --- a/gemfiles/rails_5_2.gemfile +++ b/gemfiles/rails_5_2.gemfile @@ -9,6 +9,7 @@ gem "pry-nav", platform: :jruby gem "rake" gem "rspec" gem "rubocop" +gem "attr_extras", branch: "pass-kwargs-in-explicit-static-facade", git: "https://github.com/mcmire/attr_extras" gem "activerecord-jdbcsqlite3-adapter", platform: :jruby gem "jdbc-sqlite3", platform: :jruby gem "activerecord", "~> 5.2.0" diff --git a/gemfiles/rails_5_2.gemfile.lock b/gemfiles/rails_5_2.gemfile.lock index 35dea74c..94a66d99 100644 --- a/gemfiles/rails_5_2.gemfile.lock +++ b/gemfiles/rails_5_2.gemfile.lock @@ -1,8 +1,14 @@ +GIT + remote: https://github.com/mcmire/attr_extras + revision: 6073a1d6c7ac2d1b7331a34a09ed2779c4fec83c + branch: pass-kwargs-in-explicit-static-facade + specs: + attr_extras (6.2.3) + PATH remote: .. specs: super_diff (0.4.2) - attr_extras diff-lcs patience_diff @@ -26,7 +32,6 @@ GEM thor (>= 0.14.0) arel (9.0.0) ast (2.4.0) - attr_extras (6.2.3) byebug (11.1.3) childprocess (3.0.0) coderay (1.1.2) @@ -86,6 +91,7 @@ DEPENDENCIES activerecord (~> 5.2.0) activerecord-jdbcsqlite3-adapter appraisal + attr_extras! childprocess jdbc-sqlite3 pry-byebug diff --git a/gemfiles/rails_6_0.gemfile b/gemfiles/rails_6_0.gemfile index c94ba4a6..ede2761c 100644 --- a/gemfiles/rails_6_0.gemfile +++ b/gemfiles/rails_6_0.gemfile @@ -9,6 +9,7 @@ gem "pry-nav", platform: :jruby gem "rake" gem "rspec" gem "rubocop" +gem "attr_extras", branch: "pass-kwargs-in-explicit-static-facade", git: "https://github.com/mcmire/attr_extras" gem "activerecord-jdbcsqlite3-adapter", platform: :jruby gem "jdbc-sqlite3", platform: :jruby gem "activerecord", "~> 6.0" diff --git a/gemfiles/rails_6_0.gemfile.lock b/gemfiles/rails_6_0.gemfile.lock index 144aefc2..b959b7dc 100644 --- a/gemfiles/rails_6_0.gemfile.lock +++ b/gemfiles/rails_6_0.gemfile.lock @@ -1,8 +1,14 @@ +GIT + remote: https://github.com/mcmire/attr_extras + revision: 6073a1d6c7ac2d1b7331a34a09ed2779c4fec83c + branch: pass-kwargs-in-explicit-static-facade + specs: + attr_extras (6.2.3) + PATH remote: .. specs: super_diff (0.4.2) - attr_extras diff-lcs patience_diff @@ -25,7 +31,6 @@ GEM rake thor (>= 0.14.0) ast (2.4.0) - attr_extras (6.2.3) byebug (11.1.3) childprocess (3.0.0) coderay (1.1.2) @@ -86,6 +91,7 @@ DEPENDENCIES activerecord (~> 6.0) activerecord-jdbcsqlite3-adapter appraisal + attr_extras! childprocess jdbc-sqlite3 pry-byebug diff --git a/lib/super_diff.rb b/lib/super_diff.rb index a4a375ad..c07afa90 100644 --- a/lib/super_diff.rb +++ b/lib/super_diff.rb @@ -9,27 +9,14 @@ module SuperDiff ) autoload :Configuration, "super_diff/configuration" autoload :Csi, "super_diff/csi" - autoload :DiffFormatter, "super_diff/diff_formatter" autoload :DiffFormatters, "super_diff/diff_formatters" - autoload :Differ, "super_diff/differ" autoload :Differs, "super_diff/differs" - autoload :EqualityMatcher, "super_diff/equality_matcher" autoload :EqualityMatchers, "super_diff/equality_matchers" + autoload :Errors, "super_diff/errors" autoload :Helpers, "super_diff/helpers" autoload :ImplementationChecks, "super_diff/implementation_checks" - autoload :NoDifferAvailableError, "super_diff/no_differ_available_error" - autoload( - :NoDiffFormatterAvailableError, - "super_diff/no_diff_formatter_available_error", - ) - autoload( - :NoOperationalSequencerAvailableError, - "super_diff/no_operational_sequencer_available_error", - ) autoload :ObjectInspection, "super_diff/object_inspection" - autoload :OperationSequence, "super_diff/operation_sequence" autoload :OperationSequences, "super_diff/operation_sequences" - autoload :OperationalSequencer, "super_diff/operational_sequencer" autoload :OperationalSequencers, "super_diff/operational_sequencers" autoload :Operations, "super_diff/operations" autoload :RecursionGuard, "super_diff/recursion_guard" @@ -48,4 +35,11 @@ def self.configure def self.configuration @_configuration ||= Configuration.new end + + def self.time_like?(value) + # Check for ActiveSupport's #acts_like_time? for their time-like objects + # (like ActiveSupport::TimeWithZone). + (value.respond_to?(:acts_like_time?) && value.acts_like_time?) || + value.is_a?(Time) + end end diff --git a/lib/super_diff/active_record.rb b/lib/super_diff/active_record.rb index e0bdac57..bb0ebcb3 100644 --- a/lib/super_diff/active_record.rb +++ b/lib/super_diff/active_record.rb @@ -16,28 +16,24 @@ module ActiveRecord :OperationalSequencers, "super_diff/active_record/operational_sequencers", ) - end -end -if defined?(SuperDiff::RSpec) - SuperDiff::RSpec.configure do |config| - config.add_extra_differ_class( - SuperDiff::ActiveRecord::Differs::ActiveRecordRelation, - ) - config.add_extra_operational_sequencer_class( - SuperDiff::ActiveRecord::OperationalSequencers::ActiveRecordModel, - ) - config.add_extra_operational_sequencer_class( - SuperDiff::ActiveRecord::OperationalSequencers::ActiveRecordRelation, - ) - config.add_extra_diff_formatter_class( - SuperDiff::ActiveRecord::DiffFormatters::ActiveRecordRelation, - ) + SuperDiff.configure do |config| + config.add_extra_differ_classes( + Differs::ActiveRecordRelation, + ) + config.add_extra_operational_sequencer_classes( + OperationalSequencers::ActiveRecordModel, + OperationalSequencers::ActiveRecordRelation, + ) + config.add_extra_diff_formatter_classes( + DiffFormatters::ActiveRecordRelation, + ) + config.add_extra_inspector_classes( + ObjectInspection::Inspectors::ActiveRecordModel, + ObjectInspection::Inspectors::ActiveRecordRelation, + ) + end end end require "super_diff/active_record/monkey_patches" - -SuperDiff::ObjectInspection.map.prepend( - SuperDiff::ActiveRecord::ObjectInspection::MapExtension, -) diff --git a/lib/super_diff/active_record/object_inspection/inspectors/active_record_model.rb b/lib/super_diff/active_record/object_inspection/inspectors/active_record_model.rb index 4d615605..a838448b 100644 --- a/lib/super_diff/active_record/object_inspection/inspectors/active_record_model.rb +++ b/lib/super_diff/active_record/object_inspection/inspectors/active_record_model.rb @@ -2,35 +2,45 @@ module SuperDiff module ActiveRecord module ObjectInspection module Inspectors - ActiveRecordModel = SuperDiff::ObjectInspection::InspectionTree.new do - add_text do |object| - "#<#{object.class} " + class ActiveRecordModel < SuperDiff::ObjectInspection::Inspectors::Base + def self.applies_to?(value) + value.is_a?(::ActiveRecord::Base) end - when_multiline do - add_text "{" - end + protected - nested do |object| - add_break + def inspection_tree + SuperDiff::ObjectInspection::InspectionTree.new do + add_text do |object| + "#<#{object.class} " + end - insert_separated_list( - ["id"] + (object.attributes.keys.sort - ["id"]), - separator: ",", - ) do |name| - add_text name - add_text ": " - add_inspection_of object.read_attribute(name) - end - end + when_multiline do + add_text "{" + end - add_break + nested do |object| + add_break - when_multiline do - add_text "}" - end + insert_separated_list( + ["id"] + (object.attributes.keys.sort - ["id"]), + separator: ",", + ) do |name| + add_text name + add_text ": " + add_inspection_of object.read_attribute(name) + end + end - add_text ">" + add_break + + when_multiline do + add_text "}" + end + + add_text ">" + end + end end end end diff --git a/lib/super_diff/active_record/object_inspection/inspectors/active_record_relation.rb b/lib/super_diff/active_record/object_inspection/inspectors/active_record_relation.rb index 1ab80a33..c3920424 100644 --- a/lib/super_diff/active_record/object_inspection/inspectors/active_record_relation.rb +++ b/lib/super_diff/active_record/object_inspection/inspectors/active_record_relation.rb @@ -2,15 +2,25 @@ module SuperDiff module ActiveRecord module ObjectInspection module Inspectors - ActiveRecordRelation = SuperDiff::ObjectInspection::InspectionTree.new do - add_text "#" + protected + + def inspection_tree + SuperDiff::ObjectInspection::InspectionTree.new do + add_text "#" + end + end end end end diff --git a/lib/super_diff/active_record/object_inspection/map_extension.rb b/lib/super_diff/active_record/object_inspection/map_extension.rb deleted file mode 100644 index 29969fcc..00000000 --- a/lib/super_diff/active_record/object_inspection/map_extension.rb +++ /dev/null @@ -1,18 +0,0 @@ -module SuperDiff - module ActiveRecord - module ObjectInspection - module MapExtension - def call(object) - case object - when ::ActiveRecord::Base - Inspectors::ActiveRecordModel - when ::ActiveRecord::Relation - Inspectors::ActiveRecordRelation - else - super - end - end - end - end - end -end diff --git a/lib/super_diff/active_support.rb b/lib/super_diff/active_support.rb index 6e60273d..79809d33 100644 --- a/lib/super_diff/active_support.rb +++ b/lib/super_diff/active_support.rb @@ -11,23 +11,20 @@ module ActiveSupport :OperationalSequences, "super_diff/active_support/operational_sequences", ) - end -end -if defined?(SuperDiff::RSpec) - SuperDiff::RSpec.configure do |config| - config.add_extra_differ_class( - SuperDiff::ActiveSupport::Differs::HashWithIndifferentAccess, - ) - config.add_extra_operational_sequencer_class( - SuperDiff::ActiveSupport::OperationalSequencers::HashWithIndifferentAccess, - ) - config.add_extra_diff_formatter_class( - SuperDiff::ActiveSupport::DiffFormatters::HashWithIndifferentAccess, - ) + SuperDiff.configure do |config| + config.add_extra_differ_classes( + Differs::HashWithIndifferentAccess, + ) + config.add_extra_operational_sequencer_classes( + OperationalSequencers::HashWithIndifferentAccess, + ) + config.add_extra_diff_formatter_classes( + DiffFormatters::HashWithIndifferentAccess, + ) + config.add_extra_inspector_classes( + ObjectInspection::Inspectors::HashWithIndifferentAccess, + ) + end end end - -SuperDiff::ObjectInspection.map.prepend( - SuperDiff::ActiveSupport::ObjectInspection::MapExtension, -) diff --git a/lib/super_diff/active_support/object_inspection/inspectors/hash_with_indifferent_access.rb b/lib/super_diff/active_support/object_inspection/inspectors/hash_with_indifferent_access.rb index 32981fb0..0245bca1 100644 --- a/lib/super_diff/active_support/object_inspection/inspectors/hash_with_indifferent_access.rb +++ b/lib/super_diff/active_support/object_inspection/inspectors/hash_with_indifferent_access.rb @@ -2,15 +2,25 @@ module SuperDiff module ActiveSupport module ObjectInspection module Inspectors - HashWithIndifferentAccess = SuperDiff::ObjectInspection::InspectionTree.new do - add_text "#" + protected + + def inspection_tree + SuperDiff::ObjectInspection::InspectionTree.new do + add_text "#" + end + end end end end diff --git a/lib/super_diff/active_support/object_inspection/map_extension.rb b/lib/super_diff/active_support/object_inspection/map_extension.rb deleted file mode 100644 index 79f34a30..00000000 --- a/lib/super_diff/active_support/object_inspection/map_extension.rb +++ /dev/null @@ -1,15 +0,0 @@ -module SuperDiff - module ActiveSupport - module ObjectInspection - module MapExtension - def call(object) - if object.is_a?(::HashWithIndifferentAccess) - Inspectors::HashWithIndifferentAccess - else - super - end - end - end - end - end -end diff --git a/lib/super_diff/configuration.rb b/lib/super_diff/configuration.rb index 49fbda72..a973228a 100644 --- a/lib/super_diff/configuration.rb +++ b/lib/super_diff/configuration.rb @@ -5,6 +5,7 @@ class Configuration :extra_operation_sequence_classes, :extra_operational_sequencer_classes, :extra_diff_formatter_classes, + :extra_inspector_classes, ) def initialize @@ -12,25 +13,48 @@ def initialize @extra_operation_sequence_classes = [].freeze @extra_operational_sequencer_classes = [].freeze @extra_diff_formatter_classes = [].freeze + @extra_inspector_classes = [].freeze end - def add_extra_differ_class(klass) - @extra_differ_classes = (@extra_differ_classes + [klass]).freeze + def add_extra_differ_classes(*classes) + @extra_differ_classes = (@extra_differ_classes + classes).freeze end + alias_method :add_extra_differ_class, :add_extra_differ_classes - def add_extra_operation_sequence_class(klass) - @extra_operation_sequence_classes = - (@extra_operation_sequence_classes + [klass]).freeze + def add_extra_operational_sequencer_classes(*classes) + @extra_operational_sequencer_classes = + (@extra_operational_sequencer_classes + classes).freeze end + alias_method( + :add_extra_operational_sequencer_class, + :add_extra_operational_sequencer_classes, + ) - def add_extra_operational_sequencer_class(klass) - @extra_operational_sequencer_classes = - (@extra_operational_sequencer_classes + [klass]).freeze + def add_extra_operation_sequence_classes(*classes) + @extra_operation_sequence_classes = + (@extra_operation_sequence_classes + classes).freeze end + alias_method( + :add_extra_operation_sequence_class, + :add_extra_operation_sequence_classes, + ) - def add_extra_diff_formatter_class(klass) + def add_extra_diff_formatter_classes(*classes) @extra_diff_formatter_classes = - (@extra_diff_formatter_classes + [klass]).freeze + (@extra_diff_formatter_classes + classes).freeze end + alias_method( + :add_extra_diff_formatter_class, + :add_extra_diff_formatter_classes, + ) + + def add_extra_inspector_classes(*classes) + @extra_inspector_classes = + (@extra_inspector_classes + classes).freeze + end + alias_method( + :add_extra_inspector_class, + :add_extra_inspector_classes, + ) end end diff --git a/lib/super_diff/diff_formatter.rb b/lib/super_diff/diff_formatter.rb deleted file mode 100644 index 6a76ddca..00000000 --- a/lib/super_diff/diff_formatter.rb +++ /dev/null @@ -1,40 +0,0 @@ -module SuperDiff - class DiffFormatter - extend AttrExtras.mixin - - method_object( - :operations, - [ - :indent_level!, - add_comma: false, - value_class: nil, - ], - ) - - def call - if resolved_class - resolved_class.call( - operations, - indent_level: indent_level, - add_comma: add_comma?, - value_class: value_class, - ) - else - raise NoDiffFormatterAvailableError.create(operations) - end - end - - private - - attr_query :add_comma? - - def resolved_class - available_classes.find { |klass| klass.applies_to?(operations) } - end - - def available_classes - DiffFormatters::DEFAULTS + - SuperDiff.configuration.extra_diff_formatter_classes - end - end -end diff --git a/lib/super_diff/diff_formatters.rb b/lib/super_diff/diff_formatters.rb index 8b1ff326..53046438 100644 --- a/lib/super_diff/diff_formatters.rb +++ b/lib/super_diff/diff_formatters.rb @@ -6,13 +6,9 @@ module DiffFormatters autoload :CustomObject, "super_diff/diff_formatters/custom_object" autoload :DefaultObject, "super_diff/diff_formatters/default_object" autoload :Hash, "super_diff/diff_formatters/hash" + autoload :Main, "super_diff/diff_formatters/main" autoload :MultilineString, "super_diff/diff_formatters/multiline_string" - - DEFAULTS = [ - Array, - Hash, - CustomObject, - DefaultObject, - ].freeze end end + +require "super_diff/diff_formatters/defaults" diff --git a/lib/super_diff/diff_formatters/defaults.rb b/lib/super_diff/diff_formatters/defaults.rb new file mode 100644 index 00000000..1326e179 --- /dev/null +++ b/lib/super_diff/diff_formatters/defaults.rb @@ -0,0 +1,10 @@ +module SuperDiff + module DiffFormatters + DEFAULTS = [ + Array, + Hash, + CustomObject, + DefaultObject, + ].freeze + end +end diff --git a/lib/super_diff/diff_formatters/main.rb b/lib/super_diff/diff_formatters/main.rb new file mode 100644 index 00000000..a079e098 --- /dev/null +++ b/lib/super_diff/diff_formatters/main.rb @@ -0,0 +1,41 @@ +module SuperDiff + module DiffFormatters + class Main + extend AttrExtras.mixin + + method_object( + :operations, + [ + :indent_level!, + add_comma: false, + value_class: nil, + ], + ) + + def call + if resolved_class + resolved_class.call( + operations, + indent_level: indent_level, + add_comma: add_comma?, + value_class: value_class, + ) + else + raise NoDiffFormatterAvailableError.create(operations) + end + end + + private + + attr_query :add_comma? + + def resolved_class + available_classes.find { |klass| klass.applies_to?(operations) } + end + + def available_classes + DEFAULTS + SuperDiff.configuration.extra_diff_formatter_classes + end + end + end +end diff --git a/lib/super_diff/differ.rb b/lib/super_diff/differ.rb deleted file mode 100644 index 38967fbb..00000000 --- a/lib/super_diff/differ.rb +++ /dev/null @@ -1,46 +0,0 @@ -module SuperDiff - class Differ - extend AttrExtras.mixin - - method_object( - :expected, - :actual, - [ - indent_level: 0, - index_in_collection: nil, - omit_empty: false, - ], - ) - - def call - if resolved_class - resolved_class.call( - expected, - actual, - indent_level: indent_level, - index_in_collection: index_in_collection, - ) - else - raise NoDifferAvailableError.create(expected, actual) - end - end - - private - - attr_query :omit_empty? - - def resolved_class - available_classes.find { |klass| klass.applies_to?(expected, actual) } - end - - def available_classes - classes = SuperDiff.configuration.extra_differ_classes + Differs::DEFAULTS - - if omit_empty? - classes - [Differs::Empty] - else - classes - end - end - end -end diff --git a/lib/super_diff/differs.rb b/lib/super_diff/differs.rb index da12f71e..e5780bbf 100644 --- a/lib/super_diff/differs.rb +++ b/lib/super_diff/differs.rb @@ -6,17 +6,10 @@ module Differs autoload :DefaultObject, "super_diff/differs/default_object" autoload :Empty, "super_diff/differs/empty" autoload :Hash, "super_diff/differs/hash" + autoload :Main, "super_diff/differs/main" autoload :MultilineString, "super_diff/differs/multiline_string" - autoload :Time, "super_diff/differs/time" - - DEFAULTS = [ - Array, - Hash, - Time, - MultilineString, - CustomObject, - DefaultObject, - Empty, - ].freeze + autoload :TimeLike, "super_diff/differs/time_like" end end + +require "super_diff/differs/defaults" diff --git a/lib/super_diff/differs/default_object.rb b/lib/super_diff/differs/default_object.rb index e1341307..4e0d6d90 100644 --- a/lib/super_diff/differs/default_object.rb +++ b/lib/super_diff/differs/default_object.rb @@ -8,7 +8,7 @@ def self.applies_to?(expected, actual) private def operations - OperationalSequencer.call( + OperationalSequencers::Main.call( expected: expected, actual: actual, all_or_nothing: true, diff --git a/lib/super_diff/differs/defaults.rb b/lib/super_diff/differs/defaults.rb new file mode 100644 index 00000000..0920c193 --- /dev/null +++ b/lib/super_diff/differs/defaults.rb @@ -0,0 +1,12 @@ +module SuperDiff + module Differs + DEFAULTS = [ + Array, + Hash, + TimeLike, + MultilineString, + CustomObject, + DefaultObject, + ].freeze + end +end diff --git a/lib/super_diff/differs/main.rb b/lib/super_diff/differs/main.rb new file mode 100644 index 00000000..0ff78734 --- /dev/null +++ b/lib/super_diff/differs/main.rb @@ -0,0 +1,48 @@ +module SuperDiff + module Differs + class Main + extend AttrExtras.mixin + + method_object( + :expected, + :actual, + [ + indent_level: 0, + index_in_collection: nil, + omit_empty: false, + ], + ) + + def call + if resolved_class + resolved_class.call( + expected, + actual, + indent_level: indent_level, + index_in_collection: index_in_collection, + ) + else + raise Errors::NoDifferAvailableError.create(expected, actual) + end + end + + private + + attr_query :omit_empty? + + def resolved_class + available_classes.find { |klass| klass.applies_to?(expected, actual) } + end + + def available_classes + classes = SuperDiff.configuration.extra_differ_classes + DEFAULTS + + if omit_empty? + classes + else + classes + [Empty] + end + end + end + end +end diff --git a/lib/super_diff/differs/time.rb b/lib/super_diff/differs/time_like.rb similarity index 68% rename from lib/super_diff/differs/time.rb rename to lib/super_diff/differs/time_like.rb index 2ad391e2..96dfad08 100644 --- a/lib/super_diff/differs/time.rb +++ b/lib/super_diff/differs/time_like.rb @@ -1,8 +1,8 @@ module SuperDiff module Differs - class Time < Base + class TimeLike < Base def self.applies_to?(expected, actual) - OperationalSequencers::TimeLike.applies_to?(expected, actual) + SuperDiff.time_like?(expected) && SuperDiff.time_like?(actual) end private diff --git a/lib/super_diff/equality_matcher.rb b/lib/super_diff/equality_matcher.rb deleted file mode 100644 index 4157f4f0..00000000 --- a/lib/super_diff/equality_matcher.rb +++ /dev/null @@ -1,19 +0,0 @@ -module SuperDiff - class EqualityMatcher - extend AttrExtras.mixin - - method_object [:expected!, :actual!, extra_classes: []] - - def call - resolved_class.call(expected: expected, actual: actual) - end - - private - - def resolved_class - (EqualityMatchers::DEFAULTS + extra_classes).find do |klass| - klass.applies_to?(expected) && klass.applies_to?(actual) - end - end - end -end diff --git a/lib/super_diff/equality_matchers.rb b/lib/super_diff/equality_matchers.rb index 9bb2b48d..47816c30 100644 --- a/lib/super_diff/equality_matchers.rb +++ b/lib/super_diff/equality_matchers.rb @@ -3,6 +3,7 @@ module EqualityMatchers autoload :Array, "super_diff/equality_matchers/array" autoload :Base, "super_diff/equality_matchers/base" autoload :Hash, "super_diff/equality_matchers/hash" + autoload :Main, "super_diff/equality_matchers/main" autoload :MultilineString, "super_diff/equality_matchers/multiline_string" autoload :Default, "super_diff/equality_matchers/default" autoload :Primitive, "super_diff/equality_matchers/primitive" @@ -10,14 +11,7 @@ module EqualityMatchers :SinglelineString, "super_diff/equality_matchers/singleline_string", ) - - DEFAULTS = [ - Primitive, - Array, - Hash, - MultilineString, - SinglelineString, - Default, - ].freeze end end + +require "super_diff/equality_matchers/defaults" diff --git a/lib/super_diff/equality_matchers/default.rb b/lib/super_diff/equality_matchers/default.rb index 1398b510..ae409de5 100644 --- a/lib/super_diff/equality_matchers/default.rb +++ b/lib/super_diff/equality_matchers/default.rb @@ -47,7 +47,7 @@ def diff_section end def diff - Differ.call(expected, actual, indent_level: 0) + Differs::Main.call(expected, actual, indent_level: 0) end end end diff --git a/lib/super_diff/equality_matchers/defaults.rb b/lib/super_diff/equality_matchers/defaults.rb new file mode 100644 index 00000000..5782ac61 --- /dev/null +++ b/lib/super_diff/equality_matchers/defaults.rb @@ -0,0 +1,12 @@ +module SuperDiff + module EqualityMatchers + DEFAULTS = [ + Primitive, + Array, + Hash, + MultilineString, + SinglelineString, + Default, + ].freeze + end +end diff --git a/lib/super_diff/equality_matchers/main.rb b/lib/super_diff/equality_matchers/main.rb new file mode 100644 index 00000000..b3426c1c --- /dev/null +++ b/lib/super_diff/equality_matchers/main.rb @@ -0,0 +1,21 @@ +module SuperDiff + module EqualityMatchers + class Main + extend AttrExtras.mixin + + method_object [:expected!, :actual!, extra_classes: []] + + def call + resolved_class.call(expected: expected, actual: actual) + end + + private + + def resolved_class + (DEFAULTS + extra_classes).find do |klass| + klass.applies_to?(expected) && klass.applies_to?(actual) + end + end + end + end +end diff --git a/lib/super_diff/errors.rb b/lib/super_diff/errors.rb new file mode 100644 index 00000000..d04c4738 --- /dev/null +++ b/lib/super_diff/errors.rb @@ -0,0 +1,16 @@ +module SuperDiff + module Errors + autoload( + :NoDiffFormatterAvailableError, + "super_diff/errors/no_diff_formatter_available_error", + ) + autoload( + :NoDifferAvailableError, + "super_diff/errors/no_differ_available_error", + ) + autoload( + :NoOperationalSequenceAvailableError, + "super_diff/errors/no_operational_sequencer_available_error", + ) + end +end diff --git a/lib/super_diff/errors/no_diff_formatter_available_error.rb b/lib/super_diff/errors/no_diff_formatter_available_error.rb new file mode 100644 index 00000000..b30c5011 --- /dev/null +++ b/lib/super_diff/errors/no_diff_formatter_available_error.rb @@ -0,0 +1,21 @@ +module SuperDiff + module Errors + class NoDiffFormatterAvailableError < StandardError + def self.create(operations) + allocate.tap do |error| + error.operations = operations + error.__send__(:initialize) + end + end + + attr_accessor :operations + + def initialize + super(<<-MESSAGE) +There is no diff formatter available to handle an operations object of +#{operations.class}. + MESSAGE + end + end + end +end diff --git a/lib/super_diff/errors/no_differ_available_error.rb b/lib/super_diff/errors/no_differ_available_error.rb new file mode 100644 index 00000000..902271da --- /dev/null +++ b/lib/super_diff/errors/no_differ_available_error.rb @@ -0,0 +1,24 @@ +module SuperDiff + module Errors + class NoDifferAvailableError < StandardError + def self.create(expected, actual) + allocate.tap do |error| + error.expected = expected + error.actual = actual + error.__send__(:initialize) + end + end + + attr_accessor :expected, :actual + + def initialize + super(<<-MESSAGE) +There is no differ available to handle an "expected" value of type +#{expected.class} +and an "actual" value of type +#{actual.class}. + MESSAGE + end + end + end +end diff --git a/lib/super_diff/errors/no_operational_sequencer_available_error.rb b/lib/super_diff/errors/no_operational_sequencer_available_error.rb new file mode 100644 index 00000000..2c8f193f --- /dev/null +++ b/lib/super_diff/errors/no_operational_sequencer_available_error.rb @@ -0,0 +1,22 @@ +module SuperDiff + module Errors + class NoOperationalSequencerAvailableError < StandardError + def self.create(expected, actual) + allocate.tap do |error| + error.expected = expected + error.actual = actual + error.__send__(:initialize) + end + end + + attr_accessor :expected, :actual + + def initialize + super(<<-MESSAGE) +There is no operational sequencer available to handle an "expected" value of +type #{expected.class} and an "actual" value of type #{actual.class}. + MESSAGE + end + end + end +end diff --git a/lib/super_diff/no_diff_formatter_available_error.rb b/lib/super_diff/no_diff_formatter_available_error.rb deleted file mode 100644 index 3bfc41b9..00000000 --- a/lib/super_diff/no_diff_formatter_available_error.rb +++ /dev/null @@ -1,19 +0,0 @@ -module SuperDiff - class NoDiffFormatterAvailableError < StandardError - def self.create(operations) - allocate.tap do |error| - error.operations = operations - error.__send__(:initialize) - end - end - - attr_accessor :operations - - def initialize - super(<<-MESSAGE) -There is no diff formatter available to handle an operations object of -#{operations.class}. - MESSAGE - end - end -end diff --git a/lib/super_diff/no_differ_available_error.rb b/lib/super_diff/no_differ_available_error.rb deleted file mode 100644 index 35c1ee00..00000000 --- a/lib/super_diff/no_differ_available_error.rb +++ /dev/null @@ -1,22 +0,0 @@ -module SuperDiff - class NoDifferAvailableError < StandardError - def self.create(expected, actual) - allocate.tap do |error| - error.expected = expected - error.actual = actual - error.__send__(:initialize) - end - end - - attr_accessor :expected, :actual - - def initialize - super(<<-MESSAGE) -There is no differ available to handle an "expected" value of type -#{expected.class} -and an "actual" value of type -#{actual.class}. - MESSAGE - end - end -end diff --git a/lib/super_diff/no_operational_sequencer_available_error.rb b/lib/super_diff/no_operational_sequencer_available_error.rb deleted file mode 100644 index d04f2dc4..00000000 --- a/lib/super_diff/no_operational_sequencer_available_error.rb +++ /dev/null @@ -1,20 +0,0 @@ -module SuperDiff - class NoOperationalSequencerAvailableError < StandardError - def self.create(expected, actual) - allocate.tap do |error| - error.expected = expected - error.actual = actual - error.__send__(:initialize) - end - end - - attr_accessor :expected, :actual - - def initialize - super(<<-MESSAGE) -There is no operational sequencer available to handle an "expected" value of -type #{expected.class} and an "actual" value of type #{actual.class}. - MESSAGE - end - end -end diff --git a/lib/super_diff/object_inspection.rb b/lib/super_diff/object_inspection.rb index 727f5a36..471bd79c 100644 --- a/lib/super_diff/object_inspection.rb +++ b/lib/super_diff/object_inspection.rb @@ -1,24 +1,15 @@ module SuperDiff module ObjectInspection autoload :InspectionTree, "super_diff/object_inspection/inspection_tree" - autoload :Inspector, "super_diff/object_inspection/inspector" autoload :Inspectors, "super_diff/object_inspection/inspectors" - autoload :Map, "super_diff/object_inspection/map" autoload :Nodes, "super_diff/object_inspection/nodes" - class << self - attr_accessor :map - end - def self.inspect(object, as_single_line:, indent_level: 0) - Inspector.call( - map, + Inspectors::Main.call( object, as_single_line: as_single_line, indent_level: indent_level, ) end - - self.map = Map.new end end diff --git a/lib/super_diff/object_inspection/inspector.rb b/lib/super_diff/object_inspection/inspector.rb deleted file mode 100644 index bf0c9e69..00000000 --- a/lib/super_diff/object_inspection/inspector.rb +++ /dev/null @@ -1,27 +0,0 @@ -module SuperDiff - module ObjectInspection - class Inspector - extend AttrExtras.mixin - - method_object :map, :object, [:indent_level!, :as_single_line!] - - def call - SuperDiff::RecursionGuard.substituting_recursion_of(object) do - inspector.evaluate( - object, - as_single_line: as_single_line, - indent_level: indent_level, - ) - end - end - - private - - attr_query :as_single_line? - - def inspector - @_inspector ||= map.call(object) - end - end - end -end diff --git a/lib/super_diff/object_inspection/inspectors.rb b/lib/super_diff/object_inspection/inspectors.rb index 1ac9b28a..d0f3d090 100644 --- a/lib/super_diff/object_inspection/inspectors.rb +++ b/lib/super_diff/object_inspection/inspectors.rb @@ -1,6 +1,7 @@ module SuperDiff module ObjectInspection module Inspectors + autoload :Base, "super_diff/object_inspection/inspectors/base" autoload :Array, "super_diff/object_inspection/inspectors/array" autoload( :CustomObject, @@ -11,9 +12,12 @@ module Inspectors "super_diff/object_inspection/inspectors/default_object", ) autoload :Hash, "super_diff/object_inspection/inspectors/hash" + autoload :Main, "super_diff/object_inspection/inspectors/main" autoload :Primitive, "super_diff/object_inspection/inspectors/primitive" autoload :String, "super_diff/object_inspection/inspectors/string" - autoload :Time, "super_diff/object_inspection/inspectors/time" + autoload :TimeLike, "super_diff/object_inspection/inspectors/time_like" end end end + +require "super_diff/object_inspection/inspectors/defaults" diff --git a/lib/super_diff/object_inspection/inspectors/array.rb b/lib/super_diff/object_inspection/inspectors/array.rb index 8528bbcf..d25b679d 100644 --- a/lib/super_diff/object_inspection/inspectors/array.rb +++ b/lib/super_diff/object_inspection/inspectors/array.rb @@ -1,20 +1,30 @@ module SuperDiff module ObjectInspection module Inspectors - Array = InspectionTree.new do - when_empty do - add_text "[]" + class Array < Base + def self.applies_to?(value) + value.is_a?(::Array) end - when_non_empty do - add_text "[" + protected - nested do |array| - insert_array_inspection_of(array) - end + def inspection_tree + InspectionTree.new do + when_empty do + add_text "[]" + end + + when_non_empty do + add_text "[" - add_break - add_text "]" + nested do |array| + insert_array_inspection_of(array) + end + + add_break + add_text "]" + end + end end end end diff --git a/lib/super_diff/object_inspection/inspectors/base.rb b/lib/super_diff/object_inspection/inspectors/base.rb new file mode 100644 index 00000000..4bbd1aa2 --- /dev/null +++ b/lib/super_diff/object_inspection/inspectors/base.rb @@ -0,0 +1,36 @@ +module SuperDiff + module ObjectInspection + module Inspectors + class Base + extend AttrExtras.mixin + extend ImplementationChecks + + def self.applies_to?(_value) + unimplemented_class_method! + end + + method_object :object, [:as_single_line!, :indent_level] + + def call + SuperDiff::RecursionGuard.substituting_recursion_of(object) do + inspection_tree.evaluate( + object, + as_single_line: as_single_line, + indent_level: indent_level, + ) + end + end + + protected + + def inspection_tree + unimplemented_instance_method! + end + + private + + attr_query :as_single_line? + end + end + end +end diff --git a/lib/super_diff/object_inspection/inspectors/custom_object.rb b/lib/super_diff/object_inspection/inspectors/custom_object.rb index 3324bc72..6184b2e7 100644 --- a/lib/super_diff/object_inspection/inspectors/custom_object.rb +++ b/lib/super_diff/object_inspection/inspectors/custom_object.rb @@ -1,26 +1,36 @@ module SuperDiff module ObjectInspection module Inspectors - CustomObject = InspectionTree.new do - add_text do |object| - "#<#{object.class}" + class CustomObject < Base + def self.applies_to?(value) + value.respond_to?(:attributes_for_super_diff) end - when_multiline do - add_text " {" - end + protected - nested do |object| - insert_hash_inspection_of(object.attributes_for_super_diff) - end + def inspection_tree + InspectionTree.new do + add_text do |object| + "#<#{object.class}" + end - add_break + when_multiline do + add_text " {" + end - when_multiline do - add_text "}" - end + nested do |object| + insert_hash_inspection_of(object.attributes_for_super_diff) + end - add_text ">" + add_break + + when_multiline do + add_text "}" + end + + add_text ">" + end + end end end end diff --git a/lib/super_diff/object_inspection/inspectors/default_object.rb b/lib/super_diff/object_inspection/inspectors/default_object.rb index e3be7d18..40d82b40 100644 --- a/lib/super_diff/object_inspection/inspectors/default_object.rb +++ b/lib/super_diff/object_inspection/inspectors/default_object.rb @@ -1,45 +1,59 @@ module SuperDiff module ObjectInspection module Inspectors - DefaultObject = InspectionTree.new do - when_empty do - add_text do |object| - object.inspect - end + class DefaultObject < Base + def self.applies_to?(_value) + true end - when_non_empty do - when_singleline do - add_text do |object| - object.inspect - end - end + protected - when_multiline do - add_text do |object| - "#<%s:0x%x {" % { - class: object.class, - id: object.object_id * 2, - } + def inspection_tree + # rubocop:disable Metrics/BlockLength + InspectionTree.new do + when_empty do + # rubocop:disable Style/SymbolProc + add_text do |object| + object.inspect + end + # rubocop:enable Style/SymbolProc end - nested do |object| - add_break " " - - insert_separated_list( - object.instance_variables.sort, - separator: "," - ) do |name| - add_text name.to_s - add_text "=" - add_inspection_of object.instance_variable_get(name) + when_non_empty do + when_singleline do + add_text do |object| + object.inspect + end end - end - add_break + when_multiline do + add_text do |object| + "#<%s:0x%x {" % { + class: object.class, + id: object.object_id * 2, + } + end - add_text "}>" + nested do |object| + add_break " " + + insert_separated_list( + object.instance_variables.sort, + separator: "," + ) do |name| + add_text name.to_s + add_text "=" + add_inspection_of object.instance_variable_get(name) + end + end + + add_break + + add_text "}>" + end + end end + # rubocop:enable Metrics/BlockLength end end end diff --git a/lib/super_diff/object_inspection/inspectors/defaults.rb b/lib/super_diff/object_inspection/inspectors/defaults.rb new file mode 100644 index 00000000..471ed52d --- /dev/null +++ b/lib/super_diff/object_inspection/inspectors/defaults.rb @@ -0,0 +1,15 @@ +module SuperDiff + module ObjectInspection + module Inspectors + DEFAULTS = [ + CustomObject, + Array, + Hash, + Primitive, + String, + TimeLike, + DefaultObject, + ].freeze + end + end +end diff --git a/lib/super_diff/object_inspection/inspectors/hash.rb b/lib/super_diff/object_inspection/inspectors/hash.rb index 5abf69d8..d700b6ad 100644 --- a/lib/super_diff/object_inspection/inspectors/hash.rb +++ b/lib/super_diff/object_inspection/inspectors/hash.rb @@ -1,20 +1,30 @@ module SuperDiff module ObjectInspection module Inspectors - Hash = InspectionTree.new do - when_empty do - add_text "{}" + class Hash < Base + def self.applies_to?(value) + value.is_a?(::Hash) end - when_non_empty do - add_text "{" + protected - nested do |hash| - insert_hash_inspection_of(hash) - end + def inspection_tree + InspectionTree.new do + when_empty do + add_text "{}" + end + + when_non_empty do + add_text "{" - add_break " " - add_text "}" + nested do |hash| + insert_hash_inspection_of(hash) + end + + add_break " " + add_text "}" + end + end end end end diff --git a/lib/super_diff/object_inspection/inspectors/main.rb b/lib/super_diff/object_inspection/inspectors/main.rb new file mode 100644 index 00000000..cebaf904 --- /dev/null +++ b/lib/super_diff/object_inspection/inspectors/main.rb @@ -0,0 +1,35 @@ +module SuperDiff + module ObjectInspection + module Inspectors + class Main + extend AttrExtras.mixin + + method_object :object, [:as_single_line!, :indent_level!] + + def call + if resolved_class + resolved_class.call( + object, + as_single_line: as_single_line?, + indent_level: indent_level, + ) + else + raise NoInspectorAvailableError.create(object) + end + end + + private + + attr_query :as_single_line? + + def resolved_class + available_classes.find { |klass| klass.applies_to?(object) } + end + + def available_classes + SuperDiff.configuration.extra_inspector_classes + DEFAULTS + end + end + end + end +end diff --git a/lib/super_diff/object_inspection/inspectors/primitive.rb b/lib/super_diff/object_inspection/inspectors/primitive.rb index c1f3ea46..2be68baa 100644 --- a/lib/super_diff/object_inspection/inspectors/primitive.rb +++ b/lib/super_diff/object_inspection/inspectors/primitive.rb @@ -1,12 +1,27 @@ module SuperDiff module ObjectInspection module Inspectors - Primitive = InspectionTree.new do - # rubocop:disable Style/SymbolProc - add_text do |object| - object.inspect + class Primitive < Base + def self.applies_to?(value) + case value + when true, false, nil, Symbol, Numeric, Regexp, Class + true + else + false + end + end + + protected + + def inspection_tree + InspectionTree.new do + # rubocop:disable Style/SymbolProc + add_text do |object| + object.inspect + end + # rubocop:enable Style/SymbolProc + end end - # rubocop:enable Style/SymbolProc end end end diff --git a/lib/super_diff/object_inspection/inspectors/string.rb b/lib/super_diff/object_inspection/inspectors/string.rb index 3737e418..b284816c 100644 --- a/lib/super_diff/object_inspection/inspectors/string.rb +++ b/lib/super_diff/object_inspection/inspectors/string.rb @@ -1,12 +1,22 @@ module SuperDiff module ObjectInspection module Inspectors - String = InspectionTree.new do - # rubocop:disable Style/SymbolProc - add_text do |string| - string.inspect + class String < Base + def self.applies_to?(value) + value.is_a?(::String) + end + + protected + + def inspection_tree + InspectionTree.new do + # rubocop:disable Style/SymbolProc + add_text do |string| + string.inspect + end + # rubocop:enable Style/SymbolProc + end end - # rubocop:enable Style/SymbolProc end end end diff --git a/lib/super_diff/object_inspection/inspectors/time.rb b/lib/super_diff/object_inspection/inspectors/time.rb deleted file mode 100644 index 479a414b..00000000 --- a/lib/super_diff/object_inspection/inspectors/time.rb +++ /dev/null @@ -1,13 +0,0 @@ -module SuperDiff - module ObjectInspection - module Inspectors - TIME_FORMAT = "%Y-%m-%d %H:%M:%S.%3N %Z %:z".freeze - - Time = InspectionTree.new do - add_text do |time| - "#{time.strftime(TIME_FORMAT)} (#{time.class})" - end - end - end - end -end diff --git a/lib/super_diff/object_inspection/inspectors/time_like.rb b/lib/super_diff/object_inspection/inspectors/time_like.rb new file mode 100644 index 00000000..ab4f5402 --- /dev/null +++ b/lib/super_diff/object_inspection/inspectors/time_like.rb @@ -0,0 +1,23 @@ +module SuperDiff + module ObjectInspection + module Inspectors + class TimeLike < Base + def self.applies_to?(value) + SuperDiff.time_like?(value) + end + + TIME_FORMAT = "%Y-%m-%d %H:%M:%S.%3N %Z %:z".freeze + + protected + + def inspection_tree + InspectionTree.new do + add_text do |time| + "#{time.strftime(TIME_FORMAT)} (#{time.class})" + end + end + end + end + end + end +end diff --git a/lib/super_diff/object_inspection/map.rb b/lib/super_diff/object_inspection/map.rb deleted file mode 100644 index 9d032542..00000000 --- a/lib/super_diff/object_inspection/map.rb +++ /dev/null @@ -1,30 +0,0 @@ -module SuperDiff - module ObjectInspection - class Map - def prepend(mod) - singleton_class.prepend(mod) - end - - def call(object) - if object.respond_to?(:attributes_for_super_diff) - Inspectors::CustomObject - else - case object - when Array - Inspectors::Array - when Hash - Inspectors::Hash - when String - Inspectors::String - when Time - Inspectors::Time - when true, false, nil, Symbol, Numeric, Regexp, Class - Inspectors::Primitive - else - Inspectors::DefaultObject - end - end - end - end - end -end diff --git a/lib/super_diff/operation_sequence.rb b/lib/super_diff/operation_sequence.rb deleted file mode 100644 index 28168afe..00000000 --- a/lib/super_diff/operation_sequence.rb +++ /dev/null @@ -1,34 +0,0 @@ -module SuperDiff - class OperationSequence - extend AttrExtras.mixin - - method_object :value - - def call - if resolved_class - begin - resolved_class.new([], value_class: value.class) - rescue ArgumentError - resolved_class.new([]) - end - else - raise NoOperationalSequenceAvailableError.create(value) - end - end - - private - - def resolved_class - if value.respond_to?(:attributes_for_super_diff) - OperationSequences::CustomObject - else - available_classes.find { |klass| klass.applies_to?(value) } - end - end - - def available_classes - SuperDiff.configuration.extra_operation_sequence_classes + - OperationSequences::DEFAULTS - end - end -end diff --git a/lib/super_diff/operation_sequences.rb b/lib/super_diff/operation_sequences.rb index 29a2d8ad..4a67ce63 100644 --- a/lib/super_diff/operation_sequences.rb +++ b/lib/super_diff/operation_sequences.rb @@ -5,8 +5,9 @@ module OperationSequences autoload :CustomObject, "super_diff/operation_sequences/custom_object" autoload :DefaultObject, "super_diff/operation_sequences/default_object" autoload :Hash, "super_diff/operation_sequences/hash" + autoload :Main, "super_diff/operation_sequences/main" autoload :MultilineString, "super_diff/operation_sequences/multiline_string" - - DEFAULTS = [Array, Hash, CustomObject, DefaultObject].freeze end end + +require "super_diff/operation_sequences/defaults" diff --git a/lib/super_diff/operation_sequences/default_object.rb b/lib/super_diff/operation_sequences/default_object.rb index edd0ea7c..2117ad61 100644 --- a/lib/super_diff/operation_sequences/default_object.rb +++ b/lib/super_diff/operation_sequences/default_object.rb @@ -15,7 +15,7 @@ def initialize(collection, value_class:) end def to_diff(indent_level:, add_comma: false, **_rest) - DiffFormatter.call( + DiffFormatters::Main.call( self, indent_level: indent_level, add_comma: add_comma, diff --git a/lib/super_diff/operation_sequences/defaults.rb b/lib/super_diff/operation_sequences/defaults.rb new file mode 100644 index 00000000..be151f58 --- /dev/null +++ b/lib/super_diff/operation_sequences/defaults.rb @@ -0,0 +1,5 @@ +module SuperDiff + module OperationSequences + DEFAULTS = [Array, Hash, CustomObject, DefaultObject].freeze + end +end diff --git a/lib/super_diff/operation_sequences/main.rb b/lib/super_diff/operation_sequences/main.rb new file mode 100644 index 00000000..ea630121 --- /dev/null +++ b/lib/super_diff/operation_sequences/main.rb @@ -0,0 +1,35 @@ +module SuperDiff + module OperationSequences + class Main + extend AttrExtras.mixin + + method_object :value + + def call + if resolved_class + begin + resolved_class.new([], value_class: value.class) + rescue ArgumentError + resolved_class.new([]) + end + else + raise Errors::NoOperationalSequenceAvailableError.create(value) + end + end + + private + + def resolved_class + if value.respond_to?(:attributes_for_super_diff) + CustomObject + else + available_classes.find { |klass| klass.applies_to?(value) } + end + end + + def available_classes + SuperDiff.configuration.extra_operation_sequence_classes + DEFAULTS + end + end + end +end diff --git a/lib/super_diff/operational_sequencer.rb b/lib/super_diff/operational_sequencer.rb deleted file mode 100644 index 7d31a3d1..00000000 --- a/lib/super_diff/operational_sequencer.rb +++ /dev/null @@ -1,40 +0,0 @@ -module SuperDiff - class OperationalSequencer - extend AttrExtras.mixin - - method_object [:expected!, :actual!, :all_or_nothing!] - - def call - if resolved_class - resolved_class.call( - expected: expected, - actual: actual, - ) - elsif all_or_nothing? - raise NoOperationalSequencerAvailableError.create(expected, actual) - else - nil - end - end - - private - - attr_query :all_or_nothing? - - def resolved_class - available_classes.find { |klass| klass.applies_to?(expected, actual) } - end - - def available_classes - classes = - SuperDiff.configuration.extra_operational_sequencer_classes + - OperationalSequencers::DEFAULTS - - if all_or_nothing? - classes + [OperationalSequencers::DefaultObject] - else - classes - end - end - end -end diff --git a/lib/super_diff/operational_sequencers.rb b/lib/super_diff/operational_sequencers.rb index 28902965..acb18d4d 100644 --- a/lib/super_diff/operational_sequencers.rb +++ b/lib/super_diff/operational_sequencers.rb @@ -5,13 +5,14 @@ module OperationalSequencers autoload :CustomObject, "super_diff/operational_sequencers/custom_object" autoload :DefaultObject, "super_diff/operational_sequencers/default_object" autoload :Hash, "super_diff/operational_sequencers/hash" + autoload :Main, "super_diff/operational_sequencers/main" # TODO: Where is this used? autoload( :MultilineString, "super_diff/operational_sequencers/multiline_string", ) autoload :TimeLike, "super_diff/operational_sequencers/time_like" - - DEFAULTS = [Array, Hash, CustomObject].freeze end end + +require "super_diff/operational_sequencers/defaults" diff --git a/lib/super_diff/operational_sequencers/base.rb b/lib/super_diff/operational_sequencers/base.rb index bcb53f35..3d9467b0 100644 --- a/lib/super_diff/operational_sequencers/base.rb +++ b/lib/super_diff/operational_sequencers/base.rb @@ -6,6 +6,7 @@ def self.applies_to?(_expected, _actual) end extend AttrExtras.mixin + include ImplementationChecks method_object [:expected!, :actual!] @@ -16,11 +17,11 @@ def call protected def unary_operations - SuperDiff.unimplemented_instance_method! + unimplemented_instance_method! end def build_operation_sequence - SuperDiff.unimplemented_instance_method! + unimplemented_instance_method! end private @@ -86,7 +87,7 @@ def should_compare?(operation, next_operation) end def sequence(expected, actual) - OperationalSequencer.call( + OperationalSequencers::Main.call( expected: expected, actual: actual, all_or_nothing: false, diff --git a/lib/super_diff/operational_sequencers/default_object.rb b/lib/super_diff/operational_sequencers/default_object.rb index f52e2482..16ebff72 100644 --- a/lib/super_diff/operational_sequencers/default_object.rb +++ b/lib/super_diff/operational_sequencers/default_object.rb @@ -29,7 +29,7 @@ def build_operation_sequence end def find_operation_sequence_for(value) - OperationSequence.call(value) + OperationSequences::Main.call(value) end def attribute_names diff --git a/lib/super_diff/operational_sequencers/defaults.rb b/lib/super_diff/operational_sequencers/defaults.rb new file mode 100644 index 00000000..f0c70dd0 --- /dev/null +++ b/lib/super_diff/operational_sequencers/defaults.rb @@ -0,0 +1,5 @@ +module SuperDiff + module OperationalSequencers + DEFAULTS = [Array, Hash, CustomObject].freeze + end +end diff --git a/lib/super_diff/operational_sequencers/main.rb b/lib/super_diff/operational_sequencers/main.rb new file mode 100644 index 00000000..360b3206 --- /dev/null +++ b/lib/super_diff/operational_sequencers/main.rb @@ -0,0 +1,42 @@ +module SuperDiff + module OperationalSequencers + class Main + extend AttrExtras.mixin + + method_object [:expected!, :actual!, :all_or_nothing!] + + def call + if resolved_class + resolved_class.call( + expected: expected, + actual: actual, + ) + elsif all_or_nothing? + raise NoOperationalSequencerAvailableError.create(expected, actual) + else + nil + end + end + + private + + attr_query :all_or_nothing? + + def resolved_class + available_classes.find { |klass| klass.applies_to?(expected, actual) } + end + + def available_classes + classes = + SuperDiff.configuration.extra_operational_sequencer_classes + + DEFAULTS + + if all_or_nothing? + classes + [DefaultObject] + else + classes + end + end + end + end +end diff --git a/lib/super_diff/operational_sequencers/time_like.rb b/lib/super_diff/operational_sequencers/time_like.rb index 081410bb..08397038 100644 --- a/lib/super_diff/operational_sequencers/time_like.rb +++ b/lib/super_diff/operational_sequencers/time_like.rb @@ -2,19 +2,23 @@ module SuperDiff module OperationalSequencers class TimeLike < CustomObject def self.applies_to?(expected, actual) - (expected.is_a?(Time) && actual.is_a?(Time)) || - ( - # Check for ActiveSupport's #acts_like_time? for their time-like objects - # (like ActiveSupport::TimeWithZone). - expected.respond_to?(:acts_like_time?) && expected.acts_like_time? && - actual.respond_to?(:acts_like_time?) && actual.acts_like_time? - ) + SuperDiff.time_like?(expected) && SuperDiff.time_like?(actual) end protected def attribute_names - base = ["year", "month", "day", "hour", "min", "sec", "nsec", "zone", "gmt_offset"] + base = [ + "year", + "month", + "day", + "hour", + "min", + "sec", + "nsec", + "zone", + "gmt_offset", + ] # If timezones are different, also show a normalized timestamp at the # end of the diff to help visualize why they are different moments in diff --git a/lib/super_diff/rspec.rb b/lib/super_diff/rspec.rb index f539b4de..225e06ba 100644 --- a/lib/super_diff/rspec.rb +++ b/lib/super_diff/rspec.rb @@ -11,12 +11,6 @@ module RSpec autoload :ObjectInspection, "super_diff/rspec/object_inspection" autoload :OperationalSequencers, "super_diff/rspec/operational_sequencers" - class << self - attr_accessor :extra_differ_classes - attr_accessor :extra_operational_sequencer_classes - attr_accessor :extra_diff_formatter_classes - end - def self.configure yield configuration end @@ -68,31 +62,34 @@ def self.fuzzy_object?(value) end SuperDiff.configuration.tap do |config| - config.add_extra_differ_class(Differs::CollectionContainingExactly) - config.add_extra_differ_class(Differs::CollectionIncluding) - config.add_extra_differ_class(Differs::HashIncluding) - config.add_extra_differ_class(Differs::ObjectHavingAttributes) + config.add_extra_differ_classes( + Differs::CollectionContainingExactly, + Differs::CollectionIncluding, + Differs::HashIncluding, + Differs::ObjectHavingAttributes, + ) - config.add_extra_operational_sequencer_class( + config.add_extra_operational_sequencer_classes( OperationalSequencers::CollectionContainingExactly, - ) - config.add_extra_operational_sequencer_class( OperationalSequencers::CollectionIncluding, - ) - config.add_extra_operational_sequencer_class( OperationalSequencers::HashIncluding, - ) - config.add_extra_operational_sequencer_class( OperationalSequencers::ObjectHavingAttributes, ) + + config.add_extra_inspector_classes( + ObjectInspection::Inspectors::CollectionContainingExactly, + ObjectInspection::Inspectors::CollectionIncluding, + ObjectInspection::Inspectors::HashIncluding, + ObjectInspection::Inspectors::InstanceOf, + ObjectInspection::Inspectors::KindOf, + ObjectInspection::Inspectors::ObjectHavingAttributes, + ObjectInspection::Inspectors::Primitive, + ObjectInspection::Inspectors::ValueWithin, + ) end end end require_relative "rspec/monkey_patches" -SuperDiff::ObjectInspection.map.prepend( - SuperDiff::RSpec::ObjectInspection::MapExtension, -) - SuperDiff::Csi.color_enabled = ::RSpec.configuration.color_enabled? diff --git a/lib/super_diff/rspec/differ.rb b/lib/super_diff/rspec/differ.rb index 126f9623..701de3dc 100644 --- a/lib/super_diff/rspec/differ.rb +++ b/lib/super_diff/rspec/differ.rb @@ -7,12 +7,16 @@ class Differ def diff if worth_diffing? - diff = SuperDiff::Differ.call(expected, actual, omit_empty: true) + diff = SuperDiff::Differs::Main.call( + expected, + actual, + omit_empty: true, + ) "\n\n" + diff else "" end - rescue NoDifferAvailableError + rescue SuperDiff::Errors::NoDifferAvailableError "" end diff --git a/lib/super_diff/rspec/matcher_text_template.rb b/lib/super_diff/rspec/matcher_text_template.rb index 52dbb61b..71d4fe99 100644 --- a/lib/super_diff/rspec/matcher_text_template.rb +++ b/lib/super_diff/rspec/matcher_text_template.rb @@ -3,7 +3,7 @@ module RSpec class MatcherTextTemplate MAX_LINE_LENGTH = 100 - def self.generate(&block) + def self.render(&block) new(&block).to_s end diff --git a/lib/super_diff/rspec/object_inspection.rb b/lib/super_diff/rspec/object_inspection.rb index 110aef9a..703cc577 100644 --- a/lib/super_diff/rspec/object_inspection.rb +++ b/lib/super_diff/rspec/object_inspection.rb @@ -2,7 +2,6 @@ module SuperDiff module RSpec module ObjectInspection autoload :Inspectors, "super_diff/rspec/object_inspection/inspectors" - autoload :MapExtension, "super_diff/rspec/object_inspection/map_extension" end end end diff --git a/lib/super_diff/rspec/object_inspection/inspectors.rb b/lib/super_diff/rspec/object_inspection/inspectors.rb index 14113301..b7ffa150 100644 --- a/lib/super_diff/rspec/object_inspection/inspectors.rb +++ b/lib/super_diff/rspec/object_inspection/inspectors.rb @@ -26,6 +26,10 @@ module Inspectors :ObjectHavingAttributes, "super_diff/rspec/object_inspection/inspectors/object_having_attributes", ) + autoload( + :Primitive, + "super_diff/rspec/object_inspection/inspectors/primitive", + ) autoload( :ValueWithin, "super_diff/rspec/object_inspection/inspectors/value_within", diff --git a/lib/super_diff/rspec/object_inspection/inspectors/collection_containing_exactly.rb b/lib/super_diff/rspec/object_inspection/inspectors/collection_containing_exactly.rb index 66448cd8..d80ddd40 100644 --- a/lib/super_diff/rspec/object_inspection/inspectors/collection_containing_exactly.rb +++ b/lib/super_diff/rspec/object_inspection/inspectors/collection_containing_exactly.rb @@ -2,17 +2,26 @@ module SuperDiff module RSpec module ObjectInspection module Inspectors - CollectionContainingExactly = - SuperDiff::ObjectInspection::InspectionTree.new do - add_text "#" + nested do |aliased_matcher| + insert_array_inspection_of(aliased_matcher.expected) + end + + add_break + add_text ")>" + end end + end end end end diff --git a/lib/super_diff/rspec/object_inspection/inspectors/collection_including.rb b/lib/super_diff/rspec/object_inspection/inspectors/collection_including.rb index 42a0ca2f..6213e7db 100644 --- a/lib/super_diff/rspec/object_inspection/inspectors/collection_including.rb +++ b/lib/super_diff/rspec/object_inspection/inspectors/collection_including.rb @@ -2,19 +2,25 @@ module SuperDiff module RSpec module ObjectInspection module Inspectors - CollectionIncluding = SuperDiff::ObjectInspection::InspectionTree.new do - def self.applies_to?(object) - SuperDiff::RSpec.a_collection_including?(object) + class CollectionIncluding < SuperDiff::ObjectInspection::Inspectors::Base + def self.applies_to?(value) + SuperDiff::RSpec.a_collection_including_something?(value) end - add_text "#" + add_break + add_text ")>" + end + end end end end diff --git a/lib/super_diff/rspec/object_inspection/inspectors/hash_including.rb b/lib/super_diff/rspec/object_inspection/inspectors/hash_including.rb index fb747069..fef04e34 100644 --- a/lib/super_diff/rspec/object_inspection/inspectors/hash_including.rb +++ b/lib/super_diff/rspec/object_inspection/inspectors/hash_including.rb @@ -2,18 +2,28 @@ module SuperDiff module RSpec module ObjectInspection module Inspectors - HashIncluding = SuperDiff::ObjectInspection::InspectionTree.new do - add_text "#" + protected + + def inspection_tree + SuperDiff::ObjectInspection::InspectionTree.new do + add_text "#" + end + end end end end diff --git a/lib/super_diff/rspec/object_inspection/inspectors/instance_of.rb b/lib/super_diff/rspec/object_inspection/inspectors/instance_of.rb index ac35bcea..4e36ea82 100644 --- a/lib/super_diff/rspec/object_inspection/inspectors/instance_of.rb +++ b/lib/super_diff/rspec/object_inspection/inspectors/instance_of.rb @@ -2,9 +2,19 @@ module SuperDiff module RSpec module ObjectInspection module Inspectors - InstanceOf = SuperDiff::ObjectInspection::InspectionTree.new do - add_text do |aliased_matcher| - "#" + class InstanceOf < SuperDiff::ObjectInspection::Inspectors::Base + def self.applies_to?(value) + SuperDiff::RSpec.an_instance_of_something?(value) + end + + protected + + def inspection_tree + SuperDiff::ObjectInspection::InspectionTree.new do + add_text do |aliased_matcher| + "#" + end + end end end end diff --git a/lib/super_diff/rspec/object_inspection/inspectors/kind_of.rb b/lib/super_diff/rspec/object_inspection/inspectors/kind_of.rb index 307129fa..4fe1b2d5 100644 --- a/lib/super_diff/rspec/object_inspection/inspectors/kind_of.rb +++ b/lib/super_diff/rspec/object_inspection/inspectors/kind_of.rb @@ -2,9 +2,19 @@ module SuperDiff module RSpec module ObjectInspection module Inspectors - KindOf = SuperDiff::ObjectInspection::InspectionTree.new do - add_text do |aliased_matcher| - "#" + class KindOf < SuperDiff::ObjectInspection::Inspectors::Base + def self.applies_to?(value) + SuperDiff::RSpec.a_kind_of_something?(value) + end + + protected + + def inspection_tree + SuperDiff::ObjectInspection::InspectionTree.new do + add_text do |aliased_matcher| + "#" + end + end end end end diff --git a/lib/super_diff/rspec/object_inspection/inspectors/object_having_attributes.rb b/lib/super_diff/rspec/object_inspection/inspectors/object_having_attributes.rb index 60200f7f..02f0e1b6 100644 --- a/lib/super_diff/rspec/object_inspection/inspectors/object_having_attributes.rb +++ b/lib/super_diff/rspec/object_inspection/inspectors/object_having_attributes.rb @@ -2,20 +2,29 @@ module SuperDiff module RSpec module ObjectInspection module Inspectors - ObjectHavingAttributes = - SuperDiff::ObjectInspection::InspectionTree.new do - add_text "#" + nested do |aliased_matcher| + insert_hash_inspection_of( + aliased_matcher.expected, + initial_break: nil, + ) + end + + add_break + add_text ")>" + end end + end end end end diff --git a/lib/super_diff/rspec/object_inspection/inspectors/primitive.rb b/lib/super_diff/rspec/object_inspection/inspectors/primitive.rb new file mode 100644 index 00000000..563ead17 --- /dev/null +++ b/lib/super_diff/rspec/object_inspection/inspectors/primitive.rb @@ -0,0 +1,13 @@ +module SuperDiff + module RSpec + module ObjectInspection + module Inspectors + class Primitive < SuperDiff::ObjectInspection::Inspectors::Primitive + def self.applies_to?(value) + super || value.is_a?(::RSpec::Mocks::Double) + end + end + end + end + end +end diff --git a/lib/super_diff/rspec/object_inspection/inspectors/value_within.rb b/lib/super_diff/rspec/object_inspection/inspectors/value_within.rb index b57d2bf8..c0654468 100644 --- a/lib/super_diff/rspec/object_inspection/inspectors/value_within.rb +++ b/lib/super_diff/rspec/object_inspection/inspectors/value_within.rb @@ -2,16 +2,26 @@ module SuperDiff module RSpec module ObjectInspection module Inspectors - ValueWithin = SuperDiff::ObjectInspection::InspectionTree.new do - add_text "#" + protected + + def inspection_tree + SuperDiff::ObjectInspection::InspectionTree.new do + add_text "#" + end + end end end end diff --git a/lib/super_diff/rspec/object_inspection/map_extension.rb b/lib/super_diff/rspec/object_inspection/map_extension.rb deleted file mode 100644 index 85179947..00000000 --- a/lib/super_diff/rspec/object_inspection/map_extension.rb +++ /dev/null @@ -1,29 +0,0 @@ -module SuperDiff - module RSpec - module ObjectInspection - module MapExtension - def call(object) - if SuperDiff::RSpec.a_hash_including_something?(object) - Inspectors::HashIncluding - elsif SuperDiff::RSpec.a_collection_including_something?(object) - Inspectors::CollectionIncluding - elsif SuperDiff::RSpec.an_object_having_some_attributes?(object) - Inspectors::ObjectHavingAttributes - elsif SuperDiff::RSpec.a_collection_containing_exactly_something?(object) - Inspectors::CollectionContainingExactly - elsif SuperDiff::RSpec.a_kind_of_something?(object) - Inspectors::KindOf - elsif SuperDiff::RSpec.an_instance_of_something?(object) - Inspectors::InstanceOf - elsif SuperDiff::RSpec.a_value_within_something?(object) - Inspectors::ValueWithin - elsif object.is_a?(::RSpec::Mocks::Double) - SuperDiff::ObjectInspection::Inspectors::Primitive - else - super - end - end - end - end - end -end diff --git a/spec/unit/equality_matcher_spec.rb b/spec/unit/equality_matchers/main_spec.rb similarity index 99% rename from spec/unit/equality_matcher_spec.rb rename to spec/unit/equality_matchers/main_spec.rb index 0c5c6c63..a98d2bdc 100644 --- a/spec/unit/equality_matcher_spec.rb +++ b/spec/unit/equality_matchers/main_spec.rb @@ -1,6 +1,6 @@ require "spec_helper" -RSpec.describe SuperDiff::EqualityMatcher do +RSpec.describe SuperDiff::EqualityMatchers::Main do describe "#call" do context "given the same integers" do it "returns an empty string" do diff --git a/super_diff.gemspec b/super_diff.gemspec index 9b3998d0..e1756eac 100644 --- a/super_diff.gemspec +++ b/super_diff.gemspec @@ -17,7 +17,6 @@ Gem::Specification.new do |s| s.test_files = Dir["spec/**/*"] s.executables = Dir["exe/**/*"].map { |f| File.basename(f) } - s.add_dependency "attr_extras" s.add_dependency "diff-lcs" s.add_dependency "patience_diff" end