File: test-underscored.R

package info (click to toggle)
r-cran-tidyr 0.8.3-1
  • links: PTS, VCS
  • area: main
  • in suites:
  • size: 1,368 kB
  • sloc: cpp: 359; sh: 9; makefile: 2
file content (143 lines) | stat: -rw-r--r-- 4,094 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
context("Deprecated SE variants")

test_that("complete_()", {
  df <- tibble(x = 1:2, y = 1:2, z = 3:4)
  out <- complete_(df, list("x", ~y))
  expect_identical(nrow(out), 4L)
  expect_identical(out$z, c(3L, NA, NA, 4L))
})

test_that("drop_na_() ", {
  # Specifying (a) variable(s) considers only that variable(s)
  df <- tibble(x = c(1, 2, NA), y = c("a", NA, "b"))
  exp <- tibble(x = c(1, 2), y = c("a", NA))
  res <- tidyr::drop_na_(df, "x")
  expect_identical(res, exp)

  exp <- tibble(x = c(1), y = c("a"))
  res <- tidyr::drop_na(df, c("x", "y"))
  expect_identical(res, exp)

  # Empty call drops every row
  df <- tibble(x = c(1, 2, NA), y = c("a", NA, "b"))
  res <- tidyr::drop_na_(df, character())
  expect_identical(res, tibble(x = 1, y = "a"))
})

test_that("drop_na_() works with non-syntactic names", {
  df <- tibble(`non-syntactic` = 1)
  expect_identical(drop_na_(df, "non-syntactic"), drop_na(df, `non-syntactic`))
})

test_that("expand_()", {
  df <- data.frame(x = 1:2, y = 1:2)
  out <- expand_(df, list("x", ~y))
  expect_identical(names(out), c("x", "y"))
  expect_identical(nrow(out), 4L)
})

test_that("extract_()", {
  df <- data.frame(x = c("a.b", "a.d", "b.c"))
  out <- df %>% extract_("x", "A")
  expect_identical(out$A, c("a", "a", "b"))
})

test_that("fill_()", {
  df <- tibble(x = c(1, NA, NA))
  out <- fill_(df, "x")
  expect_identical(out$x, c(1, 1, 1))
})

test_that("fill_() works with non-syntactic names", {
  df <- tibble(`non-syntactic` = 1)
  expect_identical(fill_(df, "non-syntactic"), fill(df, `non-syntactic`))
})

test_that("gather_()", {
  df <- data.frame(x = 1:5, y = 6:10)
  out <- gather_(df, "key", "val", c("x", "y"))
  expect_identical(nrow(out), 10L)
  expect_identical(names(out), c("key", "val"))
})

test_that("gather_() works with non-syntactic names", {
  df <- tibble(`non-syntactic` = 1)
  expect_identical(
    gather(df, key, val, `non-syntactic`),
    gather_(df, "key", "val", "non-syntactic")
  )
  expect_identical(
    gather(df, `key space`, `val space`, `non-syntactic`),
    gather_(df, "key space", "val space", "non-syntactic")
  )
})

test_that("nest_()", {
  df <- tibble(x = c(1, 1, 1), y = 1:3)
  expect_identical(nest_(df, "y", "y"), nest(df, y, .key = y))
})

test_that("nest_() works with non-syntactic names", {
  df <- tibble(`x` = c(1, 1, 1), `non-syntactic` = 1:3)
  expect_identical(
    nest_(df, "non-syntactic", "non-syntactic"),
    nest(df, `non-syntactic`, .key = `non-syntactic`)
  )
})

test_that("separate_()", {
  df <- tibble(x = c(NA, "a b"))
  out <- separate_(df, "x", c("x", "y"))
  expect_identical(out$x, c(NA, "a"))
  expect_identical(out$y, c(NA, "b"))
})

test_that("separate() works with non-syntactic names", {
  df <- tibble(`non-syntactic` = "1,2")
  into <- c("non", "syntactic")
  expect_identical(
    separate_(df, "non-syntactic", into),
    separate(df, `non-syntactic`, into)
  )
})
test_that("separate_rows() works with non-syntactic names", {
  df <- tibble(`non-syntactic` = 1)
  expect_identical(
    separate_rows_(df, "non-syntactic"),
    separate_rows(df, `non-syntactic`)
  )
})
test_that("spread_()", {
  df1 <- data.frame(x = c("a", "b"), y = 1:2)
  df2 <- data.frame(x = c("b", "a"), y = 2:1)
  one <- spread_(df1, "x", "y")
  two <- spread_(df2, "x", "y") %>% dplyr::select(a, b) %>% dplyr::arrange(a, b)
  expect_identical(one, two)
})

test_that("spread_() works with non-syntactic names", {
  df <- tibble(`non-syntactic` = c("a", "b"), `non syntactic` = 1:2)
  expect_identical(
    spread(df, `non-syntactic`, `non syntactic`),
    spread_(df, "non-syntactic", "non syntactic")
  )
})

test_that("unite_()", {
  df <- tibble(x = "a", y = "b")
  out <- unite_(df, "z", c("x", "y"))
  expect_named(out, "z")
  expect_identical(out$z, "a_b")
})

test_that("unite_() works with non-syntactic names", {
  df <- tibble(x = 1, `non-syntactic` = 1)
  expect_identical(
    unite_(df, "x", "non-syntactic"),
    unite(df, x, `non-syntactic`)
  )
})
test_that("unnest_()", {
  df <- tibble(x = list(1, 2:3, 4:10))
  expect_identical(unnest_(df)$x, dbl(1:10))
})