diff --git a/R/dtc_create_iso8601.R b/R/dtc_create_iso8601.R index 56518bba..633923d4 100644 --- a/R/dtc_create_iso8601.R +++ b/R/dtc_create_iso8601.R @@ -45,10 +45,10 @@ zero_pad_whole_number <- function(x, n = 2L) { # Check `n` admiraldev::assert_integer_scalar(n) - if (n < 1) rlang::abort("`n` must be positive.") + if (n < 1L) rlang::abort("`n` must be positive.") # Negative numbers are not allowed, and hence get converted to NA. - x[x < 0] <- NA_integer_ + x[x < 0L] <- NA_integer_ # Numbers that do not fit within the padding width are converted to NA x[floor(log10(x)) >= n] <- NA_integer_ @@ -88,11 +88,11 @@ yy_to_yyyy <- function(x, cutoff_2000 = 68L) { # Check `x` if (!rlang::is_integerish(x)) rlang::abort("`x` must be integerish.") - if (any(x < 0, na.rm = TRUE)) + if (any(x < 0L, na.rm = TRUE)) rlang::abort("`x` cannot have negative years.") x <- dplyr::if_else(x <= cutoff_2000, x + 2000L, x) - x <- dplyr::if_else(x <= 99, x + 1900L, x) + x <- dplyr::if_else(x <= 99L, x + 1900L, x) x } @@ -114,7 +114,7 @@ yy_to_yyyy <- function(x, cutoff_2000 = 68L) { iso8601_two_digits <- function(x) { admiraldev::assert_character_vector(x) x_int <- as.integer(stringr::str_match(x, "^\\d?\\d$")) - zero_pad_whole_number(x_int, n = 2) + zero_pad_whole_number(x_int, n = 2L) } iso8601_mday <- iso8601_two_digits @@ -150,7 +150,7 @@ iso8601_year <- function(x, cutoff_2000 = 68L) { admiraldev::assert_integer_scalar(cutoff_2000, subset = "non-negative") x_int <- as.integer(stringr::str_match(x, "^\\d{1,4}$")) x_int <- yy_to_yyyy(x_int, cutoff_2000 = cutoff_2000) - zero_pad_whole_number(x_int, n = 4) + zero_pad_whole_number(x_int, n = 4L) } #' Format as a ISO8601 month @@ -180,7 +180,7 @@ iso8601_mon <- function(x) { num_mon_chr <- num_mon num_mon_chr[is.na(num_mon)] <- iso8601_two_digits(x[is.na(num_mon)]) mon_int <- as.integer(num_mon_chr) - zero_pad_whole_number(mon_int, n = 2) + zero_pad_whole_number(mon_int, n = 2L) } #' Format as ISO8601 seconds @@ -284,31 +284,31 @@ iso8601_truncate <- function(x, empty_as_na = TRUE) { #' sdtm.oak:::format_iso8601(m) #' #' @keywords internal -format_iso8601 <- function(m, .cutoff_2000 = 68) { +format_iso8601 <- function(m, .cutoff_2000 = 68L) { admiraldev::assert_integer_scalar(.cutoff_2000) - m[, 'year'] <- iso8601_year(m[, 'year'], cutoff_2000 = .cutoff_2000) - m[, 'mon'] <- iso8601_mon(m[, 'mon']) - m[, 'mday'] <- iso8601_mday(m[, 'mday']) - m[, 'hour'] <- iso8601_hour(m[, 'hour']) - m[, 'min'] <- iso8601_min(m[, 'min']) - m[, 'sec'] <- iso8601_sec(m[, 'sec']) + m[, "year"] <- iso8601_year(m[, "year"], cutoff_2000 = .cutoff_2000) + m[, "mon"] <- iso8601_mon(m[, "mon"]) + m[, "mday"] <- iso8601_mday(m[, "mday"]) + m[, "hour"] <- iso8601_hour(m[, "hour"]) + m[, "min"] <- iso8601_min(m[, "min"]) + m[, "sec"] <- iso8601_sec(m[, "sec"]) m <- iso8601_na(m) x <- - paste0(m[, 'year'], + paste0(m[, "year"], "-", - m[, 'mon'], + m[, "mon"], "-", - m[, 'mday'], + m[, "mday"], "T", - m[, 'hour'], + m[, "hour"], ":", - m[, 'min'], + m[, "min"], ":", - m[, 'sec']) + m[, "sec"]) iso8601_truncate(x) } @@ -376,7 +376,7 @@ format_iso8601 <- function(m, .cutoff_2000 = 68) { #' # Fractional seconds #' create_iso8601("2019-120602:20:13.1230001", .format = "y-mdH:M:S", .check_format = FALSE) #' @export -create_iso8601 <- function(..., .format, .na = NULL, .cutoff_2000 = 68, .check_format = TRUE) { +create_iso8601 <- function(..., .format, .na = NULL, .cutoff_2000 = 68L, .check_format = TRUE) { dots <- rlang::dots_list(...) @@ -389,7 +389,7 @@ create_iso8601 <- function(..., .format, .na = NULL, .cutoff_2000 = 68, .check_f # Check if all vectors in `dots` are of the same length. n <- unique(lengths(dots)) if (!identical(length(n), 1L)) - rlang::abort("All vectors in `...` must be of the same length.") + rlang::abort("All vectors in `...` must be of the same length.") if (!identical(length(dots), length(.format))) rlang::abort("Number of vectors in `...` should match length of `.format`.") diff --git a/R/dtc_parse_dttm.R b/R/dtc_parse_dttm.R index 24d95fa4..cbeb8eec 100644 --- a/R/dtc_parse_dttm.R +++ b/R/dtc_parse_dttm.R @@ -1,14 +1,15 @@ #' @rdname parse_dttm #' @order 2 parse_dttm_ <- function(dttm, - fmt, - na = NULL, - sec_na = na, - min_na = na, - hour_na = na, - mday_na = na, - mon_na = na, - year_na = na) { + fmt, + na = NULL, + sec_na = na, + min_na = na, + hour_na = na, + mday_na = na, + mon_na = na, + year_na = na) { + admiraldev::assert_character_scalar(fmt) diff --git a/R/dtc_utils.R b/R/dtc_utils.R index d654c39f..46e8414a 100644 --- a/R/dtc_utils.R +++ b/R/dtc_utils.R @@ -75,9 +75,9 @@ assert_dtc_format <- function(.format) { abort_msg <- "`.format` must be either a character vector of formats of a list thereof." - switch ( + switch( typeof(.format), - character = assert_dtc_fmt(.format) , + character = assert_dtc_fmt(.format), list = purrr::map(.format, assert_dtc_format), rlang::abort(abort_msg) ) @@ -127,7 +127,7 @@ assert_capture_matrix <- function(m) { col_names <- c("year", "mon", "mday", "hour", "min", "sec") if (!all(colnames(m) %in% col_names)) - rlang::abort("`m` must have the following colnames: `year`, `mon`, `mday`, `hour`, `min` and `sec`.") + rlang::abort("`m` must have the following colnames: `year`, `mon`, `mday`, `hour`, `min` and `sec`.") invisible(m) } diff --git a/R/parse_dttm.R b/R/parse_dttm.R deleted file mode 100644 index a4f5e8ca..00000000 --- a/R/parse_dttm.R +++ /dev/null @@ -1,141 +0,0 @@ -# new_dttm_tbl <- function(sec = character(), -# min = character(), -# hour = character(), -# mday = character(), -# mon = character(), -# year = character()) { -# -# tibble::tibble( -# year = year, -# mon = mon, -# mday = mday, -# hour = hour, -# min = min, -# sec = sec -# ) -# } - -# parse_dttm <- function(dttm, -# fmt, -# na = NULL, -# sec_na = na, -# min_na = na, -# hour_na = na, -# mday_na = na, -# mon_na = na, -# year_na = na) { -# -# -# tbl_fmt_c <- parse_dttm_fmt(fmt) -# regex <- -# dttm_fmt_to_regex( -# tbl_fmt_c, -# fmt_regex = fmt_rg( -# na = na, -# sec_na = sec_na, -# min_na = min_na, -# hour_na = hour_na, -# mday_na = mday_na, -# mon_na = mon_na, -# year_na = year_na -# ) -# ) -# -# m <- stringr::str_match(dttm, regex) -# colnames(m)[1] <- "dttm" -# # Remove unnamed capture groups (these are subgroups within the dttm components) -# m <- m[, colnames(m) != "", drop = FALSE] -# dplyr::bind_rows(new_dttm_tbl(), tibble::as_tibble(m)) -# } - -# convert NA in character x to "-" -# iso8601_na <- function(x) { -# x[is.na(x)] <- "-" -# x -# } - - - -# iso8601_mday <- function(x) { -# iso8601_two_digits(x) |> iso8601_na() -# } -# -# iso8601_hour <- function(x) { -# iso8601_two_digits(x) |> iso8601_na() -# } -# -# iso8601_min <- function(x) { -# iso8601_two_digits(x) |> iso8601_na() -# } - -# iso8601_sec <- function(x) { -# x_iso8601 <- stringr::str_extract(x, "^\\d?\\d(\\.\\d*)?$") -# #x_iso8601 <- sprintf("%02f", x_dbl) -# x_iso8601 <- stringr::str_replace(x_iso8601, "^\\d(\\.\\d*)?$", "0\\0") -# x_iso8601 <- stringr::str_replace(x_iso8601, "(\\.[^0]*)(0*)$", "\\1") -# x_iso8601 <- stringr::str_remove(x_iso8601, "\\.$") -# x_iso8601[is.na(x_iso8601)] <- NA_character_ -# x_iso8601 |> iso8601_na() -# } - -# Month abbreviation (en) to numeric month mapping -# mon_abb_to_mon_num <- setNames(sprintf("%02d", seq_along(month.abb)), tolower(month.abb)) - -# iso8601_mon <- function(x) { -# -# x <- tolower(x) -# # Translate month abbreviations to numeric months -# num_mon <- mon_abb_to_mon_num[x] -# num_mon_chr <- num_mon -# num_mon_chr[is.na(num_mon)] <- iso8601_two_digits(x[is.na(num_mon)]) -# -# mon_int <- as.integer(num_mon_chr) -# x_iso8601 <- sprintf("%02d", mon_int) -# x_iso8601[is.na(mon_int)] <- NA_character_ -# iso8601_na(x_iso8601) -# } - -# iso8601_year <- function(x, cutoff_2000 = 68L) { -# x_int <- as.integer(stringr::str_match(x, "^\\d{1,4}$")) -# x_int <- ifelse (x_int <= cutoff_2000, x_int + 2000L, x_int) -# x_int <- ifelse (x_int <= 99, x_int + 1900L, x_int) -# -# -# x_iso8601 <- sprintf("%04d", x_int) -# x_iso8601[is.na(x_int)] <- NA_character_ -# iso8601_na(x_iso8601) -# } - -# iso8601_truncate <- function(x, empty_as_na = TRUE) { -# -# x <- stringr::str_remove(x, "[^\\d]*$") -# -# if (empty_as_na) x[x == ""] <- NA_character_ -# -# x -# } - -#' #' @importFrom rlang .data -#' #' @export -#' format_iso8601 <- function(dttm, fmt, na = NULL) { -#' -#' tbl <- parse_dttm(dttm, fmt, na = na) -#' tbl |> -#' dplyr::mutate( -#' year = iso8601_year(.data$year), -#' mon = iso8601_mon(.data$mon), -#' mday = iso8601_mday(.data$mday), -#' hour = iso8601_hour(.data$hour), -#' min = iso8601_min(.data$min), -#' sec = iso8601_sec(.data$sec), -#' iso8601 = dplyr::if_else( -#' !is.na(dttm), -#' iso8601_truncate( -#' stringr::str_glue("{.data$year}-{.data$mon}-{.data$mday}T{.data$hour}:{.data$min}:{.data$sec}") -#' ), -#' NA_character_ -#' ) -#' ) |> -#' dplyr::relocate(.data$iso8601, .data$dttm, .before = 1L) -#' -#' } diff --git a/R/parse_dttm_fields.R b/R/parse_dttm_fields.R deleted file mode 100644 index 082e6fb0..00000000 --- a/R/parse_dttm_fields.R +++ /dev/null @@ -1,125 +0,0 @@ -# # pseq <- -# # function(from = 1, -# # to = 1) { -# # mapply( -# # `:`, -# # from = from, -# # to = to, -# # SIMPLIFY = FALSE -# # ) |> -# # unlist() -# # } -# -# find_gaps <- function(x, x_min, x_max) { -# y <- setdiff(seq(x_min, x_max), x) -# streaks <- split(y, cumsum(c(TRUE, diff(y) != 1))) -# -# lapply(X = streaks, \(x) c(start = min(x), end = max(x))) -# } -# -# fmt_tokens <- -# list( -# sec = "S+", -# min = "M+", -# hour = "H+", -# mday = "d+", -# mon = "m+", -# year = "y+" -# ) -# -# fmt_regex <- -# list( -# sec = "(\\d{2})", -# min = "(\\d{2})", -# hour = "(\\d{2})", -# mday = "(\\d{2})", -# mon = "(\\d{2}|Jan|Feb|Mar|Apr|May|Jun|Jul|Aug|Sep|Oct|Nov|Dec)", -# year = "(\\d{4})" -# ) -# -# -# as_order <- function(fmt) { -# gsub("[^[:alpha:]]+", "", fmt) -# } -# -# fmt_token_tally <- function(fmt, tokens = fmt_tokens) { -# sapply(fmt_tokens, \(x) stringr::str_count(fmt, x)) -# } -# -# is_dt_fmt <- function(fmt) { -# -# tally <- fmt_token_tally(fmt) -# all(tally < 2) && sum(tally) > 0 -# } -# -# reg_matches <- function(x, m, invert = FALSE) { -# match <- regmatches(x, m, invert = invert) -# ifelse(length(match), match, NA_character_) -# } -# -# # parse_dt_fmt <- function(fmt, tokens = fmt_tokens) { -# # if (!is_dt_fmt(fmt)) { -# # stop("Not a valid format in `fmt`.", call. = TRUE) -# # } -# # sapply(fmt_tokens, \(x) stringr::str_match(fmt, x)) -# # } -# # -# # parse_dt_fmt2 <- function(fmt, tokens = fmt_tokens) { -# # if (!is_dt_fmt(fmt)) { -# # stop("Not a valid format in `fmt`.", call. = TRUE) -# # } -# # sapply(fmt_tokens, \(x) grep(x, fmt)) -# # } -# -# extract_tidy_match_ <- function(x, pattern) { -# -# match_data <- regexpr(pattern, x) -# match <- reg_matches(x, match_data) -# -# is_match <- !is.na(match) -# -# start <- ifelse(is_match, match_data, NA_integer_) -# len <- ifelse(is_match, attr(match_data, "match.length"), NA_integer_) -# end <- start + len - 1L -# data.frame(pat = pattern, cap = match, start = start, end = end, len = len) -# } -# -# extract_tidy_match <- function(x, patterns, sort = TRUE) { -# -# df <- -# lapply(patterns, \(str) extract_tidy_match_(x, str)) |> -# do.call(what = "rbind") -# -# df$ord <- rank(df$start) -# df$ord[is.na(df$start)] <- NA_integer_ -# -# df <- cbind(dttm_el = rownames(df), df) -# rownames(df) <- NULL -# -# if (sort) { -# df[order(df$ord),] -# } else { -# df -# } -# } -# -# -# -# -# parse_dttm_fields <- function(x, -# fmt, -# na = -# list( -# year = "UNKN", -# month = "UNK", -# day = "UN", -# hour = "", -# min = "", -# sec = "" -# )) { -# -# -# -# -# -# } diff --git a/R/parse_dttm_fmt.R b/R/parse_dttm_fmt.R index a1bf821b..a8cbb8f7 100644 --- a/R/parse_dttm_fmt.R +++ b/R/parse_dttm_fmt.R @@ -28,11 +28,11 @@ find_int_gap <- function(x, xmin = min(x), xmax = max(x)) { admiraldev::assert_integer_scalar(xmax) x <- sort(unique(x)) - x <- c(xmin - 1, x, xmax + 1) - gaps <- which(diff(x) > 1) - start <- x[gaps] + 1 - end <- x[gaps + 1] - 1 - tibble::tibble(start = x[gaps] + 1, end = x[gaps + 1] - 1) + x <- c(xmin - 1L, x, xmax + 1L) + gaps <- which(diff(x) > 1L) + start <- x[gaps] + 1L + end <- x[gaps + 1L] - 1L + tibble::tibble(start = start, end = end) } #' `regmatches()` with `NA` @@ -67,17 +67,9 @@ reg_matches <- function(x, m, invert = FALSE) { #' @returns An integer vector. #' #' @keywords internal -pseq <- - function(from, - to) { - mapply( - `:`, - from = from, - to = to, - SIMPLIFY = FALSE - ) |> - unlist() - } +pseq <- function(from, to) { + unlist(purrr::map2(.x = from, .y = to, .f = `:`)) +} #' Generate case insensitive regexps #' @@ -91,7 +83,7 @@ pseq <- #' @keywords internal str_to_anycase <- function(x) { - lst <- stringr::str_split(x, "") + lst <- stringr::str_split(x, stringr::boundary("character")) purrr::map(lst, ~ stringr::str_c(stringr::str_to_upper(.x), stringr::str_to_lower(.x))) |> purrr::map(~ sprintf("[%s]", .x)) |> purrr::map(~ stringr::str_flatten(.x)) |> @@ -131,11 +123,12 @@ months_abb_regex <- function(x = month.abb, case = c("any", "upper", "lower", "t # change the regexp for one specific dttm component # while keeping the other defaults. fmt_c <- function(sec = "S+", - min = "M+", - hour = "H+", - mday = "d+", - mon = "m+", - year = "y+") { + min = "M+", + hour = "H+", + mday = "d+", + mon = "m+", + year = "y+") { + c( sec = sec, min = min, @@ -159,38 +152,44 @@ regex_or <- function(x, .open = FALSE, .close = FALSE) { stringr::str_flatten(x, collapse = "|") } -fmt_rg <- - function(sec = "(\\b\\d|\\d{2})(\\.\\d*)?", - min = "(\\b\\d|\\d{2})", - hour = "\\d?\\d", - mday = "\\b\\d|\\d{2}", - mon = stringr::str_glue("\\d\\d|{months_abb_regex()}"), - year = "(\\d{2})?\\d{2}", - na = NULL, - sec_na = na, - min_na = na, - hour_na = na, - mday_na = na, - mon_na = na, - year_na = na - ) { - - sec_na <- ifelse(!is.null(sec_na), regex_or(sec_na, .open = TRUE), "") - min_na <- ifelse(!is.null(min_na), regex_or(min_na, .open = TRUE), "") - hour_na <- ifelse(!is.null(hour_na), regex_or(hour_na, .open = TRUE), "") - mday_na <- ifelse(!is.null(mday_na), regex_or(mday_na, .open = TRUE), "") - mon_na <- ifelse(!is.null(mon_na), regex_or(mon_na, .open = TRUE), "") - year_na <- ifelse(!is.null(year_na), regex_or(year_na, .open = TRUE), "") - - c( - sec = stringr::str_glue("(?{sec}{sec_na})"), - min = stringr::str_glue("(?{min}{min_na})"), - hour = stringr::str_glue("(?{hour}{hour_na})"), - mday = stringr::str_glue("(?{mday}{mday_na})"), - mon = stringr::str_glue("(?{mon}{mon_na})"), - year = stringr::str_glue("(?{year}{year_na})") - ) - } +fmt_rg <- function( + sec = "(\\b\\d|\\d{2})(\\.\\d*)?", + min = "(\\b\\d|\\d{2})", + hour = "\\d?\\d", + mday = "\\b\\d|\\d{2}", + mon = stringr::str_glue("\\d\\d|{months_abb_regex()}"), + year = "(\\d{2})?\\d{2}", + na = NULL, + sec_na = na, + min_na = na, + hour_na = na, + mday_na = na, + mon_na = na, + year_na = na) { + + sec_na <- + ifelse(!is.null(sec_na), regex_or(sec_na, .open = TRUE), "") + min_na <- + ifelse(!is.null(min_na), regex_or(min_na, .open = TRUE), "") + hour_na <- + ifelse(!is.null(hour_na), regex_or(hour_na, .open = TRUE), "") + mday_na <- + ifelse(!is.null(mday_na), regex_or(mday_na, .open = TRUE), "") + mon_na <- + ifelse(!is.null(mon_na), regex_or(mon_na, .open = TRUE), "") + year_na <- + ifelse(!is.null(year_na), regex_or(year_na, .open = TRUE), "") + + + c( + sec = stringr::str_glue("(?{sec}{sec_na})"), + min = stringr::str_glue("(?{min}{min_na})"), + hour = stringr::str_glue("(?{hour}{hour_na})"), + mday = stringr::str_glue("(?{mday}{mday_na})"), + mon = stringr::str_glue("(?{mon}{mon_na})"), + year = stringr::str_glue("(?{year}{year_na})") + ) +} # Scalar version of `parse_dttm_fmt()`. parse_dttm_fmt_ <- function(x, pattern) { @@ -217,12 +216,11 @@ parse_dttm_fmt <- function(fmt, patterns = fmt_c()) { fmt_dttmc$ord <- dplyr::row_number(fmt_dttmc$start) fmt_len <- nchar(fmt) - fmt_pos <- seq_len(fmt_len) start <- end <- NULL # To avoid a "no visible binding for global variable" NOTE. dttmc_pos <- with(fmt_dttmc, pseq(from = start[!is.na(start)], to = end[!is.na(end)])) # `delim_pos`: delimiter positions, i.e. positions in `fmt` in-between dttm components. - delim_pos <- find_int_gap(dttmc_pos, xmin = 1, xmax = fmt_len) + delim_pos <- find_int_gap(dttmc_pos, xmin = 1L, xmax = fmt_len) delim <- with(delim_pos, stringr::str_sub(fmt, start = start, end = end)) fmt_delim <- @@ -253,5 +251,3 @@ dttm_fmt_to_regex <- function(tbl_fmt_c, fmt_regex = fmt_rg(), anchored = TRUE) fmt_regex } - - diff --git a/man/create_iso8601.Rd b/man/create_iso8601.Rd index 15b170ec..bca57597 100644 --- a/man/create_iso8601.Rd +++ b/man/create_iso8601.Rd @@ -8,7 +8,7 @@ create_iso8601( ..., .format, .na = NULL, - .cutoff_2000 = 68, + .cutoff_2000 = 68L, .check_format = TRUE ) } diff --git a/man/format_iso8601.Rd b/man/format_iso8601.Rd index f16e6f82..ec101f24 100644 --- a/man/format_iso8601.Rd +++ b/man/format_iso8601.Rd @@ -4,7 +4,7 @@ \alias{format_iso8601} \title{Convert date/time components into ISO8601 format} \usage{ -format_iso8601(m, .cutoff_2000 = 68) +format_iso8601(m, .cutoff_2000 = 68L) } \arguments{ \item{m}{A character matrix of date/time components. It must have six diff --git a/tests/testthat/test-create_iso8601.R b/tests/testthat/test-create_iso8601.R index 7b29c1c7..42b9d4b4 100644 --- a/tests/testthat/test-create_iso8601.R +++ b/tests/testthat/test-create_iso8601.R @@ -3,17 +3,17 @@ test_that("`create_iso8601()`: individual date components", { x <- c("0", "50", "1950", "80", "1980", "2000") y0 <- create_iso8601(x, .format = "y", .check_format = FALSE) y1 <- c(NA, "2050", "1950", "1980", "1980", "2000") - expect_equal(y0, y1) + expect_identical(y0, y1) x <- c("0", "jan", "JAN", "JaN", "1", "01") y0 <- create_iso8601(x, .format = "m", .check_format = FALSE) y1 <- c(NA, "--01", "--01", "--01", NA, "--01") - expect_equal(y0, y1) + expect_identical(y0, y1) x <- c("0", "00", "1", "01", "10", "31") y0 <- create_iso8601(x, .format = "d", .check_format = FALSE) y1 <- c("----00", "----00", "----01", "----01", "----10", "----31") - expect_equal(y0, y1) + expect_identical(y0, y1) }) @@ -23,15 +23,15 @@ test_that("`create_iso8601()`: dates", { x <- c("19990101", "20000101", "990101", "991231") y0 <- create_iso8601(x, .format = "ymd", .check_format = FALSE) - expect_equal(y0, y1) + expect_identical(y0, y1) x <- c("1999-01-01", "2000-01-01", "99-01-01", "99-12-31") y0 <- create_iso8601(x, .format = "y-m-d", .check_format = FALSE) - expect_equal(y0, y1) + expect_identical(y0, y1) x <- c("1999 01 01", "2000 01 01", "99 01 01", "99 12 31") y0 <- create_iso8601(x, .format = "y m d", .check_format = FALSE) - expect_equal(y0, y1) + expect_identical(y0, y1) }) @@ -41,15 +41,15 @@ test_that("`create_iso8601()`: times: hours and minutes", { x <- c("1520", "0010", "2301", "0000") y0 <- create_iso8601(x, .format = "HM", .check_format = FALSE) - expect_equal(y0, y1) + expect_identical(y0, y1) x <- c("15:20", "00:10", "23:01", "00:00") y0 <- create_iso8601(x, .format = "H:M", .check_format = FALSE) - expect_equal(y0, y1) + expect_identical(y0, y1) x <- c("15h20", "00h10", "23h01", "00h00") y0 <- create_iso8601(x, .format = "HhM", .check_format = FALSE) - expect_equal(y0, y1) + expect_identical(y0, y1) }) @@ -58,12 +58,12 @@ test_that("`create_iso8601()`: times: hours, minutes and seconds", { x <- c("152000", "001059", "230112.123", "00002.") y0 <- create_iso8601(x, .format = "HMS", .check_format = FALSE) y1 <- c("-----T15:20:00", "-----T00:10:59", "-----T23:01:12.123", "-----T00:00:02") - expect_equal(y0, y1) + expect_identical(y0, y1) x <- c("15:20:00", "00:10:59", "23:01:12.123", "00:00:2.", "5:1:4") y0 <- create_iso8601(x, .format = "H:M:S", .check_format = FALSE) y1 <- c(y1, "-----T05:01:04") - expect_equal(y0, y1) + expect_identical(y0, y1) }) @@ -78,7 +78,6 @@ test_that("`create_iso8601()`: dates and times", { "2000-01-01T00:10", "1999-01-01T23:01", "1999-12-31T00:00") - expect_equal(iso8601_dttm, expectation) + expect_identical(iso8601_dttm, expectation) }) - diff --git a/tests/testthat/test-find_int_gap.R b/tests/testthat/test-find_int_gap.R index 18cc54a0..5fcfd453 100644 --- a/tests/testthat/test-find_int_gap.R +++ b/tests/testthat/test-find_int_gap.R @@ -1,34 +1,34 @@ test_that("`find_int_gap()`: one interval", { - tbl <- find_int_gap(c(1:3, 7:10)) + tbl <- find_int_gap(c(1L:3L, 7L:10L)) - expect_equal(tbl$start, 4) - expect_equal(tbl$end, 6) + expect_identical(tbl$start, 4L) + expect_identical(tbl$end, 6L) }) test_that("`find_int_gap()`: two intervals", { - tbl <- find_int_gap(c(1:3, 7:10, 15:20)) + tbl <- find_int_gap(c(1L:3L, 7L:10L, 15L:20L)) - expect_equal(tbl$start, c(4, 11)) - expect_equal(tbl$end, c(6, 14)) + expect_identical(tbl$start, c(4L, 11L)) + expect_identical(tbl$end, c(6L, 14L)) }) test_that("`find_int_gap()`: explicit endpoints", { - tbl <- find_int_gap(c(3:5, 8), xmin = 0, xmax = 10) + tbl <- find_int_gap(c(3L:5L, 8L), xmin = 0L, xmax = 10L) - expect_equal(tbl$start, c(0, 6, 9)) - expect_equal(tbl$end, c(2, 7, 10)) + expect_identical(tbl$start, c(0L, 6L, 9L)) + expect_identical(tbl$end, c(2L, 7L, 10L)) }) test_that("`find_int_gap()`: no intervals", { - tbl <- find_int_gap(0:5) - expect_equal(tbl, tibble::tibble(start = integer(), end = integer())) + tbl <- find_int_gap(0L:5L) + expect_identical(tbl, tibble::tibble(start = integer(), end = integer())) }) @@ -41,15 +41,15 @@ test_that("`find_int_gap()`: ensure `x` is integerish", { test_that("`find_int_gap()`: ensure `xmin` and `xmax` are integer scalars", { # Error because `xmin` and `xmax` are vectors - expect_error(find_int_gap(c(1:3, 7:10), xmin = 1:2)) - expect_error(find_int_gap(c(1:3, 7:10), xmax = 3:4)) + expect_error(find_int_gap(c(1L:3L, 7L:10L), xmin = 1L:2L)) + expect_error(find_int_gap(c(1L:3L, 7L:10L), xmax = 3L:4L)) # Error because `xmin` and `xmax` are double - expect_error(find_int_gap(c(1:3, 7:10), xmin = 1.5)) - expect_error(find_int_gap(c(1:3, 7:10), xmax = 1.5)) + expect_error(find_int_gap(c(1L:3L, 7L:10L), xmin = 1.5)) + expect_error(find_int_gap(c(1L:3L, 7L:10L), xmax = 1.5)) # Error because `xmin` and `xmax` are character - expect_error(find_int_gap(c(1:3, 7:10), xmin = "1")) - expect_error(find_int_gap(c(1:3, 7:10), xmax = "2")) + expect_error(find_int_gap(c(1L:3L, 7L:10L), xmin = "1")) + expect_error(find_int_gap(c(1L:3L, 7L:10L), xmax = "2")) }) diff --git a/tests/testthat/test-format_iso8601.R b/tests/testthat/test-format_iso8601.R index cc10bde9..460e9ecd 100644 --- a/tests/testthat/test-format_iso8601.R +++ b/tests/testthat/test-format_iso8601.R @@ -10,11 +10,11 @@ test_that("`format_iso8601()`: basic usage", { "00", "59", "10", "42", "5.15", NA ), - ncol = 6, + ncol = 6L, dimnames = list(c(), cols) ) - expect_equal( + expect_identical( format_iso8601(m), c( "1999-01-01T00:00:42", diff --git a/tests/testthat/test-iso8601.R b/tests/testthat/test-iso8601.R index 57a3d923..881b514f 100644 --- a/tests/testthat/test-iso8601.R +++ b/tests/testthat/test-iso8601.R @@ -1,7 +1,7 @@ test_that("`iso8601_na()`: basic usage", { - expect_equal(iso8601_na(c("10", "15")), c("10", "15")) - expect_equal(iso8601_na(c("10", NA_character_)), c("10", "-")) - expect_equal(iso8601_na(character()), character(0)) + expect_identical(iso8601_na(c("10", "15")), c("10", "15")) + expect_identical(iso8601_na(c("10", NA_character_)), c("10", "-")) + expect_identical(iso8601_na(character()), character(0L)) }) test_that("`iso8601_na()`: input can't be `NULL`", { @@ -12,29 +12,29 @@ test_that("`iso8601_na()`: input can't be `NULL`", { test_that("`zero_pad_whole_number()`: ensure `x` is integerish", { expect_error(zero_pad_whole_number(pi)) expect_error(zero_pad_whole_number("42")) - expect_error(zero_pad_whole_number(sqrt(2))) + expect_error(zero_pad_whole_number(sqrt(2.0))) expect_error(zero_pad_whole_number(TRUE)) - expect_no_error(zero_pad_whole_number(1)) + expect_no_error(zero_pad_whole_number(1L)) expect_no_error(zero_pad_whole_number(1.00)) - expect_no_error(zero_pad_whole_number(c(1:3))) + expect_no_error(zero_pad_whole_number(c(1L:3L))) }) test_that("`zero_pad_whole_number()`: basic usage", { - expect_equal(zero_pad_whole_number(c(-1, 0, 1)), c(NA, "00", "01")) - expect_equal(zero_pad_whole_number(c(-1, 0, 1, 10, 99, 100), n = 2), - c(NA, "00", "01", "10", "99", NA)) - expect_equal(zero_pad_whole_number(c(-1, 0, 1, 10, 99, 100), n = 3), - c(NA, "000", "001", "010", "099", "100")) + expect_identical(zero_pad_whole_number(c(-1L, 0L, 1L)), c(NA, "00", "01")) + expect_identical(zero_pad_whole_number(c(-1L, 0L, 1L, 10L, 99L, 100L), n = 2L), + c(NA, "00", "01", "10", "99", NA)) + expect_identical(zero_pad_whole_number(c(-1L, 0L, 1L, 10L, 99L, 100L), n = 3L), + c(NA, "000", "001", "010", "099", "100")) }) test_that("`zero_pad_whole_number()`: ensure `n` is scalar integer", { - expect_no_error(zero_pad_whole_number(1, n = 1)) - expect_error(zero_pad_whole_number(1, n = 1:2)) + expect_no_error(zero_pad_whole_number(1L, n = 1L)) + expect_error(zero_pad_whole_number(1L, n = 1L:2L)) }) test_that("`iso8601_two_digits()`: basic usage", { x <- c("0", "00", "1", "01", "42", "100", NA_character_, "1.") y <- c("00", "00", "01", "01", "42", NA, NA, NA) - expect_equal(iso8601_two_digits(x), y) + expect_identical(iso8601_two_digits(x), y) }) diff --git a/tests/testthat/test-parse_dttm.R b/tests/testthat/test-parse_dttm.R index f12d9d23..8da7ca9a 100644 --- a/tests/testthat/test-parse_dttm.R +++ b/tests/testthat/test-parse_dttm.R @@ -13,7 +13,7 @@ test_that("`months_abb_regex()`: default behavior (case insensitive)", { "[Nn][Oo][Vv]|", "[Dd][Ee][Cc]" ) - expect_equal(months_abb_regex(), x) + expect_identical(months_abb_regex(), x) }) test_that("`months_abb_regex()`: uppercase", { @@ -31,7 +31,7 @@ test_that("`months_abb_regex()`: uppercase", { "NOV|", "DEC" ) - expect_equal(months_abb_regex(case = "upper"), x) + expect_identical(months_abb_regex(case = "upper"), x) }) test_that("`months_abb_regex()`: lowercase", { @@ -49,5 +49,5 @@ test_that("`months_abb_regex()`: lowercase", { "nov|", "dec" ) - expect_equal(months_abb_regex(case = "lower"), x) + expect_identical(months_abb_regex(case = "lower"), x) }) diff --git a/tests/testthat/test-pseq.R b/tests/testthat/test-pseq.R index f300b561..abdbde3b 100644 --- a/tests/testthat/test-pseq.R +++ b/tests/testthat/test-pseq.R @@ -1,7 +1,7 @@ test_that("`pseq()`: scalar inputs", { - expect_equal(pseq(from = 0, to = 5), 0:5) + expect_identical(pseq(from = 0L, to = 5L), 0L:5L) }) test_that("`pseq()`: vector inputs", { - expect_equal(pseq(from = c(0, 10), to = c(5, 15)), c(0:5, 10:15)) + expect_identical(pseq(from = c(0L, 10L), to = c(5L, 15L)), c(0L:5L, 10L:15L)) }) diff --git a/tests/testthat/test-reg_matches.R b/tests/testthat/test-reg_matches.R index 979e277b..a5ecb573 100644 --- a/tests/testthat/test-reg_matches.R +++ b/tests/testthat/test-reg_matches.R @@ -1,10 +1,10 @@ test_that("`reg_matches()`: basic usage", { x <- c("sdtm.oak", "sdtm.cdisc", "adam") - m <- gregexpr("sdtm", x) + m <- gregexpr("sdtm", x, fixed = TRUE) # `regmatches()` returns `character(0)` for `"adam"` # But `reg_matches()` returns `NA` for `"adam"` - expect_equal(reg_matches(x, m), list("sdtm", "sdtm", NA_character_)) + expect_identical(reg_matches(x, m), list("sdtm", "sdtm", NA_character_)) }) diff --git a/tests/testthat/test-str_to_anycase.R b/tests/testthat/test-str_to_anycase.R index ea24c9f3..b6e372e7 100644 --- a/tests/testthat/test-str_to_anycase.R +++ b/tests/testthat/test-str_to_anycase.R @@ -1,5 +1,5 @@ test_that("`str_to_anycase()`: basic usage", { x <- c("JAN", "feb", "mAr") y <- c("[Jj][Aa][Nn]", "[Ff][Ee][Bb]", "[Mm][Aa][Rr]") - expect_equal(str_to_anycase(x), y) + expect_identical(str_to_anycase(x), y) }) diff --git a/tests/testthat/test-yy_to_yyyy.R b/tests/testthat/test-yy_to_yyyy.R index 38836222..76f4002b 100644 --- a/tests/testthat/test-yy_to_yyyy.R +++ b/tests/testthat/test-yy_to_yyyy.R @@ -1,12 +1,12 @@ test_that("`yy_to_yyyy()`: basic usage", { # Default cutoff is at `68`. - x1 <- c(0, 1, 50, 68, 69, 70) - y1 <- c(2000, 2001, 2050, 2068, 1969, 1970) - expect_equal(yy_to_yyyy(x1), y1) + x1 <- c(0L, 1L, 50L, 68L, 69L, 70L) + y1 <- c(2000L, 2001L, 2050L, 2068L, 1969L, 1970L) + expect_identical(yy_to_yyyy(x1), y1) # Different cutoff, e.g. `79`. - x2 <- 75:85 + x2 <- 75L:85L y2 <- c(2075L, 2076L, @@ -19,9 +19,9 @@ test_that("`yy_to_yyyy()`: basic usage", { 1983L, 1984L, 1985L) - expect_equal(yy_to_yyyy(x2, cutoff_2000 = 79L), y2) + expect_identical(yy_to_yyyy(x2, cutoff_2000 = 79L), y2) # Four-digit years remain altered. - x3 <- 1965:1975 - expect_equal(yy_to_yyyy(x3), x3) + x3 <- 1965L:1975L + expect_identical(yy_to_yyyy(x3), x3) })