From 0008b2e9cfe8fd4c70bb2c0874510b41592dd604 Mon Sep 17 00:00:00 2001 From: Shiyu Chen Date: Fri, 19 Jul 2024 19:31:22 +0000 Subject: [PATCH] address lintr --- tests/testthat/test-derive_blfl.R | 68 +++++++++++-------- .../{test-derve_seq.R => test-derive_seq.R} | 6 +- tests/testthat/test-dtc_parse_dttm.R | 20 +++--- tests/testthat/test-dtc_utils.R | 44 ++++++------ tests/testthat/test-iso8601.R | 14 ++-- tests/testthat/test-parse_dttm_fmt.R | 5 +- tests/testthat/test-recode.R | 4 +- 7 files changed, 84 insertions(+), 77 deletions(-) rename tests/testthat/{test-derve_seq.R => test-derive_seq.R} (94%) diff --git a/tests/testthat/test-derive_blfl.R b/tests/testthat/test-derive_blfl.R index 42f64f2b..663f9bb4 100644 --- a/tests/testthat/test-derive_blfl.R +++ b/tests/testthat/test-derive_blfl.R @@ -129,17 +129,21 @@ test_that("derive_blfl DOMAIN validation works", { }) test_that("`dtc_datepart`: basic usage", { - expect_identical(dtc_datepart( - c(NA, "", "2021", "2021-12", "2021-12-25", "2021-12-25T12:00:00") - ), - c(NA, NA, NA, NA, "2021-12-25", "2021-12-25")) + expect_identical( + dtc_datepart( + c(NA, "", "2021", "2021-12", "2021-12-25", "2021-12-25T12:00:00") + ), + c(NA, NA, NA, NA, "2021-12-25", "2021-12-25") + ) ## Prevent partial or missing dates from being set to NA - expect_identical(dtc_datepart( - c(NA, "", "2021", "2021-12", "2021-12-25", "2021-12-25T12:00:00"), - partial_as_na = FALSE + expect_identical( + dtc_datepart( + c(NA, "", "2021", "2021-12", "2021-12-25", "2021-12-25T12:00:00"), + partial_as_na = FALSE ), - c(NA, "", "2021", "2021-12", "2021-12-25", "2021-12-25")) + c(NA, "", "2021", "2021-12", "2021-12-25", "2021-12-25") + ) }) test_that("`dtc_timepart`: basic usage", { @@ -156,32 +160,36 @@ test_that("`dtc_timepart`: basic usage", { ), c(NA, NA, NA, NA, "12:30", "12:30")) # Prevent partial or missing times from being set to NA - expect_identical(dtc_timepart( - c( - NA, - "", - "2021-12-25", - "2021-12-25T12", - "2021-12-25T12:30", - "2021-12-25T12:30:59" + expect_identical( + dtc_timepart( + c( + NA, + "", + "2021-12-25", + "2021-12-25T12", + "2021-12-25T12:30", + "2021-12-25T12:30:59" + ), + partial_as_na = FALSE ), - partial_as_na = FALSE - ), - c(NA, "", "", "12", "12:30", "12:30")) + c(NA, "", "", "12", "12:30", "12:30") + ) # Do not ignore seconds, partial or missing times set to NA - expect_identical(dtc_timepart( - c( - NA, - "", - "2021-12-25", - "2021-12-25T12", - "2021-12-25T12:30", - "2021-12-25T12:30:59" + expect_identical( + dtc_timepart( + c( + NA, + "", + "2021-12-25", + "2021-12-25T12", + "2021-12-25T12:30", + "2021-12-25T12:30:59" + ), + ignore_seconds = FALSE ), - ignore_seconds = FALSE - ), - c(NA, NA, NA, NA, NA, "12:30:59")) + c(NA, NA, NA, NA, NA, "12:30:59") + ) # Do not ignore seconds and prevent partial or missing times from being set to NA expect_identical( diff --git a/tests/testthat/test-derve_seq.R b/tests/testthat/test-derive_seq.R similarity index 94% rename from tests/testthat/test-derve_seq.R rename to tests/testthat/test-derive_seq.R index 58120b55..b70398d6 100644 --- a/tests/testthat/test-derve_seq.R +++ b/tests/testthat/test-derive_seq.R @@ -5,15 +5,17 @@ test_that("`derive_seq()` works as expected", { rec_vars <- c("STUDYID", "USUBJID", "VSTESTCD", "VSDTC", "VSTPTNUM") observed_vsseq <- derive_seq(tgt_dat = vs, tgt_var = "VSSEQ", rec_vars = rec_vars) + # nolint start expected_vsseq <- tibble::tribble( ~STUDYID, ~DOMAIN, ~USUBJID, ~VSSPID, ~VSTESTCD, ~VSDTC, ~VSTPTNUM, ~VSSEQ, "ABC123", "VS", "ABC123-375", "/F:VTLS1-D:9795532-R:2", "DIABP", "2020-09-01T13:31", NA, 1L, - "ABC123", "VS", "ABC123-375", "/F:VTLS2-D:9795533-R:2", "DIABP", "2020-09-28T11:00", 2, 2L, + "ABC123", "VS", "ABC123-375", "/F:VTLS2-D:9795533-R:2", "DIABP", "2020-09-28T11:00", 2L, 2L, "ABC123", "VS", "ABC123-375", "/F:VTLS1-D:9795532-R:2", "TEMP", "2020-09-01T13:31", NA, 3L, - "ABC123", "VS", "ABC123-375", "/F:VTLS2-D:9795533-R:2", "TEMP", "2020-09-28T11:00", 2, 4L, + "ABC123", "VS", "ABC123-375", "/F:VTLS2-D:9795533-R:2", "TEMP", "2020-09-28T11:00", 2L, 4L, "ABC123", "VS", "ABC123-376", "/F:VTLS1-D:9795591-R:1", "DIABP", "2020-09-20", NA, 1L, "ABC123", "VS", "ABC123-376", "/F:VTLS1-D:9795591-R:1", "TEMP", "2020-09-20", NA, 2L ) + # nolint end expect_identical(observed_vsseq, expected_vsseq) diff --git a/tests/testthat/test-dtc_parse_dttm.R b/tests/testthat/test-dtc_parse_dttm.R index 6a0f386b..06416d5e 100644 --- a/tests/testthat/test-dtc_parse_dttm.R +++ b/tests/testthat/test-dtc_parse_dttm.R @@ -22,8 +22,8 @@ test_that("`parse_dttm()` works as expected", { # Year, Month, Day expected_ymd <- tibble::tribble( - ~year, ~mon, ~mday, ~hour, ~min, ~sec, - "2020", "05", "11", NA, NA, NA + ~year, ~mon, ~mday, ~hour, ~min, ~sec, + "2020", "05", "11", NA, NA, NA ) |> as.matrix() @@ -59,7 +59,7 @@ test_that("`parse_dttm()` works as expected", { expected_ymdhm_1 <- tibble::tribble( ~year, ~mon, ~mday, ~hour, ~min, ~sec, "2002", "05", "11", "11", "45", NA, - NA, NA, NA, NA, NA, NA + NA, NA, NA, NA, NA, NA ) |> as.matrix() @@ -69,7 +69,7 @@ test_that("`parse_dttm()` works as expected", { expected_ymdhm_2 <- tibble::tribble( ~year, ~mon, ~mday, ~hour, ~min, ~sec, NA, NA, NA, NA, NA, NA, - NA, "05", "11", "11", "45", NA, + NA, "05", "11", "11", "45", NA ) |> as.matrix() @@ -81,19 +81,19 @@ test_that("`parse_dttm()` works as expected", { expected_ymdhm_3 <- tibble::tribble( ~year, ~mon, ~mday, ~hour, ~min, ~sec, "2002", "05", "11", "11", "45", NA, - NA, "05", "11", "11", "45", NA + NA, "05", "11", "11", "45", NA ) |> as.matrix() expect_identical( parse_dttm(c("2002-05-11 11:45", "-05-11 11:45"), c("y-m-d H:M", "-m-d H:M")), expected_ymdhm_3 - ) + ) # Different date formats expected_ymdhm_4 <- tibble::tribble( ~year, ~mon, ~mday, ~hour, ~min, ~sec, - "1985", "feb", "05", "12", "55", "02", + "1985", "feb", "05", "12", "55", "02" ) |> as.matrix() @@ -108,7 +108,7 @@ test_that("`parse_dttm()` works as expected", { ~year, ~mon, ~mday, ~hour, ~min, ~sec, "2020", "05", "18", NA, NA, NA, NA, NA, NA, NA, NA, NA, - NA, NA, NA, NA, NA, NA, + NA, NA, NA, NA, NA, NA ) |> as.matrix() @@ -118,7 +118,7 @@ test_that("`parse_dttm()` works as expected", { ~year, ~mon, ~mday, ~hour, ~min, ~sec, "2020", "05", "18", NA, NA, NA, "2020", "UN", "18", NA, NA, NA, - NA, NA, NA, NA, NA, NA, + NA, NA, NA, NA, NA, NA ) |> as.matrix() @@ -131,7 +131,7 @@ test_that("`parse_dttm()` works as expected", { ~year, ~mon, ~mday, ~hour, ~min, ~sec, "2020", "05", "18", NA, NA, NA, "2020", "UN", "18", NA, NA, NA, - "2020", "UNK", "UN", NA, NA, NA, + "2020", "UNK", "UN", NA, NA, NA ) |> as.matrix() diff --git a/tests/testthat/test-dtc_utils.R b/tests/testthat/test-dtc_utils.R index 7130c568..683e5a2f 100644 --- a/tests/testthat/test-dtc_utils.R +++ b/tests/testthat/test-dtc_utils.R @@ -25,18 +25,18 @@ test_that("`assert_capture_matrix()`: basic usage", { expect_identical(assert_capture_matrix(m), m) expect_error(assert_capture_matrix(character())) - expect_error(assert_capture_matrix(matrix(data = NA_character_, nrow = 0, ncol = 0))) - expect_error(assert_capture_matrix(matrix(data = NA_character_, nrow = 1))) + expect_error(assert_capture_matrix(matrix(data = NA_character_, nrow = 0L, ncol = 0L))) + expect_error(assert_capture_matrix(matrix(data = NA_character_, nrow = 1L))) }) test_that("`complete_capture_matrix()`: basic usage", { # Input with no cols and rows - input <- matrix(data = NA_character_, nrow = 0, ncol = 0) + input <- matrix(data = NA_character_, nrow = 0L, ncol = 0L) expected_output <- matrix( data = NA_character_, - nrow = 0, - ncol = 6, + nrow = 0L, + ncol = 6L, dimnames = list(NULL, c("year", "mon", "mday", "hour", "min", "sec")) ) @@ -44,11 +44,11 @@ test_that("`complete_capture_matrix()`: basic usage", { expected_output) # Input with no cols and 1 row - input <- matrix(data = NA_character_, nrow = 1) + input <- matrix(data = NA_character_, nrow = 1L) expected_output <- matrix( data = NA_character_, - nrow = 1, - ncol = 6, + nrow = 1L, + ncol = 6L, dimnames = list(NULL, c("year", "mon", "mday", "hour", "min", "sec")) ) @@ -59,15 +59,15 @@ test_that("`complete_capture_matrix()`: basic usage", { input <- matrix( NA_character_, - nrow = 1, - ncol = 2, + nrow = 1L, + ncol = 2L, dimnames = list(NULL, c("year", "sec")) ) expected_output <- matrix( data = NA_character_, - nrow = 1, - ncol = 6, + nrow = 1L, + ncol = 6L, dimnames = list(NULL, c("year", "mon", "mday", "hour", "min", "sec")) ) @@ -78,16 +78,16 @@ test_that("`complete_capture_matrix()`: basic usage", { input <- matrix( c("2020", "10"), - nrow = 1, - ncol = 2, + nrow = 1L, + ncol = 2L, dimnames = list(NULL, c("year", "sec")) ) expected_output <- matrix( - data = c("2020", rep(NA, 4), "10"), - nrow = 1, - ncol = 6, + data = c("2020", rep(NA, 4L), "10"), + nrow = 1L, + ncol = 6L, dimnames = list(NULL, c("year", "mon", "mday", "hour", "min", "sec")) ) @@ -98,15 +98,15 @@ test_that("`complete_capture_matrix()`: basic usage", { input <- matrix( c("2020", "10"), - nrow = 1, - ncol = 2, + nrow = 1L, + ncol = 2L, dimnames = list(NULL, c("semester", "quarter")) ) expected_output <- matrix( data = NA_character_, - nrow = 1, - ncol = 6, + nrow = 1L, + ncol = 6L, dimnames = list(NULL, c("year", "mon", "mday", "hour", "min", "sec")) ) @@ -151,7 +151,7 @@ test_that("`coalesce_capture_matrices()`: basic usage", { expected_output <- tibble::tribble( ~year, ~mon, ~mday, ~hour, ~min, ~sec, - NA, NA, NA, "10", "00", "05", + NA, NA, NA, "10", "00", "05" ) |> as.matrix() diff --git a/tests/testthat/test-iso8601.R b/tests/testthat/test-iso8601.R index 454eb295..f90619b0 100644 --- a/tests/testthat/test-iso8601.R +++ b/tests/testthat/test-iso8601.R @@ -48,7 +48,7 @@ test_that("`iso8601_year()`: basic usage", { expect_identical( iso8601_year(c("0", "1", "2", "50", "68", "69", "90", "99", "00")), c("2000", "2001", "2002", "2050", "2068", "1969", "1990", "1999", "2000") - ) + ) # By default, `cutoff_2000` is at 68. expect_identical( @@ -62,11 +62,11 @@ test_that("`iso8601_year()`: basic usage", { ) # Set cutoff_2000 to something else - expect_identical(iso8601_year(as.character(0:50), cutoff_2000 = 25), - as.character(c(2000:2025, 1926:1950))) + expect_identical(iso8601_year(as.character(0L:50L), cutoff_2000 = 25L), + as.character(c(2000L:2025L, 1926L:1950L))) - expect_identical(iso8601_year(as.character(1900:1950), cutoff_2000 = 25), - as.character(c(1900:1950))) + expect_identical(iso8601_year(as.character(1900L:1950L), cutoff_2000 = 25L), + as.character(c(1900L:1950L))) }) @@ -87,7 +87,7 @@ test_that("`iso8601_mon()`: basic usage", { mon <- month.abb expect_identical(iso8601_mon(mon), - "01", "02", "03", "04", "05", "06", "07", "08", "09", "10", "11", "12") + c("01", "02", "03", "04", "05", "06", "07", "08", "09", "10", "11", "12")) }) @@ -95,7 +95,7 @@ test_that("`iso8601_sec()`: basic usage", { expect_identical( iso8601_sec(c(NA, "0", "1", "10", "59", "99", "100")), - c( NA, "00", "01", "10", "59", "99", NA) + c(NA, "00", "01", "10", "59", "99", NA) ) }) diff --git a/tests/testthat/test-parse_dttm_fmt.R b/tests/testthat/test-parse_dttm_fmt.R index 942a11d8..cfbc440f 100644 --- a/tests/testthat/test-parse_dttm_fmt.R +++ b/tests/testthat/test-parse_dttm_fmt.R @@ -312,8 +312,7 @@ test_that("`parse_dttm_fmt` works as expected", { observed_cus <- parse_dttm_fmt("year month day", fmt_cmp(year = "year", mon = "month", - mday = "day") - ) + mday = "day")) expected_cus <- tibble::tribble( ~fmt_c, ~pat, ~cap, ~start, ~end, ~len, ~ord, @@ -326,5 +325,3 @@ test_that("`parse_dttm_fmt` works as expected", { expect_identical(observed_cus, expected_cus) }) - - diff --git a/tests/testthat/test-recode.R b/tests/testthat/test-recode.R index d74e70f8..c93f50a0 100644 --- a/tests/testthat/test-recode.R +++ b/tests/testthat/test-recode.R @@ -125,7 +125,7 @@ test_that("recode(): notable cases", { }) test_that("index_for_recode(): basic usage", { - expect_identical(index_for_recode(x = 1:5, from = c(2, 4)), - as.integer(c(NA, 1, NA, 2, NA))) + expect_identical(index_for_recode(x = 1L:5L, from = c(2L, 4L)), + as.integer(c(NA, 1L, NA, 2L, NA))) })