test_that("print.spicy_freq_table prints correctly and invisibly", { library(labelled) # Create a simple labelled vector x <- labelled( c(1, 2, 2, 3, 3, 3, NA), labels = c("Low" = 1, "Medium" = 2, "High" = 3) ) var_label(x) <- "Satisfaction level" # Generate the styled invisible return (spicy_freq_table carrying the # print-method metadata). Swallow the printed output from the freq() # call itself via capture.output. capture.output(df <- freq(x)) # Test invisibility of print method expect_invisible(print.spicy_freq_table(df)) # Capture console output for inspection output <- capture.output(print.spicy_freq_table(df)) # --- General checks expect_true(any(grepl("Frequency table:", output))) expect_true(any(grepl("Valid", output))) expect_true(any(grepl("Total", output))) # --- Footer checks expect_true(any(grepl("Label: Satisfaction level", output))) expect_true(any(grepl("Class:", output))) expect_true(any(grepl("Data:", output))) # --- Structural checks expect_true(any(grepl("\u2500", output))) # has horizontal lines expect_true(any(grepl("\u2502", output))) # has vertical bars }) test_that("print.spicy_freq_table handles weighted tables", { df <- data.frame( sexe = factor(c("Male", "Female", "Female", "Male", NA)), poids = c(1.2, 0.8, 1.5, 1.0, 0.7) ) # Weighted frequency table — keep the styled invisible return so that # the metadata attributes needed by print.spicy_freq_table are present. capture.output(ftab <- freq(df, sexe, weights = poids)) output <- capture.output(print.spicy_freq_table(ftab)) expect_true(any(grepl("Weight:", output))) expect_true(any(grepl("rescaled", output))) expect_true(any(grepl("Total", output))) }) test_that("print.spicy_freq_table shows generic weight label when weight_var is empty", { df <- data.frame(x = c("A", "B", "C"), w = c(2, 3, 5)) capture.output(ftab <- freq(df, x, weights = w)) attr(ftab, "weight_var") <- NULL output <- capture.output(print.spicy_freq_table(ftab)) expect_true(any(grepl("Weight: \\(applied\\)", output))) }) test_that("print.spicy_freq_table handles variables without labels or missing values", { # Vector without label and no missing values x <- factor(c("A", "B", "B", "A", "C", "A")) capture.output(df <- freq(x)) output <- capture.output(print.spicy_freq_table(df)) # Expect no Label line, but expect Data/Class expect_false(any(grepl("^Label:", output))) expect_true(any(grepl("^Class:", output))) expect_true(any(grepl("^Data:", output))) }) test_that("print.spicy_freq_table tolerates pathological var_label values", { # Defensive: NA / non-character / multi-element labels must not # produce a Label line and must not crash. NA_character_ would # previously trigger "missing value where TRUE/FALSE needed" via # nzchar(NA_character_) returning NA inside the surrounding `if`. df <- data.frame(x = c("A", "B", "B")) capture.output(ftab <- freq(df, x)) attr(ftab, "var_label") <- NA_character_ out <- capture.output(print.spicy_freq_table(ftab)) expect_false(any(grepl("^Label:", out))) attr(ftab, "var_label") <- 42L out <- capture.output(print.spicy_freq_table(ftab)) expect_false(any(grepl("^Label:", out))) attr(ftab, "var_label") <- c("first", "second") out <- capture.output(print.spicy_freq_table(ftab)) expect_false(any(grepl("^Label:", out))) attr(ftab, "var_label") <- "" out <- capture.output(print.spicy_freq_table(ftab)) expect_false(any(grepl("^Label:", out))) })