File: test-utils_cols.R

package info (click to toggle)
r-cran-datawizard 1.0.1%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 2,300 kB
  • sloc: sh: 13; makefile: 2
file content (116 lines) | stat: -rw-r--r-- 2,471 bytes parent folder | download
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
test_char <- data.frame(
  a = c("iso", 2, 5),
  b = c("year", 3, 6),
  c = c(NA, 5, 7),
  stringsAsFactors = FALSE
)

test_num <- data.frame(
  a = c(5, 2, 5),
  b = c(3, 3, 6),
  c = c(NA, 5, 7)
)

test_na <- data.frame(
  a = c(NA, 2, 5),
  b = c(NA, 3, 6),
  c = c(NA, 5, 7)
)

test_that("row_to_colnames works", {
  test <- row_to_colnames(test_char, verbose = FALSE)
  expect_identical(
    colnames(test),
    c("iso", "year", "x1")
  )

  test <- row_to_colnames(test_num, verbose = FALSE)
  expect_identical(
    colnames(test),
    c("5", "3", "x1")
  )

  test <- row_to_colnames(test_na, verbose = FALSE)
  expect_identical(
    colnames(test),
    c("x1", "x2", "x3")
  )
})

test_that("row_to_colnames: check arg 'row'", {
  expect_error(
    row_to_colnames(test_num, row = "hi", verbose = FALSE),
    regexp = "Argument `row`"
  )
  expect_error(
    row_to_colnames(test_num, row = 6),
    regexp = "You used row = 6"
  )
  expect_error(
    row_to_colnames(test_num, row = c(3, 5), verbose = FALSE),
    regexp = "Argument `row`"
  )
  expect_identical(
    row_to_colnames(test_num, verbose = FALSE),
    row_to_colnames(test_num, row = 1, verbose = FALSE)
  )
})

test_that("row_to_colnames: check arg 'na_prefix'", {
  test <- row_to_colnames(test_char, na_prefix = "foo", verbose = FALSE)
  expect_identical(
    colnames(test),
    c("iso", "year", "foo1")
  )

  test <- row_to_colnames(test_num, na_prefix = "foo", verbose = FALSE)
  expect_identical(
    colnames(test),
    c("5", "3", "foo1")
  )
})

#-----------------------------------------------------

foo <- data.frame(
  ARG = c("BRA", "FRA"),
  `1960` = c(1960, 1960),
  `2000` = c(2000, 2000),
  stringsAsFactors = FALSE
)

test_that("colnames_to_row works", {
  test <- colnames_to_row(foo)
  expect_identical(
    colnames(test),
    c("x1", "x2", "x3")
  )
  expect_true(
    all(
      test[1, 1] == "ARG",
      test[1, 2] == "X1960",
      test[1, 3] == "X2000"
    )
  )
  expect_s3_class(test, "data.frame")
})

test_that("colnames_to_row: check arg 'prefix'", {
  test <- colnames_to_row(foo, prefix = "hi")
  expect_identical(
    colnames(test),
    c("hi1", "hi2", "hi3")
  )
  expect_error(
    colnames_to_row(test_num, prefix = 6),
    regexp = "Argument `prefix`"
  )
  expect_error(
    colnames_to_row(test_num, prefix = c("A", "B")),
    regexp = "Argument `prefix`"
  )
  expect_identical(
    colnames_to_row(test),
    colnames_to_row(test, prefix = "x")
  )
})