Skip to content

Commit f664285

Browse files
committed
Add RangeObject differ and OperationTreeBuilder
1 parent 2a0b9e5 commit f664285

File tree

7 files changed

+118
-0
lines changed

7 files changed

+118
-0
lines changed

lib/super_diff/basic.rb

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ module Basic
1414
Differs::Hash,
1515
Differs::TimeLike,
1616
Differs::DateLike,
17+
Differs::RangeObject,
1718
Differs::MultilineString,
1819
Differs::CustomObject,
1920
Differs::DefaultObject
@@ -37,6 +38,7 @@ module Basic
3738
OperationTreeBuilders::TimeLike,
3839
OperationTreeBuilders::DateLike,
3940
OperationTreeBuilders::CustomObject,
41+
OperationTreeBuilders::RangeObject,
4042
OperationTreeBuilders::DataObject
4143
)
4244

lib/super_diff/basic/differs.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ module Differs
44
autoload :Array, "super_diff/basic/differs/array"
55
autoload :CustomObject, "super_diff/basic/differs/custom_object"
66
autoload :DateLike, "super_diff/basic/differs/date_like"
7+
autoload :RangeObject, "super_diff/basic/differs/range_object"
78
autoload :DefaultObject, "super_diff/basic/differs/default_object"
89
autoload :Hash, "super_diff/basic/differs/hash"
910
autoload :MultilineString, "super_diff/basic/differs/multiline_string"
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 Differs
4+
class RangeObject < Core::AbstractDiffer
5+
def self.applies_to?(expected, actual)
6+
expected.is_a?(Range) && actual.is_a?(Range)
7+
end
8+
9+
protected
10+
11+
def operation_tree_builder_class
12+
OperationTreeBuilders::RangeObject
13+
end
14+
end
15+
end
16+
end
17+
end

lib/super_diff/basic/operation_tree_builders.rb

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@ module OperationTreeBuilders
2222
)
2323
autoload :TimeLike, "super_diff/basic/operation_tree_builders/time_like"
2424
autoload :DateLike, "super_diff/basic/operation_tree_builders/date_like"
25+
autoload :RangeObject,
26+
"super_diff/basic/operation_tree_builders/range_object"
2527

2628
class Main
2729
def self.call(*args)
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
module SuperDiff
2+
module Basic
3+
module OperationTreeBuilders
4+
class RangeObject < SuperDiff::Core::AbstractOperationTreeBuilder
5+
def self.applies_to?(expected, actual)
6+
expected.is_a?(Range) && actual.is_a?(Range)
7+
end
8+
9+
protected
10+
11+
def unary_operations
12+
[
13+
Core::UnaryOperation.new(
14+
name: :delete,
15+
collection: expected.to_s,
16+
key: 0,
17+
index: 0,
18+
value: expected.to_s
19+
),
20+
Core::UnaryOperation.new(
21+
name: :insert,
22+
collection: actual.to_s,
23+
key: 0,
24+
index: 0,
25+
value: actual.to_s
26+
)
27+
]
28+
end
29+
30+
def build_operation_tree
31+
OperationTrees::MultilineString.new([])
32+
end
33+
end
34+
end
35+
end
36+
end

spec/integration/rspec/eq_matcher_spec.rb

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1056,6 +1056,41 @@
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:
1081+
proc do
1082+
expected_line "- 5..6"
1083+
actual_line "+ 1..5"
1084+
end
1085+
)
1086+
1087+
expect(program).to produce_output_when_run(expected_output).in_color(
1088+
color_enabled
1089+
)
1090+
end
1091+
end
1092+
end
1093+
10591094
it_behaves_like "a matcher that supports elided diffs" do
10601095
let(:matcher) { :eq }
10611096
end
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
require "spec_helper"
2+
3+
RSpec.describe SuperDiff, type: :unit do
4+
describe ".diff" do
5+
subject(:diff) { described_class.diff(a, b) }
6+
7+
context "when given two Range objects" do
8+
let(:a) { 1..5 }
9+
let(:b) { 5..6 }
10+
11+
it "diffs their member attributes" do
12+
expected_output =
13+
SuperDiff::Core::Helpers
14+
.style(color_enabled: true) do
15+
expected_line "- 1..5"
16+
actual_line "+ 5..6"
17+
end
18+
.to_s
19+
.chomp
20+
21+
expect(diff).to eq(expected_output)
22+
end
23+
end
24+
end
25+
end

0 commit comments

Comments
 (0)