1context("cell specification")
2
3test_that("Cell range is converted to a cell_limit object and vice versa", {
4
5  rgA1 <- "A1:C4"
6  rgRC <- "R1C1:R4C3"
7  rgCL <- cell_limits(ul = c(1, 1), lr = c(4, 3))
8  expect_equal(as.cell_limits(rgA1), rgCL)
9  expect_equal(as.cell_limits(rgRC), rgCL)
10  expect_equal(as.range(rgCL), rgRC)
11  expect_equal(as.range(rgCL, fo = "A1"), rgA1)
12
13  rgA1sheet <- "sheet!A1:C4"
14  rgRCsheet <- "sheet!R1C1:R4C3"
15  rgCLwsn <- cell_limits(ul = c(1, 1), lr = c(4, 3), sheet = "sheet")
16  expect_equal(as.cell_limits(rgA1sheet), rgCLwsn)
17  expect_equal(as.cell_limits(rgRCsheet), rgCLwsn)
18  expect_equal(as.range(rgCLwsn), rgRCsheet)
19  expect_equal(as.range(rgCLwsn, sheet = FALSE), rgRC)
20  expect_equal(as.range(rgCLwsn, fo = "A1"), rgA1sheet)
21
22  rgA1 <- "E7"
23  rgA1A1 <- "E7:E7"
24  rgRC <- "R7C5"
25  rgRCRC <- "R7C5:R7C5"
26  rgCL <- cell_limits(ul = c(7, 5), lr = c(7, 5))
27  expect_equal(as.cell_limits(rgA1), rgCL)
28  expect_equal(as.cell_limits(rgRC), rgCL)
29  expect_equal(as.cell_limits(rgA1A1), rgCL)
30  expect_equal(as.cell_limits(rgRCRC), rgCL)
31  expect_equal(as.range(rgCL), rgRCRC)
32  expect_equal(as.range(rgCL, fo = "A1"), rgA1A1)
33
34  rgA1sheet <- "sheet!E7"
35  rgA1A1sheet <- "sheet!E7:E7"
36  rgRCsheet <- "sheet!R7C5"
37  rgRCRCsheet <- "sheet!R7C5:R7C5"
38  rgCLsheet <- cell_limits(ul = c(7, 5), lr = c(7, 5), sheet = "sheet")
39  expect_equal(as.cell_limits(rgA1sheet), rgCLsheet)
40  expect_equal(as.cell_limits(rgRCsheet), rgCLsheet)
41  expect_equal(as.cell_limits(rgA1A1sheet), rgCLsheet)
42  expect_equal(as.cell_limits(rgRCRCsheet), rgCLsheet)
43  expect_equal(as.range(rgCLsheet), rgRCRCsheet)
44  expect_equal(as.range(rgCLsheet, fo = "A1"), rgA1A1sheet)
45
46  rgCL <- cell_limits(ul = c(NA, 1), lr = c(4, NA))
47  expect_true(is.na(as.range(rgCL)))
48
49})
50
51test_that("Whitespace-containing sheet names gain/lose single quotes", {
52  x <- cell_limits(ul = c(1, 1), lr = c(4, 3), sheet = "aaa bbb")
53  expect_identical(as.range(x), "'aaa bbb'!R1C1:R4C3")
54  expect_identical(as.cell_limits("'aaa bbb'!R1C1:R4C3"), x)
55})
56
57test_that("Bad cell ranges throw errors", {
58
59  expect_warning(expect_error(as.cell_limits("eggplant")))
60  expect_warning(expect_error(as.cell_limits("A:B10")))
61  expect_warning(expect_error(as.cell_limits(":B10")))
62  expect_error(as.cell_limits("A1:R3C3"))
63  expect_error(as.cell_limits("A1:B2:C3"))
64  expect_warning(expect_error(as.cell_limits("14:17")))
65  expect_error(as.cell_limits(14:17))
66  expect_error(as.cell_limits(B2:D9))
67  expect_error(cell_limits(ul = c(-1, 1), lr = c(3, 4)))
68  expect_error(cell_limits(ul = c(0, 1), lr = c(3, 4)))
69  expect_error(cell_limits(ul = c(1, 4), lr = c(3, 1)))
70
71})
72
73test_that("Degenerate, all-NA input is tolerated", {
74
75  cl <- cell_limits()
76  expect_is(cl, "cell_limits")
77  expect_is(cl$ul, "integer")
78
79  cl2 <- cell_limits(c(NA, NA))
80  expect_identical(cl, cl2)
81
82  cl3 <- cell_limits(lr = c(NA, NA))
83  expect_identical(cl, cl3)
84
85})
86
87test_that("as.cell_limits can operate on NULL input", {
88
89  expect_identical(as.cell_limits(NULL), cell_limits())
90
91})
92
93test_that("cell_limits objects inherit from list", {
94
95  expect_is(cell_limits(), "list")
96
97})
98
99test_that("Row-only specifications work", {
100
101  expect_identical(cell_rows(c(NA, NA)), cell_limits())
102  expect_identical(cell_rows(c(NA, 3)), cell_limits(lr = c(3, NA)))
103  expect_identical(cell_rows(c(7, NA)), cell_limits(c(7, NA)))
104  expect_identical(cell_rows(c(3, NA, 10)), cell_limits(c(3, NA), c(10, NA)))
105  expect_identical(cell_rows(c(10, NA, 3)), cell_limits(c(3, NA), c(10, NA)))
106  expect_identical(cell_rows(4:16), cell_limits(c(4, NA), c(16, NA)))
107  expect_error(cell_rows(c(7, 2)))
108
109})
110
111test_that("Column-only specifications work", {
112
113  expect_identical(cell_cols(c(NA, NA)), cell_limits())
114  expect_identical(cell_cols(c(NA, 3)), cell_limits(lr = c(NA, 3)))
115  expect_identical(cell_cols(c(7, NA)), cell_limits(c(NA, 7)))
116  expect_identical(cell_cols(c(3, NA, 10)), cell_limits(c(NA, 3), c(NA, 10)))
117  expect_identical(cell_cols(c(10, NA, 3)), cell_limits(c(NA, 3), c(NA, 10)))
118  expect_identical(cell_cols(4:16), cell_limits(c(NA, 4), c(NA, 16)))
119  expect_error(cell_cols(c(7, 2)))
120
121  expect_identical(cell_cols("B:D"), cell_limits(c(NA, 2), c(NA, 4)))
122  expect_identical(cell_cols(c("C", "ZZ")), cell_limits(c(NA, 3), c(NA, 702)))
123  expect_identical(cell_cols(c("C", NA)), cell_limits(c(NA, 3)))
124  expect_error(cell_cols("Z:M"))
125  expect_error(cell_cols(c("Z", "M")))
126
127})
128
129test_that("Print method works", {
130
131  expect_output(print(cell_limits(c(NA, 7), c(3, NA))),
132                "<cell_limits (1, 7) x (3, -)>", fixed = TRUE)
133  expect_output(print(cell_limits(c(NA, 7), c(3, NA), "a sheet")),
134                "<cell_limits (1, 7) x (3, -) in 'a sheet'>", fixed = TRUE)
135
136})
137
138test_that("dim method works", {
139
140  expect_equivalent(dim(as.cell_limits("A1")), c(1, 1))
141  expect_equivalent(dim(as.cell_limits("A1:F10")), c(10, 6))
142  expect_equivalent(dim(cell_limits(c(1, 2), c(1, 5))), c(1, 4))
143  expect_equivalent(dim(cell_limits(c(NA, 2), c(1, 5))), c(1, 4))
144  expect_equivalent(dim(cell_limits(c(NA, 2), c(NA, 5))), c(NA_integer_, 4))
145  expect_equivalent(dim(cell_limits(c(1, 1))), c(NA_integer_, NA_integer_))
146
147})
148
149test_that("Cell limits can be specified via anchor", {
150
151  ## no input
152  expect_identical(anchored(), as.cell_limits("A1"))
153  expect_identical(anchored(anchor = "R4C2", dim = c(8, 2)),
154                   cell_limits(c(4, 2), c(11, 3)))
155  expect_identical(anchored(anchor = "A1", dim = c(3, 3), col_names = FALSE),
156                   cell_limits(c(1, 1), c(3, 3)))
157  expect_identical(anchored(anchor = "A1", dim = c(3, 3), col_names = TRUE),
158                   cell_limits(c(1, 1), c(4, 3)))
159
160  ## 2-dimensional input
161  input <- head(iris)
162  expect_identical(anchored(anchor = "R3C7", input = input),
163                   cell_limits(c(3, 7), c(9, 11)))
164  expect_identical(anchored(anchor = "R3C7", input = input, col_names = TRUE),
165                   cell_limits(c(3, 7), c(9, 11)))
166  expect_identical(anchored(anchor = "R3C7", input = input, col_names = FALSE),
167                   cell_limits(c(3, 7), c(8, 11)))
168  ## dim should have no effect here
169  expect_identical(anchored(anchor = "R3C7", input = input, dim = c(2,2)),
170                   cell_limits(c(3, 7), c(9, 11)))
171
172  ## 1-dimensional input
173  input <- LETTERS[1:8]
174  expect_identical(anchored(anchor = "B5", input = input),
175                   cell_limits(c(5, 2), c(12, 2)))
176  expect_identical(anchored(anchor = "B5", input = input, byrow = TRUE),
177                   cell_limits(c(5, 2), c(5, 9)))
178  ## dim and col_names should have no effect here
179  expect_identical(anchored(anchor = "B5", input = input, dim = c(5,5)),
180                   cell_limits(c(5, 2), c(12, 2)))
181  expect_identical(anchored(anchor = "B5", input = input, col_names = TRUE),
182                   cell_limits(c(5, 2), c(12, 2)))
183
184  expect_error(anchored(1))
185  expect_warning(expect_error(anchored("A")))
186  expect_error(anchored("A1:B10"))
187  expect_error(anchored(dim = "eggplant"))
188  expect_error(anchored(dim = 1:3))
189  expect_error(anchored(input = head(iris, 2),
190                        col_names = as.character(length(iris))))
191
192})
193