Skip to content

Commit

Permalink
Add inspection tree builder for ActiveSupport::OrderedOptions (#202)
Browse files Browse the repository at this point in the history
I believe this fixes #199 and, I suspect, fixes #163. See
#199 (comment)
for more background. You can reproduce errors like those raised in the
aforementioned issues by checking out
26e1f6c and running `bundle exec rspec
spec/unit/active_support/object_inspection_spec.rb`.

The solution I opted for here was to create a hash-like inspector for
`ActiveSupport::OrderedOptions` that takes precedence over the default
`CustomObject` inspector, which in turn [takes precedence
over](https://github.com/mcmire/super_diff/blob/fb6718a2b60bc8135424295cc069a4b984983f77/lib/super_diff/object_inspection/inspection_tree_builders/defaults.rb#L5-L7)
the `Hash` inspector.

The `OrderedOptions` inspection tree is basically a copy-paste of the
one for `HashWithIndifferentAccess` – let me know if I should try to DRY
those up.
  • Loading branch information
jas14 authored Nov 13, 2023
1 parent 7772aa1 commit 8fdca7b
Show file tree
Hide file tree
Showing 6 changed files with 146 additions and 1 deletion.
3 changes: 2 additions & 1 deletion lib/super_diff/active_support.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,8 @@ module ActiveSupport
OperationTreeBuilders::HashWithIndifferentAccess
)
config.add_extra_inspection_tree_builder_classes(
ObjectInspection::InspectionTreeBuilders::HashWithIndifferentAccess
ObjectInspection::InspectionTreeBuilders::HashWithIndifferentAccess,
ObjectInspection::InspectionTreeBuilders::OrderedOptions
)
end
end
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,10 @@ module InspectionTreeBuilders
:HashWithIndifferentAccess,
"super_diff/active_support/object_inspection/inspection_tree_builders/hash_with_indifferent_access"
)
autoload(
:OrderedOptions,
"super_diff/active_support/object_inspection/inspection_tree_builders/ordered_options"
)
end
end
end
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
module SuperDiff
module ActiveSupport
module ObjectInspection
module InspectionTreeBuilders
class OrderedOptions < SuperDiff::ObjectInspection::InspectionTreeBuilders::Hash
def self.applies_to?(value)
value.is_a?(::ActiveSupport::OrderedOptions)
end

def call
SuperDiff::ObjectInspection::InspectionTree.new do
as_lines_when_rendering_to_lines(collection_bookend: :open) do
add_text "#<OrderedOptions {"
end

when_rendering_to_string { add_text " " }

nested do |ordered_options|
insert_hash_inspection_of(ordered_options.to_hash)
end

when_rendering_to_string { add_text " " }

as_lines_when_rendering_to_lines(collection_bookend: :close) do
add_text "}>"
end
end
end
end
end
end
end
end
3 changes: 3 additions & 0 deletions spec/spec_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,9 @@
config.default_formatter = "documentation" if !%w[true 1].include?(ENV["CI"])

config.filter_run_excluding active_record: true unless active_record_available
unless defined?(ActiveSupport)
config.filter_run_excluding active_support: true
end

config.order = :random
Kernel.srand config.seed
Expand Down
42 changes: 42 additions & 0 deletions spec/support/shared_examples/active_support.rb
Original file line number Diff line number Diff line change
Expand Up @@ -64,4 +64,46 @@
end
end
end

context "when comparing OrderedOptions and Hash instances",
active_record: true do
it "produces the correct failure message when used in the positive" do
as_both_colored_and_uncolored do |color_enabled|
snippet = <<~RUBY
expected = {beep: :bip}
actual = ::ActiveSupport::OrderedOptions[beep: :boop]
expect(expected).to eq(actual)
RUBY
program =
make_rspec_rails_test_program(snippet, color_enabled: color_enabled)

expected_output =
build_expected_output(
color_enabled: color_enabled,
snippet: "expect(expected).to eq(actual)",
expectation:
proc do
line do
plain "Expected "
actual "{ beep: :bip }"
plain " to eq "
expected "#<OrderedOptions { beep: :boop }>"
plain "."
end
end,
diff:
proc do
plain_line " {"
expected_line "- beep: :boop"
actual_line "+ beep: :bip"
plain_line " }"
end
)

expect(program).to produce_output_when_run(expected_output).in_color(
color_enabled
)
end
end
end
end
62 changes: 62 additions & 0 deletions spec/unit/active_support/object_inspection_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
require "spec_helper"

RSpec.describe SuperDiff, type: :unit do
describe ".inspect_object",
"for ActiveSupport objects",
active_support: true do
context "given an ActiveSupport::OrderedOptions object" do
context "given as_lines: false" do
it "returns an inspected version of the object" do
string =
described_class.inspect_object(
::ActiveSupport::OrderedOptions[name: "Bob", age: 42],
as_lines: false
)
expect(string).to eq(%(#<OrderedOptions { name: "Bob", age: 42 }>))
end
end

context "given as_lines: true" do
it "returns an inspected version of the object as multiple Lines" do
tiered_lines =
described_class.inspect_object(
::ActiveSupport::OrderedOptions[name: "Bob", age: 42],
as_lines: true,
type: :delete,
indentation_level: 1
)
expect(tiered_lines).to match(
[
an_object_having_attributes(
type: :delete,
indentation_level: 1,
value: "#<OrderedOptions {",
collection_bookend: :open
),
an_object_having_attributes(
type: :delete,
indentation_level: 2,
prefix: "name: ",
value: "\"Bob\"",
add_comma: true
),
an_object_having_attributes(
type: :delete,
indentation_level: 2,
prefix: "age: ",
value: "42",
add_comma: false
),
an_object_having_attributes(
type: :delete,
indentation_level: 1,
value: "}>",
collection_bookend: :close
)
]
)
end
end
end
end
end

1 comment on commit 8fdca7b

@jk779
Copy link

@jk779 jk779 commented on 8fdca7b Nov 14, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

❤️ thanks !

Please sign in to comment.