Expect.Matchers.Temporal (Expect v3.3.0)
View Source Provides matchers that work with Date, DateTime, and NaiveDateTime.
Summary
Functions
Verifies that expected occurs on the same calendar day (day, month, year) as the provided date.
Verifies that the given Date, DateTime or NaiveDateTime occurs strictly after the expected Date or DateTime.
Verifies that the given DateTime or NaiveDateTime occurs at the expected time.
Verifies that the given Date, DateTime or NaiveDateTime occurs strictly before the expected Date or DateTime.
Functions
@spec be_same_day(Date.t() | DateTime.t() | NaiveDateTime.t()) :: Expect.Matchers.t()
Verifies that expected occurs on the same calendar day (day, month, year) as the provided date.
Accepts either Date or DateTime values.
iex> expect(~D[2000-12-30], to: be_same_day(~D[2000-12-30]))
:ok
iex> expect(~D[2000-12-30], to_not: be_same_day(~D[2000-01-01]))
:ok
@spec occur_after(Date.t() | DateTime.t() | NaiveDateTime.t()) :: Expect.Matchers.t()
Verifies that the given Date, DateTime or NaiveDateTime occurs strictly after the expected Date or DateTime.
Works the way you would expect when comparing Dates against DateTimes, which is that a Date is considered to be equivalent to midnight at the start of the day. Thus any DateTime at least one millisecond after midnight would occur after its equivalent Date.
Do not compare DateTime against NaiveDateTime. See notes below.
iex> expect(~U[2000-12-31 23:55:59Z], to: occur_after(~D[1999-01-01]))
:ok
iex> expect(~N[2000-12-31 23:55:59], to: occur_after(~D[1999-01-01]))
:ok
iex> expect(~D[2000-12-31], to: occur_after(~D[1999-01-01]))
:ok
iex> expect(~U[2000-12-31 23:55:59Z], to: occur_after(~U[1999-01-01 00:00:00Z]))
:ok
iex> expect(~D[2000-12-31], to: occur_after(~U[1999-01-01 00:00:00Z]))
:ok
iex> expect(~N[2000-12-31 23:55:59Z], to: occur_after(~N[1999-01-01 00:00:00]))
:ok
iex> expect(~D[2000-12-31], to: occur_after(~N[1999-01-01 00:00:00]))
:okDo not compare DateTime against NaiveDateTime
For the same reasons of correctness that the elixir standard library doesn't allow you to compare DateTime against NaiveDateTime, expect's temporal matchers refuse to make such comparisons.
It's always safe to compare DateTime (with or without timezone) against Dates, but comparing DateTime with timezone against one is not well defined, and can lead to subtle bugs.
iex> expect(~U[2000-12-31 23:55:59Z], to: occur_after(~N[1999-01-01 00:00:00]))
** (Expect.AssertionError) Expected ~U[2000-12-31 23:55:59Z] to occur after ~N[1999-01-01 00:00:00], but it is dangerous to compare DateTime against NaiveDateTime...
@spec occur_at(Keyword.t()) :: Expect.Matchers.t()
Verifies that the given DateTime or NaiveDateTime occurs at the expected time.
iex> expect(~U[2000-12-31 23:55:59Z], to: occur_at(hour: 23))
:okYou can specify any of :hour, :minute, and or :second level precision in the opts
iex> expect(~U[2000-12-31 23:55:59Z], to: occur_at(hour: 23))
:ok
iex> expect(~U[2000-12-31 23:55:59Z], to: occur_at(minute: 55))
:ok
iex> expect(~U[2000-12-31 23:55:59Z], to: occur_at(second: 59))
:ok
@spec occur_before(Date.t() | DateTime.t() | NaiveDateTime.t()) :: Expect.Matchers.t()
Verifies that the given Date, DateTime or NaiveDateTime occurs strictly before the expected Date or DateTime.
Works the way you would expect when comparing Dates against DateTimes, which is that a Date is considered to be equivalent to midnight at the start of the day. Thus any DateTime at least one millisecond after midnight would occur after its equivalent Date.
Do not compare DateTime against NaiveDateTime. See notes below.
iex> expect(~U[2000-12-31 23:59:59Z], to: occur_before(~D[2001-01-01]))
:ok
iex> expect(~N[2000-12-31 23:59:59], to: occur_before(~D[2001-01-01]))
:ok
iex> expect(~D[1998-12-31], to: occur_before(~D[1999-01-01]))
:ok
iex> expect(~U[2000-12-31 23:55:59Z], to: occur_before(~U[2001-01-01 00:00:00Z]))
:ok
iex> expect(~D[2000-12-31], to: occur_before(~U[2001-01-01 00:00:00Z]))
:ok
iex> expect(~N[2000-12-31 23:55:59Z], to: occur_before(~N[2001-01-01 00:00:00]))
:ok
iex> expect(~D[2000-12-31], to: occur_before(~N[2001-01-01 00:00:00]))
:okDo not compare DateTime against NaiveDateTime
For the same reasons of correctness that the elixir standard library doesn't allow you to compare DateTime against NaiveDateTime, expect's temporal matchers refuse to make such comparisons.
It's always safe to compare DateTime (with or without timezone) against Dates, but comparing DateTime with timezone against one is not well defined, and can lead to subtle bugs.
iex> expect(~U[2000-12-31 23:55:59Z], to: occur_before(~N[2001-01-01 00:00:00]))
** (Expect.AssertionError) Expected ~U[2000-12-31 23:55:59Z] to occur before ~N[2001-01-01 00:00:00], but it is dangerous to compare DateTime against NaiveDateTime...