-
Notifications
You must be signed in to change notification settings - Fork 56
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: date like objects support (#198)
Tbh, I just went through appearances of `/(TimeLike|time_like)/` and created similar logic for date like objects :D I'm really hesitant about whether `DateTime` objects should be considered `date_like` (although ActiveSupport defines `DateTime#acts_like_date?`). Since `Date` and `DateTime` are never equal, `expect(Date.new(2020, 1, 1)).to eq(DateTime.new(2020, 1, 1))` will result in a confusing diff (see the [spec/support/shared_examples/active_support.rb](https://github.com/mcmire/super_diff/pull/198/files#diff-4076c9b6c197bc5a4b90327f7efc27f9a0bc5f40ec98dfe94adf9c08f103a269R103-R143))
- Loading branch information
Showing
14 changed files
with
412 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
module SuperDiff | ||
module Differs | ||
class DateLike < Base | ||
def self.applies_to?(expected, actual) | ||
SuperDiff.date_like?(expected) && SuperDiff.date_like?(actual) | ||
end | ||
|
||
protected | ||
|
||
def operation_tree_builder_class | ||
OperationTreeBuilders::DateLike | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,6 +4,7 @@ module Differs | |
Array, | ||
Hash, | ||
TimeLike, | ||
DateLike, | ||
MultilineString, | ||
CustomObject, | ||
DefaultObject | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
41 changes: 41 additions & 0 deletions
41
lib/super_diff/object_inspection/inspection_tree_builders/date_like.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
module SuperDiff | ||
module ObjectInspection | ||
module InspectionTreeBuilders | ||
class DateLike < Base | ||
def self.applies_to?(value) | ||
SuperDiff.date_like?(value) | ||
end | ||
|
||
def call | ||
InspectionTree.new do | ||
as_lines_when_rendering_to_lines(collection_bookend: :open) do | ||
add_text { |date| "#<#{date.class} " } | ||
|
||
when_rendering_to_lines { add_text "{" } | ||
end | ||
|
||
when_rendering_to_string do | ||
add_text { |date| date.strftime("%Y-%m-%d") } | ||
end | ||
|
||
when_rendering_to_lines do | ||
nested do |date| | ||
insert_separated_list(%i[year month day]) do |name| | ||
add_text name.to_s | ||
add_text ": " | ||
add_inspection_of date.public_send(name) | ||
end | ||
end | ||
end | ||
|
||
as_lines_when_rendering_to_lines(collection_bookend: :close) do | ||
when_rendering_to_lines { add_text "}" } | ||
|
||
add_text ">" | ||
end | ||
end | ||
end | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -7,6 +7,7 @@ module InspectionTreeBuilders | |
Hash, | ||
Primitive, | ||
TimeLike, | ||
DateLike, | ||
DefaultObject | ||
].freeze | ||
end | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
module SuperDiff | ||
module OperationTreeBuilders | ||
class DateLike < CustomObject | ||
def self.applies_to?(expected, actual) | ||
SuperDiff.date_like?(expected) && SuperDiff.date_like?(actual) | ||
end | ||
|
||
protected | ||
|
||
def attribute_names | ||
%w[year month day] | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,5 @@ | ||
module SuperDiff | ||
module OperationTreeBuilders | ||
DEFAULTS = [Array, Hash, TimeLike, CustomObject].freeze | ||
DEFAULTS = [Array, Hash, TimeLike, DateLike, CustomObject].freeze | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.