From 4ca61b2ab3f2b309c7ca091d7291192abd4e45ec Mon Sep 17 00:00:00 2001 From: kartikeya Date: Fri, 15 Sep 2023 14:37:59 +0530 Subject: [PATCH] adding tests --- R/utils.R | 15 ++++++++++++- tests/testthat/test-utils.R | 42 ++++++++++++++++++++++++++++++++++++- 2 files changed, 55 insertions(+), 2 deletions(-) diff --git a/R/utils.R b/R/utils.R index d5af0d74..91b89ab2 100644 --- a/R/utils.R +++ b/R/utils.R @@ -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) @@ -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") @@ -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 @@ -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 @@ -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 @@ -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 diff --git a/tests/testthat/test-utils.R b/tests/testthat/test-utils.R index 9c39c722..47867284 100644 --- a/tests/testthat/test-utils.R +++ b/tests/testthat/test-utils.R @@ -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") +})