Skip to content

Commit

Permalink
updating unit-test
Browse files Browse the repository at this point in the history
  • Loading branch information
kartikeya committed Sep 20, 2023
1 parent 8abe363 commit 9541307
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 6 deletions.
2 changes: 1 addition & 1 deletion R/Renderer.R
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,7 @@ Renderer <- R6::R6Class( # nolint: object_name_linter.
params <- lapply(params, function(l) if (is.character(l)) shQuote(l) else l)
block_content <- block$get_content()
if (identical(report_type, "powerpoint_presentation")) {
block_content_lst <- split_text_into_blocks(block_content, 30)
block_content_lst <- split_text_block(block_content, 30)
paste(
unlist(
lapply(seq_along(block_content_lst), function(b) {
Expand Down
36 changes: 35 additions & 1 deletion R/utils.R
Original file line number Diff line number Diff line change
Expand Up @@ -231,6 +231,40 @@ padding_lst <- function(ft, indents) {
}, seq_len(length(indents)), ft)
}

#' Split a text block into smaller blocks with a specified number of lines.
#'
#' This function takes a block of text and divides it into smaller blocks, each containing
#' a specified number of lines.
#'
#' @param block_text A character vector containing the input block of text.
#' @param n The number of lines per block.
#'
#' @return A list of character vectors, where each element is a smaller block of text
#' containing 'n' lines. If the input block of text has fewer lines than 'n', the
#' entire block is returned as a single element list.
#'
#' @keywords internal
split_text_into_blocks <- function(block_text, n) {
lines <- strsplit(block_text, "\n")[[1]]
num_lines <- length(lines)

if (num_lines <= n) {
return(list(block_text))
}

num_blocks <- ceiling(num_lines / n)
blocks <- vector("list", length = num_blocks)

for (i in 1:num_blocks) {
start <- (i - 1) * n + 1
end <- min(i * n, num_lines)
block <- paste(lines[start:end], collapse = "\n")
blocks[[i]] <- block
}

return(blocks)
}

#' Split a text block into smaller blocks with a specified number of lines.
#'
#' Divide text block into smaller blocks.
Expand All @@ -252,7 +286,7 @@ split_text_block <- function(x, n) {
lines <- strsplit(x, "\n")[[1]]

if (length(lines) <= n) {
return(list(x))
return(x)
}

nblocks <- ceiling(length(lines) / n)
Expand Down
15 changes: 11 additions & 4 deletions tests/testthat/test-utils.R
Original file line number Diff line number Diff line change
Expand Up @@ -52,11 +52,18 @@ test_that("padding_lst applies padding to a flextable based on indentation level
expect_is(padded_ft, "flextable")
})

test_that("split_text_into_blocks", {
block_text <- "Line 1\nLine 2\nLine 3\nLine 4\nLine 5\nLine 6"
block_text <- "Line 1\nLine 2\nLine 3\nLine 4\nLine 5\nLine 6"

test_that("split_text_block - splits text block into equal parts", {
n <- 2
result <- split_text_into_blocks(block_text, n)
expected_result <- list("Line 1\nLine 2", "Line 3\nLine 4", "Line 5\nLine 6")
result <- split_text_block(block_text, n)
expected_result <- as.array(c("Line 1\nLine 2", "Line 3\nLine 4", "Line 5\nLine 6"))
expect_equal(result, expected_result)
})

test_that("split_text_block - n greater than the number of line breaks", {
n <- 7
result <- split_text_block(block_text, n)
expect_equal(result, block_text)
})

Check warning on line 69 in tests/testthat/test-utils.R

View workflow job for this annotation

GitHub Actions / SuperLinter 🦸‍♀️ / SuperLinter 🦸‍♂️

file=/github/workspace/tests/testthat/test-utils.R,line=69,col=1,[trailing_blank_lines_linter] Trailing blank lines are superfluous.

0 comments on commit 9541307

Please sign in to comment.