Skip to content

Commit

Permalink
refactor: use toString, expect_named, lengths, expect_null, fixed in …
Browse files Browse the repository at this point in the history
…grepl (#109)

* refactor: use toString, expect_named, lengths, expect_null, fixed in grepl

* chore: update .lintr config

* tests: use expect_match

* docs: null_ok docs
  • Loading branch information
m-muecke authored Jun 10, 2024
1 parent 19835f8 commit ebdcf1d
Show file tree
Hide file tree
Showing 12 changed files with 35 additions and 31 deletions.
5 changes: 2 additions & 3 deletions .lintr
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
linters: with_defaults(
# lintr defaults: https://github.com/jimhester/lintr#available-linters
linters: linters_with_defaults(
# lintr defaults: https://lintr.r-lib.org/reference/default_linters.html
# the following setup changes/removes certain linters
assignment_linter = NULL, # do not force using <- for assignments
object_name_linter = object_name_linter(c("snake_case", "CamelCase")), # only allow snake case and camel case object names
cyclocomp_linter = NULL, # do not check function complexity
commented_code_linter = NULL, # allow code in comments
line_length_linter = line_length_linter(120)
)

2 changes: 2 additions & 0 deletions R/Callback.R
Original file line number Diff line number Diff line change
Expand Up @@ -224,6 +224,8 @@ clbks = function(.keys) {
#' Assertions for [Callback] class.
#'
#' @param callback ([Callback]).
#' @param null_ok (`logical(1)`)\cr
#' If `TRUE`, `NULL` is allowed.
#'
#' @return [Callback] | List of [Callback]s.
#' @export
Expand Down
12 changes: 6 additions & 6 deletions R/as_short_string.R
Original file line number Diff line number Diff line change
Expand Up @@ -32,11 +32,11 @@ as_short_string = function(x, width = 30L, num_format = "%.4g") {
string = sprintf("%s(0)", cl)
} else {
string = switch(cl,
"numeric" = paste0(sprintf(num_format, x), collapse = ","),
"integer" = paste0(as.character(x), collapse = ","),
"logical" = paste0(as.character(x), collapse = ","),
"character" = paste0(x, collapse = ","),
"expression" = as.character(x),
numeric = paste0(sprintf(num_format, x), collapse = ","),
integer = paste0(as.character(x), collapse = ","),
logical = paste0(as.character(x), collapse = ","),
character = paste0(x, collapse = ","),
expression = as.character(x),
sprintf("<%s>", cl)
)
}
Expand All @@ -52,7 +52,7 @@ as_short_string = function(x, width = 30L, num_format = "%.4g") {
}
ns = names2(x, missing_val = "<unnamed>")
ss = lapply(x, convert)
paste0(paste0(ns, "=", ss), collapse = ", ")
toString(paste0(ns, "=", ss))
} else {
convert(x)
}
Expand Down
2 changes: 1 addition & 1 deletion R/format_bib.R
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ cite_bib = function(..., bibentries = NULL, envir = parent.frame()) {
})

if (length(str) >= 3L) {
str = c(paste0(head(str, -1L), collapse = ", "), tail(str, 1L))
str = c(toString(head(str, -1L)), tail(str, 1L))
}

paste0(str, collapse = " and ")
Expand Down
2 changes: 1 addition & 1 deletion R/topo_sort.R
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ topo_sort = function(nodes) {
nodes = copy(nodes) # copy ref to be sure
n = nrow(nodes)
# sort nodes with few parent to start
o = order(map_int(nodes$parents, length), decreasing = FALSE)
o = order(lengths(nodes$parents), decreasing = FALSE)
nodes = nodes[o]

nodes$topo = nodes$depth = NA_integer_ # cols for topo-index and depth layer in sort
Expand Down
3 changes: 3 additions & 0 deletions man/assert_callback.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions tests/testthat/test_encapsulate.R
Original file line number Diff line number Diff line change
Expand Up @@ -47,12 +47,12 @@ test_that("timeout", {
res = encapsulate("evaluate", .f = f, .args = list(x = 1), .timeout = 1)
expect_null(res$result)
expect_true("error" %in% res$log$class)
expect_true(any(grepl("time limit", res$log$msg)))
expect_match(res$log$msg, "time limit", fixed = TRUE)

res = encapsulate("callr", .f = f, .args = list(x = 1), .timeout = 1)
expect_null(res$result)
expect_true("error" %in% res$log$class)
expect_true(any(grepl("time limit", res$log$msg)))
expect_match(res$log$msg, "time limit", fixed = TRUE)
})


Expand Down
20 changes: 10 additions & 10 deletions tests/testthat/test_insert.R
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ test_that("insert_named.list", {
x = remove_named(x, c("d", "e"))
expect_list(x, len = 3L)
expect_set_equal(names(x), letters[1:3])
expect_equal(x$d, NULL)
expect_null(x$d)

x = insert_named(list(), list(a = 1))
expect_list(x, len = 1L)
Expand All @@ -32,7 +32,7 @@ test_that("insert_named.environment", {

x = remove_named(x, c("d", "e"))
expect_environment(x, contains = letters[1:3])
expect_equal(x$d, NULL)
expect_null(x$d)

x = insert_named(new.env(), list(a = 1))
expect_environment(x, contains = "a")
Expand All @@ -42,33 +42,33 @@ test_that("insert_named.environment", {
test_that("insert_named.data.frame", {
x = as.data.frame(named_list(letters[1:3], 1))
x = insert_named(x, list(d = 1))
expect_data_frame(x, nrows = 1, ncols = 4)
expect_data_frame(x, nrows = 1L, ncols = 4L)
expect_set_equal(names(x), letters[1:4])
expect_equal(x$d, 1)

x = remove_named(x, c("d", "e"))
expect_data_frame(x, nrows = 1, ncols = 3)
expect_data_frame(x, nrows = 1L, ncols = 3L)
expect_set_equal(names(x), letters[1:3])
expect_equal(x$d, NULL)
expect_null(x$d)

x = insert_named(data.frame(), list(a = 1))
expect_data_frame(x, nrows = 1, ncols = 1)
expect_data_frame(x, nrows = 1L, ncols = 1L)
expect_equal(x$a, 1)
})

test_that("insert_named.data.table", {
x = as.data.table(named_list(letters[1:3], 1))
x = insert_named(x, list(d = 1))
expect_data_table(x, nrows = 1, ncols = 4)
expect_data_table(x, nrows = 1L, ncols = 4L)
expect_set_equal(names(x), letters[1:4])
expect_equal(x$d, 1)

x = remove_named(x, c("d", "e"))
expect_data_table(x, nrows = 1, ncols = 3)
expect_data_table(x, nrows = 1L, ncols = 3L)
expect_set_equal(names(x), letters[1:3])
expect_equal(x$d, NULL)
expect_null(x$d)

x = insert_named(data.table(), list(a = 1))
expect_data_table(x, nrows = 1, ncols = 1)
expect_data_table(x, nrows = 1L, ncols = 1L)
expect_equal(x$a, 1)
})
6 changes: 3 additions & 3 deletions tests/testthat/test_map.R
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ test_that("imap", {

res = imap_chr(x, fun)
expect_character(res, len = 2)
expect_equal(names(res), c("a", "b"))
expect_named(res, c("a", "b"))
expect_equal(unname(res), c("a:1", "b:2"))

x = list(1L, 2L)
Expand Down Expand Up @@ -153,7 +153,7 @@ test_that("keep", {
x = list(a = 1:3, b = c("a", "b"), c = runif(3))
out = keep(x, is.numeric)
expect_list(out, len = 2L)
expect_equal(names(out), c("a", "c"))
expect_named(out, c("a", "c"))

x = iris
out = keep(x, is.numeric)
Expand All @@ -168,7 +168,7 @@ test_that("discard", {
x = list(a = 1:3, b = c("a", "b"), c = runif(3))
out = discard(x, is.numeric)
expect_list(out, len = 1L)
expect_equal(names(out), "b")
expect_named(out, "b")

x = iris
out = discard(x, is.numeric)
Expand Down
2 changes: 1 addition & 1 deletion tests/testthat/test_names2.R
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,5 @@ test_that("names2", {
expect_identical(names2(x, ""), c("a", rep.int("", 2)))

names(x) = letters[1:3]
expect_identical(names(x), names2(x))
expect_named(x, names2(x))
})
6 changes: 3 additions & 3 deletions tests/testthat/test_set_params.R
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ test_that("set_params works for ... with correct inputs", {
.ps = paradox::ps(a = paradox::p_dbl(), b = paradox::p_dbl())
.ps$values$a = 1
set_params(.ps, b = 2, .insert = FALSE)
expect_true(is.null(.ps$values$a))
expect_null(.ps$values$a)
expect_true(.ps$values$b == 2)
.ps$values = list(a = 1)
set_params(.ps, b = 2, .insert = TRUE)
Expand All @@ -23,7 +23,7 @@ test_that("set_params works for .values with correct inputs", {
.ps = paradox::ps(a = paradox::p_dbl(), b = paradox::p_dbl())
.ps$values$a = 1
set_params(.ps, .values = list(b = 2), .insert = FALSE)
expect_true(is.null(.ps$values$a))
expect_null(.ps$values$a)
expect_true(.ps$values$b == 2)
.ps$values = list(a = 1)
set_params(.ps, .values = list(b = 2), .insert = TRUE)
Expand All @@ -41,7 +41,7 @@ test_that("set_params works for .values and ... with correct inputs", {

.ps$values = list(a = 1)
set_params(.ps, b = 2, .values = list(c = 3), .insert = FALSE)
expect_true(is.null(.ps$values$a))
expect_null(.ps$values$a)
expect_true(.ps$values$b == 2)
expect_true(.ps$values$c == 3)
})
2 changes: 1 addition & 1 deletion tests/testthat/test_strip_srcrefs.R
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,5 @@ test_that("remove srcref from function", {
f = function(x) NULL
attr(f, "srcref") = "mock_srcrefs"
f_strip = strip_srcrefs(f)
expect_true(is.null(attr(f_strip, "srcref")))
expect_null(attr(f_strip, "srcref"))
})

0 comments on commit ebdcf1d

Please sign in to comment.