Skip to content

Commit c99a68b

Browse files
committed
Add inspectors for more fuzzy matchers
Ensure that these fuzzy matcher objects are inspected in diff output: * `an_instance_of(...)` * `a_kind_of(...)` * `a_value_within(...).of(...)`
1 parent b2c48e1 commit c99a68b

File tree

9 files changed

+170
-5
lines changed

9 files changed

+170
-5
lines changed

lib/super_diff/object_inspection/inspection_tree.rb

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -107,8 +107,12 @@ def insert_separated_list(enumerable, separator: ",")
107107
end
108108
end
109109

110-
def add_inspection_of(value)
111-
add_node :inspection, value
110+
def add_inspection_of(value = nil, &block)
111+
if block
112+
add_node :inspection, &block
113+
else
114+
add_node :inspection, value
115+
end
112116
end
113117

114118
def apply_tree(tree)

lib/super_diff/object_inspection/nodes/inspection.rb

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,16 @@ module SuperDiff
22
module ObjectInspection
33
module Nodes
44
class Inspection < Base
5-
def evaluate(_object, indent_level:, as_single_line:)
5+
def evaluate(object, indent_level:, as_single_line:)
6+
value =
7+
if block
8+
tree.evaluate_block(object, &block)
9+
else
10+
immediate_value
11+
end
12+
613
SuperDiff::ObjectInspection.inspect(
7-
immediate_value,
14+
value,
815
indent_level: indent_level,
916
as_single_line: as_single_line,
1017
)

lib/super_diff/rspec.rb

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,21 @@ def self.a_collection_containing_exactly_something?(value)
4848
value.base_matcher.is_a?(::RSpec::Matchers::BuiltIn::ContainExactly)
4949
end
5050

51+
def self.a_kind_of_something?(value)
52+
fuzzy_object?(value) &&
53+
value.base_matcher.is_a?(::RSpec::Matchers::BuiltIn::BeAKindOf)
54+
end
55+
56+
def self.an_instance_of_something?(value)
57+
fuzzy_object?(value) &&
58+
value.base_matcher.is_a?(::RSpec::Matchers::BuiltIn::BeAnInstanceOf)
59+
end
60+
61+
def self.a_value_within_something?(value)
62+
fuzzy_object?(value) &&
63+
value.base_matcher.is_a?(::RSpec::Matchers::BuiltIn::BeWithin)
64+
end
65+
5166
def self.fuzzy_object?(value)
5267
value.is_a?(::RSpec::Matchers::AliasedMatcher)
5368
end

lib/super_diff/rspec/object_inspection/inspectors.rb

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,22 @@ module Inspectors
1414
:HashIncluding,
1515
"super_diff/rspec/object_inspection/inspectors/hash_including",
1616
)
17+
autoload(
18+
:InstanceOf,
19+
"super_diff/rspec/object_inspection/inspectors/instance_of",
20+
)
21+
autoload(
22+
:KindOf,
23+
"super_diff/rspec/object_inspection/inspectors/kind_of",
24+
)
1725
autoload(
1826
:ObjectHavingAttributes,
1927
"super_diff/rspec/object_inspection/inspectors/object_having_attributes",
2028
)
29+
autoload(
30+
:ValueWithin,
31+
"super_diff/rspec/object_inspection/inspectors/value_within",
32+
)
2133
end
2234
end
2335
end
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
module SuperDiff
2+
module RSpec
3+
module ObjectInspection
4+
module Inspectors
5+
InstanceOf = SuperDiff::ObjectInspection::InspectionTree.new do
6+
add_text do |aliased_matcher|
7+
"#<an instance of #{aliased_matcher.expected}>"
8+
end
9+
end
10+
end
11+
end
12+
end
13+
end
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
module SuperDiff
2+
module RSpec
3+
module ObjectInspection
4+
module Inspectors
5+
KindOf = SuperDiff::ObjectInspection::InspectionTree.new do
6+
add_text do |aliased_matcher|
7+
"#<a kind of #{aliased_matcher.expected}>"
8+
end
9+
end
10+
end
11+
end
12+
end
13+
end
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
module SuperDiff
2+
module RSpec
3+
module ObjectInspection
4+
module Inspectors
5+
ValueWithin = SuperDiff::ObjectInspection::InspectionTree.new do
6+
add_text "#<a value within "
7+
8+
add_inspection_of do |aliased_matcher|
9+
aliased_matcher.base_matcher.instance_variable_get("@delta")
10+
end
11+
12+
add_text " of "
13+
add_inspection_of(&:expected)
14+
add_text ">"
15+
end
16+
end
17+
end
18+
end
19+
end

lib/super_diff/rspec/object_inspection/map_extension.rb

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,12 @@ def call(object)
1111
Inspectors::ObjectHavingAttributes
1212
elsif SuperDiff::RSpec.a_collection_containing_exactly_something?(object)
1313
Inspectors::CollectionContainingExactly
14+
elsif SuperDiff::RSpec.a_kind_of_something?(object)
15+
Inspectors::KindOf
16+
elsif SuperDiff::RSpec.an_instance_of_something?(object)
17+
Inspectors::InstanceOf
18+
elsif SuperDiff::RSpec.a_value_within_something?(object)
19+
Inspectors::ValueWithin
1420
elsif object.is_a?(::RSpec::Mocks::Double)
1521
SuperDiff::ObjectInspection::Inspectors::Primitive
1622
else

spec/unit/object_inspection_spec.rb

Lines changed: 77 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -601,7 +601,7 @@
601601
as_single_line: false,
602602
)
603603
expect(inspection).to match(
604-
/#<SuperDiff::Test::EmptyClass:0x[a-z0-9]+>/
604+
/#<SuperDiff::Test::EmptyClass:0x[a-z0-9]+>/,
605605
)
606606
end
607607
end
@@ -734,6 +734,82 @@
734734
end
735735
end
736736

737+
context "given a kind-of-<something>" do
738+
context "given as_single_line: true" do
739+
it "returns a representation of the object on a single line" do
740+
inspection = described_class.inspect(
741+
a_kind_of(Symbol),
742+
as_single_line: true,
743+
)
744+
745+
expect(inspection).to eq(%(#<a kind of Symbol>))
746+
end
747+
end
748+
749+
context "given as_single_line: false" do
750+
it "returns a representation of the object on a single line" do
751+
inspection = described_class.inspect(
752+
a_kind_of(Symbol),
753+
as_single_line: false,
754+
)
755+
756+
expect(inspection).to eq(%(#<a kind of Symbol>))
757+
end
758+
end
759+
end
760+
761+
context "given an-instance-of-<something>" do
762+
context "given as_single_line: true" do
763+
it "returns a representation of the object on a single line" do
764+
inspection = described_class.inspect(
765+
an_instance_of(Symbol),
766+
as_single_line: true,
767+
)
768+
769+
expect(inspection).to eq(%(#<an instance of Symbol>))
770+
end
771+
end
772+
773+
context "given as_single_line: false" do
774+
it "returns a representation of the object on a single line" do
775+
inspection = described_class.inspect(
776+
an_instance_of(Symbol),
777+
as_single_line: false,
778+
)
779+
780+
expect(inspection).to eq(%(#<an instance of Symbol>))
781+
end
782+
end
783+
end
784+
785+
context "given a-value-within-<something>" do
786+
context "given as_single_line: true" do
787+
it "returns a representation of the object on a single line" do
788+
inspection = described_class.inspect(
789+
a_value_within(1).of(Time.utc(2020, 4, 9)),
790+
as_single_line: true,
791+
)
792+
793+
expect(inspection).to eq(
794+
%(#<a value within 1 of 2020-04-09 00:00:00.000 UTC +00:00 (Time)>),
795+
)
796+
end
797+
end
798+
799+
context "given as_single_line: false" do
800+
it "returns a representation of the object on a single line" do
801+
inspection = described_class.inspect(
802+
a_value_within(1).of(Time.utc(2020, 4, 9)),
803+
as_single_line: false,
804+
)
805+
806+
expect(inspection).to eq(
807+
%(#<a value within 1 of 2020-04-09 00:00:00.000 UTC +00:00 (Time)>),
808+
)
809+
end
810+
end
811+
end
812+
737813
context "given a Double" do
738814
context "that is anonymous" do
739815
context "given as_single_line: true" do

0 commit comments

Comments
 (0)