Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 6 additions & 2 deletions R/test-generator.R
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
4 changes: 4 additions & 0 deletions R/testthat-helper-tables.R
Original file line number Diff line number Diff line change
Expand Up @@ -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()
}
Expand Down
91 changes: 91 additions & 0 deletions tests/testthat/test-testGenerator.R
Original file line number Diff line number Diff line change
@@ -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())
)
})
Loading