diff --git a/R/test-generator.R b/R/test-generator.R index 2c5dd0f..6f0e511 100644 --- a/R/test-generator.R +++ b/R/test-generator.R @@ -664,13 +664,17 @@ getTests <- function(results) { unitTestType <- "plot" } - if (!is.null(unitTestType) && unitTestType == "plot" || (unitTestType == "table" && length(x[["data"]]) > 0)) { + if (!is.null(unitTestType) && (unitTestType == "plot" || unitTestType == "table")) { testid <- length(tests) tests[[paste0("itemToUnitTest-", testid)]] <<- list( title = unlist(x[["title"]]), id = testid, type = unitTestType, - data = ifelse(unitTestType == "table", makeTestTable(x[["data"]], print = FALSE), "") + data = if (unitTestType == "table") { + if (length(x[["data"]]) > 0) makeTestTable(x[["data"]], print = FALSE) else "list()" + } else { + "" + } ) x[["itemToUnitTest"]] <- testid } diff --git a/R/testthat-helper-tables.R b/R/testthat-helper-tables.R index fbddbed..77cd455 100644 --- a/R/testthat-helper-tables.R +++ b/R/testthat-helper-tables.R @@ -38,6 +38,10 @@ #' @export expect_equal_tables expect_equal_tables <- function(test, ref, label=NULL) { if (length(test) == 0) { + if (length(ref) == 0) { + testthat::succeed() + return() + } expect(FALSE, getEmptyTestMsg("expect_equal_tables()")) return() } diff --git a/tests/testthat/test-testGenerator.R b/tests/testthat/test-testGenerator.R new file mode 100644 index 0000000..82aac9e --- /dev/null +++ b/tests/testthat/test-testGenerator.R @@ -0,0 +1,91 @@ +context("test-generator") + +test_that("getTests captures tables with empty data", { + # Mock a JASP results structure with an empty table + results <- list( + emptyTable = list( + data = list(), + schema = list(fields = list()), + title = "Empty Table" + ), + nonEmptyTable = list( + data = list( + list(col1 = 1, col2 = "a"), + list(col1 = 2, col2 = "b") + ), + schema = list(fields = list( + list(name = "col1", type = "number"), + list(name = "col2", type = "string") + )), + title = "Non-Empty Table" + ) + ) + + tests <- jaspTools:::getTests(results) + + # Should capture both tables (2 total) + expect_equal(length(tests), 2) + + # Verify both table types are captured + types <- vapply(tests, function(t) t$type, character(1)) + expect_true(all(types == "table")) + + # Verify titles + titles <- vapply(tests, function(t) t$title, character(1)) + expect_true("Empty Table" %in% titles) + expect_true("Non-Empty Table" %in% titles) + + # Verify empty table has "list()" as data + emptyTest <- tests[[which(titles == "Empty Table")]] + expect_equal(emptyTest$data, "list()") + + # Verify non-empty table has actual data + nonEmptyTest <- tests[[which(titles == "Non-Empty Table")]] + expect_true(nchar(nonEmptyTest$data) > 6) # longer than "list()" +}) + +test_that("getTests captures tables nested inside containers", { + # Mock a nested JASP results structure (container with tables) + results <- list( + topTable = list( + data = list(list(x = 1)), + schema = list(fields = list(list(name = "x", type = "number"))), + title = "Top Level Table" + ), + container = list( + title = "My Container", + nestedTable = list( + data = list(list(y = 2)), + schema = list(fields = list(list(name = "y", type = "number"))), + title = "Nested Table" + ), + emptyNestedTable = list( + data = list(), + schema = list(fields = list()), + title = "Empty Nested Table" + ) + ) + ) + + tests <- jaspTools:::getTests(results) + + # Should capture all 3 tables + expect_equal(length(tests), 3) + + titles <- vapply(tests, function(t) t$title, character(1)) + expect_true("Top Level Table" %in% titles) + expect_true("Nested Table" %in% titles) + expect_true("Empty Nested Table" %in% titles) +}) + +test_that("expect_equal_tables passes when both tables are empty", { + # Both empty should succeed + expect_silent(expect_equal_tables(list(), list())) +}) + +test_that("expect_equal_tables fails when ref is empty but test is not", { + # Non-empty test with empty ref should fail + expect_failure( + expect_equal_tables(list(list(x = 1)), list()) + ) +})