Skip to content

Commit aab0bfd

Browse files
committed
consolidate two test functions
1 parent 0d726cb commit aab0bfd

File tree

2 files changed

+68
-42
lines changed

2 files changed

+68
-42
lines changed

tests/testthat/test-table_overlaps.R tests/testthat/test-tables.R

+68
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
12
test_that("write_datatable over tables", {
23

34
overwrite_table_error <- "Cannot overwrite existing table with another table"
@@ -91,3 +92,70 @@ test_that("write_data over tables", {
9192
wb$add_data_table(sheet = 1, x = head(iris)[, 1:3], startCol = 1, startRow = 30)
9293
wb$add_data(sheet = 1, x = tail(iris), startCol = 1, startRow = 31, colNames = FALSE)
9394
})
95+
96+
test_that("Validate Table Names", {
97+
wb <- wb_add_worksheet(wb_workbook(), "Sheet 1")
98+
99+
## case
100+
expect_equal(wb_validate_table_name(wb, "test"), "test")
101+
expect_equal(wb_validate_table_name(wb, "TEST"), "test")
102+
expect_equal(wb_validate_table_name(wb, "Test"), "test")
103+
104+
## length
105+
expect_error(wb_validate_table_name(wb, paste(sample(LETTERS, size = 300, replace = TRUE), collapse = "")), regexp = "tableName must be less than 255 characters")
106+
107+
## look like cell ref
108+
expect_error(wb_validate_table_name(wb, "R1C2"), regexp = "tableName cannot be the same as a cell reference, such as R1C1", fixed = TRUE)
109+
expect_error(wb_validate_table_name(wb, "A1"), regexp = "tableName cannot be the same as a cell reference", fixed = TRUE)
110+
111+
expect_error(wb_validate_table_name(wb, "R06821C9682"), regexp = "tableName cannot be the same as a cell reference, such as R1C1", fixed = TRUE)
112+
expect_error(wb_validate_table_name(wb, "ABD918751"), regexp = "tableName cannot be the same as a cell reference", fixed = TRUE)
113+
114+
expect_error(wb_validate_table_name(wb, "A$100"), regexp = "'$' character cannot exist in a tableName", fixed = TRUE)
115+
expect_error(wb_validate_table_name(wb, "A12$100"), regexp = "'$' character cannot exist in a tableName", fixed = TRUE)
116+
117+
tbl_nm <- "性別"
118+
expect_equal(wb_validate_table_name(wb, tbl_nm), tbl_nm)
119+
})
120+
121+
test_that("Existing Table Names", {
122+
wb <- wb_add_worksheet(wb_workbook(), "Sheet 1")
123+
124+
## Existing names - case in-sensitive
125+
wb$add_data_table(sheet = 1, x = head(iris), tableName = "Table1")
126+
expect_error(wb_validate_table_name(wb, "Table1"), regexp = "table with name 'table1' already exists", fixed = TRUE)
127+
expect_error(wb$add_data_table(sheet = 1, x = head(iris), tableName = "Table1", startCol = 10), regexp = "table with name 'table1' already exists", fixed = TRUE)
128+
129+
expect_error(wb_validate_table_name(wb, "TABLE1"), regexp = "table with name 'table1' already exists", fixed = TRUE)
130+
expect_error(wb$add_data_table(sheet = 1, x = head(iris), tableName = "TABLE1", startCol = 20), regexp = "table with name 'table1' already exists", fixed = TRUE)
131+
132+
expect_error(wb_validate_table_name(wb, "table1"), regexp = "table with name 'table1' already exists", fixed = TRUE)
133+
expect_error(wb$add_data_table(sheet = 1, x = head(iris), tableName = "table1", startCol = 30), regexp = "table with name 'table1' already exists", fixed = TRUE)
134+
})
135+
136+
test_that("custom table styles work", {
137+
138+
# at the moment we have no interface to add custom table styles
139+
wb <- wb_workbook() %>%
140+
wb_add_worksheet()
141+
142+
wb$styles_mgr$styles$dxfs <- c(
143+
"<dxf><fill><patternFill><bgColor theme=\"7\" tint=\"0.79998168889431442\"/></patternFill></fill></dxf>",
144+
"<dxf><fill><patternFill><bgColor theme=\"5\" tint=\"0.79998168889431442\"/></patternFill></fill></dxf>",
145+
"<dxf><border><left style=\"slantDashDot\"><color auto=\"1\"/></left><right style=\"slantDashDot\"><color auto=\"1\"/></right><top style=\"slantDashDot\"><color auto=\"1\"/></top><bottom style=\"slantDashDot\"><color auto=\"1\"/></bottom><vertical style=\"slantDashDot\"><color auto=\"1\"/></vertical><horizontal style=\"slantDashDot\"><color auto=\"1\"/></horizontal></border></dxf>",
146+
"<dxf><fill><patternFill><bgColor rgb=\"FFC00000\"/></patternFill></fill></dxf>"
147+
)
148+
wb$styles_mgr$dxf <- data.frame(
149+
typ = "dxf",
150+
id = seq_along(wb$styles_mgr$styles$dxfs) - 1L,
151+
name = wb$styles_mgr$styles$dxfs
152+
)
153+
154+
wb$styles_mgr$styles$tableStyles <- "<tableStyles count=\"1\" defaultTableStyle=\"TableStyleMedium2\" defaultPivotStyle=\"PivotStyleLight16\"><tableStyle name=\"RedTableStyle\" pivot=\"0\" count=\"4\" xr9:uid=\"{91A57EDA-14C5-4643-B7E3-C78161B6BBA4}\"><tableStyleElement type=\"wholeTable\" dxfId=\"2\"/><tableStyleElement type=\"headerRow\" dxfId=\"3\"/><tableStyleElement type=\"firstRowStripe\" dxfId=\"0\"/><tableStyleElement type=\"secondColumnStripe\" dxfId=\"1\"/></tableStyle></tableStyles>"
155+
156+
157+
expect_silent(wb$add_data_table(x = mtcars, tableStyle = "RedTableStyle"))
158+
wb$add_worksheet()
159+
expect_error(wb$add_data_table(x = mtcars, tableStyle = "RedTableStyle1"), "Invalid table style.")
160+
161+
})

tests/testthat/test-validate_table_name.R

-42
This file was deleted.

0 commit comments

Comments
 (0)