File: test-accessors.R

package info (click to toggle)
r-cran-blob 1.2.1-2
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, bullseye
  • size: 180 kB
  • sloc: sh: 13; makefile: 2
file content (70 lines) | stat: -rw-r--r-- 1,701 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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
context("accessors")

test_that("subsetting blob returns blob", {
  x <- blob(!!!as.raw(1:5))
  expect_s3_class(x[1], "blob")
})

test_that("subsetting can return NA", {
  x <- blob(!!!as.raw(1:5))
  expect_identical(x[NA_integer_], blob(NULL))
  expect_identical(x[c(5, NA)], blob(as.raw(5L), NULL))
})

test_that("subset assignment works", {
  x <- blob(!!!as.raw(1:5))
  x[3] <- blob(raw(1))
  expect_identical(x, blob(!!!as.raw(c(1:2, 0L, 4:5))))
  x[[4]] <- raw(1)
  expect_identical(x, blob(!!!as.raw(c(1:2, 0L, 0L, 5L))))
  x[7] <- blob(raw(1))
  expect_identical(x, blob(!!!as.raw(c(1:2, 0L, 0L, 5L)), NULL, raw(1)))
})

test_that("can't insert objects of incorrect type", {
  x <- blob(!!!as.raw(1:5))

  expect_error(x[[1]] <- 1, class = "vctrs_error_incompatible", fixed = TRUE)
  expect_error(x[1] <- 1, class = "vctrs_error_incompatible", fixed = TRUE)
})

test_that("can insert raw or NULL", {
  x <- blob(!!!as.raw(1:4))

  x[[1]] <- as.raw(0)
  x[2] <- list(as.raw(0))
  x[[3]] <- NULL
  x[4] <- list(NULL)

  expect_equal(x, blob(as.raw(0), as.raw(0), NULL, NULL))
})

test_that("can combine", {
  expect_identical(
    c(blob(raw(4), raw(5)), blob(raw(7))),
    blob(raw(4), raw(5), raw(7))
  )
  expect_identical(
    # Doesn't work with c()
    vec_c(list(raw(4), raw(5)), blob(raw(7))),
    blob(raw(4), raw(5), raw(7))
  )
  expect_identical(
    vec_c(list(raw(7)), blob(raw(4), raw(5)), list(raw(7))),
    blob(raw(7), raw(4), raw(5), raw(7))
  )
  expect_identical(
    vec_c(NA, blob()),
    blob(NULL)
  )
  expect_identical(
    c(blob(), NA),
    blob(NULL)
  )
  expect_error(
    c(blob(raw(4), raw(5)), raw(7))
  )
  expect_error(
    c(blob(raw(4), raw(5)), 7)
  )
})