1test_that("can select negatively (#2519)", {
2  withr::local_options(lifecycle_verbosity = "quiet")
3
4  expect_identical(select_(mtcars, ~ -cyl), mtcars[-2])
5})
6
7test_that("select yields proper names", {
8  withr::local_options(lifecycle_verbosity = "quiet")
9
10  expect_identical(names(select_(mtcars, ~ cyl:hp)), c("cyl", "disp", "hp"))
11})
12
13test_that("lazydots are named and arrange() doesn't fail (it assumes empty names)", {
14  withr::local_options(lifecycle_verbosity = "quiet")
15
16  dots <- compat_lazy_dots(list(), env(), "cyl")
17  expect_identical(names(dots), "")
18  expect_identical(arrange_(mtcars, "cyl"), arrange(mtcars, cyl))
19})
20
21test_that("mutate_each_() and summarise_each_() handle lazydots", {
22  withr::local_options(lifecycle_verbosity = "quiet")
23
24  cyl_chr <- mutate_each_(mtcars, list(as.character), "cyl")$cyl
25  expect_identical(cyl_chr, as.character(mtcars$cyl))
26
27  cyl_mean <- summarise_each_(mtcars, list(mean), "cyl")$cyl
28  expect_equal(cyl_mean, mean(mtcars$cyl))
29})
30
31test_that("select_vars_() handles lazydots", {
32  withr::local_options(lifecycle_verbosity = "quiet")
33
34  expect_identical(select_vars_(letters, c("a", "b")), set_names(c("a", "b")))
35  expect_identical(
36    select_vars_(letters, c("a", "b"), include = "c"),
37    set_names(c("c", "a", "b"))
38  )
39  expect_identical(
40    select_vars_(letters, c("a", "b"), exclude = "b"),
41    set_names(c("a"))
42  )
43})
44
45df <- tibble(
46  a = c(1:3, 2:3),
47  b = letters[c(1:4, 4L)]
48)
49
50test_that("arrange_ works", {
51  withr::local_options(lifecycle_verbosity = "quiet")
52
53  expect_equal(
54    arrange_(df, ~ -a),
55    arrange(df, -a)
56  )
57
58  expect_equal(
59    arrange_(df, .dots = list(quote(-a))),
60    arrange(df, -a)
61  )
62
63  expect_equal(
64    arrange_(df, .dots = list(~ -a)),
65    arrange(df, -a)
66  )
67})
68
69test_that("count_() works", {
70  withr::local_options(lifecycle_verbosity = "quiet")
71
72  expect_equal(
73    count_(df, ~ b),
74    count(df, b)
75  )
76
77  expect_equal(
78    count_(df, ~ b, wt = quote(a)),
79    count(df, b, wt = a)
80  )
81
82  wt <- 1:4
83  expect_identical(
84    count_(df, "b", "wt"),
85    count(df, b, wt = wt)
86  )
87
88  expect_identical(
89    add_count(df, b),
90    add_count_(df, ~ b)
91  )
92})
93
94test_that("distinct_() works", {
95  withr::local_options(lifecycle_verbosity = "quiet")
96
97  expect_equal(
98    distinct_(df, ~ a),
99    distinct(df, a)
100  )
101
102  expect_equal(
103    distinct_(df, .dots = list(quote(a))),
104    distinct(df, a)
105  )
106
107  expect_equal(
108    distinct_(df, .dots = list(~ a)),
109    distinct(df, a)
110  )
111
112  expect_equal(
113    distinct_(df %>% group_by(b), ~ a, .dots = NULL),
114    distinct(df %>% group_by(b), a)
115  )
116
117  expect_equal(
118    distinct_(df %>% group_by(b), .dots = list(quote(a))),
119    distinct(df %>% group_by(b), a)
120  )
121
122  expect_equal(
123    distinct_(df %>% group_by(b), .dots = list(~ a)),
124    distinct(df %>% group_by(b), a)
125  )
126})
127
128test_that("do_() works", {
129  withr::local_options(lifecycle_verbosity = "quiet")
130
131  expect_equal(
132    do_(df, ~ tibble(-.$a)),
133    do(df, tibble(-.$a))
134  )
135
136  expect_equal(
137    do_(df, .dots = list(quote(dplyr::tibble(-.$a)))),
138    do(df, tibble(-.$a))
139  )
140
141  expect_equal(
142    do_(df, .dots = list(~ dplyr::tibble(-.$a))),
143    do(df, tibble(-.$a))
144  )
145
146  foo <- "foobar"
147  expect_identical(
148    do_(df, .dots = "tibble(foo)"),
149    do(df, tibble(foo))
150  )
151
152  expect_equal(
153    do_(df %>% group_by(b), ~ tibble(-.$a)),
154    do(df %>% group_by(b), tibble(-.$a))
155  )
156
157  expect_equal(
158    do_(df %>% group_by(b), .dots = list(quote(dplyr::tibble(-.$a)))),
159    do(df %>% group_by(b), tibble(-.$a))
160  )
161
162  expect_equal(
163    do_(df %>% group_by(b), .dots = list(~ dplyr::tibble(-.$a))),
164    do(df %>% group_by(b), tibble(-.$a))
165  )
166})
167
168test_that("filter_() works", {
169  withr::local_options(lifecycle_verbosity = "quiet")
170
171  expect_equal(
172    filter_(df, ~ a > 1),
173    filter(df, a > 1)
174  )
175
176  expect_equal(
177    filter_(df, .dots = list(quote(a > 1))),
178    filter(df, a > 1)
179  )
180
181  cnd <- rep(TRUE, 5)
182  expect_identical(
183    filter_(df, .dots = "cnd"),
184    filter(df, cnd)
185  )
186})
187
188test_that("group_by_() works", {
189  withr::local_options(lifecycle_verbosity = "quiet")
190
191  expect_equal(
192    group_by_(df, ~ a),
193    group_by(df, a)
194  )
195
196  expect_equal(
197    group_by_(df, ~ -a),
198    group_by(df, -a)
199  )
200
201  expect_equal(
202    group_by_(df, .dots = "a"),
203    group_by(df, a)
204  )
205
206  expect_equal(
207    group_by_(df, .dots = list(quote(-a))),
208    group_by(df, -a)
209  )
210
211  expect_equal(
212    group_by_(df, .dots = list(~ -a)),
213    group_by(df, -a)
214  )
215})
216
217test_that("mutate_() works", {
218  withr::local_options(lifecycle_verbosity = "quiet")
219
220  expect_equal(
221    mutate_(df, c = ~ -a),
222    mutate(df, c = -a)
223  )
224
225  expect_equal(
226    mutate_(df, .dots = list(c = quote(-a))),
227    mutate(df, c = -a)
228  )
229
230  expect_equal(
231    mutate_(df, .dots = list(c = ~ -a)),
232    mutate(df, c = -a)
233  )
234
235  expect_identical(
236    mutate_(df, ~ -a),
237    mutate(df, -a)
238  )
239
240  foo <- "foobar"
241  expect_identical(
242    mutate_(df, .dots = "foo"),
243    mutate(df, foo)
244  )
245})
246
247test_that("rename_() works", {
248  withr::local_options(lifecycle_verbosity = "quiet")
249
250  expect_equal(
251    rename_(df, c = ~ a),
252    rename(df, c = a)
253  )
254
255  expect_equal(
256    rename_(df, .dots = list(c = quote(a))),
257    rename(df, c = a)
258  )
259
260  expect_equal(
261    rename_(df, .dots = list(c = ~ a)),
262    rename(df, c = a)
263  )
264})
265
266test_that("select_() works", {
267  withr::local_options(lifecycle_verbosity = "quiet")
268
269  expect_equal(
270    select_(df, ~ a),
271    select(df, a)
272  )
273
274  expect_equal(
275    select_(df, ~ -a),
276    select(df, -a)
277  )
278
279  expect_equal(
280    select_(df, .dots = "a"),
281    select(df, a)
282  )
283
284  expect_equal(
285    select_(df, .dots = list(quote(-a))),
286    select(df, -a)
287  )
288
289  expect_equal(
290    select_(df, .dots = list(~ -a)),
291    select(df, -a)
292  )
293})
294
295test_that("slice_() works", {
296  withr::local_options(lifecycle_verbosity = "quiet")
297
298  expect_equal(
299    slice_(df, ~ 2:n()),
300    slice(df, 2:n())
301  )
302
303  expect_equal(
304    slice_(df, .dots = list(quote(2:n()))),
305    slice(df, 2:n())
306  )
307
308  expect_equal(
309    slice_(df, .dots = list(~ 2:n())),
310    slice(df, 2:n())
311  )
312
313  pos <- 3
314  expect_identical(
315    slice_(df, .dots = "pos:n()"),
316    slice(df, pos:n())
317  )
318})
319
320test_that("summarise_() works", {
321  withr::local_options(lifecycle_verbosity = "quiet")
322
323  expect_equal(
324    summarise_(df, a = ~ mean(a)),
325    summarise(df, a = mean(a))
326  )
327
328  expect_equal(
329    summarise_(df, .dots = list(a = quote(mean(a)))),
330    summarise(df, a = mean(a))
331  )
332
333  expect_equal(
334    summarise_(df, .dots = list(a = ~ mean(a))),
335    summarise(df, a = mean(a))
336  )
337
338  my_mean <- mean
339  expect_identical(
340    summarise_(df, .dots = c(a = "my_mean(a)")),
341    summarise(df, a = my_mean(a))
342  )
343
344  expect_equal(
345    summarise_(df %>% group_by(b), a = ~ mean(a)),
346    summarise(df %>% group_by(b), a = mean(a))
347  )
348
349  expect_equal(
350    summarise_(df %>% group_by(b), .dots = list(a = quote(mean(a)))),
351    summarise(df %>% group_by(b), a = mean(a))
352  )
353
354  expect_equal(
355    summarise_(df %>% group_by(b), .dots = list(a = ~ mean(a))),
356    summarise(df %>% group_by(b), a = mean(a))
357  )
358})
359
360test_that("summarize_() works", {
361  withr::local_options(lifecycle_verbosity = "quiet")
362
363  expect_equal(
364    summarize_(df, a = ~ mean(a)),
365    summarize(df, a = mean(a))
366  )
367
368  expect_equal(
369    summarize_(df, .dots = list(a = quote(mean(a)))),
370    summarize(df, a = mean(a))
371  )
372
373  expect_equal(
374    summarize_(df, .dots = list(a = ~ mean(a))),
375    summarize(df, a = mean(a))
376  )
377
378  expect_equal(
379    summarize_(df %>% group_by(b), a = ~ mean(a)),
380    summarize(df %>% group_by(b), a = mean(a))
381  )
382
383  expect_equal(
384    summarize_(df %>% group_by(b), .dots = list(a = quote(mean(a)))),
385    summarize(df %>% group_by(b), a = mean(a))
386  )
387
388  expect_equal(
389    summarize_(df %>% group_by(b), .dots = list(a = ~ mean(a))),
390    summarize(df %>% group_by(b), a = mean(a))
391  )
392})
393
394test_that("transmute_() works", {
395  withr::local_options(lifecycle_verbosity = "quiet")
396
397  expect_equal(
398    transmute_(df, c = ~ -a),
399    transmute(df, c = -a)
400  )
401
402  expect_equal(
403    transmute_(df, .dots = list(c = quote(-a))),
404    transmute(df, c = -a)
405  )
406
407  expect_equal(
408    transmute_(df, .dots = list(c = ~ -a)),
409    transmute(df, c = -a)
410  )
411
412  foo <- "foobar"
413  expect_identical(
414    transmute_(df, .dots = "foo"),
415    transmute(df, foo)
416  )
417})
418
419test_that("_each() and _all() families agree", {
420  withr::local_options(lifecycle_verbosity = "quiet")
421
422  df <- data.frame(x = 1:3, y = 1:3)
423
424  expect_equal(summarise_each(df, list(mean)), summarise_all(df, mean))
425  expect_equal(summarise_each(df, list(mean), x), summarise_at(df, vars(x), mean))
426  expect_equal(summarise_each(df, list(mean = mean), x), summarise_at(df, vars(x), list(mean = mean)))
427  expect_equal(summarise_each(df, list(mean = mean), x:y), summarise_at(df, vars(x:y), list(mean = mean)))
428  expect_equal(summarise_each(df, list(mean), x:y), summarise_at(df, vars(x:y), mean))
429  expect_equal(summarise_each(df, list(mean), z = y), summarise_at(df, vars(z = y), mean))
430
431  expect_equal(mutate_each(df, list(mean)), mutate_all(df, mean))
432  expect_equal(mutate_each(df, list(mean), x), mutate_at(df, vars(x), mean))
433  expect_equal(mutate_each(df, list(mean = mean), x), mutate_at(df, vars(x), list(mean = mean)))
434  expect_equal(mutate_each(df, list(mean = mean), x:y), mutate_at(df, vars(x:y), list(mean = mean)))
435  expect_equal(mutate_each(df, list(mean), x:y), mutate_at(df, vars(x:y), mean))
436  expect_equal(mutate_each(df, list(mean), z = y), mutate_at(df, vars(z = y), mean))
437})
438