diff --git a/.Rbuildignore b/.Rbuildignore index e2c4f963..11c776ea 100644 --- a/.Rbuildignore +++ b/.Rbuildignore @@ -21,3 +21,5 @@ ^LICENSE\.md$ ^\.github$ ^CRAN-SUBMISSION$ +^[.]?air[.]toml$ +^\.vscode$ diff --git a/.vscode/extensions.json b/.vscode/extensions.json new file mode 100644 index 00000000..344f76eb --- /dev/null +++ b/.vscode/extensions.json @@ -0,0 +1,5 @@ +{ + "recommendations": [ + "Posit.air-vscode" + ] +} diff --git a/.vscode/settings.json b/.vscode/settings.json new file mode 100644 index 00000000..a9f69fe4 --- /dev/null +++ b/.vscode/settings.json @@ -0,0 +1,10 @@ +{ + "[r]": { + "editor.formatOnSave": true, + "editor.defaultFormatter": "Posit.air-vscode" + }, + "[quarto]": { + "editor.formatOnSave": true, + "editor.defaultFormatter": "quarto.quarto" + } +} diff --git a/R/compat-obj-type.R b/R/compat-obj-type.R index d86dca51..a7162de5 100644 --- a/R/compat-obj-type.R +++ b/R/compat-obj-type.R @@ -37,7 +37,6 @@ # - Added documentation. # - Added changelog. - #' Return English-friendly type #' @param x Any R object. #' @param value Whether to describe the value of `x`. Special values @@ -73,12 +72,11 @@ obj_type_friendly <- function(x, value = TRUE) { typeof(x), logical = "`NA`", integer = "an integer `NA`", - double = - if (is.nan(x)) { - "`NaN`" - } else { - "a numeric `NA`" - }, + double = if (is.nan(x)) { + "`NaN`" + } else { + "a numeric `NA`" + }, complex = "a complex `NA`", character = "a character `NA`", .rlang_stop_unexpected_typeof(x) @@ -280,14 +278,16 @@ obj_type_oo <- function(x) { #' @param ... Arguments passed to [abort()]. #' @inheritParams args_error_context #' @noRd -stop_input_type <- function(x, - what, - ..., - allow_na = FALSE, - allow_null = FALSE, - show_value = TRUE, - arg = caller_arg(x), - call = caller_env()) { +stop_input_type <- function( + x, + what, + ..., + allow_na = FALSE, + allow_null = FALSE, + show_value = TRUE, + arg = caller_arg(x), + call = caller_env() +) { # From compat-cli.R cli <- env_get_list( nms = c("format_arg", "format_code"), diff --git a/R/compat-purrr.R b/R/compat-purrr.R index efb9a9ca..cef754b1 100644 --- a/R/compat-purrr.R +++ b/R/compat-purrr.R @@ -90,11 +90,16 @@ args_recycle <- function(args) { } pmap <- function(.l, .f, ...) { args <- args_recycle(.l) - do.call("mapply", c( - FUN = list(quote(.f)), - args, MoreArgs = quote(list(...)), - SIMPLIFY = FALSE, USE.NAMES = FALSE - )) + do.call( + "mapply", + c( + FUN = list(quote(.f)), + args, + MoreArgs = quote(list(...)), + SIMPLIFY = FALSE, + USE.NAMES = FALSE + ) + ) } probe <- function(.x, .p, ...) { @@ -200,5 +205,4 @@ vec_index <- function(x) { names(x) %||% seq_along(x) } - # nocov end diff --git a/R/compat-types-check.R b/R/compat-types-check.R index 3e46ca43..1f52cbd0 100644 --- a/R/compat-types-check.R +++ b/R/compat-types-check.R @@ -28,12 +28,14 @@ # Scalars ----------------------------------------------------------------- -check_bool <- function(x, - ..., - allow_na = FALSE, - allow_null = FALSE, - arg = caller_arg(x), - call = caller_env()) { +check_bool <- function( + x, + ..., + allow_na = FALSE, + allow_null = FALSE, + arg = caller_arg(x), + call = caller_env() +) { if (!missing(x)) { if (is_bool(x)) { return(invisible(NULL)) @@ -57,13 +59,15 @@ check_bool <- function(x, ) } -check_string <- function(x, - ..., - allow_empty = TRUE, - allow_na = FALSE, - allow_null = FALSE, - arg = caller_arg(x), - call = caller_env()) { +check_string <- function( + x, + ..., + allow_empty = TRUE, + allow_na = FALSE, + allow_null = FALSE, + arg = caller_arg(x), + call = caller_env() +) { if (!missing(x)) { is_string <- .rlang_check_is_string( x, @@ -87,10 +91,7 @@ check_string <- function(x, ) } -.rlang_check_is_string <- function(x, - allow_empty, - allow_na, - allow_null) { +.rlang_check_is_string <- function(x, allow_empty, allow_na, allow_null) { if (is_string(x)) { if (allow_empty || !is_string(x, "")) { return(TRUE) @@ -108,11 +109,13 @@ check_string <- function(x, FALSE } -check_name <- function(x, - ..., - allow_null = FALSE, - arg = caller_arg(x), - call = caller_env()) { +check_name <- function( + x, + ..., + allow_null = FALSE, + arg = caller_arg(x), + call = caller_env() +) { if (!missing(x)) { is_string <- .rlang_check_is_string( x, @@ -136,15 +139,17 @@ check_name <- function(x, ) } -check_number_decimal <- function(x, - ..., - min = -Inf, - max = Inf, - allow_infinite = TRUE, - allow_na = FALSE, - allow_null = FALSE, - arg = caller_arg(x), - call = caller_env()) { +check_number_decimal <- function( + x, + ..., + min = -Inf, + max = Inf, + allow_infinite = TRUE, + allow_na = FALSE, + allow_null = FALSE, + arg = caller_arg(x), + call = caller_env() +) { .rlang_types_check_number( x, ..., @@ -159,14 +164,16 @@ check_number_decimal <- function(x, ) } -check_number_whole <- function(x, - ..., - min = -Inf, - max = Inf, - allow_na = FALSE, - allow_null = FALSE, - arg = caller_arg(x), - call = caller_env()) { +check_number_whole <- function( + x, + ..., + min = -Inf, + max = Inf, + allow_na = FALSE, + allow_null = FALSE, + arg = caller_arg(x), + call = caller_env() +) { .rlang_types_check_number( x, ..., @@ -181,31 +188,35 @@ check_number_whole <- function(x, ) } -.rlang_types_check_number <- function(x, - ..., - min = -Inf, - max = Inf, - allow_decimal = FALSE, - allow_infinite = FALSE, - allow_na = FALSE, - allow_null = FALSE, - arg = caller_arg(x), - call = caller_env()) { +.rlang_types_check_number <- function( + x, + ..., + min = -Inf, + max = Inf, + allow_decimal = FALSE, + allow_infinite = FALSE, + allow_na = FALSE, + allow_null = FALSE, + arg = caller_arg(x), + call = caller_env() +) { if (allow_decimal) { what <- "a number" } else { what <- "a whole number" } - .stop <- function(x, what, ...) stop_input_type( - x, - what, - ..., - allow_na = allow_na, - allow_null = allow_null, - arg = arg, - call = call - ) + .stop <- function(x, what, ...) { + stop_input_type( + x, + what, + ..., + allow_na = allow_na, + allow_null = allow_null, + arg = arg, + call = call + ) + } if (!missing(x)) { is_number <- is_number( @@ -234,9 +245,12 @@ check_number_whole <- function(x, if (allow_null && is_null(x)) { return(invisible(NULL)) } - if (allow_na && (identical(x, NA) || - identical(x, na_dbl) || - identical(x, na_int))) { + if ( + allow_na && + (identical(x, NA) || + identical(x, na_dbl) || + identical(x, na_int)) + ) { return(invisible(NULL)) } } @@ -244,9 +258,7 @@ check_number_whole <- function(x, .stop(x, what, ...) } -is_number <- function(x, - allow_decimal = FALSE, - allow_infinite = FALSE) { +is_number <- function(x, allow_decimal = FALSE, allow_infinite = FALSE) { if (!typeof(x) %in% c("integer", "double")) { return(FALSE) } @@ -265,11 +277,13 @@ is_number <- function(x, TRUE } -check_symbol <- function(x, - ..., - allow_null = FALSE, - arg = caller_arg(x), - call = caller_env()) { +check_symbol <- function( + x, + ..., + allow_null = FALSE, + arg = caller_arg(x), + call = caller_env() +) { if (!missing(x)) { if (is_symbol(x)) { return(invisible(NULL)) @@ -289,11 +303,13 @@ check_symbol <- function(x, ) } -check_arg <- function(x, - ..., - allow_null = FALSE, - arg = caller_arg(x), - call = caller_env()) { +check_arg <- function( + x, + ..., + allow_null = FALSE, + arg = caller_arg(x), + call = caller_env() +) { if (!missing(x)) { if (is_symbol(x)) { return(invisible(NULL)) @@ -313,11 +329,13 @@ check_arg <- function(x, ) } -check_call <- function(x, - ..., - allow_null = FALSE, - arg = caller_arg(x), - call = caller_env()) { +check_call <- function( + x, + ..., + allow_null = FALSE, + arg = caller_arg(x), + call = caller_env() +) { if (!missing(x)) { if (is_call(x)) { return(invisible(NULL)) @@ -337,11 +355,13 @@ check_call <- function(x, ) } -check_environment <- function(x, - ..., - allow_null = FALSE, - arg = caller_arg(x), - call = caller_env()) { +check_environment <- function( + x, + ..., + allow_null = FALSE, + arg = caller_arg(x), + call = caller_env() +) { if (!missing(x)) { if (is_environment(x)) { return(invisible(NULL)) @@ -361,11 +381,13 @@ check_environment <- function(x, ) } -check_function <- function(x, - ..., - allow_null = FALSE, - arg = caller_arg(x), - call = caller_env()) { +check_function <- function( + x, + ..., + allow_null = FALSE, + arg = caller_arg(x), + call = caller_env() +) { if (!missing(x)) { if (is_function(x)) { return(invisible(NULL)) @@ -385,11 +407,13 @@ check_function <- function(x, ) } -check_closure <- function(x, - ..., - allow_null = FALSE, - arg = caller_arg(x), - call = caller_env()) { +check_closure <- function( + x, + ..., + allow_null = FALSE, + arg = caller_arg(x), + call = caller_env() +) { if (!missing(x)) { if (is_closure(x)) { return(invisible(NULL)) @@ -409,11 +433,13 @@ check_closure <- function(x, ) } -check_formula <- function(x, - ..., - allow_null = FALSE, - arg = caller_arg(x), - call = caller_env()) { +check_formula <- function( + x, + ..., + allow_null = FALSE, + arg = caller_arg(x), + call = caller_env() +) { if (!missing(x)) { if (is_formula(x)) { return(invisible(NULL)) @@ -436,11 +462,13 @@ check_formula <- function(x, # Vectors ----------------------------------------------------------------- -check_character <- function(x, - ..., - allow_null = FALSE, - arg = caller_arg(x), - call = caller_env()) { +check_character <- function( + x, + ..., + allow_null = FALSE, + arg = caller_arg(x), + call = caller_env() +) { if (!missing(x)) { if (is_character(x)) { return(invisible(NULL)) diff --git a/R/count.R b/R/count.R index 10e93840..e6e1c2db 100644 --- a/R/count.R +++ b/R/count.R @@ -37,11 +37,12 @@ str_count <- function(string, pattern = "") { check_lengths(string, pattern) - switch(type(pattern), + switch( + type(pattern), empty = , bound = stri_count_boundaries(string, opts_brkiter = opts(pattern)), fixed = stri_count_fixed(string, pattern, opts_fixed = opts(pattern)), - coll = stri_count_coll(string, pattern, opts_collator = opts(pattern)), + coll = stri_count_coll(string, pattern, opts_collator = opts(pattern)), regex = stri_count_regex(string, pattern, opts_regex = opts(pattern)) ) } diff --git a/R/detect.R b/R/detect.R index 449cc936..99c06465 100644 --- a/R/detect.R +++ b/R/detect.R @@ -42,12 +42,28 @@ str_detect <- function(string, pattern, negate = FALSE) { check_lengths(string, pattern) check_bool(negate) - switch(type(pattern), + switch( + type(pattern), empty = no_empty(), bound = no_boundary(), - fixed = stri_detect_fixed(string, pattern, negate = negate, opts_fixed = opts(pattern)), - coll = stri_detect_coll(string, pattern, negate = negate, opts_collator = opts(pattern)), - regex = stri_detect_regex(string, pattern, negate = negate, opts_regex = opts(pattern)) + fixed = stri_detect_fixed( + string, + pattern, + negate = negate, + opts_fixed = opts(pattern) + ), + coll = stri_detect_coll( + string, + pattern, + negate = negate, + opts_collator = opts(pattern) + ), + regex = stri_detect_regex( + string, + pattern, + negate = negate, + opts_regex = opts(pattern) + ) ) } @@ -78,14 +94,30 @@ str_starts <- function(string, pattern, negate = FALSE) { check_lengths(string, pattern) check_bool(negate) - switch(type(pattern), + switch( + type(pattern), empty = no_empty(), bound = no_boundary(), - fixed = stri_startswith_fixed(string, pattern, negate = negate, opts_fixed = opts(pattern)), - coll = stri_startswith_coll(string, pattern, negate = negate, opts_collator = opts(pattern)), + fixed = stri_startswith_fixed( + string, + pattern, + negate = negate, + opts_fixed = opts(pattern) + ), + coll = stri_startswith_coll( + string, + pattern, + negate = negate, + opts_collator = opts(pattern) + ), regex = { pattern2 <- paste0("^(", pattern, ")") - stri_detect_regex(string, pattern2, negate = negate, opts_regex = opts(pattern)) + stri_detect_regex( + string, + pattern2, + negate = negate, + opts_regex = opts(pattern) + ) } ) } @@ -96,14 +128,30 @@ str_ends <- function(string, pattern, negate = FALSE) { check_lengths(string, pattern) check_bool(negate) - switch(type(pattern), + switch( + type(pattern), empty = no_empty(), bound = no_boundary(), - fixed = stri_endswith_fixed(string, pattern, negate = negate, opts_fixed = opts(pattern)), - coll = stri_endswith_coll(string, pattern, negate = negate, opts_collator = opts(pattern)), + fixed = stri_endswith_fixed( + string, + pattern, + negate = negate, + opts_fixed = opts(pattern) + ), + coll = stri_endswith_coll( + string, + pattern, + negate = negate, + opts_collator = opts(pattern) + ), regex = { pattern2 <- paste0("(", pattern, ")$") - stri_detect_regex(string, pattern2, negate = negate, opts_regex = opts(pattern)) + stri_detect_regex( + string, + pattern2, + negate = negate, + opts_regex = opts(pattern) + ) } ) } @@ -148,7 +196,9 @@ str_like <- function(string, pattern, ignore_case = deprecated()) { check_lengths(string, pattern) check_character(pattern) if (inherits(pattern, "stringr_pattern")) { - cli::cli_abort("{.arg pattern} must be a plain string, not a stringr modifier.") + cli::cli_abort( + "{.arg pattern} must be a plain string, not a stringr modifier." + ) } if (lifecycle::is_present(ignore_case)) { lifecycle::deprecate_warn( @@ -175,7 +225,9 @@ str_ilike <- function(string, pattern) { check_lengths(string, pattern) check_character(pattern) if (inherits(pattern, "stringr_pattern")) { - cli::cli_abort(tr_("{.arg pattern} must be a plain string, not a stringr modifier.")) + cli::cli_abort(tr_( + "{.arg pattern} must be a plain string, not a stringr modifier." + )) } pattern <- regex(like_to_regex(pattern), ignore_case = TRUE) @@ -183,7 +235,11 @@ str_ilike <- function(string, pattern) { } like_to_regex <- function(pattern) { - converted <- stri_replace_all_regex(pattern, "(? n) { NA_character_ - } else{ + } else { x[[n + 1 - i]] } } @@ -122,7 +151,11 @@ str_split_i <- function(string, pattern, i) { } } -check_positive_integer <- function(x, arg = caller_arg(x), call = caller_env()) { +check_positive_integer <- function( + x, + arg = caller_arg(x), + call = caller_env() +) { if (!identical(x, Inf)) { check_number_whole(x, min = 1, arg = arg, call = call) } diff --git a/R/sub.R b/R/sub.R index 961ac94e..7b5315cb 100644 --- a/R/sub.R +++ b/R/sub.R @@ -75,7 +75,12 @@ str_sub <- function(string, start = 1L, end = -1L) { #' @export #' @rdname str_sub "str_sub<-" <- function(string, start = 1L, end = -1L, omit_na = FALSE, value) { - vctrs::vec_size_common(string = string, start = start, end = end, value = value) + vctrs::vec_size_common( + string = string, + start = start, + end = end, + value = value + ) if (is.matrix(start)) { stri_sub(string, from = start, omit_na = omit_na) <- value diff --git a/R/subset.R b/R/subset.R index 0cde8324..0644737a 100644 --- a/R/subset.R +++ b/R/subset.R @@ -30,13 +30,14 @@ str_subset <- function(string, pattern, negate = FALSE) { check_lengths(string, pattern) check_bool(negate) - switch(type(pattern), - empty = no_empty(), - bound = no_boundary(), - fixed = string[str_detect(string, pattern, negate = negate)], - coll = string[str_detect(string, pattern, negate = negate)], - regex = string[str_detect(string, pattern, negate = negate)]) - + switch( + type(pattern), + empty = no_empty(), + bound = no_boundary(), + fixed = string[str_detect(string, pattern, negate = negate)], + coll = string[str_detect(string, pattern, negate = negate)], + regex = string[str_detect(string, pattern, negate = negate)] + ) } #' Find matching indices diff --git a/R/trim.R b/R/trim.R index 435c5ac0..c3da899e 100644 --- a/R/trim.R +++ b/R/trim.R @@ -19,10 +19,11 @@ str_trim <- function(string, side = c("both", "left", "right")) { side <- arg_match(side) - switch(side, - left = stri_trim_left(string), + switch( + side, + left = stri_trim_left(string), right = stri_trim_right(string), - both = stri_trim_both(string) + both = stri_trim_both(string) ) } diff --git a/R/trunc.R b/R/trunc.R index e3459a9a..46bab09b 100644 --- a/R/trunc.R +++ b/R/trunc.R @@ -17,8 +17,12 @@ #' str_trunc(x, 20, "left"), #' str_trunc(x, 20, "center") #' ) -str_trunc <- function(string, width, side = c("right", "left", "center"), - ellipsis = "...") { +str_trunc <- function( + string, + width, + side = c("right", "left", "center"), + ellipsis = "..." +) { check_number_whole(width) side <- arg_match(side) check_string(ellipsis) @@ -29,18 +33,24 @@ str_trunc <- function(string, width, side = c("right", "left", "center"), if (width... < 0) { cli::cli_abort( - tr_("`width` ({width}) is shorter than `ellipsis` ({str_length(ellipsis)}).") + tr_( + "`width` ({width}) is shorter than `ellipsis` ({str_length(ellipsis)})." + ) ) } - string[too_long] <- switch(side, - right = str_c(str_sub(string[too_long], 1, width...), ellipsis), - left = str_c(ellipsis, str_sub(string[too_long], len[too_long] - width... + 1, -1)), + string[too_long] <- switch( + side, + right = str_c(str_sub(string[too_long], 1, width...), ellipsis), + left = str_c( + ellipsis, + str_sub(string[too_long], len[too_long] - width... + 1, -1) + ), center = str_c( - str_sub(string[too_long], 1, ceiling(width... / 2)), - ellipsis, - str_sub(string[too_long], len[too_long] - floor(width... / 2) + 1, -1) - ) + str_sub(string[too_long], 1, ceiling(width... / 2)), + ellipsis, + str_sub(string[too_long], len[too_long] - floor(width... / 2) + 1, -1) + ) ) string } diff --git a/R/utils.R b/R/utils.R index 248afe84..8cda3a37 100644 --- a/R/utils.R +++ b/R/utils.R @@ -8,7 +8,12 @@ #' @usage lhs \%>\% rhs NULL -check_lengths <- function(string, pattern, replacement = NULL, error_call = caller_env()) { +check_lengths <- function( + string, + pattern, + replacement = NULL, + error_call = caller_env() +) { # stringi already correctly recycles vectors of length 0 and 1 # we just want more stringent vctrs checks for other lengths vctrs::vec_size_common( @@ -23,7 +28,10 @@ no_boundary <- function(call = caller_env()) { cli::cli_abort(tr_("{.arg pattern} can't be a boundary."), call = call) } no_empty <- function(call = caller_env()) { - cli::cli_abort(tr_("{.arg pattern} can't be the empty string ({.code \"\"})."), call = call) + cli::cli_abort( + tr_("{.arg pattern} can't be the empty string ({.code \"\"})."), + call = call + ) } tr_ <- function(...) { diff --git a/R/view.R b/R/view.R index 80ee5622..e31a65c0 100644 --- a/R/view.R +++ b/R/view.R @@ -48,7 +48,13 @@ #' str_view(c("abc", "def", "fghi"), "e", match = NA) #' # or just those that don't match: #' str_view(c("abc", "def", "fghi"), "e", match = FALSE) -str_view <- function(string, pattern = NULL, match = TRUE, html = FALSE, use_escapes = FALSE) { +str_view <- function( + string, + pattern = NULL, + match = TRUE, + html = FALSE, + use_escapes = FALSE +) { rec <- vctrs::vec_recycle_common(string = string, pattern = pattern) string <- rec$string pattern <- rec$pattern @@ -77,7 +83,13 @@ str_view <- function(string, pattern = NULL, match = TRUE, html = FALSE, use_esc #' @rdname str_view #' @usage NULL #' @export -str_view_all <- function(string, pattern = NULL, match = NA, html = FALSE, use_escapes = FALSE) { +str_view_all <- function( + string, + pattern = NULL, + match = NA, + html = FALSE, + use_escapes = FALSE +) { lifecycle::deprecate_warn("1.5.0", "str_view_all()", "str_view()") str_view( diff --git a/R/word.R b/R/word.R index e23e89ef..a71ba6b9 100644 --- a/R/word.R +++ b/R/word.R @@ -50,7 +50,7 @@ word <- function(string, start = 1L, end = start, sep = fixed(" ")) { # Extract locations starts <- mapply(function(word, loc) word[loc, "start"], words, start) - ends <- mapply(function(word, loc) word[loc, "end"], words, end) + ends <- mapply(function(word, loc) word[loc, "end"], words, end) str_sub(string, starts, ends) } diff --git a/R/wrap.R b/R/wrap.R index 53d9693c..a27a9e70 100644 --- a/R/wrap.R +++ b/R/wrap.R @@ -24,11 +24,13 @@ #' cat(str_wrap(thanks, width = 60, indent = 2), "\n") #' cat(str_wrap(thanks, width = 60, exdent = 2), "\n") #' cat(str_wrap(thanks, width = 0, exdent = 2), "\n") -str_wrap <- function(string, - width = 80, - indent = 0, - exdent = 0, - whitespace_only = TRUE) { +str_wrap <- function( + string, + width = 80, + indent = 0, + exdent = 0, + whitespace_only = TRUE +) { check_number_decimal(width) if (width <= 0) { width <- 1 @@ -37,7 +39,13 @@ str_wrap <- function(string, check_number_whole(exdent) check_bool(whitespace_only) - out <- stri_wrap(string, width = width, indent = indent, exdent = exdent, - whitespace_only = whitespace_only, simplify = FALSE) + out <- stri_wrap( + string, + width = width, + indent = indent, + exdent = exdent, + whitespace_only = whitespace_only, + simplify = FALSE + ) vapply(out, str_c, collapse = "\n", character(1)) } diff --git a/air.toml b/air.toml new file mode 100644 index 00000000..e69de29b diff --git a/data-raw/samples.R b/data-raw/samples.R index a7ff518c..c49e3cbd 100644 --- a/data-raw/samples.R +++ b/data-raw/samples.R @@ -2,7 +2,8 @@ words <- rcorpora::corpora("words/common")$commonWords fruit <- rcorpora::corpora("foods/fruits")$fruits html <- read_html("https://harvardsentences.com") -html %>% html_elements("li") %>% +html %>% + html_elements("li") %>% html_text() %>% iconv(to = "ASCII//translit") %>% writeLines("data-raw/harvard-sentences.txt") diff --git a/tests/testthat/test-detect.R b/tests/testthat/test-detect.R index 65ba41e8..6401ba59 100644 --- a/tests/testthat/test-detect.R +++ b/tests/testthat/test-detect.R @@ -25,7 +25,6 @@ test_that("str_starts() and str_ends() match expected strings", { }) test_that("can use fixed() and coll()", { - expect_equal(str_detect("X", fixed(".")), FALSE) expect_equal(str_starts("X", fixed(".")), FALSE) expect_equal(str_ends("X", fixed(".")), FALSE) @@ -54,7 +53,6 @@ test_that("functions use tidyverse recycling rules", { # str_like ---------------------------------------------------------------- - test_that("str_like is case sensitive", { expect_true(str_like("abc", "ab%")) expect_false(str_like("abc", "AB%")) @@ -75,7 +73,7 @@ test_that("str_ilike works", { expect_snapshot(str_ilike("abc", regex("x")), error = TRUE) }) -test_that("like_to_regex generates expected regexps",{ +test_that("like_to_regex generates expected regexps", { expect_equal(like_to_regex("ab%"), "^ab.*$") expect_equal(like_to_regex("ab_"), "^ab.$") diff --git a/tests/testthat/test-escape.R b/tests/testthat/test-escape.R index b9b36f77..2f5cfa78 100644 --- a/tests/testthat/test-escape.R +++ b/tests/testthat/test-escape.R @@ -5,4 +5,3 @@ test_that("multiplication works", { ) expect_equal(str_escape("\\"), "\\\\") }) - diff --git a/tests/testthat/test-extract.R b/tests/testthat/test-extract.R index e5124721..8623f7da 100644 --- a/tests/testthat/test-extract.R +++ b/tests/testthat/test-extract.R @@ -62,4 +62,3 @@ test_that("can extract boundaries", { list(c("a", "b", "c")) ) }) - diff --git a/tests/testthat/test-glue.R b/tests/testthat/test-glue.R index ddc79a26..512c3971 100644 --- a/tests/testthat/test-glue.R +++ b/tests/testthat/test-glue.R @@ -8,6 +8,8 @@ test_that("verify wrapper is functional", { test_that("verify trim is functional", { expect_equal(as.character(str_glue("L1\t \n \tL2")), "L1\t \nL2") - expect_equal(as.character(str_glue("L1\t \n \tL2", .trim = FALSE)), "L1\t \n \tL2") - + expect_equal( + as.character(str_glue("L1\t \n \tL2", .trim = FALSE)), + "L1\t \n \tL2" + ) }) diff --git a/tests/testthat/test-interp.R b/tests/testthat/test-interp.R index f8045735..e1511003 100644 --- a/tests/testthat/test-interp.R +++ b/tests/testthat/test-interp.R @@ -1,6 +1,6 @@ test_that("str_interp works with default env", { subject <- "statistics" - number <- 7 + number <- 7 floating <- 6.656 expect_equal( @@ -49,8 +49,8 @@ test_that("str_interp works in the absense of placeholders", { }) test_that("str_interp fails when encountering nested placeholders", { - msg <- "This will never see the light of day" - num <- 1.2345 + msg <- "This will never see the light of day" + num <- 1.2345 expect_snapshot(error = TRUE, { str_interp("${${msg}}") @@ -64,7 +64,6 @@ test_that("str_interp fails when input is not a character string", { test_that("str_interp wraps parsing errors", { expect_snapshot(str_interp("This is a ${1 +}"), error = TRUE) - }) test_that("str_interp formats list independetly of other placeholders", { diff --git a/tests/testthat/test-locate.R b/tests/testthat/test-locate.R index 11641e39..20849b14 100644 --- a/tests/testthat/test-locate.R +++ b/tests/testthat/test-locate.R @@ -2,12 +2,15 @@ test_that("basic location matching works", { expect_equal(str_locate("abc", "a")[1, ], c(start = 1, end = 1)) expect_equal(str_locate("abc", "b")[1, ], c(start = 2, end = 2)) expect_equal(str_locate("abc", "c")[1, ], c(start = 3, end = 3)) - expect_equal(str_locate("abc", ".+")[1, ], c(start = 1, end = 3)) + expect_equal(str_locate("abc", ".+")[1, ], c(start = 1, end = 3)) }) test_that("uses tidyverse recycling rules", { expect_error(str_locate(1:2, 1:3), class = "vctrs_error_incompatible_size") - expect_error(str_locate_all(1:2, 1:3), class = "vctrs_error_incompatible_size") + expect_error( + str_locate_all(1:2, 1:3), + class = "vctrs_error_incompatible_size" + ) }) test_that("locations are integers", { @@ -27,7 +30,7 @@ test_that("both string and patterns are vectorised", { locs <- str_locate(strings, c("a", "d")) expect_equal(locs[, "start"], c(1, 1)) - expect_equal(locs[, "end"], c(1, 1)) + expect_equal(locs[, "end"], c(1, 1)) locs <- str_locate_all(c("abab"), c("a", "b")) expect_equal(locs[[1]][, "start"], c(1, 3)) diff --git a/tests/testthat/test-match.R b/tests/testthat/test-match.R index f0960edd..0c97bf01 100644 --- a/tests/testthat/test-match.R +++ b/tests/testthat/test-match.R @@ -3,9 +3,20 @@ num <- matrix(sample(9, 10 * 10, replace = T), ncol = 10) num_flat <- apply(num, 1, str_c, collapse = "") phones <- str_c( - "(", num[, 1], num[, 2], num[, 3], ") ", - num[, 4], num[, 5], num[, 6], " ", - num[, 7], num[, 8], num[, 9], num[, 10]) + "(", + num[, 1], + num[, 2], + num[, 3], + ") ", + num[, 4], + num[, 5], + num[, 6], + " ", + num[, 7], + num[, 8], + num[, 9], + num[, 10] +) test_that("empty strings return correct matrix of correct size", { skip_if_not_installed("stringi", "1.2.2") @@ -36,8 +47,10 @@ test_that("single match works when all match", { }) test_that("match returns NA when some inputs don't match", { - matches <- str_match(c(phones, "blah", NA), - "\\(([0-9]{3})\\) ([0-9]{3}) ([0-9]{4})") + matches <- str_match( + c(phones, "blah", NA), + "\\(([0-9]{3})\\) ([0-9]{3}) ([0-9]{4})" + ) expect_equal(nrow(matches), length(phones) + 2) expect_equal(ncol(matches), 4) @@ -56,10 +69,11 @@ test_that("match_all returns NA when option group doesn't match", { test_that("multiple match works", { phones_one <- str_c(phones, collapse = " ") - multi_match <- str_match_all(phones_one, - "\\(([0-9]{3})\\) ([0-9]{3}) ([0-9]{4})") - single_matches <- str_match(phones, - "\\(([0-9]{3})\\) ([0-9]{3}) ([0-9]{4})") + multi_match <- str_match_all( + phones_one, + "\\(([0-9]{3})\\) ([0-9]{3}) ([0-9]{4})" + ) + single_matches <- str_match(phones, "\\(([0-9]{3})\\) ([0-9]{3}) ([0-9]{4})") expect_equal(multi_match[[1]], single_matches) }) diff --git a/tests/testthat/test-modifiers.R b/tests/testthat/test-modifiers.R index 74e2765e..d5b59f0c 100644 --- a/tests/testthat/test-modifiers.R +++ b/tests/testthat/test-modifiers.R @@ -48,5 +48,4 @@ test_that("stringr_pattern methods", { ex <- coll(c("foo", "bar")) expect_true(inherits(ex[1], "stringr_pattern")) expect_true(inherits(ex[[1]], "stringr_pattern")) - }) diff --git a/tests/testthat/test-pad.R b/tests/testthat/test-pad.R index 098845e3..110f1df3 100644 --- a/tests/testthat/test-pad.R +++ b/tests/testthat/test-pad.R @@ -1,27 +1,30 @@ test_that("long strings are unchanged", { lengths <- sample(40:100, 10) - strings <- vapply(lengths, function(x) - str_c(letters[sample(26, x, replace = T)], collapse = ""), - character(1)) + strings <- vapply( + lengths, + function(x) { + str_c(letters[sample(26, x, replace = T)], collapse = "") + }, + character(1) + ) padded <- str_pad(strings, width = 30) expect_equal(str_length(padded), str_length(strings)) }) test_that("directions work for simple case", { - pad <- function(direction) str_pad("had", direction, width = 10) - expect_equal(pad("right"), "had ") - expect_equal(pad("left"), " had") - expect_equal(pad("both"), " had ") + expect_equal(pad("right"), "had ") + expect_equal(pad("left"), " had") + expect_equal(pad("both"), " had ") }) test_that("padding based of length works", { # \u4e2d is a 2-characters-wide Chinese character pad <- function(...) str_pad("\u4e2d", ..., side = "both") - expect_equal(pad(width = 6), " \u4e2d ") + expect_equal(pad(width = 6), " \u4e2d ") expect_equal(pad(width = 5, use_width = FALSE), " \u4e2d ") }) diff --git a/tests/testthat/test-replace.R b/tests/testthat/test-replace.R index a3d2c678..db90bf77 100644 --- a/tests/testthat/test-replace.R +++ b/tests/testthat/test-replace.R @@ -62,8 +62,8 @@ test_that("can replace multiple values", { }) test_that("can use formula", { - expect_equal(str_replace("abc", "b", ~ "x"), "axc") - expect_equal(str_replace_all("abc", "b", ~ "x"), "axc") + expect_equal(str_replace("abc", "b", ~"x"), "axc") + expect_equal(str_replace_all("abc", "b", ~"x"), "axc") }) test_that("replacement can be different length", { @@ -103,7 +103,7 @@ test_that("works with zero length match", { test_that("replacement function must return correct type/length", { expect_snapshot(error = TRUE, { - str_replace_all("x", "x", ~ 1) + str_replace_all("x", "x", ~1) str_replace_all("x", "x", ~ c("a", "b")) }) }) diff --git a/tests/testthat/test-split.R b/tests/testthat/test-split.R index 77f39bb1..7078a77d 100644 --- a/tests/testthat/test-split.R +++ b/tests/testthat/test-split.R @@ -12,9 +12,15 @@ test_that("str_split functions as expected", { test_that("str_split() can split by special patterns", { expect_equal(str_split("ab", ""), list(c("a", "b"))) - expect_equal(str_split("this that.", boundary("word")), list(c("this", "that"))) + expect_equal( + str_split("this that.", boundary("word")), + list(c("this", "that")) + ) expect_equal(str_split("a-b", fixed("-")), list(c("a", "b"))) - expect_equal(str_split("aXb", coll("X", ignore_case = TRUE)), list(c("a", "b"))) + expect_equal( + str_split("aXb", coll("X", ignore_case = TRUE)), + list(c("a", "b")) + ) }) test_that("boundary() can be recycled", { @@ -48,7 +54,8 @@ test_that("str_split_1 takes string and returns character vector", { test_that("str_split_fixed pads with empty string", { expect_equal( str_split_fixed(c("a", "a-b"), "-", 1), - cbind(c("a", "a-b"))) + cbind(c("a", "a-b")) + ) expect_equal( str_split_fixed(c("a", "a-b"), "-", 2), cbind(c("a", "a"), c("", "b")) diff --git a/tests/testthat/test-trim.R b/tests/testthat/test-trim.R index edc96eed..6ae536af 100644 --- a/tests/testthat/test-trim.R +++ b/tests/testthat/test-trim.R @@ -1,22 +1,22 @@ test_that("trimming removes spaces", { - expect_equal(str_trim("abc "), "abc") - expect_equal(str_trim(" abc"), "abc") + expect_equal(str_trim("abc "), "abc") + expect_equal(str_trim(" abc"), "abc") expect_equal(str_trim(" abc "), "abc") }) test_that("trimming removes tabs", { - expect_equal(str_trim("abc\t"), "abc") - expect_equal(str_trim("\tabc"), "abc") + expect_equal(str_trim("abc\t"), "abc") + expect_equal(str_trim("\tabc"), "abc") expect_equal(str_trim("\tabc\t"), "abc") }) test_that("side argument restricts trimming", { - expect_equal(str_trim(" abc ", "left"), "abc ") + expect_equal(str_trim(" abc ", "left"), "abc ") expect_equal(str_trim(" abc ", "right"), " abc") }) test_that("str_squish removes excess spaces from all parts of string", { - expect_equal(str_squish("ab\t\tc\t"), "ab c") - expect_equal(str_squish("\ta bc"), "a bc") + expect_equal(str_squish("ab\t\tc\t"), "ab c") + expect_equal(str_squish("\ta bc"), "a bc") expect_equal(str_squish("\ta\t bc\t"), "a bc") }) diff --git a/tests/testthat/test-trunc.R b/tests/testthat/test-trunc.R index 58c0a58d..ecb66b6e 100644 --- a/tests/testthat/test-trunc.R +++ b/tests/testthat/test-trunc.R @@ -17,27 +17,28 @@ test_that("truncations work for all elements of a vector", { }) test_that("truncations work for all sides", { + trunc <- function(direction, width) { + str_trunc( + "This string is moderately long", + direction, + width = width + ) + } - trunc <- function(direction, width) str_trunc( - "This string is moderately long", - direction, - width = width - ) - - expect_equal(trunc("right", 20), "This string is mo...") - expect_equal(trunc("left", 20), "...s moderately long") + expect_equal(trunc("right", 20), "This string is mo...") + expect_equal(trunc("left", 20), "...s moderately long") expect_equal(trunc("center", 20), "This stri...ely long") - expect_equal(trunc("right", 3), "...") - expect_equal(trunc("left", 3), "...") + expect_equal(trunc("right", 3), "...") + expect_equal(trunc("left", 3), "...") expect_equal(trunc("center", 3), "...") - expect_equal(trunc("right", 4), "T...") - expect_equal(trunc("left", 4), "...g") + expect_equal(trunc("right", 4), "T...") + expect_equal(trunc("left", 4), "...g") expect_equal(trunc("center", 4), "T...") - expect_equal(trunc("right", 5), "Th...") - expect_equal(trunc("left", 5), "...ng") + expect_equal(trunc("right", 5), "Th...") + expect_equal(trunc("left", 5), "...ng") expect_equal(trunc("center", 5), "T...g") }) @@ -50,8 +51,12 @@ test_that("does not truncate to a length shorter than elipsis", { test_that("str_trunc correctly snips rhs-of-ellipsis for truncated strings", { trunc <- function(width, side) { - str_trunc(c("", "a", "aa", "aaa", "aaaa", "aaaaaaa"), width, side, - ellipsis = "..") + str_trunc( + c("", "a", "aa", "aaa", "aaaa", "aaaaaaa"), + width, + side, + ellipsis = ".." + ) } expect_equal(trunc(4, "right"), c("", "a", "aa", "aaa", "aaaa", "aa..")) diff --git a/tests/testthat/test-view.R b/tests/testthat/test-view.R index 333f989d..f8cc28bd 100644 --- a/tests/testthat/test-view.R +++ b/tests/testthat/test-view.R @@ -29,7 +29,7 @@ test_that("view highlights whitespace (except a space/nl)", { }) }) -test_that("view displays message for empty vectors",{ +test_that("view displays message for empty vectors", { expect_snapshot(str_view(character())) })