Class: Appydays::SpecHelpers::MatchTime

Inherits:
Object
  • Object
show all
Includes:
RSpec::Matchers::Composable
Defined in:
lib/appydays/spec_helpers.rb

Instance Method Summary collapse

Constructor Details

#initialize(expected) ⇒ MatchTime

Returns a new instance of MatchTime.



63
64
65
66
67
68
69
70
71
72
# File 'lib/appydays/spec_helpers.rb', line 63

def initialize(expected)
  @expected = expected
  if expected == :now
    @expected_t = Time.now
    @tolerance = 5
  else
    @expected_t = self.time(expected)
    @tolerance = 0.001
  end
end

Instance Method Details

#failure_messageObject



103
104
105
# File 'lib/appydays/spec_helpers.rb', line 103

def failure_message
  return "expected %s to be within %s of %s" % [@actual_t, @tolerance, @expected_t]
end

#matches?(actual) ⇒ Boolean

Returns:

  • (Boolean)


81
82
83
84
85
# File 'lib/appydays/spec_helpers.rb', line 81

def matches?(actual)
  @actual_t = self.time(actual)
  @actual_t = self.change_tz(@actual_t, @expected_t.zone) if @actual_t
  return RSpec::Matchers::BuiltIn::BeWithin.new(@tolerance).of(@expected_t).matches?(@actual_t)
end

#time(s) ⇒ Object



74
75
76
77
78
79
# File 'lib/appydays/spec_helpers.rb', line 74

def time(s)
  return nil if s.nil?
  return Time.parse(s) if s.is_a?(String)
  return Time.at(s) if s.is_a?(Numeric)
  return s.to_time
end

#within(tolerance) ⇒ Object



98
99
100
101
# File 'lib/appydays/spec_helpers.rb', line 98

def within(tolerance)
  @tolerance = tolerance
  return self
end