From 07f57ddbb94d2cb8e8399669e1933827516b6a5f Mon Sep 17 00:00:00 2001 From: James Baird Date: Mon, 24 Jul 2023 21:02:41 +0100 Subject: [PATCH] Add pipe_consistency tests --- tests/testthat/test-pipe-consistency-linter.R | 43 +++++++++++++++++++ 1 file changed, 43 insertions(+) create mode 100644 tests/testthat/test-pipe-consistency-linter.R diff --git a/tests/testthat/test-pipe-consistency-linter.R b/tests/testthat/test-pipe-consistency-linter.R new file mode 100644 index 000000000..7498aa388 --- /dev/null +++ b/tests/testthat/test-pipe-consistency-linter.R @@ -0,0 +1,43 @@ +test_that("pipe_consistency skips allowed usage", { + expect_lint("1:3 %>% mean() %>% as.character()", NULL, pipe_consistency_linter()) + expect_lint("1:3 |> mean() |> as.character()", NULL, pipe_consistency_linter()) + # With no pipes + expect_lint("x <- 1:5", NULL, pipe_consistency_linter()) + # Across multiple lines + expect_lint( + c("1:3 %>%", "mean() %>%", "as.character()"), + NULL, + pipe_consistency_linter() + ) +}) + +test_that("pipe_consistency lints inconsistent usage", { + expected_msg <- rex::rex("Use consistent pipe operators (either all %>% or all |>).") + expect_lint( + "1:3 |> mean() %>% as.character()", + list( + list(message = expected_msg, line_number = 1L, column_number = 5L), + list(message = expected_msg, line_number = 1L, column_number = 15L) + ), + pipe_consistency_linter() + ) + + expect_lint( + "1:3 %>% mean() |> as.character()", + list( + list(message = expected_msg, line_number = 1L, column_number = 5L), + list(message = expected_msg, line_number = 1L, column_number = 16L) + ), + pipe_consistency_linter() + ) + + # Across lines + expect_lint( + c("1:3 %>%", "mean() |>", "as.character()"), + list( + list(message = expected_msg, line_number = 1L, column_number = 5L), + list(message = expected_msg, line_number = 2L, column_number = 8L) + ), + pipe_consistency_linter() + ) +})