[rspec-devel] [ rspec-Feature Requests-7721 ] create date proximity expectation

noreply at rubyforge.org noreply at rubyforge.org
Thu Jan 11 07:58:33 EST 2007


Feature Requests item #7721, was opened at 2007-01-10 01:20
You can respond by visiting: 
http://rubyforge.org/tracker/?func=detail&atid=3152&aid=7721&group_id=797

>Category: expectation module
Group: None
>Status: Closed
Priority: 3
Submitted By: Steve Ross (sxross)
>Assigned to: David Chelimsky (dchelimsky)
Summary: create date proximity expectation

Initial Comment:
Often I am testing dates, but don't need absolute equality (time ticks away between lines of code, or the date matters but the time doesn't). Proposal:

extend Time, DateTime, and Date to have expectations like:

some_date.should_be_close_to(1.week.ago, :days => 1)

There may be some nicer syntax for this, but I stole it from a simple implementation (below) I did in Test::Unit.

module Test
  module Unit
    module Assertions
      SECONDS_IN_MINUTE = 60
      SECONDS_IN_HOUR = SECONDS_IN_MINUTE * 60
      SECONDS_IN_DAY = SECONDS_IN_HOUR * 24
      
      def assert_date_proximity(expected_date, observed_date, fuzziness = {})
        raise "first date must be of type Time" unless expected_date.class == Time
        raise "second date must be of type Time" unless observed_date.class == Time
        options = {:seconds => 0, :minutes => 1, :hours => 0, :days => 0}.merge(fuzziness)
        time_threshold = 0
        time_threshold += options[:days] * SECONDS_IN_DAY if options[:days]
        time_threshold += options[:hours] * SECONDS_IN_HOUR if options[:hours]
        time_threshold += options[:minutes] * SECONDS_IN_MINUTE if options[:minutes]
        time_threshold += options[:seconds] if options[:seconds]
        assert_block('date not within specified threshold. expected <%s> / got <%s> thresh. <%d sec>' % [expected_date, observed_date, time_threshold]) do
          (observed_date - expected_date).abs <= time_threshold
        end
      end
      
    end
  end
end


----------------------------------------------------------------------

>Comment By: David Chelimsky (dchelimsky)
Date: 2007-01-11 12:58

Message:
Rather than adding a new expectations, we've provided a mechanism for easily creating your own custom expectations (as of trunk-1371). This will be documented and part of the next release (0.7.6 || 0.8)

----------------------------------------------------------------------

You can respond by visiting: 
http://rubyforge.org/tracker/?func=detail&atid=3152&aid=7721&group_id=797


More information about the rspec-devel mailing list