R/assert.R

Defines functions assert_detailed_log is_detailed_log

Documented in is_detailed_log

#' Check whether an object is a detailed log
#'
#' @param log The object to inspect.
#' @param nested Whether or not the object to inspect should be nested or not.
#'   `NA` indicates that both nested and unnested logs shoud pass the
#'   inspection.
#' @keywords internal
is_detailed_log <- function(log, nested = NA) {
  expected_names_nested <- c(
    "short_hash", "author_name", "date", "short_message", "hash",
    "left_parent", "right_parent", "author_email", "weekday", "month",
    "monthday", "time", "year", "timezone", "message", "description",
    "total_files_changed", "total_insertions", "total_deletions",
    "commit_nr", "short_description", "is_merge", "nested"
  )
  expected_names_unnested <- c(
    setdiff(expected_names_nested, "nested"),
    c("changed_file", "edits", "insertions", "deletions", "is_exact")
  )

  is_nested_log <- expect_names(log, expected_names_nested)
  is_unnested_log <- expect_names(log, expected_names_unnested)
  if (is.na(nested)) {
    passes <- is_nested_log || is_unnested_log
  } else if (nested == TRUE) {
    passes <- is_nested_log
  } else {
    passes <- is_unnested_log
  }
  passes
}

#' @importFrom purrr when
assert_detailed_log <- function(log, nested = NA) {
  msg <- when(nested,
    is.na(.) ~ "a",
    . ~ "a nested",
    !. ~ "an unnested"
  )
  if (!is_detailed_log(log, nested)) {
    stop(paste("log is not", msg, "detailed log"), call. = FALSE)
  }
}

expect_names <- function(log, expected_names) {
  setequal(names(log), expected_names)
}
lorenzwalthert/gitsum documentation built on Jan. 17, 2021, 9:34 p.m.