test_that("output is a single character string", { result <- generate_comment_box("Hello", print_box = FALSE) expect_type(result, "character") expect_length(result, 1) }) test_that("box always has 5 lines minimum (border, empty, text, empty, border)", { result <- generate_comment_box("Hello", print_box = FALSE) lines <- strsplit(result, "\n")[[1]] expect_gte(length(lines), 5) }) test_that("invalid alignment throws an error", { expect_error( generate_comment_box("Hello", align = "justify", print_box = FALSE), "Invalid alignment" ) }) test_that("long words are split and do not overflow the box", { long_word <- paste(rep("a", 200), collapse = "") result <- generate_comment_box(long_word, width = 80, print_box = FALSE) lines <- strsplit(result, "\n")[[1]] expect_true(all(nchar(lines) == 80)) }) test_that("multi-word comment wraps correctly within width", { comment <- paste(rep("word", 30), collapse = " ") result <- generate_comment_box(comment, width = 80, print_box = FALSE) lines <- strsplit(result, "\n")[[1]] expect_true(all(nchar(lines) == 80)) }) test_that("print_box = TRUE returns invisibly and prints", { expect_output( result <- generate_comment_box("Hello", print_box = TRUE), regexp = "Hello" ) expect_type(result, "character") }) # ---- section_title = FALSE : comportement d'origine ---- test_that("section_title = FALSE : first and last lines are full '#' borders", { result <- generate_comment_box("Hello", width = 80, section_title = FALSE, print_box = FALSE) lines <- strsplit(result, "\n")[[1]] border <- paste(rep("#", 80), collapse = "") expect_equal(lines[1], border) expect_equal(lines[length(lines)], border) }) test_that("section_title = FALSE : box width matches requested width", { for (w in c(20, 40, 60, 80, 100)) { result <- generate_comment_box("Hello", width = w, section_title = FALSE, print_box = FALSE) lines <- strsplit(result, "\n")[[1]] expect_true(all(nchar(lines) == w), label = paste("width =", w)) } }) test_that("section_title = FALSE : all alignments produce correct width", { for (align in c("left", "center", "right")) { result <- generate_comment_box("Test alignment", align = align, width = 80, section_title = FALSE, print_box = FALSE) lines <- strsplit(result, "\n")[[1]] expect_true(all(nchar(lines) == 80), label = paste("align =", align)) } }) test_that("section_title = FALSE : border_style is ignored", { ref <- generate_comment_box("Hello", width = 80, section_title = FALSE, print_box = FALSE) styled <- generate_comment_box("Hello", width = 80, section_title = FALSE, border_style = "soft", print_box = FALSE) expect_equal(ref, styled) }) # ---- section_title = TRUE : comportement par style ---- test_that("section_title = TRUE : invalid border_style throws an error", { expect_error( generate_comment_box("Hello", section_title = TRUE, border_style = "unknown", print_box = FALSE), "Invalid border_style" ) }) # Paramètres de chaque style : top_char, bottom_char styles_params <- list( sharp = list(top = "#", bottom = "/"), double = list(top = "=", bottom = "/"), light = list(top = "-", bottom = "~"), soft = list(top = "=", bottom = "~"), wave = list(top = "#", bottom = "~") ) test_that("section_title = TRUE : all styles produce correct width", { for (style_name in names(styles_params)) { for (w in c(20, 40, 80, 100)) { result <- generate_comment_box("Test", width = w, section_title = TRUE, border_style = style_name, print_box = FALSE) lines <- strsplit(result, "\n")[[1]] expect_true(all(nchar(lines) == w), label = paste("style =", style_name, "/ width =", w)) } } }) test_that("section_title = TRUE : top border contains comment text", { for (style_name in names(styles_params)) { result <- generate_comment_box("My Section", width = 80, section_title = TRUE, border_style = style_name, print_box = FALSE) lines <- strsplit(result, "\n")[[1]] expect_true(grepl("My Section", lines[1], fixed = TRUE), label = paste("style =", style_name)) } }) test_that("section_title = TRUE : top border triggers RStudio section (ends with 4+ trigger chars)", { # Les caractères déclencheurs RStudio sont '#', '-' et '=' trigger_chars <- c("#", "-", "=") for (style_name in names(styles_params)) { top_char <- styles_params[[style_name]]$top expect_true(top_char %in% trigger_chars, label = paste("style =", style_name, ": top_char doit être #, - ou =")) result <- generate_comment_box("Hello", width = 80, section_title = TRUE, border_style = style_name, print_box = FALSE) lines <- strsplit(result, "\n")[[1]] pattern <- paste0("[", top_char, "]{4,}$") expect_true(grepl(pattern, lines[1]), label = paste("style =", style_name, ": top se termine par 4+", top_char)) } }) test_that("section_title = TRUE : bottom border does NOT end with RStudio trigger chars", { trigger_pattern <- "[#\\-=]{4,}$" for (style_name in names(styles_params)) { result <- generate_comment_box("Hello", width = 80, section_title = TRUE, border_style = style_name, print_box = FALSE) lines <- strsplit(result, "\n")[[1]] last <- lines[length(lines)] expect_false(grepl(trigger_pattern, last), label = paste("style =", style_name, ": bottom ne doit pas déclencher une section RStudio")) } }) test_that("section_title = TRUE : bottom border uses the correct fill character", { for (style_name in names(styles_params)) { bot_char <- styles_params[[style_name]]$bottom result <- generate_comment_box("Hello", width = 80, section_title = TRUE, border_style = style_name, print_box = FALSE) lines <- strsplit(result, "\n")[[1]] last <- lines[length(lines)] # Le bord inférieur est '#' + (width-1) bot_char expected_bottom <- paste0("#", paste(rep(bot_char, 79), collapse = "")) expect_equal(last, expected_bottom, label = paste("style =", style_name)) } }) test_that("section_title = TRUE : long comment is truncated with '...' in the header", { long_comment <- paste(rep("a", 200), collapse = "") result <- generate_comment_box(long_comment, width = 80, print_box = FALSE) lines <- strsplit(result, "\n")[[1]] expect_true(grepl("\\.\\.\\.", lines[1])) expect_true(all(nchar(lines) == 80)) }) test_that("section_title = TRUE : all alignments produce correct width (default style)", { for (align in c("left", "center", "right")) { result <- generate_comment_box("Test alignment", align = align, width = 80, print_box = FALSE) lines <- strsplit(result, "\n")[[1]] expect_true(all(nchar(lines) == 80), label = paste("align =", align)) } })