Skip to content

Commit ebdcf1d

Browse files
authored
refactor: use toString, expect_named, lengths, expect_null, fixed in 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
1 parent 19835f8 commit ebdcf1d

File tree

12 files changed

+35
-31
lines changed

12 files changed

+35
-31
lines changed

.lintr

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,9 @@
1-
linters: with_defaults(
2-
# lintr defaults: https://github.com/jimhester/lintr#available-linters
1+
linters: linters_with_defaults(
2+
# lintr defaults: https://lintr.r-lib.org/reference/default_linters.html
33
# the following setup changes/removes certain linters
44
assignment_linter = NULL, # do not force using <- for assignments
55
object_name_linter = object_name_linter(c("snake_case", "CamelCase")), # only allow snake case and camel case object names
66
cyclocomp_linter = NULL, # do not check function complexity
77
commented_code_linter = NULL, # allow code in comments
88
line_length_linter = line_length_linter(120)
99
)
10-

R/Callback.R

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -224,6 +224,8 @@ clbks = function(.keys) {
224224
#' Assertions for [Callback] class.
225225
#'
226226
#' @param callback ([Callback]).
227+
#' @param null_ok (`logical(1)`)\cr
228+
#' If `TRUE`, `NULL` is allowed.
227229
#'
228230
#' @return [Callback] | List of [Callback]s.
229231
#' @export

R/as_short_string.R

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -32,11 +32,11 @@ as_short_string = function(x, width = 30L, num_format = "%.4g") {
3232
string = sprintf("%s(0)", cl)
3333
} else {
3434
string = switch(cl,
35-
"numeric" = paste0(sprintf(num_format, x), collapse = ","),
36-
"integer" = paste0(as.character(x), collapse = ","),
37-
"logical" = paste0(as.character(x), collapse = ","),
38-
"character" = paste0(x, collapse = ","),
39-
"expression" = as.character(x),
35+
numeric = paste0(sprintf(num_format, x), collapse = ","),
36+
integer = paste0(as.character(x), collapse = ","),
37+
logical = paste0(as.character(x), collapse = ","),
38+
character = paste0(x, collapse = ","),
39+
expression = as.character(x),
4040
sprintf("<%s>", cl)
4141
)
4242
}
@@ -52,7 +52,7 @@ as_short_string = function(x, width = 30L, num_format = "%.4g") {
5252
}
5353
ns = names2(x, missing_val = "<unnamed>")
5454
ss = lapply(x, convert)
55-
paste0(paste0(ns, "=", ss), collapse = ", ")
55+
toString(paste0(ns, "=", ss))
5656
} else {
5757
convert(x)
5858
}

R/format_bib.R

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ cite_bib = function(..., bibentries = NULL, envir = parent.frame()) {
4949
})
5050

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

5555
paste0(str, collapse = " and ")

R/topo_sort.R

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ topo_sort = function(nodes) {
3333
nodes = copy(nodes) # copy ref to be sure
3434
n = nrow(nodes)
3535
# sort nodes with few parent to start
36-
o = order(map_int(nodes$parents, length), decreasing = FALSE)
36+
o = order(lengths(nodes$parents), decreasing = FALSE)
3737
nodes = nodes[o]
3838

3939
nodes$topo = nodes$depth = NA_integer_ # cols for topo-index and depth layer in sort

man/assert_callback.Rd

Lines changed: 3 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

tests/testthat/test_encapsulate.R

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -47,12 +47,12 @@ test_that("timeout", {
4747
res = encapsulate("evaluate", .f = f, .args = list(x = 1), .timeout = 1)
4848
expect_null(res$result)
4949
expect_true("error" %in% res$log$class)
50-
expect_true(any(grepl("time limit", res$log$msg)))
50+
expect_match(res$log$msg, "time limit", fixed = TRUE)
5151

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

5858

tests/testthat/test_insert.R

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ test_that("insert_named.list", {
88
x = remove_named(x, c("d", "e"))
99
expect_list(x, len = 3L)
1010
expect_set_equal(names(x), letters[1:3])
11-
expect_equal(x$d, NULL)
11+
expect_null(x$d)
1212

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

3333
x = remove_named(x, c("d", "e"))
3434
expect_environment(x, contains = letters[1:3])
35-
expect_equal(x$d, NULL)
35+
expect_null(x$d)
3636

3737
x = insert_named(new.env(), list(a = 1))
3838
expect_environment(x, contains = "a")
@@ -42,33 +42,33 @@ test_that("insert_named.environment", {
4242
test_that("insert_named.data.frame", {
4343
x = as.data.frame(named_list(letters[1:3], 1))
4444
x = insert_named(x, list(d = 1))
45-
expect_data_frame(x, nrows = 1, ncols = 4)
45+
expect_data_frame(x, nrows = 1L, ncols = 4L)
4646
expect_set_equal(names(x), letters[1:4])
4747
expect_equal(x$d, 1)
4848

4949
x = remove_named(x, c("d", "e"))
50-
expect_data_frame(x, nrows = 1, ncols = 3)
50+
expect_data_frame(x, nrows = 1L, ncols = 3L)
5151
expect_set_equal(names(x), letters[1:3])
52-
expect_equal(x$d, NULL)
52+
expect_null(x$d)
5353

5454
x = insert_named(data.frame(), list(a = 1))
55-
expect_data_frame(x, nrows = 1, ncols = 1)
55+
expect_data_frame(x, nrows = 1L, ncols = 1L)
5656
expect_equal(x$a, 1)
5757
})
5858

5959
test_that("insert_named.data.table", {
6060
x = as.data.table(named_list(letters[1:3], 1))
6161
x = insert_named(x, list(d = 1))
62-
expect_data_table(x, nrows = 1, ncols = 4)
62+
expect_data_table(x, nrows = 1L, ncols = 4L)
6363
expect_set_equal(names(x), letters[1:4])
6464
expect_equal(x$d, 1)
6565

6666
x = remove_named(x, c("d", "e"))
67-
expect_data_table(x, nrows = 1, ncols = 3)
67+
expect_data_table(x, nrows = 1L, ncols = 3L)
6868
expect_set_equal(names(x), letters[1:3])
69-
expect_equal(x$d, NULL)
69+
expect_null(x$d)
7070

7171
x = insert_named(data.table(), list(a = 1))
72-
expect_data_table(x, nrows = 1, ncols = 1)
72+
expect_data_table(x, nrows = 1L, ncols = 1L)
7373
expect_equal(x$a, 1)
7474
})

tests/testthat/test_map.R

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ test_that("imap", {
4242

4343
res = imap_chr(x, fun)
4444
expect_character(res, len = 2)
45-
expect_equal(names(res), c("a", "b"))
45+
expect_named(res, c("a", "b"))
4646
expect_equal(unname(res), c("a:1", "b:2"))
4747

4848
x = list(1L, 2L)
@@ -153,7 +153,7 @@ test_that("keep", {
153153
x = list(a = 1:3, b = c("a", "b"), c = runif(3))
154154
out = keep(x, is.numeric)
155155
expect_list(out, len = 2L)
156-
expect_equal(names(out), c("a", "c"))
156+
expect_named(out, c("a", "c"))
157157

158158
x = iris
159159
out = keep(x, is.numeric)
@@ -168,7 +168,7 @@ test_that("discard", {
168168
x = list(a = 1:3, b = c("a", "b"), c = runif(3))
169169
out = discard(x, is.numeric)
170170
expect_list(out, len = 1L)
171-
expect_equal(names(out), "b")
171+
expect_named(out, "b")
172172

173173
x = iris
174174
out = discard(x, is.numeric)

tests/testthat/test_names2.R

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,5 +7,5 @@ test_that("names2", {
77
expect_identical(names2(x, ""), c("a", rep.int("", 2)))
88

99
names(x) = letters[1:3]
10-
expect_identical(names(x), names2(x))
10+
expect_named(x, names2(x))
1111
})

tests/testthat/test_set_params.R

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ test_that("set_params works for ... with correct inputs", {
1111
.ps = paradox::ps(a = paradox::p_dbl(), b = paradox::p_dbl())
1212
.ps$values$a = 1
1313
set_params(.ps, b = 2, .insert = FALSE)
14-
expect_true(is.null(.ps$values$a))
14+
expect_null(.ps$values$a)
1515
expect_true(.ps$values$b == 2)
1616
.ps$values = list(a = 1)
1717
set_params(.ps, b = 2, .insert = TRUE)
@@ -23,7 +23,7 @@ test_that("set_params works for .values with correct inputs", {
2323
.ps = paradox::ps(a = paradox::p_dbl(), b = paradox::p_dbl())
2424
.ps$values$a = 1
2525
set_params(.ps, .values = list(b = 2), .insert = FALSE)
26-
expect_true(is.null(.ps$values$a))
26+
expect_null(.ps$values$a)
2727
expect_true(.ps$values$b == 2)
2828
.ps$values = list(a = 1)
2929
set_params(.ps, .values = list(b = 2), .insert = TRUE)
@@ -41,7 +41,7 @@ test_that("set_params works for .values and ... with correct inputs", {
4141

4242
.ps$values = list(a = 1)
4343
set_params(.ps, b = 2, .values = list(c = 3), .insert = FALSE)
44-
expect_true(is.null(.ps$values$a))
44+
expect_null(.ps$values$a)
4545
expect_true(.ps$values$b == 2)
4646
expect_true(.ps$values$c == 3)
4747
})

tests/testthat/test_strip_srcrefs.R

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,5 +2,5 @@ test_that("remove srcref from function", {
22
f = function(x) NULL
33
attr(f, "srcref") = "mock_srcrefs"
44
f_strip = strip_srcrefs(f)
5-
expect_true(is.null(attr(f_strip, "srcref")))
5+
expect_null(attr(f_strip, "srcref"))
66
})

0 commit comments

Comments
 (0)