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

Failed Assertions on Date Instances Do Not Output a Diff #182

Closed
sshaw opened this issue Mar 15, 2023 · 4 comments · Fixed by #198
Closed

Failed Assertions on Date Instances Do Not Output a Diff #182

sshaw opened this issue Mar 15, 2023 · 4 comments · Fixed by #198
Labels

Comments

@sshaw
Copy link
Contributor

sshaw commented Mar 15, 2023

0.9.0, running under Rails:

expect(Date.new(2020, 2, 1)).to eq Date.new(2020, 2, 2)

Results in:

  Expected #<Date:0x0000000124fed510> to eq #<Date:0x0000000124fed470>.

       Diff:

       ┌ (Key) ──────────────────────────┐
       │ ‹-› in expected, not in actual  │
       │ ‹+› in actual, not in expected  │
       │ ‹ › in both expected and actual │
       └─────────────────────────────────┘

         #<Date:0x0000000124fed510 {
         }>

If it is changed Time:

expect(Date.new(2020, 2, 1).to_time).to eq Date.new(2020, 2, 2).to_time

Then a diff is output.

@sshaw
Copy link
Contributor Author

sshaw commented Mar 16, 2023

0.9.0, running under Rails:

Rails 7.0.4.3

@mcmire
Copy link
Collaborator

mcmire commented Mar 18, 2023

This makes sense — super_diff handles Time and Time-like objects specially, but not Date objects. We'd want to add a new OperationTreeBuilder similar to TimeLike and then also add a new date_like? top-level helper like time_like?, then add tests to match.

I can work on a PR when I get a chance!

@thefotios
Copy link

This has bugged me for a while as well and I wrote up a quick solution that could help folks until a real solution comes along (or I get off my lazy 🧈 and open a PR).

This module effectively copies the Differ and TreeBuilder from TimeLike and totally self contained. Just drop this in a spec/support file and you'll get useful (albeit ugly) diffs like
image

module SuperDiffDate
  module Base
    extend ActiveSupport::Concern

    class_methods do
      def applies_to?(expected, actual) = [expected, actual].all?(&method(:date_like?))

      private def date_like?(value)
        (value.respond_to?(:acts_like_date?) && value.acts_like_date?) ||
        value.is_a?(Date)
      end
    end
  end

  class TreeBuilder < SuperDiff::OperationTreeBuilders::CustomObject
    include Base
    protected def attribute_names = %w[year month day]
  end

  class Differ < SuperDiff::Differs::Base
    include Base
    protected def operation_tree_builder_class = TreeBuilder
  end
end

SuperDiff.configure do |config|
  config.add_extra_differ_class(SuperDiffDate::Differ)
  config.add_extra_operation_tree_builder_class(SuperDiffDate::TreeBuilder)
end

@mcmire
Copy link
Collaborator

mcmire commented May 18, 2023

Thanks for posting a workaround @thefotios, glad you were able to navigate the codebase well enough to figure out something :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants