Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[tests increase coverage #916

Merged
merged 6 commits into from
Jan 27, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 21 additions & 21 deletions R/baseXML.R
Original file line number Diff line number Diff line change
Expand Up @@ -189,28 +189,28 @@ genBaseWorkbook <- function() {
# workbookProtection

list(
fileVersion = NULL,
fileSharing = NULL,
workbookPr = '<workbookPr date1904="false"/>',
alternateContent = NULL,
revisionPtr = NULL,
absPath = NULL, # "x15ac:absPath"
workbookProtection = NULL,
bookViews = NULL,
sheets = NULL,
functionGroups = NULL,
externalReferences = NULL,
definedNames = NULL,
calcPr = NULL,
oleSize = NULL,
fileVersion = NULL,
fileSharing = NULL,
workbookPr = '<workbookPr date1904="false"/>',
alternateContent = NULL,
revisionPtr = NULL,
absPath = NULL, # "x15ac:absPath"
workbookProtection = NULL,
bookViews = NULL,
sheets = NULL,
functionGroups = NULL,
externalReferences = NULL,
definedNames = NULL,
calcPr = NULL,
oleSize = NULL,
customWorkbookViews = NULL,
pivotCaches = NULL,
smartTagPr = NULL,
smartTagTypes = NULL,
webPublishing = NULL,
fileRecoveryPr = NULL,
webPublishObjects = NULL,
extLst = NULL
pivotCaches = NULL,
smartTagPr = NULL,
smartTagTypes = NULL,
webPublishing = NULL,
fileRecoveryPr = NULL,
webPublishObjects = NULL,
extLst = NULL
)
}

Expand Down
5 changes: 2 additions & 3 deletions R/converters.R
Original file line number Diff line number Diff line change
Expand Up @@ -34,11 +34,10 @@ col2int <- function(x) {

if (!is.character(x)) {
stop("x must be character")

if (anyNA(x))
stop("x must be a valid character")
}

if (anyNA(x)) stop("x contains NA")

if (any(grepl(":", x))) {
# loop through x until all ":" are replaced with integer sequences
while (any(grepl(":", x))) {
Expand Down
17 changes: 0 additions & 17 deletions R/utils.R
Original file line number Diff line number Diff line change
Expand Up @@ -26,18 +26,6 @@ paste_c <- function(..., sep = "", collapse = " ", unlist = FALSE) {
`%||%` <- function(x, y) if (is.null(x)) y else x
`%|||%` <- function(x, y) if (length(x)) x else y

na_to_null <- function(x) {
if (is.null(x)) {
return(NULL)
}

if (isTRUE(is.na(x))) {
return(NULL)
}

x
}

# opposite of %in%
`%out%` <- function(x, table) {
match(x, table, nomatch = 0L) == 0L
Expand Down Expand Up @@ -95,11 +83,6 @@ wapply <- function(x, FUN, ...) {
which(vapply(x, FUN, FUN.VALUE = NA, ...))
}

has_chr <- function(x, na = FALSE) {
# na controls if NA is returned as TRUE or FALSE
vapply(nzchar(x, keepNA = !na), isTRUE, NA)
}

dir_create <- function(..., warn = TRUE, recurse = FALSE) {
# create path and directory -- returns path
path <- file.path(...)
Expand Down
14 changes: 10 additions & 4 deletions R/write.R
Original file line number Diff line number Diff line change
Expand Up @@ -840,7 +840,7 @@ write_data_table <- function(
if (!data_table) {

## write autoFilter, can only have a single filter per worksheet
if (withFilter) {
if (withFilter) { # TODO: replace ref calculation with wb_dims()
coords <- data.frame("x" = c(startRow, startRow + nRow + colNames - 1L), "y" = c(startCol, startCol + nCol - 1L))
ref <- stri_join(get_cell_refs(coords), collapse = ":")

Expand All @@ -851,11 +851,17 @@ write_data_table <- function(

dn <- sprintf('<definedName name="_xlnm._FilterDatabase" localSheetId="%s" hidden="1">%s</definedName>', sheet - 1L, dfn)

if (length(wb$workbook$definedNames) > 0) {
ind <- grepl('name="_xlnm._FilterDatabase"', wb$workbook$definedNames)
if (length(ind) > 0) {
if (!is.null(wbdn <- wb$get_named_regions())) {

ind <- wbdn$name == "_xlnm._FilterDatabase" & wbdn$localSheetId == sheet - 1L
if (any(ind)) {
wb$workbook$definedNames[ind] <- dn
} else {
wb$workbook$definedNames <- c(
wb$workbook$definedNames, dn
)
}

} else {
wb$workbook$definedNames <- dn
}
Expand Down
17 changes: 17 additions & 0 deletions tests/testthat/test-class-workbook.R
Original file line number Diff line number Diff line change
Expand Up @@ -941,3 +941,20 @@ test_that("wbWorkbook print works", {
expect_equal(exp, got)

})

test_that("genBaseWorkbook() works", {

# kinda superfluous. if it wouldn't work openxlsx2 would be broken

exp <- c(
"fileVersion", "fileSharing", "workbookPr", "alternateContent",
"revisionPtr", "absPath", "workbookProtection", "bookViews",
"sheets", "functionGroups", "externalReferences", "definedNames",
"calcPr", "oleSize", "customWorkbookViews", "pivotCaches", "smartTagPr",
"smartTagTypes", "webPublishing", "fileRecoveryPr", "webPublishObjects",
"extLst"
)
got <- names(genBaseWorkbook())
expect_equal(exp, got)

})
19 changes: 19 additions & 0 deletions tests/testthat/test-converters.R
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ test_that("col2int", {
expect_equal(c(1, 3, 4), col2int(c("A", "C:D")))
expect_equal(c(1, 3, 4, 11), col2int(c("A", "C:D", "K")))
expect_equal(c(1, 3, 4, 11, 27, 28, 29, 30), col2int(c("A", "C:D", "K", "AA:AD")))
expect_error(col2int(c("a", NA_character_, "c")), "x contains NA")

})

Expand Down Expand Up @@ -47,3 +48,21 @@ test_that("get_cell_refs() works for multiple cells.", {

expect_error(get_cell_refs(c(1:2, c("a", "b"))))
})

test_that("", {

op <- options(
"openxlsx2.maxWidth" = 10,
"openxlsx2.minWidth" = 8
)
on.exit(options(op), add = TRUE)

exp <- 10
got <- calc_col_width(wb_workbook()$get_base_font(), 11)
expect_equal(exp, got)

exp <- 8
got <- calc_col_width(wb_workbook()$get_base_font(), 7)
expect_equal(exp, got)

})
15 changes: 15 additions & 0 deletions tests/testthat/test-utils.R
Original file line number Diff line number Diff line change
Expand Up @@ -343,6 +343,21 @@ test_that("string formating", {
got <- wb_to_df(wb)$txt
expect_equal(exp, got)

exp <- structure(
"<r><rPr><i/><strike/><rFont val=\"Arial\"/><charset/><outline val=\"1\"/><vertAlign val=\"5\"/></rPr><t>test</t></r>",
class = c("character", "fmt_txt")
)
got <- fmt_txt(
"test",
italic = TRUE,
strike = TRUE,
font = "Arial",
charset = "",
outline = TRUE,
vert_align = 5
)
expect_equal(exp, got)

})

test_that("outdec = \",\" works", {
Expand Down
49 changes: 49 additions & 0 deletions tests/testthat/test-write.R
Original file line number Diff line number Diff line change
Expand Up @@ -745,3 +745,52 @@ test_that("dimension limits work", {
)

})

test_that("numfmt option works", {

op <- options("openxlsx2.numFmt" = "$ #.0")
on.exit(options(op), add = TRUE)

wb <- wb_workbook()$add_worksheet()$add_data(x = 1:10)

exp <- "<numFmt numFmtId=\"165\" formatCode=\"$ #.0\"/>"
got <- wb$styles_mgr$styles$numFmts
expect_equal(exp, got)

})

test_that("comma option works", {

op <- options("openxlsx2.commaFormat" = "#.0")
on.exit(options(op), add = TRUE)

dat <- data.frame(x = 1:10 + rnorm(1:10))
class(dat$x) <- c("comma", class(dat$x))

wb <- wb_workbook()$add_worksheet()$add_data(x = dat)

exp <- "<numFmt numFmtId=\"165\" formatCode=\"#.0\"/>"
got <- wb$styles_mgr$styles$numFmts
expect_equal(exp, got)

})

test_that("filter works with wb_add_data()", {

wb <- wb_workbook()$
add_worksheet()$add_data(x = mtcars, with_filter = TRUE)$
add_worksheet()$add_data(x = mtcars, with_filter = TRUE)$
add_data(x = cars, with_filter = TRUE)

exp <- "<autoFilter ref=\"A1:K33\"/>"
got <- wb$worksheets[[1]]$autoFilter
expect_equal(exp, got)

exp <- c(
"<definedName name=\"_xlnm._FilterDatabase\" localSheetId=\"0\" hidden=\"1\">'Sheet 1'!$A$1:$K$33</definedName>",
"<definedName name=\"_xlnm._FilterDatabase\" localSheetId=\"1\" hidden=\"1\">'Sheet 2'!$A$1:$B$51</definedName>"
)
got <- wb$workbook$definedNames
expect_equal(exp, got)

})
Loading