Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add useful diff representation of Time-like values #61

Merged
merged 2 commits into from
Nov 26, 2019
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
2 changes: 2 additions & 0 deletions lib/super_diff/differs.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,12 @@ module Differs
autoload :Empty, "super_diff/differs/empty"
autoload :Hash, "super_diff/differs/hash"
autoload :MultilineString, "super_diff/differs/multiline_string"
autoload :Time, "super_diff/differs/time"

DEFAULTS = [
Array,
Hash,
Time,
MultilineString,
CustomObject,
DefaultObject,
Expand Down
24 changes: 24 additions & 0 deletions lib/super_diff/differs/time.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
module SuperDiff
module Differs
class Time < Base
def self.applies_to?(expected, actual)
OperationalSequencers::TimeLike.applies_to?(expected, actual)
end

def call
operations.to_diff(indent_level: indent_level)
end

private

def operations
OperationalSequencers::TimeLike.call(
expected: expected,
actual: actual,
extra_operational_sequencer_classes: extra_operational_sequencer_classes,
extra_diff_formatter_classes: extra_diff_formatter_classes,
)
end
end
end
end
1 change: 1 addition & 0 deletions lib/super_diff/object_inspection/inspectors.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ module Inspectors
autoload :Hash, "super_diff/object_inspection/inspectors/hash"
autoload :Primitive, "super_diff/object_inspection/inspectors/primitive"
autoload :String, "super_diff/object_inspection/inspectors/string"
autoload :Time, "super_diff/object_inspection/inspectors/time"
end
end
end
13 changes: 13 additions & 0 deletions lib/super_diff/object_inspection/inspectors/time.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
module SuperDiff
module ObjectInspection
module Inspectors
TIME_FORMAT = "%Y-%m-%d %H:%M:%S.%3N %Z %:z".freeze

Time = InspectionTree.new do
add_text do |time|
"#{time.strftime(TIME_FORMAT)} (#{time.class})"
end
end
end
end
end
2 changes: 2 additions & 0 deletions lib/super_diff/object_inspection/map.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ def call(object)
Inspectors::Hash
when String
Inspectors::String
when Time
Inspectors::Time
when true, false, nil, Symbol, Numeric, Regexp, Class
Inspectors::Primitive
else
Expand Down
1 change: 1 addition & 0 deletions lib/super_diff/operational_sequencers.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ module OperationalSequencers
:MultilineString,
"super_diff/operational_sequencers/multiline_string",
)
autoload :TimeLike, "super_diff/operational_sequencers/time_like"

DEFAULTS = [Array, Hash, CustomObject].freeze
end
Expand Down
30 changes: 30 additions & 0 deletions lib/super_diff/operational_sequencers/time_like.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
module SuperDiff
module OperationalSequencers
class TimeLike < CustomObject
def self.applies_to?(expected, actual)
(expected.is_a?(Time) && actual.is_a?(Time)) ||
(
# Check for ActiveSupport's #acts_like_time? for their time-like objects
# (like ActiveSupport::TimeWithZone).
expected.respond_to?(:acts_like_time?) && expected.acts_like_time? &&
actual.respond_to?(:acts_like_time?) && actual.acts_like_time?
)
end

protected

def attribute_names
base = ["year", "month", "day", "hour", "min", "sec", "nsec", "zone", "gmt_offset"]

# If timezones are different, also show a normalized timestamp at the
# end of the diff to help visualize why they are different moments in
# time.
if actual.zone != expected.zone
base + ["utc"]
else
base
end
end
end
end
end
142 changes: 139 additions & 3 deletions spec/integration/rspec/eq_matcher_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -142,24 +142,160 @@

it "produces the correct failure message when used in the negative" do
as_both_colored_and_uncolored do |color_enabled|
snippet = %|expect("Jennifer").to eq("Marty")|
snippet = %|expect("Jennifer").not_to eq("Jennifer")|
program = make_plain_test_program(
snippet,
color_enabled: color_enabled,
)

expected_output = build_expected_output(
color_enabled: color_enabled,
snippet: %|expect("Jennifer").to eq("Marty")|,
snippet: %|expect("Jennifer").not_to eq("Jennifer")|,
expectation: proc {
line do
plain "Expected "
beta %|"Jennifer"|
plain " not to eq "
alpha %|"Jennifer"|
plain "."
end
},
)

expect(program).
to produce_output_when_run(expected_output).
in_color(color_enabled)
end
end
end

context "when comparing two different Time instances" do
it "produces the correct failure message when used in the positive" do
as_both_colored_and_uncolored do |color_enabled|
snippet = <<~RUBY
expected = Time.utc(2011, 12, 13, 14, 15, 16)
actual = Time.utc(2011, 12, 13, 14, 15, 16, 500_000)
expect(expected).to eq(actual)
RUBY
program = make_plain_test_program(
snippet,
color_enabled: color_enabled,
)

expected_output = build_expected_output(
color_enabled: color_enabled,
snippet: %|expect(expected).to eq(actual)|,
expectation: proc {
line do
plain "Expected "
beta %|2011-12-13 14:15:16.000 UTC +00:00 (Time)|
plain " to eq "
alpha %|"Marty"|
alpha %|2011-12-13 14:15:16.500 UTC +00:00 (Time)|
plain "."
end
},
diff: proc {
plain_line " #<Time {"
plain_line " year: 2011,"
plain_line " month: 12,"
plain_line " day: 13,"
plain_line " hour: 14,"
plain_line " min: 15,"
plain_line " sec: 16,"
alpha_line "- nsec: 500000000,"
beta_line "+ nsec: 0,"
plain_line " zone: \"UTC\","
plain_line " gmt_offset: 0"
plain_line " }>"
},
)

expect(program).
to produce_output_when_run(expected_output).
in_color(color_enabled)
end
end

it "produces the correct failure message when used in the negative" do
as_both_colored_and_uncolored do |color_enabled|
snippet = <<~RUBY
time = Time.utc(2011, 12, 13, 14, 15, 16)
expect(time).not_to eq(time)
RUBY
program = make_plain_test_program(
snippet,
color_enabled: color_enabled,
)

expected_output = build_expected_output(
color_enabled: color_enabled,
snippet: %|expect(time).not_to eq(time)|,
newline_before_expectation: true,
expectation: proc {
line do
plain " Expected "
beta %|2011-12-13 14:15:16.000 UTC +00:00 (Time)|
end

line do
plain "not to eq "
alpha %|2011-12-13 14:15:16.000 UTC +00:00 (Time)|
end
},
)

expect(program).
to produce_output_when_run(expected_output).
in_color(color_enabled)
end
end
end

context "when comparing two different Time and ActiveSupport::TimeWithZone instances" do
it "produces the correct failure message when used in the positive" do
as_both_colored_and_uncolored do |color_enabled|
snippet = <<~RUBY
expected = Time.utc(2011, 12, 13, 14, 15, 16)
actual = Time.utc(2011, 12, 13, 15, 15, 16).in_time_zone("Europe/Stockholm")
expect(expected).to eq(actual)
RUBY
program = make_rspec_rails_test_program(
snippet,
color_enabled: color_enabled,
)

expected_output = build_expected_output(
color_enabled: color_enabled,
snippet: %|expect(expected).to eq(actual)|,
expectation: proc {
line do
plain "Expected "
beta %|2011-12-13 14:15:16.000 UTC +00:00 (Time)|
end

line do
plain " to eq "
alpha %|2011-12-13 16:15:16.000 CET +01:00 (ActiveSupport::TimeWithZone)|
end
},
diff: proc {
plain_line " #<ActiveSupport::TimeWithZone {"
plain_line " year: 2011,"
plain_line " month: 12,"
plain_line " day: 13,"
alpha_line "- hour: 16,"
beta_line "+ hour: 14,"
plain_line " min: 15,"
plain_line " sec: 16,"
plain_line " nsec: 0,"
alpha_line "- zone: \"CET\","
beta_line "+ zone: \"UTC\","
alpha_line "- gmt_offset: 3600,"
beta_line "+ gmt_offset: 0,"
alpha_line "- utc: 2011-12-13 15:15:16.000 UTC +00:00 (Time)"
beta_line "+ utc: 2011-12-13 14:15:16.000 UTC +00:00 (Time)"
plain_line " }>"
},
)

expect(program).
Expand Down