Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 6 additions & 2 deletions lib/super_diff/object_inspection/inspection_tree.rb
Original file line number Diff line number Diff line change
Expand Up @@ -107,8 +107,12 @@ def insert_separated_list(enumerable, separator: ",")
end
end

def add_inspection_of(value)
add_node :inspection, value
def add_inspection_of(value = nil, &block)
if block
add_node :inspection, &block
else
add_node :inspection, value
end
end

def apply_tree(tree)
Expand Down
11 changes: 9 additions & 2 deletions lib/super_diff/object_inspection/nodes/inspection.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,16 @@ module SuperDiff
module ObjectInspection
module Nodes
class Inspection < Base
def evaluate(_object, indent_level:, as_single_line:)
def evaluate(object, indent_level:, as_single_line:)
value =
if block
tree.evaluate_block(object, &block)
else
immediate_value
end

SuperDiff::ObjectInspection.inspect(
immediate_value,
value,
indent_level: indent_level,
as_single_line: as_single_line,
)
Expand Down
15 changes: 15 additions & 0 deletions lib/super_diff/rspec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,21 @@ def self.a_collection_containing_exactly_something?(value)
value.base_matcher.is_a?(::RSpec::Matchers::BuiltIn::ContainExactly)
end

def self.a_kind_of_something?(value)
fuzzy_object?(value) &&
value.base_matcher.is_a?(::RSpec::Matchers::BuiltIn::BeAKindOf)
end

def self.an_instance_of_something?(value)
fuzzy_object?(value) &&
value.base_matcher.is_a?(::RSpec::Matchers::BuiltIn::BeAnInstanceOf)
end

def self.a_value_within_something?(value)
fuzzy_object?(value) &&
value.base_matcher.is_a?(::RSpec::Matchers::BuiltIn::BeWithin)
end

def self.fuzzy_object?(value)
value.is_a?(::RSpec::Matchers::AliasedMatcher)
end
Expand Down
12 changes: 12 additions & 0 deletions lib/super_diff/rspec/object_inspection/inspectors.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,22 @@ module Inspectors
:HashIncluding,
"super_diff/rspec/object_inspection/inspectors/hash_including",
)
autoload(
:InstanceOf,
"super_diff/rspec/object_inspection/inspectors/instance_of",
)
autoload(
:KindOf,
"super_diff/rspec/object_inspection/inspectors/kind_of",
)
autoload(
:ObjectHavingAttributes,
"super_diff/rspec/object_inspection/inspectors/object_having_attributes",
)
autoload(
:ValueWithin,
"super_diff/rspec/object_inspection/inspectors/value_within",
)
end
end
end
Expand Down
13 changes: 13 additions & 0 deletions lib/super_diff/rspec/object_inspection/inspectors/instance_of.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
module SuperDiff
module RSpec
module ObjectInspection
module Inspectors
InstanceOf = SuperDiff::ObjectInspection::InspectionTree.new do
add_text do |aliased_matcher|
"#<an instance of #{aliased_matcher.expected}>"
end
end
end
end
end
end
13 changes: 13 additions & 0 deletions lib/super_diff/rspec/object_inspection/inspectors/kind_of.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
module SuperDiff
module RSpec
module ObjectInspection
module Inspectors
KindOf = SuperDiff::ObjectInspection::InspectionTree.new do
add_text do |aliased_matcher|
"#<a kind of #{aliased_matcher.expected}>"
end
end
end
end
end
end
19 changes: 19 additions & 0 deletions lib/super_diff/rspec/object_inspection/inspectors/value_within.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
module SuperDiff
module RSpec
module ObjectInspection
module Inspectors
ValueWithin = SuperDiff::ObjectInspection::InspectionTree.new do
add_text "#<a value within "

add_inspection_of do |aliased_matcher|
aliased_matcher.base_matcher.instance_variable_get("@delta")
end

add_text " of "
add_inspection_of(&:expected)
add_text ">"
end
end
end
end
end
6 changes: 6 additions & 0 deletions lib/super_diff/rspec/object_inspection/map_extension.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,12 @@ def call(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
Expand Down
78 changes: 77 additions & 1 deletion spec/unit/object_inspection_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -601,7 +601,7 @@
as_single_line: false,
)
expect(inspection).to match(
/#<SuperDiff::Test::EmptyClass:0x[a-z0-9]+>/
/#<SuperDiff::Test::EmptyClass:0x[a-z0-9]+>/,
)
end
end
Expand Down Expand Up @@ -734,6 +734,82 @@
end
end

context "given a kind-of-<something>" do
context "given as_single_line: true" do
it "returns a representation of the object on a single line" do
inspection = described_class.inspect(
a_kind_of(Symbol),
as_single_line: true,
)

expect(inspection).to eq(%(#<a kind of Symbol>))
end
end

context "given as_single_line: false" do
it "returns a representation of the object on a single line" do
inspection = described_class.inspect(
a_kind_of(Symbol),
as_single_line: false,
)

expect(inspection).to eq(%(#<a kind of Symbol>))
end
end
end

context "given an-instance-of-<something>" do
context "given as_single_line: true" do
it "returns a representation of the object on a single line" do
inspection = described_class.inspect(
an_instance_of(Symbol),
as_single_line: true,
)

expect(inspection).to eq(%(#<an instance of Symbol>))
end
end

context "given as_single_line: false" do
it "returns a representation of the object on a single line" do
inspection = described_class.inspect(
an_instance_of(Symbol),
as_single_line: false,
)

expect(inspection).to eq(%(#<an instance of Symbol>))
end
end
end

context "given a-value-within-<something>" do
context "given as_single_line: true" do
it "returns a representation of the object on a single line" do
inspection = described_class.inspect(
a_value_within(1).of(Time.utc(2020, 4, 9)),
as_single_line: true,
)

expect(inspection).to eq(
%(#<a value within 1 of 2020-04-09 00:00:00.000 UTC +00:00 (Time)>),
)
end
end

context "given as_single_line: false" do
it "returns a representation of the object on a single line" do
inspection = described_class.inspect(
a_value_within(1).of(Time.utc(2020, 4, 9)),
as_single_line: false,
)

expect(inspection).to eq(
%(#<a value within 1 of 2020-04-09 00:00:00.000 UTC +00:00 (Time)>),
)
end
end
end

context "given a Double" do
context "that is anonymous" do
context "given as_single_line: true" do
Expand Down