tests/testthat/test-shorter_interval.R

test_that("int_build() | General test", {
  expect_equal(int_build(
    x = hms::parse_hm("23:00"),
    y = hms::parse_hm("01:00"),
    method = "shorter"
  ),
  lubridate::as.interval(
    lubridate::ymd_hms("1970-01-01 23:00:00"),
    lubridate::ymd_hms("1970-01-02 01:00:00")
  ))

  # `x == y`
  expect_equal(int_build(
    x = lubridate::as_datetime("1985-01-15 12:00:00"),
    y = lubridate::as_datetime("2020-09-10 12:00:00"),
    method = "shorter"
  ),
  lubridate::as.interval(
    lubridate::ymd_hms("1970-01-01 12:00:00"),
    lubridate::ymd_hms("1970-01-01 12:00:00")
  ))

  # `method = "longer"`
  expect_equal(int_build(
    x = lubridate::as_datetime("1985-01-15 12:00:00"),
    y = lubridate::as_datetime("2020-09-10 12:00:00"),
    method = "longer"
  ),
  lubridate::as.interval(
    lubridate::ymd_hms("1970-01-01 12:00:00"),
    lubridate::ymd_hms("1970-01-02 12:00:00")
  ))

  # `NA` cases
  int_build(
    x = hms::parse_hm("23:00"),
    y = hms::as_hms(NA),
    method = "shorter"
  ) |>
    expect_equal(lubridate::as.interval(NA))

  expect_equal(int_build(
    x = c(hms::parse_hm("22:45"), hms::parse_hm("12:00")),
    y = c(hms::parse_hm("04:15"), hms::parse_hm("09:00")),
    method = "shorter"
  ),
  c(lubridate::as.interval(
    lubridate::ymd_hms("1970-01-01 22:45:00"),
    lubridate::ymd_hms("1970-01-02 04:15:00")
  ),
  lubridate::as.interval(
    lubridate::ymd_hms("1970-01-01 09:00:00"), # nolint
    lubridate::ymd_hms("1970-01-01 12:00:00"))
  ))
})

test_that("int_build() | error test", {
  # checkmate::assert_multi_class(x, c("hms", "POSIXt"))
  expect_error(int_build(
    x = 1, y = hms::hms(1), method = "shorter"
  ),
  "Assertion on 'x' failed"
  )

  # prettycheck::assert_numeric( ... [1]
  int_build(
    x = hms::hms(-1),
    y = hms::hms(1),
    method = "shorter"
  ) |>
    expect_error()

  int_build(
    x = -hms::hms(86401),
    y = hms::hms(1),
    method = "shorter"
  ) |>
    expect_error()

  # checkmate::assert_multi_class(y, c("hms", "POSIXt"))
  expect_error(int_build(
    x = hms::hms(1), y = 1, method = "shorter"
  ),
  "Assertion on 'y' failed"
  )

  # prettycheck::assert_numeric( ... [2]
  int_build(
    x = hms::hms(1),
    y = hms::hms(-1),
    method = "shorter"
  ) |>
    expect_error()

  int_build(
    x = hms::hms(1),
    y = hms::hms(86401),
    method = "shorter"
  ) |>
    expect_error()

  # assert_identical(x, y, type = "length")
  expect_error(int_build(
    x = hms::hms(1), y = c(hms::hms(1), hms::hms(1)), method = "shorter"
  ))

  # checkmate::assert_choice(method, method_choices)
  expect_error(int_build(
    x = hms::hms(1), y = hms::hms(1), method = 1
  ),
  "Assertion on 'method' failed"
  )
})

test_that("int_build() | Warning test", {
  expect_message(int_build(
    x = hms::parse_hm("00:00"), y = hms::parse_hm("12:00"),
    method = "shorter"
  ),
  "Element '1' of 'x' and 'y' have intervals equal to "
  )
})

test_that("shorter_int() | General test", {
  expect_equal(shorter_int(
    x = lubridate::parse_date_time("01:10:00", "HMS"),
    y = lubridate::parse_date_time("11:45:00", "HMS")
  ),
  lubridate::as.interval(
    lubridate::ymd_hms("1970-01-01 01:10:00"), # nolint
    lubridate::ymd_hms("1970-01-01 11:45:00"))
  )

  # `x == y`
  expect_equal(shorter_int(
    x = lubridate::as_datetime("1915-02-14 05:00:00"),
    y = lubridate::as_datetime("1970-07-01 05:00:00")
  ),
  lubridate::as.interval(
    lubridate::ymd_hms("1970-01-01 05:00:00"),
    lubridate::ymd_hms("1970-01-01 05:00:00")
  ))
})

test_that("shorter_duration() | General test", {
  expect_equal(shorter_duration(
    x = lubridate::parse_date_time("01:10:00", "HMS"),
    y = lubridate::parse_date_time("11:45:00", "HMS")
  ),
  lubridate::as.duration(lubridate::as.interval(
    lubridate::ymd_hms("1970-01-01 01:10:00"),
    lubridate::ymd_hms("1970-01-01 11:45:00")
  )))
})

test_that("longer_int() | General test", {
  expect_equal(longer_int(
    x = lubridate::parse_date_time("01:10:00", "HMS"),
    y = lubridate::parse_date_time("11:45:00", "HMS")
  ),
  lubridate::as.interval(
    lubridate::ymd_hms("1970-01-01 11:45:00"), # nolint
    lubridate::ymd_hms("1970-01-02 01:10:00"))
  )

  # `x == y`
  expect_equal(longer_int(
    x = lubridate::as_datetime("1915-02-14 05:00:00"),
    y = lubridate::as_datetime("1970-07-01 05:00:00")
  ),
  lubridate::as.interval(
    lubridate::ymd_hms("1970-01-01 05:00:00"),
    lubridate::ymd_hms("1970-01-02 05:00:00")
  ))
})

test_that("longer_duration() | General test", {
  expect_equal(longer_duration(
    x = lubridate::parse_date_time("01:10:00", "HMS"),
    y = lubridate::parse_date_time("11:45:00", "HMS")
  ),
  lubridate::as.duration(lubridate::as.interval(
    lubridate::ymd_hms("1970-01-01 11:45:00"),
    lubridate::ymd_hms("1970-01-02 01:10:00")
  )))
})
gipso/lubritime documentation built on April 17, 2025, 12:03 p.m.