File: test-utils.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 (46 lines) | stat: -rw-r--r-- 1,152 bytes parent folder | download | duplicates (2)
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
test_that(".coerce_to_dataframe works for matrices", {
  mat <- matrix(c(1, 2, 3, 11, 12, 13), nrow = 2, ncol = 3, byrow = TRUE)

  expect_equal(
    .coerce_to_dataframe(mat),
    data.frame(
      V1 = c(1, 11),
      V2 = c(2, 12),
      V3 = c(3, 13)
    )
  )
})

test_that(".coerce_to_dataframe works for vectors and list", {
  expect_equal(
    .coerce_to_dataframe(1:3),
    data.frame(data = 1:3)
  )

  expect_equal(
    .coerce_to_dataframe(c("a", "b", "c")),
    data.frame(data = c("a", "b", "c"), stringsAsFactors = FALSE)
  )

  expect_equal(
    .coerce_to_dataframe(list(var1 = 1:3, var2 = 4:6)),
    data.frame(var1 = 1:3, var2 = 4:6)
  )
})

test_that(".coerce_to_dataframe errors correctly if can't coerce", {
  expect_error(
    .coerce_to_dataframe(list(var1 = 1:3, var2 = 4:5)),
    regexp = "object that can be coerced"
  )
})

test_that(".is_sorted works", {
  expect_true(.is_sorted(1:3))
  expect_true(.is_sorted(c("a", "b", "c")))
  expect_true(.is_sorted(factor(c("a", "b", "c"))))

  expect_false(.is_sorted(c(1, 3, 2)))
  expect_false(.is_sorted(c("b", "a", "c")))
  expect_false(.is_sorted(factor(c("b", "a", "c"))))
})