diff --git a/R/modify_pd.R b/R/modify_pd.R index c91a1534c..09bf30ef6 100644 --- a/R/modify_pd.R +++ b/R/modify_pd.R @@ -24,11 +24,13 @@ indent_curly <- function(pd, indent_by) { #' @describeIn update_indention Indents operators indent_op <- function(pd, indent_by, - token = c(math_token, - logical_token, - special_token, - "LEFT_ASSIGN", - "'$'")) { + token = c( + math_token, + logical_token, + special_token, + "LEFT_ASSIGN", + "'$'" + )) { indent_indices <- compute_indent_indices(pd, token, indent_last = TRUE) pd$indent[indent_indices] <- pd$indent[indent_indices] + indent_by pd diff --git a/R/nested.R b/R/nested.R index a4327c091..7c10e6778 100644 --- a/R/nested.R +++ b/R/nested.R @@ -40,11 +40,11 @@ tokenize <- function(text) { #' @param pd A parse table. enhance_mapping_special <- function(pd) { pd$token <- with(pd, case_when( - token != "SPECIAL" ~ token, - text == "%>%" ~ special_and("PIPE"), - text == "%in%" ~ special_and("IN"), - TRUE ~ special_and("OTHER") - )) + token != "SPECIAL" ~ token, + text == "%>%" ~ special_and("PIPE"), + text == "%in%" ~ special_and("IN"), + TRUE ~ special_and("OTHER") + )) pd } @@ -64,8 +64,10 @@ add_terminal_token_after <- function(pd_flat) { filter(terminal) %>% arrange(line1, col1) - data_frame(id = terminals$id, - token_after = lead(terminals$token, default = "")) %>% + data_frame( + id = terminals$id, + token_after = lead(terminals$token, default = "") + ) %>% left_join(pd_flat, ., by = "id") } @@ -75,8 +77,10 @@ add_terminal_token_before <- function(pd_flat) { filter(terminal) %>% arrange(line1, col1) - data_frame(id = terminals$id, - token_before = lag(terminals$token, default = "")) %>% + data_frame( + id = terminals$id, + token_before = lag(terminals$token, default = "") + ) %>% left_join(pd_flat, ., by = "id") } @@ -144,7 +148,6 @@ combine_children <- function(child, internal_child) { bound <- bind_rows(child, internal_child) if (nrow(bound) == 0) return(NULL) bound[order(bound$line1, bound$col1), ] - } #' Get the start right diff --git a/R/parsed.R b/R/parsed.R index 664288e2f..d585d3656 100644 --- a/R/parsed.R +++ b/R/parsed.R @@ -7,7 +7,6 @@ #' spaces. #' @importFrom utils tail create_filler <- function(pd_flat) { - pd_flat$line3 <- lead(pd_flat$line1, default = tail(pd_flat$line2, 1)) pd_flat$col3 <- lead(pd_flat$col1, default = tail(pd_flat$col2, 1) + 1L) pd_flat$newlines <- pd_flat$line3 - pd_flat$line2 @@ -29,4 +28,3 @@ create_filler <- function(pd_flat) { ret } - diff --git a/R/reindent.R b/R/reindent.R index 8cb7d78f8..a649c8594 100644 --- a/R/reindent.R +++ b/R/reindent.R @@ -29,8 +29,8 @@ update_indention_ref_fun_call <- function(pd_nested) { non_comment <- which(pd_nested$token != "COMMENT") first_non_comment_after_call <- non_comment[non_comment > 2][1] if ((current_is_call) && - nrow(pd_nested) > 3 && - pd_nested$lag_newlines[first_non_comment_after_call] == 0) { + nrow(pd_nested) > 3 && + pd_nested$lag_newlines[first_non_comment_after_call] == 0) { candidates <- 3:(nrow(pd_nested) - 1) child_is_call <- map_lgl(pd_nested$child, is_function_call) @@ -58,7 +58,7 @@ update_indention_ref_fun_call <- function(pd_nested) { #' } update_indention_ref_fun_dec <- function(pd_nested) { if (pd_nested$token[1] == "FUNCTION" && - nrow(pd_nested) > 3) { + nrow(pd_nested) > 3) { seq <- 3:(nrow(pd_nested) - 1) pd_nested$indention_ref_id[seq] <- pd_nested$id[1] } @@ -130,5 +130,4 @@ apply_ref_indention_one <- function(flattened_pd, target_token) { flattened_pd$col1[cols_to_update] <- flattened_pd$col1[cols_to_update] + shift flattened_pd$col2[cols_to_update] <- flattened_pd$col2[cols_to_update] + shift flattened_pd - } diff --git a/R/relevel.R b/R/relevel.R index f51f4ba31..1df9e6e97 100644 --- a/R/relevel.R +++ b/R/relevel.R @@ -21,7 +21,7 @@ flatten_operators <- function(pd_nested) { #' @include token.R flatten_operators_one <- function(pd_nested) { pd_token_left <- c(special_token, math_token, "'$'") - pd_token_right <- c(special_token, "LEFT_ASSIGN", "'+'", "'-'") + pd_token_right <- c(special_token, "LEFT_ASSIGN", "'+'", "'-'") bound <- pd_nested %>% flatten_pd(pd_token_left, left = TRUE) %>% flatten_pd(pd_token_right, left = FALSE) @@ -64,4 +64,3 @@ bind_with_child <- function(pd_nested, pos) { bind_rows(pd_nested$child[[pos]]) %>% arrange(line1, col1) } - diff --git a/R/rules-line_break.R b/R/rules-line_break.R index bd90ac187..d3f3b7cd9 100644 --- a/R/rules-line_break.R +++ b/R/rules-line_break.R @@ -66,18 +66,18 @@ set_line_break_after_opening_if_call_is_multi_line <- function(pd, except_token_after = NULL, except_text_before = NULL) { - if (!is_function_call(pd)) return(pd) - npd <- nrow(pd) - is_multi_line <- any(pd$lag_newlines[seq2(3, npd - 1)] > 0) - if (!is_multi_line) return(pd) - exception_pos <- c( - which(pd$token %in% except_token_after), - ifelse(pd$child[[1]]$text[1] %in% except_text_before, 3L, NA) - ) - pd$lag_newlines[3] <- 0L - pd$lag_newlines[setdiff(3, exception_pos)] <- 1L - pd -} + if (!is_function_call(pd)) return(pd) + npd <- nrow(pd) + is_multi_line <- any(pd$lag_newlines[seq2(3, npd - 1)] > 0) + if (!is_multi_line) return(pd) + exception_pos <- c( + which(pd$token %in% except_token_after), + ifelse(pd$child[[1]]$text[1] %in% except_text_before, 3L, NA) + ) + pd$lag_newlines[3] <- 0L + pd$lag_newlines[setdiff(3, exception_pos)] <- 1L + pd + } #' @describeIn set_line_break_if_call_is_multi_line Sets line break before diff --git a/R/rules-other.R b/R/rules-other.R index 754849960..6a2ef23e2 100644 --- a/R/rules-other.R +++ b/R/rules-other.R @@ -2,21 +2,21 @@ add_brackets_in_pipe <- function(pd) { has_no_brackets <- (pd$token_before == "SPECIAL-PIPE") & (pd$token == "SYMBOL") & (pd$text != ".") if (!any(has_no_brackets)) return(pd) - new <- data_frame(token = c("'('", "')'"), - text = c("(", ")"), - lag_newlines = rep(0, 2), - terminal = rep(TRUE, 2), - spaces = rep(0, 2), - multi_line = rep(FALSE, 2), - indention_ref_id = rep(NA, 2), - line1 = pd$line1[has_no_brackets], - line2 = line1, - col1 = pd$col1[has_no_brackets] + c(0.3, 0.6), - col2 = col1 + 1:2, - indent = rep(0, 2), - child = rep(list(NULL), 2) - ) + new <- data_frame( + token = c("'('", "')'"), + text = c("(", ")"), + lag_newlines = rep(0, 2), + terminal = rep(TRUE, 2), + spaces = rep(0, 2), + multi_line = rep(FALSE, 2), + indention_ref_id = rep(NA, 2), + line1 = pd$line1[has_no_brackets], + line2 = line1, + col1 = pd$col1[has_no_brackets] + c(0.3, 0.6), + col2 = col1 + 1:2, + indent = rep(0, 2), + child = rep(list(NULL), 2) + ) pd <- bind_rows(pd, new) pd - } diff --git a/R/rules-spacing.R b/R/rules-spacing.R index 86c39c4b3..16774d31e 100644 --- a/R/rules-spacing.R +++ b/R/rules-spacing.R @@ -27,7 +27,7 @@ remove_space_after_unary_pm <- function(pd_flat) { pm_after <- pd_flat$token %in% op_pm pd_flat$spaces[pm_after & (pd_flat$newlines == 0L) & - (dplyr::lag(pd_flat$token) %in% op_pm_unary_after)] <- 0L + (dplyr::lag(pd_flat$token) %in% op_pm_unary_after)] <- 0L pd_flat } @@ -48,7 +48,8 @@ fix_quotes <- function(pd_flat) { vapply( lapply(pd_flat$text[str_const][str_const_change], parse_text), deparse, - character(1L)) + character(1L) + ) pd_flat } @@ -108,7 +109,7 @@ remove_space_before_comma <- function(pd_flat) { comma_after <- pd_flat$token == "','" if (!any(comma_after)) return(pd_flat) comma_before <- lead(comma_after, default = FALSE) - idx <- comma_before & (pd_flat$newlines == 0L) + idx <- comma_before & (pd_flat$newlines == 0L) pd_flat$spaces[idx] <- 0L pd_flat } @@ -149,11 +150,13 @@ start_comments_with_space <- function(pd, force_one = FALSE) { comments <- pd[comment_pos, ] - non_comments <-pd[pd$token != "COMMENT", ] + non_comments <- pd[pd$token != "COMMENT", ] - comments <- extract(comments, text, - c("prefix", "space_after_prefix", "text"), - regex = "^(#+'*)( *)(.*)$") + comments <- extract( + comments, text, + c("prefix", "space_after_prefix", "text"), + regex = "^(#+'*)( *)(.*)$" + ) comments$space_after_prefix <- nchar( comments$space_after_prefix, type = "width" ) @@ -183,7 +186,6 @@ set_space_before_comments <- function(pd_flat) { comment_before <- lead(comment_after, default = FALSE) pd_flat$spaces[comment_before & (pd_flat$newlines == 0L)] <- 1L pd_flat - } remove_space_after_excl <- function(pd_flat) { @@ -229,5 +231,4 @@ set_space_between_eq_sub_and_comma <- function(pd) { op_before <- which(pd$token == "EQ_SUB" & lead(pd$token == "','")) pd$spaces[op_before] <- 1L pd - } diff --git a/R/serialize.R b/R/serialize.R index 1e94fa78d..ad0e990dd 100644 --- a/R/serialize.R +++ b/R/serialize.R @@ -10,10 +10,10 @@ serialize_parse_data_flattened <- function(flattened_pd) { map(lag_newlines, add_newlines), map(lag_spaces, add_spaces), text, - collapse = "")) %>% + collapse = "" + ) + ) %>% .[["text_ws"]] %>% strsplit("\n", fixed = TRUE) %>% .[[1L]] - - } diff --git a/R/serialized_tests.R b/R/serialized_tests.R index 8a49afef2..08f1cab96 100644 --- a/R/serialized_tests.R +++ b/R/serialized_tests.R @@ -29,12 +29,15 @@ test_collection <- function(test, sub_test = NULL, path <- rprojroot::find_testthat_root_file(test) pattern <- if_else(!is.null(sub_test), - paste0("^", sub_test, ".*", "in\\.R$"), - "in\\.R$") + paste0("^", sub_test, ".*", "in\\.R$"), + "in\\.R$" + ) - in_names <- list.files(file.path(path), - pattern = pattern, - full.names = FALSE) + in_names <- list.files( + file.path(path), + pattern = pattern, + full.names = FALSE + ) if (length(in_names) < 1) stop("no items to check") @@ -45,12 +48,14 @@ test_collection <- function(test, sub_test = NULL, out_trees <- construct_tree(in_items) - pwalk(list(in_items, out_items, in_names, out_names, out_trees), - transform_and_check, - transformer = transformer, - write_back = write_back, - write_tree = write_tree, - ...) + pwalk( + list(in_items, out_items, in_names, out_names, out_trees), + transform_and_check, + transformer = transformer, + write_back = write_back, + write_tree = write_tree, + ... + ) } #' Construct *-out.R from a *-in.R @@ -93,7 +98,6 @@ transform_and_check <- function(in_item, out_item, transformer, write_back, write_tree = FALSE, out_tree = "_tree", ...) { - read_in <- utf8::read_lines_enc(in_item) if (write_tree) { create_tree(read_in) %>% @@ -101,17 +105,23 @@ transform_and_check <- function(in_item, out_item, } transformed <- read_in %>% transformer(...) - transformed <- suppressMessages(utf8::transform_lines_enc(out_item, + transformed <- suppressMessages(utf8::transform_lines_enc( + out_item, function(x) transformed, - write_back = write_back)) + write_back = write_back + )) if (transformed) { target <- utf8::read_lines_enc(out_item) - warning(in_name, " was different from ", out_name, - immediate. = TRUE, call. = FALSE) + warning( + in_name, " was different from ", out_name, + immediate. = TRUE, call. = FALSE + ) } else { - message(in_name, " was identical to ", out_name, - immediate. = TRUE, call. = FALSE) + message( + in_name, " was identical to ", out_name, + immediate. = TRUE, call. = FALSE + ) } } @@ -137,10 +147,10 @@ NULL style_empty <- function(text) { transformers <- list( # transformer functions - filler = create_filler, + filler = create_filler, line_break = NULL, - space = NULL, - token = NULL, + space = NULL, + token = NULL, # transformer options use_raw_indention = FALSE, @@ -153,13 +163,12 @@ style_empty <- function(text) { #' @describeIn test_transformer Transformations for indention based on curly #' brackets only. style_indent_curly <- function(text) { - transformers <- list( # transformer functions - filler = create_filler, + filler = create_filler, line_break = NULL, - space = partial(indent_curly, indent_by = 2), - token = NULL, + space = partial(indent_curly, indent_by = 2), + token = NULL, # transformer options use_raw_indention = FALSE, @@ -175,11 +184,13 @@ style_indent_curly <- function(text) { style_indent_curly_round <- function(text) { transformers <- list( # transformer functions - filler = create_filler, + filler = create_filler, line_break = NULL, - space = c(partial(indent_curly, indent_by = 2), - partial(indent_round, indent_by = 2)), - token = NULL, + space = c( + partial(indent_curly, indent_by = 2), + partial(indent_round, indent_by = 2) + ), + token = NULL, # transformer options use_raw_indention = FALSE, @@ -192,13 +203,12 @@ style_indent_curly_round <- function(text) { #' @describeIn test_transformer Transformations for indention based on operators style_op <- function(text) { - transformers <- list( # transformer functions - filler = create_filler, + filler = create_filler, line_break = NULL, - space = partial(indent_op, indent_by = 2), - token = NULL, + space = partial(indent_op, indent_by = 2), + token = NULL, # transformer options use_raw_indention = FALSE, @@ -207,5 +217,4 @@ style_op <- function(text) { transformed_text <- parse_transform_serialize(text, transformers) transformed_text - } diff --git a/R/style_guides.R b/R/style_guides.R index 717eb77af..02c6d1ac6 100644 --- a/R/style_guides.R +++ b/R/style_guides.R @@ -50,7 +50,6 @@ tidyverse_style <- function(scope = "tokens", strict = TRUE, indent_by = 2, start_comments_with_one_space = FALSE) { - scope <- character_to_ordered( scope, c("none", "spaces", "indention", "line_breaks", "tokens") @@ -77,8 +76,10 @@ tidyverse_style <- function(scope = "tokens", set_space_after_bang_bang, remove_space_before_dollar, remove_space_around_colons, - partial(start_comments_with_space, - force_one = start_comments_with_one_space), + partial( + start_comments_with_space, + force_one = start_comments_with_one_space + ), remove_space_after_unary_pm_nested, set_space_before_comments, @@ -120,13 +121,12 @@ tidyverse_style <- function(scope = "tokens", NULL ) - create_style_guide( - # transformer functions - filler = create_filler, - line_break = line_break_manipulators, - space = space_manipulators, - token = token_manipulators, - indention = indention_modifier, + create_style_guide( # transformer functions + filler = create_filler, + line_break = line_break_manipulators, + space = space_manipulators, + token = token_manipulators, + indention = indention_modifier, # transformer options use_raw_indention = use_raw_indention ) @@ -178,9 +178,10 @@ create_style_guide <- function(filler = create_filler, #' construction of the factor fails. character_to_ordered <- function(x, levels, name = substitute(x)) { if (!all((x %in% levels))) { - stop("all values in ", name, " must be one of the following: ", - paste(levels, collapse = ", ")) + stop( + "all values in ", name, " must be one of the following: ", + paste(levels, collapse = ", ") + ) } factor(x, levels = levels, ordered = TRUE) } - diff --git a/R/transform.R b/R/transform.R index 0ba92a53e..917b1c764 100644 --- a/R/transform.R +++ b/R/transform.R @@ -72,7 +72,6 @@ make_transformer <- function(transformers) { function(text) { transformed_text <- parse_transform_serialize(text, transformers) transformed_text - } } @@ -109,8 +108,10 @@ parse_transform_serialize <- function(text, transformers) { apply_transformers <- function(pd_nested, transformers) { transformed_line_breaks <- pre_visit( pd_nested, - c(transformers$filler, - transformers$line_break) + c( + transformers$filler, + transformers$line_break + ) ) transformed_updated_multi_line <- post_visit( @@ -132,5 +133,4 @@ apply_transformers <- function(pd_nested, transformers) { ) transformed_absolute_indent - } diff --git a/R/unindent.R b/R/unindent.R index e5e59dd9f..897075c1c 100644 --- a/R/unindent.R +++ b/R/unindent.R @@ -22,9 +22,11 @@ set_unindention_child <- function(pd, token = "')'", unindent_by) { non_candidates <- pd[-cand_ind, ] - candidates$child <- map(candidates$child, - unindent_child, - unindent_by = abs(pd$indent[closing] - pd$indent[closing-1])) + candidates$child <- map( + candidates$child, + unindent_child, + unindent_by = abs(pd$indent[closing] - pd$indent[closing - 1]) + ) bind_rows(candidates, non_candidates) %>% arrange(line1, col1) @@ -40,7 +42,7 @@ unindent_child <- function(pd, token = c("')'", "'}'"), unindent_by = 2) { if (!("indent" %in% names(pd))) { pd$indent <- 0 } - if ((length(closing) > 0) && (closing == nrow(pd))) { + if ((length(closing) > 0) && (closing == nrow(pd))) { pd$indent[closing] <- pd$indent[closing] - unindent_by } pd diff --git a/R/visit.R b/R/visit.R index d216c1dd6..79ee0f658 100644 --- a/R/visit.R +++ b/R/visit.R @@ -40,8 +40,10 @@ post_visit <- function(pd_nested, funs) { #' @family visitors #' @importFrom purrr reduce visit_one <- function(pd_flat, funs) { - reduce(funs, function(x, fun) fun(x), - .init = pd_flat) + reduce( + funs, function(x, fun) fun(x), + .init = pd_flat + ) } @@ -59,19 +61,22 @@ context_to_terminals <- function(pd_nested, outer_indent, outer_spaces, outer_indention_refs) { - if (is.null(pd_nested)) return() pd_transformed <- context_towards_terminals( pd_nested, outer_lag_newlines, outer_indent, outer_spaces, outer_indention_refs ) - pd_transformed$child <- pmap(list(pd_transformed$child, - pd_transformed$lag_newlines, - pd_transformed$indent, - pd_transformed$spaces, - pd_transformed$indention_ref_id), - context_to_terminals) + pd_transformed$child <- pmap( + list( + pd_transformed$child, + pd_transformed$lag_newlines, + pd_transformed$indent, + pd_transformed$spaces, + pd_transformed$indention_ref_id + ), + context_to_terminals + ) pd_transformed } @@ -172,11 +177,10 @@ enrich_terminals <- function(flattened_pd, use_raw_indention = FALSE) { choose_indention <- function(flattened_pd, use_raw_indention) { if (!use_raw_indention) { flattened_pd$lag_spaces <- ifelse(flattened_pd$lag_newlines > 0, - flattened_pd$indent, - flattened_pd$lag_spaces) + flattened_pd$indent, + flattened_pd$lag_spaces + ) } flattened_pd$indent <- NULL flattened_pd } - - diff --git a/R/ws.R b/R/ws.R index 44252be97..ca6035847 100644 --- a/R/ws.R +++ b/R/ws.R @@ -78,7 +78,6 @@ style_text <- function(text, ..., style = tidyverse_style, transformers = style(...)) { - transformer <- make_transformer(transformers) transformer(text) } @@ -113,7 +112,6 @@ style_dir <- function(path = ".", prettify_any <- function(transformers, recursive) { files <- dir(path = ".", pattern = "[.][rR]$", recursive = recursive, full.names = TRUE) transform_files(files, transformers) - } #' Style a file diff --git a/tests/testthat/test-indention_curly.R b/tests/testthat/test-indention_curly.R index b646033f9..a359f9b9e 100644 --- a/tests/testthat/test-indention_curly.R +++ b/tests/testthat/test-indention_curly.R @@ -1,37 +1,45 @@ context("indent curly brackets") test_that("indention on one-liner curley only is not changed", { - expect_warning(test_collection("indention_curly_brackets", - "one_line_curly", - transformer = style_indent_curly), NA) - + expect_warning(test_collection( + "indention_curly_brackets", + "one_line_curly", + transformer = style_indent_curly + ), NA) }) test_that("indention with multi-line curley only is correct", { - expect_warning(test_collection("indention_curly_brackets", - "multi_line_curly_only", - transformer = style_indent_curly), NA) - + expect_warning(test_collection( + "indention_curly_brackets", + "multi_line_curly_only", + transformer = style_indent_curly + ), NA) }) test_that("indention with multi-line curley and round is correct", { - expect_warning(test_collection("indention_curly_brackets", - "multi_line_curly_round_only", - transformer = style_indent_curly_round), NA) - + expect_warning(test_collection( + "indention_curly_brackets", + "multi_line_curly_round_only", + transformer = style_indent_curly_round + ), NA) }) -test_that(paste("complete styling via top level api is correct", - "(round, curly, spacing)"), { - expect_warning(test_collection("indention_curly_brackets", - "multi_line_curly_round_spacing", - transformer = style_text), NA) - - expect_warning(test_collection("indention_curly_brackets", - "multi_line_curly_while_for_if_fun", - transformer = style_text), NA) +test_that(paste( + "complete styling via top level api is correct", + "(round, curly, spacing)" +), { + expect_warning(test_collection( + "indention_curly_brackets", + "multi_line_curly_round_spacing", + transformer = style_text + ), NA) + expect_warning(test_collection( + "indention_curly_brackets", + "multi_line_curly_while_for_if_fun", + transformer = style_text + ), NA) }) diff --git a/tests/testthat/test-indention_multiple.R b/tests/testthat/test-indention_multiple.R index a2923d478..7bd603f16 100644 --- a/tests/testthat/test-indention_multiple.R +++ b/tests/testthat/test-indention_multiple.R @@ -1,45 +1,53 @@ context("test indent multiple") test_that("multiple round brackets don't cause extraindention", { - expect_warning(test_collection("indention_multiple", - "round_only", - transformer = style_text), NA) - - expect_warning(test_collection("indention_multiple", - "round_closing_on_same_line", - transformer = style_text), NA) + expect_warning(test_collection( + "indention_multiple", + "round_only", + transformer = style_text + ), NA) + + expect_warning(test_collection( + "indention_multiple", + "round_closing_on_same_line", + transformer = style_text + ), NA) }) test_that("multiple curly brackets don't cause extraindention", { - expect_warning(test_collection("indention_multiple", - "curly_only", - transformer = style_text), NA) - + expect_warning(test_collection( + "indention_multiple", + "curly_only", + transformer = style_text + ), NA) }) test_that("multiple curly and round brackets don't cause extraindention", { - expect_warning(test_collection("indention_multiple", - "curly_and_round", - transformer = style_text), NA) - + expect_warning(test_collection( + "indention_multiple", + "curly_and_round", + transformer = style_text + ), NA) }) test_that("multiple curly and round brackets overall test", { - expect_warning(test_collection("indention_multiple", - "overall", - transformer = style_text, - write_back = TRUE), NA) - + expect_warning(test_collection( + "indention_multiple", + "overall", + transformer = style_text, + write_back = TRUE + ), NA) }) test_that("edge cases work", { - expect_warning(test_collection("indention_multiple", - "edge_", - transformer = style_text), NA) - + expect_warning(test_collection( + "indention_multiple", + "edge_", + transformer = style_text + ), NA) }) diff --git a/tests/testthat/test-indention_operators.R b/tests/testthat/test-indention_operators.R index 5e11a9ffa..a0ba19c16 100644 --- a/tests/testthat/test-indention_operators.R +++ b/tests/testthat/test-indention_operators.R @@ -1,43 +1,57 @@ context("indention operators") test_that("pipe is indended correctly", { - expect_warning(test_collection("indention_operators", - "pipe", - transformer = style_text, - write_back = TRUE), NA) + expect_warning(test_collection( + "indention_operators", + "pipe", + transformer = style_text, + write_back = TRUE + ), NA) }) test_that("mathematical operators are indended correctly", { - expect_warning(test_collection("indention_operators", - "plus_minus", - transformer = style_op), NA) + expect_warning(test_collection( + "indention_operators", + "plus_minus", + transformer = style_op + ), NA) - expect_warning(test_collection("indention_operators", - "multiply_divide", - transformer = style_op), NA) + expect_warning(test_collection( + "indention_operators", + "multiply_divide", + transformer = style_op + ), NA) }) test_that("while / for / if without curly brackets", { - expect_warning(test_collection("indention_operators", - "while_for_if_without_curly", - transformer = style_text), NA) + expect_warning(test_collection( + "indention_operators", + "while_for_if_without_curly", + transformer = style_text + ), NA) }) test_that("logical, special and EQ_SUB tokens are indented correctly", { - expect_warning(test_collection("indention_operators", - "logical_special", - transformer = style_text), NA) + expect_warning(test_collection( + "indention_operators", + "logical_special", + transformer = style_text + ), NA) - expect_warning(test_collection("indention_operators", - "eq", - transformer = style_text), NA) + expect_warning(test_collection( + "indention_operators", + "eq", + transformer = style_text + ), NA) }) test_that("dollar is indented and spaced correctly", { - expect_warning(test_collection("indention_operators", - "dollar", - transformer = style_text), NA) + expect_warning(test_collection( + "indention_operators", + "dollar", + transformer = style_text + ), NA) }) test_that( @@ -47,12 +61,16 @@ test_that( "indention_operators", "not_first_trigger", transformer = style_text ), - NA) -}) + NA + ) + } +) test_that("overall", { - expect_warning(test_collection("indention_operators", - "overall", - transformer = style_text), NA) + expect_warning(test_collection( + "indention_operators", + "overall", + transformer = style_text + ), NA) }) diff --git a/tests/testthat/test-indention_round_brackets.R b/tests/testthat/test-indention_round_brackets.R index 6a15cbcd9..49d18c0f6 100644 --- a/tests/testthat/test-indention_round_brackets.R +++ b/tests/testthat/test-indention_round_brackets.R @@ -1,21 +1,21 @@ context("Function calls with round brackets") test_that("one-line function call yields correct indention", { - expect_warning(test_collection("indention_round_brackets", - "one_line", - transformer = style_text), NA) - - + expect_warning(test_collection( + "indention_round_brackets", + "one_line", + transformer = style_text + ), NA) }) ## ............................................................................ test_that(paste("multi-line function call yields correct indention"), { - - expect_warning(test_collection("indention_round_brackets", - "multi_line", - transformer = style_text), NA) - + expect_warning(test_collection( + "indention_round_brackets", + "multi_line", + transformer = style_text + ), NA) }) ## ............................................................................ @@ -26,10 +26,9 @@ context("grouping arithmetic expressions with round brackets. ") # Does NOT cover indention by operators such as +" test_that("arithmetic grouping with braces yields correctly indention", { - - expect_warning(test_collection("indention_round_brackets", - "arithmetic", - transformer = style_text), NA) - - + expect_warning(test_collection( + "indention_round_brackets", + "arithmetic", + transformer = style_text + ), NA) }) diff --git a/tests/testthat/test-line_breaks_and_other.R b/tests/testthat/test-line_breaks_and_other.R index 3854b8d1e..19e8c2c6f 100644 --- a/tests/testthat/test-line_breaks_and_other.R +++ b/tests/testthat/test-line_breaks_and_other.R @@ -1,33 +1,44 @@ context("linebreaking added / removed correctly") test_that("other manipulations are correct", { - expect_warning(test_collection("line_breaks_and_other", "other", - transformer = style_text), NA) + expect_warning(test_collection( + "line_breaks_and_other", "other", + transformer = style_text + ), NA) }) test_that("line breaks involing curly brackets", { - expect_warning(test_collection("line_breaks_and_other", "curly", - transformer = style_text), NA) + expect_warning(test_collection( + "line_breaks_and_other", "curly", + transformer = style_text + ), NA) }) test_that("line breaks involing curly brackets", { - expect_warning(test_collection("line_breaks_and_other", "edge_comment_and_curly", - transformer = style_text), NA) + expect_warning(test_collection( + "line_breaks_and_other", "edge_comment_and_curly", + transformer = style_text + ), NA) }) test_that("adding and removing line breaks", { - expect_warning(test_collection("line_breaks_and_other", "if", - transformer = style_text), NA) + expect_warning(test_collection( + "line_breaks_and_other", "if", + transformer = style_text + ), NA) }) test_that("adding and removing line breaks", { - expect_warning(test_collection("line_breaks_and_other", "if", - transformer = style_text), NA) + expect_warning(test_collection( + "line_breaks_and_other", "if", + transformer = style_text + ), NA) }) test_that("no line break after %>% if next token is comment", { - expect_warning(test_collection("line_breaks_and_other", "pipe_and", - transformer = style_text), NA) + expect_warning(test_collection( + "line_breaks_and_other", "pipe_and", + transformer = style_text + ), NA) }) - diff --git a/tests/testthat/test-line_breaks_fun_call.R b/tests/testthat/test-line_breaks_fun_call.R index 32db8b837..c12160612 100644 --- a/tests/testthat/test-line_breaks_fun_call.R +++ b/tests/testthat/test-line_breaks_fun_call.R @@ -1,23 +1,31 @@ context("line breaks for function calls") test_that("line breaks work in general", { - expect_warning(test_collection("line_breaks_fun_call", - "token_dependent_mixed", - transformer = style_text), NA) + expect_warning(test_collection( + "line_breaks_fun_call", + "token_dependent_mixed", + transformer = style_text + ), NA) - expect_warning(test_collection("line_breaks_fun_call", - "token_dependent_complex", - transformer = style_text), NA) + expect_warning(test_collection( + "line_breaks_fun_call", + "token_dependent_complex", + transformer = style_text + ), NA) }) test_that("line breaks work with comments", { - expect_warning(test_collection("line_breaks_fun_call", - "token_dependent_comments", - transformer = style_text), NA) + expect_warning(test_collection( + "line_breaks_fun_call", + "token_dependent_comments", + transformer = style_text + ), NA) }) test_that("line breaks work with exceptions", { - expect_warning(test_collection("line_breaks_fun_call", - "switch_ifelse", - transformer = style_text), NA) + expect_warning(test_collection( + "line_breaks_fun_call", + "switch_ifelse", + transformer = style_text + ), NA) }) diff --git a/tests/testthat/test-multiple_expressions.R b/tests/testthat/test-multiple_expressions.R index 825b985b1..2af7a4fed 100644 --- a/tests/testthat/test-multiple_expressions.R +++ b/tests/testthat/test-multiple_expressions.R @@ -2,13 +2,17 @@ library("testthat") context("multiple expressions") test_that("simple multiple expressions are styled correctly", { - expect_warning(test_collection("multiple_expressions", - "two_simple", - transformer = style_text), NA) + expect_warning(test_collection( + "multiple_expressions", + "two_simple", + transformer = style_text + ), NA) }) test_that("complex multiple expressions are styled correctly", { - expect_warning(test_collection("multiple_expressions", - "three_complex", - transformer = style_text), NA) + expect_warning(test_collection( + "multiple_expressions", + "three_complex", + transformer = style_text + ), NA) }) diff --git a/tests/testthat/test-parse_comments.R b/tests/testthat/test-parse_comments.R index 549ead317..31ae4c8e1 100644 --- a/tests/testthat/test-parse_comments.R +++ b/tests/testthat/test-parse_comments.R @@ -2,41 +2,52 @@ library("testthat") context("correctly treats comments") test_that("spacing within comments is done correctly", { - expect_warning(test_collection("parse_comments", - "within_spacing_with_force", - transformer = style_text, - style = tidyverse_style, - start_comments_with_one_space = TRUE), NA) - - expect_warning(test_collection("parse_comments", - "within_spacing_without_force", - transformer = style_text, - style = tidyverse_style, - start_comments_with_one_space = FALSE), NA) - - expect_warning(test_collection("parse_comments", - "eol_eof_spaces", - transformer = style_text), NA) + expect_warning(test_collection( + "parse_comments", + "within_spacing_with_force", + transformer = style_text, + style = tidyverse_style, + start_comments_with_one_space = TRUE + ), NA) + + expect_warning(test_collection( + "parse_comments", + "within_spacing_without_force", + transformer = style_text, + style = tidyverse_style, + start_comments_with_one_space = FALSE + ), NA) + + expect_warning(test_collection( + "parse_comments", + "eol_eof_spaces", + transformer = style_text + ), NA) }) test_that("spacing before comments is done correctly", { - }) test_that("comments are treated corectly", { - expect_warning(test_collection("parse_comments", - "mixed", - transformer = style_empty), NA) - - expect_warning(test_collection("parse_comments", - "just_comments", - transformer = style_empty), NA) - - - expect_warning(test_collection("parse_comments", - "with_indention", - transformer = style_text, - write_back = TRUE), NA) + expect_warning(test_collection( + "parse_comments", + "mixed", + transformer = style_empty + ), NA) + + expect_warning(test_collection( + "parse_comments", + "just_comments", + transformer = style_empty + ), NA) + + + expect_warning(test_collection( + "parse_comments", + "with_indention", + transformer = style_text, + write_back = TRUE + ), NA) # top-level test with indention }) diff --git a/tests/testthat/test-public_api.R b/tests/testthat/test-public_api.R index 2d9e68e04..a1420f40b 100644 --- a/tests/testthat/test-public_api.R +++ b/tests/testthat/test-public_api.R @@ -19,5 +19,3 @@ test_that("styler can style file", { test_that("styler does not return error when there is no file to style", { expect_error(style_dir(paste0(base, "/xyzemptydir"), strict = FALSE), NA) }) - - diff --git a/tests/testthat/test-serialize_tests.R b/tests/testthat/test-serialize_tests.R index 6c65a58df..99cc58cf4 100644 --- a/tests/testthat/test-serialize_tests.R +++ b/tests/testthat/test-serialize_tests.R @@ -1,30 +1,48 @@ context("test testing functions") test_that("No files to compare returns error", { - expect_error(test_collection("serialize_tests", "xyz", - transformer = as_is),"no items") + expect_error(test_collection( + "serialize_tests", "xyz", + transformer = as_is + ), "no items") }) test_that("Can handle multiple in for one out file", { - expect_warning(test_collection("serialize_tests", "k2", - transformer = identity), - c("k2\\-another\\-in_file.*k2\\-out")) + expect_warning( + test_collection( + "serialize_tests", "k2", + transformer = identity + ), + c("k2\\-another\\-in_file.*k2\\-out") + ) - expect_warning(test_collection("serialize_tests", "k2", - transformer = identity), - c("k2\\-in.*k2\\-out")) + expect_warning( + test_collection( + "serialize_tests", "k2", + transformer = identity + ), + c("k2\\-in.*k2\\-out") + ) }) test_that("properly detects non-match", { - expect_warning(test_collection("serialize_tests", "k3", - transformer = identity, - write_back = FALSE), - "different") + expect_warning( + test_collection( + "serialize_tests", "k3", + transformer = identity, + write_back = FALSE + ), + "different" + ) }) test_that("properly detects match", { - expect_message(test_collection("serialize_tests", "correct", - transformer = identity), - "identical") + expect_message( + test_collection( + "serialize_tests", "correct", + transformer = identity + ), + "identical" + ) }) diff --git a/tests/testthat/test-spacing.R b/tests/testthat/test-spacing.R index 04bef8973..84ee4a7d6 100644 --- a/tests/testthat/test-spacing.R +++ b/tests/testthat/test-spacing.R @@ -3,11 +3,13 @@ context("spacing") test_that("curly braces", { expect_warning(test_collection( "spacing", "round", - transformer = style_text), NA) + transformer = style_text + ), NA) }) test_that(":, ::, and :::", { expect_warning(test_collection( "spacing", "colon", - transformer = style_text), NA) + transformer = style_text + ), NA) }) diff --git a/tests/testthat/test-start_line.R b/tests/testthat/test-start_line.R index fd3ffd87e..c0b319177 100644 --- a/tests/testthat/test-start_line.R +++ b/tests/testthat/test-start_line.R @@ -1,6 +1,8 @@ context("start token") test_that("leading spaces are preseved at start of text", { - expect_warning(test_collection("start_line", - transformer = style_empty, write_back = TRUE), NA) + expect_warning(test_collection( + "start_line", + transformer = style_empty, write_back = TRUE + ), NA) }) diff --git a/tests/testthat/test-strict.R b/tests/testthat/test-strict.R index c4075ca13..feeee81b3 100644 --- a/tests/testthat/test-strict.R +++ b/tests/testthat/test-strict.R @@ -4,14 +4,16 @@ test_that("can style example source file with strict = TRUE", { expect_warning(test_collection( "strict", "strict", transformer = style_text, - strict = TRUE), NA) + strict = TRUE + ), NA) }) test_that("can style example source file with strict = FALSE", { expect_warning(test_collection( "strict", "non_strict", transformer = style_text, - strict = FALSE), NA) + strict = FALSE + ), NA) }) test_that("removes space at EOL", { @@ -29,6 +31,8 @@ test_that("Space placed after 'if' and before '('", { }) test_that("space before comma is removed", { - expect_equal(style_text("c( 1, 16 , 333 , 33 , 1)"), - "c(1, 16, 333, 33, 1)") + expect_equal( + style_text("c( 1, 16 , 333 , 33 , 1)"), + "c(1, 16, 333, 33, 1)" + ) }) diff --git a/tests/testthat/test-tidyeval.R b/tests/testthat/test-tidyeval.R index 7362f8fb6..cab4adf7a 100644 --- a/tests/testthat/test-tidyeval.R +++ b/tests/testthat/test-tidyeval.R @@ -1,16 +1,22 @@ context("tidyeval") test_that("no spaces within bang-bang operator !!!", { - expect_warning(test_collection("tidyeval", "bang_bang", - transformer = style_text), NA) + expect_warning(test_collection( + "tidyeval", "bang_bang", + transformer = style_text + ), NA) }) test_that(":= has correct spacing", { - expect_warning(test_collection("tidyeval", "setting_var", - transformer = style_text), NA) + expect_warning(test_collection( + "tidyeval", "setting_var", + transformer = style_text + ), NA) }) test_that("Space before comma if preceding token is EQ_SUB", { - expect_warning(test_collection("tidyeval", "eq_sub", - transformer = style_text), NA) + expect_warning(test_collection( + "tidyeval", "eq_sub", + transformer = style_text + ), NA) }) diff --git a/tests/testthat/test-transformer-grouping.R b/tests/testthat/test-transformer-grouping.R index 79233f06d..330af2eec 100644 --- a/tests/testthat/test-transformer-grouping.R +++ b/tests/testthat/test-transformer-grouping.R @@ -3,14 +3,16 @@ context("grouped transformers") test_that("no indention manipulation but spaces manipulation", { expect_warning(test_collection( "transformer_grouping", "scope_spaces", - transformer = style_text, style = tidyverse_style, scope = "spaces"), NA) + transformer = style_text, style = tidyverse_style, scope = "spaces" + ), NA) }) test_that("no line-break manipulation", { expect_warning(test_collection( "transformer_grouping", "scope_indention", transformer = style_text, - style = tidyverse_style, scope = "indention"), NA) + style = tidyverse_style, scope = "indention" + ), NA) }) @@ -19,7 +21,8 @@ test_that("no token manipulation", { "transformer_grouping", "scope_line_breaks", transformer = style_text, style = tidyverse_style, - scope = "line_breaks"), NA) + scope = "line_breaks" + ), NA) }) test_that("no space manipulation", { @@ -27,7 +30,8 @@ test_that("no space manipulation", { "transformer_grouping", "scope_tokens", transformer = style_text, style = tidyverse_style, - scope = "tokens"), NA) + scope = "tokens" + ), NA) }) @@ -36,5 +40,6 @@ test_that("no manipulation at all", { "transformer_grouping", "scope_none", transformer = style_text, style = tidyverse_style, - scope= "none"), NA) + scope = "none" + ), NA) }) diff --git a/tests/testthat/test-unary.R b/tests/testthat/test-unary.R index 0972447ac..fe3d30ef2 100644 --- a/tests/testthat/test-unary.R +++ b/tests/testthat/test-unary.R @@ -1,15 +1,21 @@ context("no spaces before unary operator") test_that("no spaces before unary operator", { - expect_warning(test_collection("unary_spacing", - "unary_simple", - transformer = style_text), NA) + expect_warning(test_collection( + "unary_spacing", + "unary_simple", + transformer = style_text + ), NA) - expect_warning(test_collection("unary_spacing", - "unary_complex", - transformer = style_text), NA) + expect_warning(test_collection( + "unary_spacing", + "unary_complex", + transformer = style_text + ), NA) - expect_warning(test_collection("unary_spacing", - "unary_indention", - transformer = style_text), NA) + expect_warning(test_collection( + "unary_spacing", + "unary_indention", + transformer = style_text + ), NA) }) diff --git a/tests/testthat/test-unindention.R b/tests/testthat/test-unindention.R index bb1e4cd9c..9b7daa621 100644 --- a/tests/testthat/test-unindention.R +++ b/tests/testthat/test-unindention.R @@ -1,25 +1,32 @@ context("unindention") test_that("round brackets are unindented correctly", { - expect_warning(test_collection("unindention", - "mixed", - transformer = style_indent_curly_round, - write_back = TRUE), NA) + expect_warning(test_collection( + "unindention", + "mixed", + transformer = style_indent_curly_round, + write_back = TRUE + ), NA) }) test_that("tokens are not dropped in named vector", { - expect_warning(test_collection("unindention", - "vec", - transformer = style_text, - write_back = TRUE), NA) + expect_warning(test_collection( + "unindention", + "vec", + transformer = style_text, + write_back = TRUE + ), NA) }) -test_that(paste("if last token is multi-line and no line break precede,", - "unindention is correct"), { - expect_warning(test_collection("unindention", - "vec", - transformer = style_text, - write_back = TRUE), NA) +test_that(paste( + "if last token is multi-line and no line break precede,", + "unindention is correct" +), { + expect_warning(test_collection( + "unindention", + "vec", + transformer = style_text, + write_back = TRUE + ), NA) }) -