Debian Codegraph @

tests/testthat/test-helpers-vector.R

raw
1 test_that("`all_of()` fails even if `.strict` is FALSE", {
2 expect_snapshot(
3 error = TRUE,
4 select_loc(letters2, all_of(c("a", "bar", "c")), strict = FALSE)
5 )
6 })
7
8 test_that("any_of() and all_of() preserve order (#186)", {
9 df <- data.frame(x = 1, y = 2)
10 expect_identical(select_loc(df, any_of(c("y", "x"))), c(y = 2L, x = 1L))
11 expect_identical(select_loc(df, all_of(c("y", "x"))), c(y = 2L, x = 1L))
12
13 df <- data.frame(x = 1, y = 2, z = 3)
14 expect_identical(
15 select_loc(df, any_of(c("y", "z", "y", "x", "d", "z"))),
16 c(y = 2L, z = 3L, x = 1L)
17 )
18 })
19
20 test_that("all_of() and any_of() handle named vectors", {
21 expect_identical(select_loc(letters2, all_of(c("a", foo = "b"))), c(a = 1L, foo = 2L))
22 expect_identical(select_loc(letters2, any_of(c("a", foo = "b", "bar"))), c(a = 1L, foo = 2L))
23 })
24
25 test_that("all_of() is strict", {
26 expect_error(select_loc(letters2, all_of(c("a", "foo"))), class = "vctrs_error_subscript_oob")
27 })
28
29 test_that("any_of() is lax", {
30 expect_identical(
31 select_loc(letters2, any_of(c("a", "foo"))),
32 select_loc(letters2, a)
33 )
34 expect_identical(
35 select_loc(letters2, -any_of(c("a", "foo"))),
36 select_loc(letters2, -a)
37 )
38 })
39
40 test_that("all_of() and any_of() check their inputs", {
41 expect_snapshot({
42 (expect_error(select_loc(letters2, all_of(NA))))
43 (expect_error(select_loc(letters2, any_of(NA))))
44
45 (expect_error(select_loc(letters2, all_of(TRUE))))
46 (expect_error(select_loc(letters2, any_of(TRUE))))
47
48 (expect_error(select_loc(letters2, any_of(is.factor))))
49 (expect_error(select_loc(letters2, all_of(is.factor))))
50 })
51 })
52
53 test_that("any_of() errors out of context", {
54 expect_snapshot({
55 (expect_error(any_of()))
56 })
57 })
58
59 test_that("all_of() is deprecated out of context (#269)", {
60 expect_snapshot(out <- all_of("x"))
61 expect_equal(out, "x")
62 })
63
64 test_that("any_of generates informative error if ... not empty", {
65 local_vars(letters)
66
67 expect_snapshot(error = TRUE, {
68 any_of("b", "c", "d")
69 })
70 })
71
72 test_that("all_of() returns an integer vector", {
73 with_vars(
74 letters,
75 expect_equal(all_of(c("b", "c", "d")), 2:4)
76 )
77 })