Skip to content

Commit

Permalink
adding tests
Browse files Browse the repository at this point in the history
  • Loading branch information
kartikeya committed Sep 15, 2023
1 parent 52a773c commit 4ca61b2
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 2 deletions.
15 changes: 14 additions & 1 deletion R/utils.R
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,7 @@ to_flextable <- function(content) {
} else if (inherits(content, "data.frame")) {
ft <- flextable::flextable(content)
} else {
stop(paste0("Unsupported class `(", format(class(content)), ")` when exporting table")
stop(paste0("Unsupported class `(", format(class(content)), ")` when exporting table"))
}

ft <- custom_theme(ft)
Expand All @@ -163,7 +163,10 @@ to_flextable <- function(content) {
ft
}

#' Apply a custom theme to a `flextable`
#' @noRd
#'
#' @keywords internal
custom_theme <- function(ft) {
ft <- flextable::fontsize(ft, size = 8, part = "body")
ft <- flextable::bold(ft, part = "header")
Expand All @@ -173,7 +176,12 @@ custom_theme <- function(ft) {
ft
}

#' Get the merge index for a single span.
#' This function retrieves the merge index for a single span,
#' which is used in merging cells.
#' @noRd
#'
#' @keywords internal
get_merge_index_single <- function(span) {
ret <- list()
j <- 1
Expand All @@ -186,6 +194,8 @@ get_merge_index_single <- function(span) {
return(ret)
}

#' Get the merge index for multiple spans.
#' This function merges cells in a `flextable` at specified row and column indices.
#' @noRd
#'
#' @keywords internal
Expand All @@ -200,6 +210,7 @@ get_merge_index <- function(spans) {
unlist(ret, recursive = FALSE, use.names = FALSE)
}

#' Merge cells in a `flextable` at specified indices
#' @noRd
#'
#' @keywords internal
Expand All @@ -209,6 +220,8 @@ merge_at_indice <- function(ft, lst, part) {
}, lst, ft)
}

#' Apply padding to a `flextable` based on indentation levels.
#' This function applies padding to a `flextable` based on indentation levels provided as a vector.
#' @noRd
#'
#' @keywords internal
Expand Down
42 changes: 41 additions & 1 deletion tests/testthat/test-utils.R
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,48 @@ testthat::test_that("panel_item", {
testthat::expect_s3_class(panel_item("LABEL", shiny::tags$div()), "shiny.tag")
})

testthat::test_that("Test to_flextable", {
testthat::test_that("Test to_flextable: supported class", {
data_frame <- data.frame(A = 1:3, B = 4:6)
flextable_output <- to_flextable(data_frame)
testthat::expect_s3_class(flextable_output, "flextable")
})

testthat::test_that("Test to_flextable: unsupported class", {
unsupported_data <- list(a = 1, b = 2)
expect_error(to_flextable(unsupported_data), "Unsupported class")
})

test_that("custom theme to flextable", {
sample_ft <- flextable::qflextable(head(mtcars))
themed_ft <- custom_theme(sample_ft)
expect_is(themed_ft, "flextable")
})

test_that("get_merge_index_single", {
sample_span <- c(1, 2, 1, 3)
merge_index <- get_merge_index_single(sample_span)
expect_is(merge_index, "list")
})

test_that("get_merge_index", {
sample_spans <- matrix(c(1, 2, 1, 3, 2, 1, 1, 1), ncol = 2)
merge_index <- get_merge_index(sample_spans)
expect_is(merge_index, "list")
})

test_that("merge_at_indice", {
sample_ft <- flextable::qflextable(head(mtcars))
merge_indices <- list(
list(i = 1, j = 1:2),
list(i = 2, j = 3:4)
)
merged_ft <- merge_at_indice(sample_ft, lst = merge_indices, part = "body")
expect_is(merged_ft, "flextable")
})

test_that("padding_lst applies padding to a flextable based on indentation levels", {
sample_ft <- flextable::qflextable(head(mtcars))
sample_indents <- c(1, 2, 1, 3, 2)
padded_ft <- padding_lst(sample_ft, sample_indents)
expect_is(padded_ft, "flextable")
})

0 comments on commit 4ca61b2

Please sign in to comment.