1
2test_that("wk_vctr class works", {
3  x <- structure(1:5, class = "wk_vctr")
4  expect_s3_class(x, "wk_vctr")
5  expect_s3_class(x[1:2], "wk_vctr")
6  expect_identical(
7    c(x, x),
8    structure(c(1:5, 1:5), class = "wk_vctr")
9  )
10  expect_output(print(x), "wk_vctr")
11  expect_output(print(stats::setNames(x, as.character(1:5))), "wk_vctr")
12  expect_output(print(x[0]), "wk_vctr")
13  expect_output(print(wk_set_crs(x, 1234)), "CRS=1234")
14  expect_output(expect_identical(str(x), x), "wk_vctr")
15  expect_output(expect_identical(str(x[0]), x[0]), "wk_vctr\\[0\\]")
16
17  old_opt <- options(max.print = 1000)
18  expect_output(
19    print(structure(1:1001, class = "wk_vctr")),
20    "Reached max.print"
21  )
22  options(old_opt)
23
24  x[[3]] <- 13L
25  expect_identical(unclass(x), c(1L, 2L, 13L, 4L, 5L))
26
27  expect_identical(
28    data.frame(col_name = x),
29    new_data_frame(list(col_name = x))
30  )
31  expect_error(as.data.frame(x), "cannot coerce")
32})
33
34test_that("rep() and rep_len() works for list wk_vctrs", {
35  expect_identical(
36    rep(structure(list(NULL), class = "wk_vctr"), 3),
37    structure(list(NULL, NULL, NULL), class = "wk_vctr")
38  )
39
40  expect_identical(
41    rep_len(structure(list(NULL), class = "wk_vctr"), 3),
42    structure(list(NULL, NULL, NULL), class = "wk_vctr")
43  )
44
45  expect_identical(
46    rep(structure(list(), class = "wk_vctr"), 3),
47    structure(list(), class = "wk_vctr")
48  )
49
50  expect_identical(
51    rep_len(structure(list(), class = "wk_vctr"), 3),
52    structure(list(NULL, NULL, NULL), class = "wk_vctr")
53  )
54})
55
56test_that("rep() and rep_len() works for chr wk_vctrs", {
57  expect_identical(
58    rep(structure(NA_character_, class = "wk_vctr"), 3),
59    structure(rep(NA_character_, 3), class = "wk_vctr")
60  )
61
62  expect_identical(
63    rep_len(structure(NA_character_, class = "wk_vctr"), 3),
64    structure(rep(NA_character_, 3), class = "wk_vctr")
65  )
66
67  expect_identical(
68    rep(structure(character(), class = "wk_vctr"), 3),
69    structure(character(), class = "wk_vctr")
70  )
71
72  expect_identical(
73    rep_len(structure(character(), class = "wk_vctr"), 3),
74    structure(rep(NA_character_, 3), class = "wk_vctr")
75  )
76})
77
78test_that("c() for wk_vctr handles crs attributes", {
79  expect_identical(
80    wk_crs(c(wkt("POINT (0 1)", crs = wk_crs_inherit()), wkt("POINT (0 2)", crs = 1234))),
81    1234
82  )
83
84  expect_error(
85    wk_crs(c(wkt("POINT (0 1)"), wkt("POINT (0 2)", crs = 1234))),
86    "are not equal"
87  )
88})
89
90test_that("wk_vctr objects with different subclasses can't be combined", {
91  expect_error(
92    c(as_wkt("POINT EMPTY"), as_wkb("POINT EMPTY")),
93    "Can't combine"
94  )
95})
96