Skip to content

Commit 9e13afc

Browse files
lucaserasjas14
andauthored
Add handling for Ranges (#267)
Fix for #154. Adds a RangeObject inspection tree builder so Range objects are printed as strings, like this: ``` (1..2) ``` instead of as default objects: ``` #<Range:0x123456 ... ``` --------- Co-authored-by: Joe Stein <[email protected]>
1 parent 613bc30 commit 9e13afc

File tree

6 files changed

+74
-1
lines changed

6 files changed

+74
-1
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66

77
- Improve inspection of Module. [#263](https://github.com/splitwise/super_diff/pull/263) by [@phorsuedzie](https://github.com/phorsuedzie)
88
- Fix multiline string diff with blank lines. [#266](https://github.com/splitwise/super_diff/pull/263)
9+
- Improve inspection of Range objects. [#267](https://github.com/splitwise/super_diff/pull/267)
910

1011
## 0.13.0 - 2024-09-22
1112

lib/super_diff/basic.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ module Basic
2727
InspectionTreeBuilders::TimeLike,
2828
InspectionTreeBuilders::DateLike,
2929
InspectionTreeBuilders::DataObject,
30+
InspectionTreeBuilders::RangeObject,
3031
InspectionTreeBuilders::DefaultObject
3132
)
3233

lib/super_diff/basic/inspection_tree_builders.rb

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,15 +10,19 @@ module InspectionTreeBuilders
1010
:DataObject,
1111
"super_diff/basic/inspection_tree_builders/data_object"
1212
)
13+
autoload :DateLike, "super_diff/basic/inspection_tree_builders/date_like"
1314
autoload(
1415
:DefaultObject,
1516
"super_diff/basic/inspection_tree_builders/default_object"
1617
)
1718
autoload :Hash, "super_diff/basic/inspection_tree_builders/hash"
1819
autoload :Primitive, "super_diff/basic/inspection_tree_builders/primitive"
20+
autoload(
21+
:RangeObject,
22+
"super_diff/basic/inspection_tree_builders/range_object"
23+
)
1924
autoload :String, "super_diff/basic/inspection_tree_builders/string"
2025
autoload :TimeLike, "super_diff/basic/inspection_tree_builders/time_like"
21-
autoload :DateLike, "super_diff/basic/inspection_tree_builders/date_like"
2226
end
2327
end
2428
end
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
module SuperDiff
2+
module Basic
3+
module InspectionTreeBuilders
4+
class RangeObject < Core::AbstractInspectionTreeBuilder
5+
def self.applies_to?(value)
6+
value.is_a?(Range)
7+
end
8+
9+
def call
10+
Core::InspectionTree.new do |t1|
11+
t1.as_lines_when_rendering_to_lines { |t2| t2.add_text object.to_s }
12+
end
13+
end
14+
end
15+
end
16+
end
17+
end

spec/integration/rspec/eq_matcher_spec.rb

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1056,6 +1056,37 @@
10561056
end
10571057
end
10581058

1059+
context "when comparing ranges" do
1060+
it "produces the correct failure message when used in the positive" do
1061+
as_both_colored_and_uncolored do |color_enabled|
1062+
snippet = "expect(1..5).to eq(5..6)"
1063+
program = make_plain_test_program(snippet, color_enabled: color_enabled)
1064+
1065+
expected_output =
1066+
build_expected_output(
1067+
color_enabled: color_enabled,
1068+
snippet: snippet,
1069+
newline_before_expectation: true,
1070+
expectation:
1071+
proc do
1072+
line do
1073+
plain "Expected "
1074+
actual "1..5"
1075+
plain " to eq "
1076+
expected "5..6"
1077+
plain "."
1078+
end
1079+
end,
1080+
diff: nil
1081+
)
1082+
1083+
expect(program).to produce_output_when_run(expected_output).in_color(
1084+
color_enabled
1085+
)
1086+
end
1087+
end
1088+
end
1089+
10591090
it_behaves_like "a matcher that supports elided diffs" do
10601091
let(:matcher) { :eq }
10611092
end
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
require "spec_helper"
2+
3+
RSpec.describe SuperDiff, type: :unit do
4+
describe ".inspect_object" do
5+
context "given as_lines: false" do
6+
subject(:output) do
7+
described_class.inspect_object(object, as_lines: false)
8+
end
9+
10+
context "given a simple range" do
11+
let(:object) { 1..5 }
12+
13+
it "shows the data" do
14+
expect(output).to eq("1..5")
15+
end
16+
end
17+
end
18+
end
19+
end

0 commit comments

Comments
 (0)