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
2 changes: 1 addition & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ Description: Automates fixed item parameter linking for test linking under
the item response theory paradigm using mirt package estimates.
License: GPL-3 | file LICENSE
Imports: mirt, methods
Suggests: testthat (>= 3.0.0)
Suggests: testthat (>= 3.0.0), mockery
Encoding: UTF-8
Config/testthat/edition: 3
Config/roxygen2/version: 8.0.0
6 changes: 3 additions & 3 deletions R/aFIPC.R
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,7 @@ autoFIPC <-
}
for (attempt in seq_len(3)) {
n <- readline(prompt = "Is it correct? (1: Yes 2: No) : ")
if (grepl("^[0-9]+$", n)) {
if (grepl("^[12]$", n)) {
return(as.integer(n))
}
}
Expand Down Expand Up @@ -171,7 +171,7 @@ autoFIPC <-
readline(
prompt = "Do you want to use default BILOG-MG priors for oldform Data? (1: Yes 2: No) : "
)
if (grepl("^[0-9]+$", n)) {
if (grepl("^[12]$", n)) {
return(as.integer(n))
}
}
Expand Down Expand Up @@ -390,7 +390,7 @@ autoFIPC <-
readline(
prompt = "Do you want to use default BILOG-MG priors for newform Data? (1: Yes 2: No) : "
)
if (grepl("^[0-9]+$", n)) {
if (grepl("^[12]$", n)) {
return(as.integer(n))
}
}
Expand Down
77 changes: 77 additions & 0 deletions tests/testthat/test-interactive-choice-validation.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
test_that("common-item confirmation rejects values outside 1 or 2", {
mockery::stub(aFIPC::autoFIPC, "interactive", TRUE)
mock_readline <- mockery::mock("0", "3", "12", cycle = TRUE)
mockery::stub(aFIPC::autoFIPC, "readline", mock_readline)

expect_error(
aFIPC::autoFIPC(
newformXData = data.frame(new_item_1 = c(1, 0, 1)),
oldformYData = data.frame(old_item_1 = c(0, 1, 0)),
newformCommonItemNames = "new_item_1",
oldformCommonItemNames = "old_item_1",
confirmCommonItems = NULL
),
"Too many invalid common item confirmation attempts"
)
})

test_that("old-form prior prompt rejects malformed and out-of-range choices", {
mockery::stub(aFIPC::autoFIPC, "interactive", TRUE)
oversized_integer <- paste(rep("9", 1000), collapse = "")
mock_readline <- mockery::mock(" 1", "abc", oversized_integer, cycle = TRUE)
mockery::stub(aFIPC::autoFIPC, "readline", mock_readline)

test_data <- data.frame(
item1 = rep(c(0, 1), 50),
item2 = rep(c(1, 0), 50),
item3 = rep(c(0, 0, 1, 1), 25)
)

expect_error(
aFIPC::autoFIPC(
newformXData = test_data,
oldformYData = test_data,
newformCommonItemNames = c("item1", "item2"),
oldformCommonItemNames = c("item1", "item2"),
confirmCommonItems = TRUE,
itemtype = "3PL",
oldformBILOGprior = NULL
),
"Too many invalid oldform BILOG prior attempts"
)
})

test_that("new-form prior prompt rejects malformed and out-of-range choices", {
mockery::stub(aFIPC::autoFIPC, "interactive", TRUE)
oversized_integer <- paste(rep("9", 1000), collapse = "")
mock_readline <- mockery::mock("1", "3", "abc", oversized_integer, cycle = TRUE)
mockery::stub(aFIPC::autoFIPC, "readline", mock_readline)

# autoFIPC reads these mirt slots directly before reaching the new-form prompt.
fake_model <- new("SingleGroupClass")
fake_model@OptimInfo <- list(secondordertest = TRUE)
fake_model@Data <- list(K = c(2, 2))

mockery::stub(aFIPC::autoFIPC, "mirt::mirt", fake_model)
mockery::stub(aFIPC::autoFIPC, "mirt::multipleGroup", fake_model)
mockery::stub(aFIPC::autoFIPC, "aFIPC:::make_aFIPC_model", fake_model)

test_data <- data.frame(
item1 = c(1, 0, 1, 0, 1),
item2 = c(0, 1, 0, 1, 0)
)

expect_error(
aFIPC::autoFIPC(
newformXData = test_data,
oldformYData = test_data,
newformCommonItemNames = "item1",
oldformCommonItemNames = "item1",
confirmCommonItems = TRUE,
itemtype = "3PL",
oldformBILOGprior = NULL,
newformBILOGprior = NULL
),
"Too many invalid newform BILOG prior attempts"
)
})
Loading